Skip to content

Commit

Permalink
fix(bug): ugh build went wrong some where [NEED FIXING]
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
  • Loading branch information
aarnphm committed Dec 14, 2024
1 parent 258baf2 commit 547afa1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 70 deletions.
2 changes: 1 addition & 1 deletion quartz/components/TableOfContents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export default ((userOpts?: Partial<Options>) => {
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[] = []
Expand Down
138 changes: 70 additions & 68 deletions quartz/plugins/transformers/pseudocode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,77 +153,79 @@ export const Pseudocode: QuartzTransformerPlugin<Partial<Options>> = (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)
})
}
},
]
},
Expand Down
3 changes: 2 additions & 1 deletion quartz/processors/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 547afa1

Please sign in to comment.