From a70e846b0a14f3065bbf613221fee0e3b09c12ef Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Wed, 02 Aug 2023 05:47:16 +0000
Subject: [PATCH] flag to allow ofm replace in html embed
---
quartz/plugins/transformers/ofm.ts | 40 +++++++++++++++++++++++++++++++++++++++-
1 files changed, 39 insertions(+), 1 deletions(-)
diff --git a/quartz/plugins/transformers/ofm.ts b/quartz/plugins/transformers/ofm.ts
index 4a45b02..23b97d6 100644
--- a/quartz/plugins/transformers/ofm.ts
+++ b/quartz/plugins/transformers/ofm.ts
@@ -1,7 +1,7 @@
import { PluggableList } from "unified"
import { QuartzTransformerPlugin } from "../types"
import { Root, HTML, BlockContent, DefinitionContent, Code } from "mdast"
-import { findAndReplace } from "mdast-util-find-and-replace"
+import { Replace, findAndReplace as mdastFindReplace } from "mdast-util-find-and-replace"
import { slug as slugAnchor } from "github-slugger"
import rehypeRaw from "rehype-raw"
import { visit } from "unist-util-visit"
@@ -10,6 +10,9 @@
// @ts-ignore
import calloutScript from "../../components/scripts/callout.inline.ts"
import { FilePath, canonicalizeServer, pathToRoot, slugTag, slugifyFilePath } from "../../path"
+import { toHast } from "mdast-util-to-hast"
+import { toHtml } from "hast-util-to-html"
+import { PhrasingContent } from "mdast-util-find-and-replace/lib"
export interface Options {
comments: boolean
@@ -18,6 +21,7 @@
callouts: boolean
mermaid: boolean
parseTags: boolean
+ enableInHtmlEmbed: boolean
}
const defaultOptions: Options = {
@@ -27,6 +31,7 @@
callouts: true,
mermaid: true,
parseTags: true,
+ enableInHtmlEmbed: false,
}
const icons = {
@@ -117,6 +122,39 @@
userOpts,
) => {
const opts = { ...defaultOptions, ...userOpts }
+
+ const findAndReplace = opts.enableInHtmlEmbed
+ ? (tree: Root, regex: RegExp, replace?: Replace | null | undefined) => {
+ if (replace) {
+ const mdastToHtml = (ast: PhrasingContent) => {
+ const hast = toHast(ast, { allowDangerousHtml: true })!
+ return toHtml(hast, { allowDangerousHtml: true })
+ }
+
+ visit(tree, "html", (node: HTML) => {
+ if (typeof replace === "string") {
+ node.value = node.value.replace(regex, replace)
+ } else {
+ node.value = node.value.replaceAll(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, regex, replace)
+ }
+ : mdastFindReplace
+
return {
name: "ObsidianFlavoredMarkdown",
textTransform(_ctx, src) {
--
Gitblit v1.10.0