fix: Line breaks inside of imported HTML image src fail import

This commit is contained in:
Tom Moor
2022-09-12 23:08:59 +01:00
parent fe3ff1215e
commit edd7aed7b2
2 changed files with 29 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
import TurndownService from "turndown";
/**
* A turndown plugin overriding inbuilt image parsing behavior
*
* @param turndownService The TurndownService instance.
*/
export default function images(turndownService: TurndownService) {
turndownService.addRule("image", {
filter: "img",
replacement(content, node) {
// @ts-expect-error getAttribute exists
const alt = cleanAttribute(node.getAttribute("alt"));
// @ts-expect-error getAttribute exists
const src = (node.getAttribute("src") || "").replace(/\n+/g, "");
// @ts-expect-error getAttribute exists
const title = cleanAttribute(node.getAttribute("title"));
const titlePart = title ? ' "' + title + '"' : "";
return src ? "![" + alt + "]" + "(" + src + titlePart + ")" : "";
},
});
}
function cleanAttribute(attribute: string) {
return attribute ? attribute.replace(/(\n+\s*)+/g, "\n") : "";
}

View File

@@ -3,6 +3,7 @@ import TurndownService from "turndown";
import breaks from "./breaks";
import confluenceCodeBlock from "./confluence-code-block";
import confluenceTaskList from "./confluence-task-list";
import images from "./images";
/**
* Turndown converts HTML to Markdown and is used in the importer code.
@@ -25,6 +26,7 @@ const service = new TurndownService({
.use(gfm)
.use(confluenceTaskList)
.use(confluenceCodeBlock)
.use(images)
.use(breaks);
export default service;