From 2c8d0f8ab60cbf2e5e5338709e63267d466fb068 Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Mon, 22 Jan 2024 10:29:57 -0800 Subject: [PATCH] fix: more robust ofm comment handling --- quartz/plugins/transformers/ofm.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/quartz/plugins/transformers/ofm.ts b/quartz/plugins/transformers/ofm.ts index 74a4523..c067296 100644 --- a/quartz/plugins/transformers/ofm.ts +++ b/quartz/plugins/transformers/ofm.ts @@ -1,5 +1,5 @@ import { QuartzTransformerPlugin } from "../types" -import { Root, Html, Image, BlockContent, DefinitionContent, Paragraph, Code } from "mdast" +import { Root, Html, BlockContent, DefinitionContent, Paragraph, Code } from "mdast" import { Element, Literal, Root as HtmlRoot } from "hast" import { ReplaceFunction, findAndReplace as mdastFindReplace } from "mdast-util-find-and-replace" import { slug as slugAnchor } from "github-slugger" @@ -125,7 +125,7 @@ export const wikilinkRegex = new RegExp( "g", ) const highlightRegex = new RegExp(/==([^=]+)==/, "g") -const commentRegex = new RegExp(/%%(.+)%%/, "g") +const commentRegex = new RegExp(/%%[\s\S]*?%%/, "g") // from https://github.com/escwxyz/remark-obsidian-callout/blob/main/src/index.ts const calloutRegex = new RegExp(/^\[\!(\w+)\]([+-]?)/) const calloutLineRegex = new RegExp(/^> *\[\!\w+\][+-]?.*$/, "gm") @@ -151,6 +151,15 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin return { name: "ObsidianFlavoredMarkdown", textTransform(_ctx, src) { + // do comments at text level + if (opts.comments) { + if (src instanceof Buffer) { + src = src.toString() + } + + src = src.replace(commentRegex, "") + } + // pre-transform blockquotes if (opts.callouts) { if (src instanceof Buffer) { @@ -163,14 +172,6 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin }) } - // do comments at text level - if (opts.comments) { - if (src instanceof Buffer) { - src = src.toString() - } - src.replace(commentRegex, "") - } - // pre-transform wikilinks (fix anchors to things that may contain illegal syntax e.g. codeblocks, latex) if (opts.wikilinks) { if (src instanceof Buffer) {