forked from github/quartz
- glob.ts: add optional respectGitignore param (default true) so callers
can opt out of gitignore filtering for generated directories
- build.ts: pass respectGitignore=false when globbing content/ so
gitignored-but-generated markdown files are always picked up
- static.ts: copy top-level static/ → output/ root with gitignore disabled,
mirroring Hugo's convention so ox-hugo images at static/ox-hugo/* are
served at /ox-hugo/* as expected by the exported markdown src paths
- oxhugofm.ts: run replaceFigureWithMdImg before removeHugoShortcode so the
precise figure regex sees the intact shortcode before the generic {{.*}}
stripper destroys it; also upgrade figureTagRegex to figureShortcodeRegex
which matches the full shortcode form ox-hugo actually emits
116 lines
3.9 KiB
TypeScript
116 lines
3.9 KiB
TypeScript
import { QuartzTransformerPlugin } from "../types"
|
|
import rehypeRaw from "rehype-raw"
|
|
import { PluggableList } from "unified"
|
|
|
|
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")
|
|
// Matches the full Hugo {{< figure src="..." ... >}} shortcode and captures src.
|
|
// Must run before the generic shortcode stripper to avoid partial-match issues
|
|
// with captions that contain HTML (e.g. <span class="figure-number">).
|
|
const figureShortcodeRegex = new RegExp(/{{<\s*figure\b[^}]*\bsrc="([^"]*)"[^}]*>}}/, "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>> = (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.replaceFigureWithMdImg) {
|
|
src = src.toString()
|
|
src = src.replaceAll(figureShortcodeRegex, (_value, ...capture) => {
|
|
const [imgSrc] = capture
|
|
return ``
|
|
})
|
|
}
|
|
|
|
if (opts.removeHugoShortcode) {
|
|
src = src.toString()
|
|
src = src.replaceAll(hugoShortcodeRegex, (_value, ...capture) => {
|
|
const [scContent] = capture
|
|
return scContent
|
|
})
|
|
}
|
|
|
|
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
|
|
},
|
|
htmlPlugins() {
|
|
const plugins: PluggableList = [rehypeRaw]
|
|
return plugins
|
|
},
|
|
}
|
|
}
|