fix(markdown): keep the first image reference definition

This commit is contained in:
Mohamed Boudra
2026-10-15 16:25:58 +02:00
parent e851a71ded
commit f829ce6ef3
2 changed files with 11 additions and 1 deletions
@@ -302,3 +302,12 @@ it("preserves angle-bracket image destinations and authored link titles during H
'![shot](<blob:shot>) and [open](<my file.png> "<b>literal title</b>")',
);
});
it("uses the first repeated image reference definition", () => {
const source = "![chart][image]\n\n[image]: first.png\n[image]: second.png";
const prepared = prepareMarkdownSource(source);
expect(prepared.images.map((image) => image.source)).toEqual(["first.png"]);
expect(prepared.render(new Map([["first.png", "blob:first"]]))).toBe(
"![chart](<blob:first>)\n\n[image]: first.png\n[image]: second.png",
);
});
@@ -74,7 +74,8 @@ export function prepareMarkdownSource(
const definitions = new Map<string, { url: string; title?: string | null }>();
function definitionsIn(node: Nodes) {
if (node.type === "definition") definitions.set(node.identifier, node);
if (node.type === "definition" && !definitions.has(node.identifier))
definitions.set(node.identifier, node);
if ("children" in node) node.children.forEach(definitionsIn);
}
definitionsIn(getTree());