fix: Line breaks inside of imported HTML image src fail import
This commit is contained in:
27
server/utils/turndown/images.ts
Normal file
27
server/utils/turndown/images.ts
Normal 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") : "";
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user