fix(markdown): preserve literal image fallback labels

This commit is contained in:
Mohamed Boudra
2026-10-15 16:54:07 +02:00
parent f90e160ac6
commit b88b018d5f
2 changed files with 19 additions and 2 deletions
@@ -335,3 +335,16 @@ it("normalizes images following HTML code in the same HTML block", () => {
expect(prepared.render(new Map([["shot.png", "blob:shot"]]))).toContain("![](<blob:shot>)");
expect(prepared.render()).toContain("`\nx\n`");
});
it("preserves literal Markdown punctuation in unsupported and failed image labels", () => {
const label = String.raw`\*literal\* \[label\]`;
expect(
prepareMarkdownSource(`![${label}](badge.svg)`, undefined, { supportsSvg: false }).render(),
).toBe(label);
expect(
prepareMarkdownSource(`![${label}](https://example.com/badge.png)`).render(
new Map(),
new Set(["https://example.com/badge.png"]),
),
).toBe(label);
});
@@ -90,7 +90,7 @@ export function prepareMarkdownSource(
options.supportsSvg === false &&
/(?:\.svg(?:[?#]|$)|^data:image\/svg\+xml)/i.test(target.url)
) {
edits.push({ start, end, text: node.alt ?? "" });
edits.push({ start, end, text: escapeMarkdownText(node.alt ?? "") });
return;
}
if (target)
@@ -153,7 +153,7 @@ export function prepareMarkdownSource(
end: image.end,
text: url
? `![${image.alt.replace(/[[\]\\]/g, "\\$&")}](<${url}>${image.title ? ` ${JSON.stringify(image.title)}` : ""})`
: image.alt,
: escapeMarkdownText(image.alt),
});
}
let result = source;
@@ -164,3 +164,7 @@ export function prepareMarkdownSource(
},
};
}
function escapeMarkdownText(text: string): string {
return text.replace(/[!-/:-@[-`{-~]/g, "\\$&");
}