From b002d51ace2893a09f3ec0e15188a15f73de4553 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 28 Aug 2023 09:19:53 -0400 Subject: [PATCH] Add support for iframes in imported HTML --- server/utils/turndown/frames.ts | 21 +++++++++++++++++++++ server/utils/turndown/index.ts | 2 ++ 2 files changed, 23 insertions(+) create mode 100644 server/utils/turndown/frames.ts diff --git a/server/utils/turndown/frames.ts b/server/utils/turndown/frames.ts new file mode 100644 index 000000000..f73565598 --- /dev/null +++ b/server/utils/turndown/frames.ts @@ -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") : ""; +} diff --git a/server/utils/turndown/index.ts b/server/utils/turndown/index.ts index 03eaab61d..56b093602 100644 --- a/server/utils/turndown/index.ts +++ b/server/utils/turndown/index.ts @@ -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)