mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-27 23:04:05 -06:00
Obsidian Parser (InHtmlEmbed)
This commit is contained in:
parent
7790db45c4
commit
4e830a5931
@ -4,13 +4,16 @@ import { JSResource } from "../../../util/resources"
|
||||
import { SKIP } from "unist-util-visit"
|
||||
import { Root } from "mdast"
|
||||
import { Pluggable } from "unified"
|
||||
import { mdastFindReplaceInHtml } from "../../transformers/markdown"
|
||||
|
||||
interface Options {
|
||||
enabled: Boolean
|
||||
inHtml: Boolean
|
||||
}
|
||||
|
||||
const defaultOptions: Options = {
|
||||
enabled: true,
|
||||
inHtml: false,
|
||||
}
|
||||
|
||||
const arrowMapping: Record<string, string> = {
|
||||
@ -53,7 +56,7 @@ export const ObsidianArrow: QuartzParser<Partial<Options>> = (userOpts) => {
|
||||
}
|
||||
},
|
||||
])
|
||||
mdastFindReplace(tree, replacements)
|
||||
mdastFindReplaceInHtml(tree, replacements, opts.inHtml)
|
||||
}
|
||||
}
|
||||
return plug
|
||||
|
||||
@ -9,13 +9,16 @@ import { PhrasingContent } from "mdast-util-find-and-replace/lib"
|
||||
import { capitalize } from "../../../util/lang"
|
||||
import { toHast } from "mdast-util-to-hast"
|
||||
import { toHtml } from "hast-util-to-html"
|
||||
import { mdastToHtml } from "../../transformers/markdown"
|
||||
|
||||
interface Options {
|
||||
enabled: Boolean
|
||||
inHtml: Boolean
|
||||
}
|
||||
|
||||
const defaultOptions: Options = {
|
||||
enabled: true,
|
||||
inHtml: false,
|
||||
}
|
||||
|
||||
// from https://github.com/escwxyz/remark-obsidian-callout/blob/main/src/index.ts
|
||||
@ -58,11 +61,6 @@ function canonicalizeCallout(calloutName: string): keyof typeof calloutMapping {
|
||||
return calloutMapping[normalizedCallout] ?? calloutName
|
||||
}
|
||||
|
||||
const mdastToHtml = (ast: PhrasingContent | Paragraph) => {
|
||||
const hast = toHast(ast, { allowDangerousHtml: true })!
|
||||
return toHtml(hast, { allowDangerousHtml: true })
|
||||
}
|
||||
|
||||
export const ObsidianCallouts: QuartzParser<Partial<Options>> = (userOpts) => {
|
||||
const opts: Options = { ...defaultOptions, ...userOpts }
|
||||
return {
|
||||
|
||||
@ -7,13 +7,16 @@ import { JSResource } from "../../../util/resources"
|
||||
import { Element, Literal, Root as HtmlRoot } from "hast"
|
||||
import { Root } from "mdast"
|
||||
import { Pluggable } from "unified"
|
||||
import { mdastFindReplaceInHtml } from "../../transformers/markdown"
|
||||
|
||||
interface Options {
|
||||
enabled: Boolean
|
||||
inHtml: Boolean
|
||||
}
|
||||
|
||||
const defaultOptions: Options = {
|
||||
enabled: true,
|
||||
inHtml: false,
|
||||
}
|
||||
|
||||
export const ObsidianCheckboxes: QuartzParser<Partial<Options>> = (userOpts) => {
|
||||
@ -29,7 +32,7 @@ export const ObsidianCheckboxes: QuartzParser<Partial<Options>> = (userOpts) =>
|
||||
markdownPlugins(_ctx) {
|
||||
const plug: Pluggable = (tree: Root, _file) => {
|
||||
const replacements: [RegExp, string | ReplaceFunction][] = []
|
||||
mdastFindReplace(tree, replacements)
|
||||
mdastFindReplaceInHtml(tree, replacements, opts.inHtml)
|
||||
}
|
||||
return plug
|
||||
},
|
||||
|
||||
@ -3,13 +3,16 @@ import { ReplaceFunction, findAndReplace as mdastFindReplace } from "mdast-util-
|
||||
import { JSResource } from "../../../util/resources"
|
||||
import { Root } from "mdast"
|
||||
import { Pluggable } from "unified"
|
||||
import { mdastFindReplaceInHtml } from "../../transformers/markdown"
|
||||
|
||||
interface Options {
|
||||
enabled: Boolean
|
||||
inHtml: Boolean
|
||||
}
|
||||
|
||||
const defaultOptions: Options = {
|
||||
enabled: true,
|
||||
inHtml: false,
|
||||
}
|
||||
|
||||
const commentRegex = new RegExp(/%%[\s\S]*?%%/g)
|
||||
@ -33,7 +36,7 @@ export const ObsidianComments: QuartzParser<Partial<Options>> = (userOpts) => {
|
||||
const plug: Pluggable = (tree: Root, _file) => {
|
||||
if (opts.enabled) {
|
||||
const replacements: [RegExp, string | ReplaceFunction][] = []
|
||||
mdastFindReplace(tree, replacements)
|
||||
mdastFindReplaceInHtml(tree, replacements, opts.inHtml)
|
||||
}
|
||||
}
|
||||
return plug
|
||||
|
||||
@ -3,13 +3,16 @@ import { ReplaceFunction, findAndReplace as mdastFindReplace } from "mdast-util-
|
||||
import { JSResource } from "../../../util/resources"
|
||||
import { Root } from "mdast"
|
||||
import { Pluggable } from "unified"
|
||||
import { mdastFindReplaceInHtml } from "../../transformers/markdown"
|
||||
|
||||
interface Options {
|
||||
enabled: Boolean
|
||||
inHtml: Boolean
|
||||
}
|
||||
|
||||
const defaultOptions: Options = {
|
||||
enabled: true,
|
||||
inHtml: false,
|
||||
}
|
||||
|
||||
const highlightRegex = new RegExp(/==([^=]+)==/g)
|
||||
@ -40,7 +43,7 @@ export const ObsidianHighlights: QuartzParser<Partial<Options>> = (userOpts) =>
|
||||
}
|
||||
},
|
||||
])
|
||||
mdastFindReplace(tree, replacements)
|
||||
mdastFindReplaceInHtml(tree, replacements, opts.inHtml)
|
||||
}
|
||||
}
|
||||
return plug
|
||||
|
||||
@ -6,10 +6,12 @@ import { Pluggable } from "unified"
|
||||
|
||||
interface Options {
|
||||
enabled: Boolean
|
||||
inHtml: Boolean
|
||||
}
|
||||
|
||||
const defaultOptions: Options = {
|
||||
enabled: true,
|
||||
inHtml: false,
|
||||
}
|
||||
|
||||
export const ObsidianMermaid: QuartzParser<Partial<Options>> = (userOpts) => {
|
||||
|
||||
@ -4,13 +4,16 @@ import { FilePath, pathToRoot, slugTag, slugifyFilePath } from "../../../util/pa
|
||||
import { JSResource } from "../../../util/resources"
|
||||
import { Root } from "mdast"
|
||||
import { Pluggable } from "unified"
|
||||
import { mdastFindReplaceInHtml } from "../../transformers/markdown"
|
||||
|
||||
interface Options {
|
||||
enabled: Boolean
|
||||
inHtml: Boolean
|
||||
}
|
||||
|
||||
const defaultOptions: Options = {
|
||||
enabled: true,
|
||||
inHtml: false,
|
||||
}
|
||||
|
||||
// (?:^| ) -> non-capturing group, tag should start be separated by a space or be the start of the line
|
||||
@ -66,7 +69,7 @@ export const ObsidianTags: QuartzParser<Partial<Options>> = (userOpts) => {
|
||||
}
|
||||
},
|
||||
])
|
||||
mdastFindReplace(tree, replacements)
|
||||
mdastFindReplaceInHtml(tree, replacements, opts.inHtml)
|
||||
}
|
||||
return plug
|
||||
},
|
||||
|
||||
@ -4,13 +4,16 @@ import { FilePath, splitAnchor, slugifyFilePath } from "../../../util/path"
|
||||
import { JSResource } from "../../../util/resources"
|
||||
import { Root } from "mdast"
|
||||
import { Pluggable } from "unified"
|
||||
import { mdastFindReplaceInHtml } from "../../transformers/markdown"
|
||||
|
||||
interface Options {
|
||||
enabled: Boolean
|
||||
inHtml: Boolean
|
||||
}
|
||||
|
||||
const defaultOptions: Options = {
|
||||
enabled: true,
|
||||
inHtml: false,
|
||||
}
|
||||
|
||||
const externalLinkRegex = /^https?:\/\//i
|
||||
@ -152,7 +155,7 @@ export const ObsidianWikilinks: QuartzParser<Partial<Options>> = (userOpts) => {
|
||||
}
|
||||
},
|
||||
])
|
||||
mdastFindReplace(tree, replacements)
|
||||
mdastFindReplaceInHtml(tree, replacements, opts.inHtml)
|
||||
}
|
||||
return plug
|
||||
},
|
||||
|
||||
@ -150,6 +150,41 @@ const defaultRoamOptions: RoamOptions = {
|
||||
attributeComponent: true,
|
||||
}
|
||||
|
||||
export const mdastToHtml = (ast: PhrasingContent | Paragraph) => {
|
||||
const hast = toHast(ast, { allowDangerousHtml: true })!
|
||||
return toHtml(hast, { allowDangerousHtml: true })
|
||||
}
|
||||
|
||||
export const mdastFindReplaceInHtml = (
|
||||
tree: Root,
|
||||
replacements: [RegExp, string | ReplaceFunction][],
|
||||
inHtml: Boolean,
|
||||
) => {
|
||||
if (inHtml) {
|
||||
visit(tree, "html", (node: Html) => {
|
||||
for (const [regex, replace] of replacements) {
|
||||
if (typeof replace === "string") {
|
||||
node.value = node.value.replace(regex, replace)
|
||||
} else {
|
||||
node.value = node.value.replace(regex, (substring: string, ...args) => {
|
||||
const replaceValue = replace(substring, ...args)
|
||||
if (typeof replaceValue === "string") {
|
||||
return replaceValue
|
||||
} else if (Array.isArray(replaceValue)) {
|
||||
return replaceValue.map(mdastToHtml).join("")
|
||||
} else if (typeof replaceValue === "object" && replaceValue !== null) {
|
||||
return mdastToHtml(replaceValue)
|
||||
} else {
|
||||
return substring
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
mdastFindReplace(tree, replacements)
|
||||
}
|
||||
|
||||
export const CommonMarkFlavoredMarkdown: QuartzTransformerPlugin<Partial<CommonMarkOptions>> = (
|
||||
userOpts,
|
||||
) => {
|
||||
@ -251,6 +286,8 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<ObsidianO
|
||||
markdownPlugins(ctx) {
|
||||
const plugins: PluggableList = []
|
||||
|
||||
const inHtml = opts.enableInHtmlEmbed
|
||||
|
||||
/*plugins.push(() => {
|
||||
return (tree: Root, file) => {
|
||||
//const replacements: [RegExp, string | ReplaceFunction][] = []
|
||||
@ -265,12 +302,20 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<ObsidianO
|
||||
//mdastFindReplace(tree, replacements)
|
||||
}
|
||||
})*/
|
||||
plugins.push(ObsidianWikilinks({ enabled: opts.wikilinks }).markdownPlugins(ctx))
|
||||
plugins.push(ObsidianHighlights({ enabled: opts.highlight }).markdownPlugins(ctx))
|
||||
plugins.push(ObsidianArrow({ enabled: opts.parseArrows }).markdownPlugins(ctx))
|
||||
plugins.push(ObsidianTags({ enabled: opts.parseTags }).markdownPlugins(ctx))
|
||||
plugins.push(ObsidianCallouts({ enabled: opts.callouts }).markdownPlugins(ctx))
|
||||
plugins.push(ObsidianMermaid({ enabled: opts.mermaid }).markdownPlugins(ctx))
|
||||
plugins.push(
|
||||
ObsidianWikilinks({ enabled: opts.wikilinks, inHtml: inHtml }).markdownPlugins(ctx),
|
||||
)
|
||||
plugins.push(
|
||||
ObsidianHighlights({ enabled: opts.highlight, inHtml: inHtml }).markdownPlugins(ctx),
|
||||
)
|
||||
plugins.push(
|
||||
ObsidianArrow({ enabled: opts.parseArrows, inHtml: inHtml }).markdownPlugins(ctx),
|
||||
)
|
||||
plugins.push(ObsidianTags({ enabled: opts.parseTags, inHtml: inHtml }).markdownPlugins(ctx))
|
||||
plugins.push(
|
||||
ObsidianCallouts({ enabled: opts.callouts, inHtml: inHtml }).markdownPlugins(ctx),
|
||||
)
|
||||
plugins.push(ObsidianMermaid({ enabled: opts.mermaid, inHtml: inHtml }).markdownPlugins(ctx))
|
||||
|
||||
return plugins
|
||||
},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user