Obsidian Parsers (Arrow)

This commit is contained in:
Emile Bangma 2024-09-19 09:42:56 +00:00
parent add7f3ab2e
commit 23e71e95f4
3 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,42 @@
import { QuartzTransformerPlugin } from "../../types"
import { ReplaceFunction, findAndReplace as mdastFindReplace } from "mdast-util-find-and-replace"
import { SKIP } from "unist-util-visit"
import { Root } from "mdast"
const arrowMapping: Record<string, string> = {
"->": "&rarr;",
"-->": "&rArr;",
"=>": "&rArr;",
"==>": "&rArr;",
"<-": "&larr;",
"<--": "&lArr;",
"<=": "&lArr;",
"<==": "&lArr;",
}
const arrowRegex = new RegExp(/(-{1,2}>|={1,2}>|<-{1,2}|<={1,2})/g)
export const ObsidianMarkdownArrow: QuartzTransformerPlugin = () => {
return {
name: "ObsidianMarkdownArrow",
markdownPlugins() {
return [
(tree: Root) => {
const replacements: [RegExp, string | ReplaceFunction][] = []
replacements.push([
arrowRegex,
(value: string, ..._capture: string[]) => {
const maybeArrow = arrowMapping[value]
if (maybeArrow === undefined) return SKIP
return {
type: "html",
value: `<span>${maybeArrow}</span>`,
}
},
])
mdastFindReplace(tree, replacements)
},
]
},
}
}

View File

@ -0,0 +1 @@
export { ObsidianMarkdownArrow } from "./arrows"

View File

@ -35,6 +35,8 @@ import smartypants from "remark-smartypants"
import rehypeSlug from "rehype-slug"
import rehypeAutolinkHeadings from "rehype-autolink-headings"
import { ObsidianMarkdownArrow } from "../parsers/obsidian"
export interface CommonMarkOptions {
option1: Boolean
}
@ -170,6 +172,39 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<ObsidianO
const opts = { ...defaultObsidianOptions, ...userOpts }
return {
name: "ObsidianFlavoredMarkdown",
textTransform(_ctx, src) {
return src
},
markdownPlugins(_ctx) {
const plugins: PluggableList = []
plugins.push(() => {
return (tree: Root, file) => {
//const replacements: [RegExp, string | ReplaceFunction][] = []
//const base = pathToRoot(file.data.slug!)
if (opts.parseArrows) {
ObsidianMarkdownArrow()
}
//mdastFindReplace(tree, replacements)
}
})
return plugins
},
htmlPlugins() {
const plugins: PluggableList = [rehypeRaw]
plugins.push(() => {})
return plugins
},
externalResources() {
const js: JSResource[] = []
return { js }
},
}
}