Add support for iframes in imported HTML

This commit is contained in:
Tom Moor
2023-08-28 09:19:53 -04:00
parent 3e6a22e369
commit b002d51ace
2 changed files with 23 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
import TurndownService from "turndown";
/**
* A turndown plugin to convert iframes to markdown links.
*
* @param turndownService The TurndownService instance.
*/
export default function images(turndownService: TurndownService) {
turndownService.addRule("frames", {
filter: "iframe",
replacement(content, node: HTMLIFrameElement) {
const src = (node.getAttribute("src") || "").replace(/\n+/g, "");
const title = cleanAttribute(node.getAttribute("title") || "");
return src ? "[" + (title || src) + "]" + "(" + src + ")" : "";
},
});
}
function cleanAttribute(attribute: string) {
return attribute ? attribute.replace(/(\n+\s*)+/g, "\n") : "";
}

View File

@@ -4,6 +4,7 @@ import breaks from "./breaks";
import confluenceCodeBlock from "./confluence-code-block";
import confluenceTaskList from "./confluence-task-list";
import emptyLists from "./empty-lists";
import frames from "./frames";
import images from "./images";
/**
@@ -25,6 +26,7 @@ const service = new TurndownService({
})
.remove(["script", "style", "title", "head"])
.use(gfm)
.use(frames)
.use(confluenceTaskList)
.use(confluenceCodeBlock)
.use(images)