From 547afa1c04e99254adcf9b3c7604310faa050e0d Mon Sep 17 00:00:00 2001 From: Aaron Pham Date: Sat, 14 Dec 2024 10:36:27 -0500 Subject: [PATCH] fix(bug): ugh build went wrong some where [NEED FIXING] Signed-off-by: Aaron Pham --- quartz/components/TableOfContents.tsx | 2 +- quartz/plugins/transformers/pseudocode.ts | 138 +++++++++++----------- quartz/processors/parse.ts | 3 +- 3 files changed, 73 insertions(+), 70 deletions(-) diff --git a/quartz/components/TableOfContents.tsx b/quartz/components/TableOfContents.tsx index 7e696aac..4753705c 100644 --- a/quartz/components/TableOfContents.tsx +++ b/quartz/components/TableOfContents.tsx @@ -164,7 +164,7 @@ export default ((userOpts?: Partial) => { return htmlToJsx(fileData.filePath!, tocAst) } - mutateTransclude(tree as Root, allFiles, fileData) + // mutateTransclude(tree as Root, allFiles, fileData) // const entries = extractTransclude(clone(tree) as Root, allFiles, fileData.toc) const sectionToc: TocEntry[] = [] diff --git a/quartz/plugins/transformers/pseudocode.ts b/quartz/plugins/transformers/pseudocode.ts index 4bbfe7d0..0d06cd97 100644 --- a/quartz/plugins/transformers/pseudocode.ts +++ b/quartz/plugins/transformers/pseudocode.ts @@ -153,77 +153,79 @@ export const Pseudocode: QuartzTransformerPlugin> = (userOpts) }, htmlPlugins() { return [ - () => (tree: HTMLRoot, _file) => { - visit(tree, "raw", (node: Literal, index, parent) => { - const lineNoMatch = node.value.match(/data-line-number=([^>\s]+)/) - if (!lineNoMatch || !node.value.includes(`class="${opts.css}"`)) { - return - } - const lineNo = lineNoMatch[1].toLowerCase() - const enableLineNumber = lineNo === "true" - - // PERF: we are currently doing one round trip from text -> html -> hast - // pseudocode (katex backend) --|renderToString|--> html string --|fromHtml|--> hast - // ideally, we should cut this down to render directly to hast - const value = latexBlock.shift() - const [inlineMacros, algo] = extractInlineMacros(value ?? "") - // TODO: Might be able to optimize. - // find all $ enclosements in source, and add the preamble. - const mathRegex = /\$(.*?)\$/g - const algoWithPreamble = algo.replace(mathRegex, (_, p1) => { - return `$${inlineMacros}${p1}$` - }) - - const markup = renderToString(algoWithPreamble!, { - ...opts?.renderer, - lineNumber: enableLineNumber, - }) - if (opts.removeCaptionCount) { - node.value = removeCaptionCount(markup, opts?.renderer?.titlePrefix ?? "Algorithm") - } else { - node.value = markup - } - - const htmlNode = fromHtml(node.value, { fragment: true }) - const renderedContainer = htmlNode.children[0] as Element - renderedContainer.properties.dataInlineMacros = inlineMacros - renderedContainer.properties.dataSettings = JSON.stringify(opts) - - const button: Element = h( - "span", - { - type: "button", - class: "clipboard-button ps-clipboard", - ariaLabel: "Copy pseudocode to clipboard", - ariaHidden: true, - tabindex: -1, - }, - [ - s("svg", { width: 16, height: 16, viewbox: "0 0 16 16", class: "copy-icon" }, [ - s("use", { href: "#github-copy" }), - ]), - s("svg", { width: 16, height: 16, viewbox: "0 0 16 16", class: "check-icon" }, [ - s("use", { - href: "#github-check", - fillRule: "evenodd", - fill: "rgb(63, 185, 80)", - }), - ]), - ], - ) - const mathML: Element = h("span", { class: "ps-mathml" }, [ - h("math", { xmlns: "http://www.w3.org/1998/Math/MathML" }, [ - h("semantics", [ - h("annotation", { encoding: "application/x-tex" }, [ - { type: "text", value: JSON.stringify(algoWithPreamble) }, + () => (tree: HTMLRoot, file) => { + if (file.data.pseudocode) { + visit(tree, "raw", (node: Literal, index, parent) => { + const lineNoMatch = node.value.match(/data-line-number=([^>\s]+)/) + if (!lineNoMatch || !node.value.includes(`class="${opts.css}"`)) { + return + } + const lineNo = lineNoMatch[1].toLowerCase() + const enableLineNumber = lineNo === "true" + + // PERF: we are currently doing one round trip from text -> html -> hast + // pseudocode (katex backend) --|renderToString|--> html string --|fromHtml|--> hast + // ideally, we should cut this down to render directly to hast + const value = latexBlock.shift() + const [inlineMacros, algo] = extractInlineMacros(value ?? "") + // TODO: Might be able to optimize. + // find all $ enclosements in source, and add the preamble. + const mathRegex = /\$(.*?)\$/g + const algoWithPreamble = algo.replace(mathRegex, (_, p1) => { + return `$${inlineMacros}${p1}$` + }) + + const markup = renderToString(algoWithPreamble!, { + ...opts?.renderer, + lineNumber: enableLineNumber, + }) + if (opts.removeCaptionCount) { + node.value = removeCaptionCount(markup, opts?.renderer?.titlePrefix ?? "Algorithm") + } else { + node.value = markup + } + + const htmlNode = fromHtml(node.value, { fragment: true }) + const renderedContainer = htmlNode.children[0] as Element + renderedContainer.properties.dataInlineMacros = inlineMacros + renderedContainer.properties.dataSettings = JSON.stringify(opts) + + const button: Element = h( + "span", + { + type: "button", + class: "clipboard-button ps-clipboard", + ariaLabel: "Copy pseudocode to clipboard", + ariaHidden: true, + tabindex: -1, + }, + [ + s("svg", { width: 16, height: 16, viewbox: "0 0 16 16", class: "copy-icon" }, [ + s("use", { href: "#github-copy" }), + ]), + s("svg", { width: 16, height: 16, viewbox: "0 0 16 16", class: "check-icon" }, [ + s("use", { + href: "#github-check", + fillRule: "evenodd", + fill: "rgb(63, 185, 80)", + }), + ]), + ], + ) + const mathML: Element = h("span", { class: "ps-mathml" }, [ + h("math", { xmlns: "http://www.w3.org/1998/Math/MathML" }, [ + h("semantics", [ + h("annotation", { encoding: "application/x-tex" }, [ + { type: "text", value: JSON.stringify(algoWithPreamble) }, + ]), ]), ]), - ]), - ]) + ]) - renderedContainer.children = [button, mathML, ...renderedContainer.children] - parent!.children.splice(index!, 1, renderedContainer) - }) + renderedContainer.children = [button, mathML, ...renderedContainer.children] + parent!.children.splice(index!, 1, renderedContainer) + }) + } }, ] }, diff --git a/quartz/processors/parse.ts b/quartz/processors/parse.ts index 2bd530c6..e83146f2 100644 --- a/quartz/processors/parse.ts +++ b/quartz/processors/parse.ts @@ -103,7 +103,8 @@ export function createFileParser(ctx: BuildCtx, fps: FilePath[]) { console.log(`[process] ${fp} -> ${file.data.slug} (${perf.timeSince()})`) } } catch (err) { - trace(`\nFailed to process \`${fp}\``, err as Error) + // FIXME: wtf went wrong, check pseudocode and toc parsing + // trace(`\nFailed to process \`${fp}\``, err as Error) } }