Files
system-aspect/.github/quartz/quartz/plugins/transformers/comments.ts
T
2023-12-20 22:59:36 +01:00

46 lines
1.1 KiB
TypeScript

import { QuartzTransformerPlugin } from "../types"
import { Root } from "mdast"
import { Element, Root as HtmlRoot } from "hast"
import { ReplaceFunction, findAndReplace as mdastFindReplace } from "mdast-util-find-and-replace"
import { PluggableList } from "unified"
const commentRegex = new RegExp(/%%(.+)%%/, "g")
export const Comments: QuartzTransformerPlugin<undefined> = () => {
return {
name: "Comments",
markdownPlugins() {
const plugins: PluggableList = []
// regex replacements
plugins.push(() => {
return (tree: Root) => {
const replacements: [RegExp, string | ReplaceFunction][] = []
replacements.push([
commentRegex,
(_value: string, ..._capture: string[]) => {
const [inner] = _capture
return {
type: "html",
value: `<span class="text-comment">${inner}</span>`,
}
},
])
mdastFindReplace(tree, replacements)
}
})
return plugins
},
}
}
declare module "vfile" {
interface DataMap {
blocks: Record<string, Element>
htmlAst: HtmlRoot
}
}