From 23d2cfbfd3e31b405ec0c8783f618029c708618f Mon Sep 17 00:00:00 2001 From: kiycoh Date: Fri, 26 Dec 2025 21:54:24 +0100 Subject: [PATCH] feat: new marker interface for highlight parsing logic and validation --- quartz/plugins/transformers/ofm.ts | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/quartz/plugins/transformers/ofm.ts b/quartz/plugins/transformers/ofm.ts index bfc9aae5e..d2a0cfed2 100644 --- a/quartz/plugins/transformers/ofm.ts +++ b/quartz/plugins/transformers/ofm.ts @@ -7,6 +7,7 @@ import { DefinitionContent, Paragraph, Code, + Parent, } from "mdast" import { Element, Literal, Root as HtmlRoot } from "hast" import { ReplaceFunction, findAndReplace as mdastFindReplace } from "mdast-util-find-and-replace" @@ -100,6 +101,19 @@ const arrowMapping: Record = { "<=": "⇐", "<==": "⇐", } +// Marker node used for highlighting +interface HighlightMarker { + type: "highlightMarker" +} + +function isHighlightMarker(node: unknown): node is HighlightMarker { + return ( + typeof node === "object" && + node !== null && + "type" in node && + (node as { type: unknown }).type === "highlightMarker" + ) +} function canonicalizeCallout(calloutName: string): keyof typeof calloutMapping { const normalizedCallout = calloutName.toLowerCase() as keyof typeof calloutMapping @@ -391,11 +405,13 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin> visit(tree, (node) => { if (!("children" in node)) return - const children = (node as any).children as any[] + const parent = node as Parent + const children = parent.children + if (children.length === 0) return const markers: number[] = [] for (let i = 0; i < children.length; i++) { - if (children[i].type === "highlightMarker") { + if (isHighlightMarker(children[i])) { markers.push(i) } } @@ -413,11 +429,11 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin> const htmlContent = content .map((n) => { const hast = toHast(n, { allowDangerousHtml: true }) - return toHtml(hast as any, { allowDangerousHtml: true }) + return hast ? toHtml(hast, { allowDangerousHtml: true }) : "" }) .join("") - const newNode = { + const newNode: Html = { type: "html", value: `${htmlContent}`, }