feat(markdown): preserve image width/height attribute

This commit is contained in:
Elian Doran 2025-04-05 03:01:06 +03:00
parent 1f98e75c54
commit 319cccfb15
No known key found for this signature in database
2 changed files with 27 additions and 1 deletions

View File

@ -250,4 +250,22 @@ describe("Markdown export", () => {
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
it("converts image if it has no custom properties", () => {
const html = /*html*/`<p><img src="Include Note_image.png"></p>`;
const expected = `![](Include%20Note_image.png)`;
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
it("preserves image verbatim if it has a width or height attribute", () => {
const scenarios = [
`<img src="Include Note_image.png" width="16" height="16">`,
`<img src="Include Note_image.png" width="16">`,
`<img src="Include Note_image.png" height="16">`
];
for (const expected of scenarios) {
const html = /*html*/`<p>${expected}</p>`;
expect(markdownExportService.toMarkdown(html)).toBe(expected);
}
});
});

View File

@ -97,7 +97,15 @@ function buildImageFilter() {
const imageFilter: TurndownService.Rule = {
filter: "img",
replacement(content, node) {
replacement(content, _node) {
const node = _node as HTMLElement;
// Preserve image verbatim if it has a width or height attribute.
if (node.hasAttribute("width") || node.hasAttribute("height")) {
return node.outerHTML;
}
// TODO: Deduplicate with upstream.
const untypedNode = (node as any);
const alt = escapeMarkdown(cleanAttribute(untypedNode.getAttribute('alt')))
const src = escapeLinkDestination(untypedNode.getAttribute('src') || '')