mirror of
https://gitlab.com/MisterBiggs/brain-quartz.git
synced 2025-07-26 16:21:26 +00:00
.github
content
docs
quartz
cli
components
i18n
plugins
emitters
filters
transformers
description.ts
frontmatter.ts
gfm.ts
index.ts
lastmod.ts
latex.ts
linebreaks.ts
links.ts
ofm.ts
oxhugofm.ts
syntax.ts
toc.ts
index.ts
types.ts
vfile.ts
processors
static
styles
util
bootstrap-cli.mjs
bootstrap-worker.mjs
build.ts
cfg.ts
depgraph.test.ts
depgraph.ts
worker.ts
.gitattributes
.gitignore
.npmrc
.prettierignore
.prettierrc
CODE_OF_CONDUCT.md
Dockerfile
LICENSE.txt
README.md
globals.d.ts
index.d.ts
package-lock.json
package.json
quartz.config.ts
quartz.layout.ts
tsconfig.json
ox-hugo currently supports the following syntax for latex equations: - https://orgmode.org/manual/LaTeX-fragments.html - https://ox-hugo.scripter.co/doc/equations This syntax is supported by mathjax as is mentioned in the ox-hugo documentation. But quartz uses remark-math which has some issues with the \( \) syntax. See https://github.com/remarkjs/remark-math/issues/39 This change adds few more transformations to the OxHugoFlavouredMarkdown plugin, which makes a best effort conversion of this syntax into what the Quartz Latex transformer plugin supports. With these changes, the generated files show latex formatting with default quartz configuration. Sidenote on `\_` escape by ox-hugo: ox-hugo escapes, _ using \_, we match against it after we transform equations into what quartz supports($$ and $). This could be achieved using lookaround like regex as follows ```js (?<=(\$|\$\$)[\s\S]*) -> Positive lookbehind for $ or $$ \\_ -> Matches \_ (?=[\s\S]*(?:\1)) Positive lookahead for $ or $$ if matched const escapedUnderscoreRegex = new RegExp(/(?<=(\$|\$\$)[\s\S]*)\\_(?=[\s\S]*(?:\1))/, "g") ```` But since lookahead/behind can slow things down on large files, we just look up all equations with $ and $$ delimiters and then try replacing \_
109 lines
3.4 KiB
TypeScript
109 lines
3.4 KiB
TypeScript
import { QuartzTransformerPlugin } from "../types"
|
|
|
|
export interface Options {
|
|
/** Replace {{ relref }} with quartz wikilinks []() */
|
|
wikilinks: boolean
|
|
/** Remove pre-defined anchor (see https://ox-hugo.scripter.co/doc/anchors/) */
|
|
removePredefinedAnchor: boolean
|
|
/** Remove hugo shortcode syntax */
|
|
removeHugoShortcode: boolean
|
|
/** Replace <figure/> with ![]() */
|
|
replaceFigureWithMdImg: boolean
|
|
|
|
/** Replace org latex fragments with $ and $$ */
|
|
replaceOrgLatex: boolean
|
|
}
|
|
|
|
const defaultOptions: Options = {
|
|
wikilinks: true,
|
|
removePredefinedAnchor: true,
|
|
removeHugoShortcode: true,
|
|
replaceFigureWithMdImg: true,
|
|
replaceOrgLatex: true,
|
|
}
|
|
|
|
const relrefRegex = new RegExp(/\[([^\]]+)\]\(\{\{< relref "([^"]+)" >\}\}\)/, "g")
|
|
const predefinedHeadingIdRegex = new RegExp(/(.*) {#(?:.*)}/, "g")
|
|
const hugoShortcodeRegex = new RegExp(/{{(.*)}}/, "g")
|
|
const figureTagRegex = new RegExp(/< ?figure src="(.*)" ?>/, "g")
|
|
// \\\\\( -> matches \\(
|
|
// (.+?) -> Lazy match for capturing the equation
|
|
// \\\\\) -> matches \\)
|
|
const inlineLatexRegex = new RegExp(/\\\\\((.+?)\\\\\)/, "g")
|
|
// (?:\\begin{equation}|\\\\\(|\\\\\[) -> start of equation
|
|
// ([\s\S]*?) -> Matches the block equation
|
|
// (?:\\\\\]|\\\\\)|\\end{equation}) -> end of equation
|
|
const blockLatexRegex = new RegExp(
|
|
/(?:\\begin{equation}|\\\\\(|\\\\\[)([\s\S]*?)(?:\\\\\]|\\\\\)|\\end{equation})/,
|
|
"g",
|
|
)
|
|
// \$\$[\s\S]*?\$\$ -> Matches block equations
|
|
// \$.*?\$ -> Matches inline equations
|
|
const quartzLatexRegex = new RegExp(/\$\$[\s\S]*?\$\$|\$.*?\$/, "g")
|
|
|
|
/**
|
|
* ox-hugo is an org exporter backend that exports org files to hugo-compatible
|
|
* markdown in an opinionated way. This plugin adds some tweaks to the generated
|
|
* markdown to make it compatible with quartz but the list of changes applied it
|
|
* is not exhaustive.
|
|
* */
|
|
export const OxHugoFlavouredMarkdown: QuartzTransformerPlugin<Partial<Options> | undefined> = (
|
|
userOpts,
|
|
) => {
|
|
const opts = { ...defaultOptions, ...userOpts }
|
|
return {
|
|
name: "OxHugoFlavouredMarkdown",
|
|
textTransform(_ctx, src) {
|
|
if (opts.wikilinks) {
|
|
src = src.toString()
|
|
src = src.replaceAll(relrefRegex, (value, ...capture) => {
|
|
const [text, link] = capture
|
|
return `[${text}](${link})`
|
|
})
|
|
}
|
|
|
|
if (opts.removePredefinedAnchor) {
|
|
src = src.toString()
|
|
src = src.replaceAll(predefinedHeadingIdRegex, (value, ...capture) => {
|
|
const [headingText] = capture
|
|
return headingText
|
|
})
|
|
}
|
|
|
|
if (opts.removeHugoShortcode) {
|
|
src = src.toString()
|
|
src = src.replaceAll(hugoShortcodeRegex, (value, ...capture) => {
|
|
const [scContent] = capture
|
|
return scContent
|
|
})
|
|
}
|
|
|
|
if (opts.replaceFigureWithMdImg) {
|
|
src = src.toString()
|
|
src = src.replaceAll(figureTagRegex, (value, ...capture) => {
|
|
const [src] = capture
|
|
return ``
|
|
})
|
|
}
|
|
|
|
if (opts.replaceOrgLatex) {
|
|
src = src.toString()
|
|
src = src.replaceAll(inlineLatexRegex, (value, ...capture) => {
|
|
const [eqn] = capture
|
|
return `$${eqn}$`
|
|
})
|
|
src = src.replaceAll(blockLatexRegex, (value, ...capture) => {
|
|
const [eqn] = capture
|
|
return `$$${eqn}$$`
|
|
})
|
|
|
|
// ox-hugo escapes _ as \_
|
|
src = src.replaceAll(quartzLatexRegex, (value) => {
|
|
return value.replaceAll("\\_", "_")
|
|
})
|
|
}
|
|
return src
|
|
},
|
|
}
|
|
}
|