diff --git a/quartz/plugins/transformers/oxhugofm.ts b/quartz/plugins/transformers/oxhugofm.ts index 303566e..4fb5e2c 100644 --- a/quartz/plugins/transformers/oxhugofm.ts +++ b/quartz/plugins/transformers/oxhugofm.ts @@ -27,7 +27,10 @@ const defaultOptions: Options = { const relrefRegex = new RegExp(/\[([^\]]+)\]\(\{\{< relref "([^"]+)" >\}\}\)/, "g") const predefinedHeadingIdRegex = new RegExp(/(.*) {#(?:.*)}/, "g") const hugoShortcodeRegex = new RegExp(/{{(.*)}}/, "g") -const figureTagRegex = new RegExp(/< ?figure src="(.*)" ?>/, "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. ). +const figureShortcodeRegex = new RegExp(/{{<\s*figure\b[^}]*\bsrc="([^"]*)"[^}]*>}}/, "g") // \\\\\( -> matches \\( // (.+?) -> Lazy match for capturing the equation // \\\\\) -> matches \\) @@ -70,19 +73,19 @@ export const OxHugoFlavouredMarkdown: QuartzTransformerPlugin> }) } - if (opts.removeHugoShortcode) { + if (opts.replaceFigureWithMdImg) { src = src.toString() - src = src.replaceAll(hugoShortcodeRegex, (_value, ...capture) => { - const [scContent] = capture - return scContent + src = src.replaceAll(figureShortcodeRegex, (_value, ...capture) => { + const [imgSrc] = capture + return `![](${imgSrc})` }) } - if (opts.replaceFigureWithMdImg) { + if (opts.removeHugoShortcode) { src = src.toString() - src = src.replaceAll(figureTagRegex, (_value, ...capture) => { - const [src] = capture - return `![](${src})` + src = src.replaceAll(hugoShortcodeRegex, (_value, ...capture) => { + const [scContent] = capture + return scContent }) }