fix: prevent underscore emphasis in wikilink image filenames

Image wikilinks like `![[my_photo_2024.png]]` are broken when the
textTransform phase re-emits them as raw text, because the Markdown
parser interprets `_photo_` as emphasis (`<em>photo</em>`), destroying
the wikilink syntax before it can be resolved to an `<img>` tag.

Convert image embeds (`![[…]]`) with recognised image extensions to
standard Markdown image syntax with angle-bracket destinations
(`![alt](<filename>)`) early in the textTransform callback, so the
Markdown parser sees a proper image node instead of raw text with
underscores.

Fixes #2291

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
kanaupi 2026-02-15 20:54:26 +09:00
parent ec00a40aef
commit 7caddf6230

View File

@ -202,6 +202,17 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>>
return `${embedDisplay}[${displayAlias.replace(/^\|/, "")}](${rawFp})`
}
// Convert image embeds to standard markdown to prevent underscores
// in filenames from being parsed as emphasis by the markdown parser.
// Uses angle-bracket destination syntax to preserve special characters.
if (embedDisplay === "!" && fp) {
const ext = path.extname(fp).toLowerCase()
if ([".png", ".jpg", ".jpeg", ".gif", ".bmp", ".svg", ".webp"].includes(ext)) {
const alt = displayAlias ? displayAlias.replace(/^\|/, "").trim() : ""
return `![${alt}](<${fp}>)`
}
}
return `${embedDisplay}[[${fp}${displayAnchor}${displayAlias}]]`
})
}