mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-19 10:54:06 -06:00
* Initial plan * Initial analysis and plan for decoupling completion Co-authored-by: saberzero1 <8161064+saberzero1@users.noreply.github.com> * docs: add @plugin annotations to transformers missing documentation Co-authored-by: saberzero1 <8161064+saberzero1@users.noreply.github.com> * docs: mark decoupling phases and success criteria as complete Co-authored-by: saberzero1 <8161064+saberzero1@users.noreply.github.com> * fix: move @plugin annotation in roam.ts to correct location Move the @plugin documentation block to immediately precede the RoamFlavoredMarkdown export, consistent with other transformer files (gfm.ts, syntax.ts, linebreaks.ts). Previously it was placed before the regex constant declarations. Co-authored-by: saberzero1 <8161064+saberzero1@users.noreply.github.com> * Changes before error encountered Co-authored-by: saberzero1 <8161064+saberzero1@users.noreply.github.com> * refactor: move documentation files from docs/ to project root Move IMPLEMENTATION_SUMMARY.md, PLUGIN_MIGRATION.md, and SECURITY_SUMMARY.md from docs/ directory to project root to keep them separate from user-facing documentation. Co-authored-by: saberzero1 <8161064+saberzero1@users.noreply.github.com> * config: add implementation docs to ignore patterns Add IMPLEMENTATION_SUMMARY.md, PLUGIN_MIGRATION.md, and SECURITY_SUMMARY.md to ignorePatterns in quartz.config.ts to exclude them from the documentation build. These files are implementation documentation for the project itself, not user-facing documentation. Co-authored-by: saberzero1 <8161064+saberzero1@users.noreply.github.com> * chore: remove build output directories from git tracking Remove public-current and public-v4 directories that were accidentally committed during build testing. These directories are already covered by .gitignore and should not be tracked in the repository. Co-authored-by: saberzero1 <8161064+saberzero1@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: saberzero1 <8161064+saberzero1@users.noreply.github.com>
121 lines
3.8 KiB
TypeScript
121 lines
3.8 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")
|
|
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")
|
|
|
|
/**
|
|
* @plugin OxHugoFlavouredMarkdown
|
|
* @category Transformer
|
|
*
|
|
* @reads None
|
|
* @writes None (transforms ox-hugo markdown to Quartz-compatible format)
|
|
*
|
|
* @dependencies None
|
|
*
|
|
* 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.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
|
|
},
|
|
htmlPlugins() {
|
|
const plugins: PluggableList = [rehypeRaw]
|
|
return plugins
|
|
},
|
|
}
|
|
}
|