fix(markdown): normalize HTML following code elements

This commit is contained in:
Mohamed Boudra
2026-10-15 16:53:16 +02:00
parent 65f16f6322
commit f90e160ac6
2 changed files with 12 additions and 9 deletions
+5 -9
View File
@@ -85,15 +85,11 @@ export function normalizeMarkdownHtml(source: string): string {
// Inline HTML code has separate opening/closing AST nodes. Convert the pair
// together so its contents keep backslashes and get a safe backtick delimiter.
const code = /^<code\b[^>]*>[\s\S]*?<\/code\s*>/i.exec(source.slice(range.start));
if (code) {
result += convert(code[0], range.start);
cursor = range.start + code[0].length;
} else {
result += source
.slice(range.start, range.end)
.replace(HTML, (raw, offset) => convert(raw, range.start + offset));
cursor = range.end;
}
const end = code ? Math.max(range.end, range.start + code[0].length) : range.end;
result += source
.slice(range.start, end)
.replace(HTML, (raw, offset) => convert(raw, range.start + offset));
cursor = end;
}
return result + source.slice(cursor);
}
@@ -328,3 +328,10 @@ it("keeps paragraph, heading and table-row boundaries when stripping HTML", () =
const rendered = prepareMarkdownSource(source).render();
expect(rendered).toBe("First\n\nSecond \nline\n\nHeading\n\nA B\n\nC D");
});
it("normalizes images following HTML code in the same HTML block", () => {
const prepared = prepareMarkdownSource('<code>\nx\n</code>\n<img src="shot.png">');
expect(prepared.images.map((image) => image.source)).toEqual(["shot.png"]);
expect(prepared.render(new Map([["shot.png", "blob:shot"]]))).toContain("![](<blob:shot>)");
expect(prepared.render()).toContain("`\nx\n`");
});