Files
outline/server/utils/turndown/sanitizeTables.ts
Tom Moor 7c2a3142a4 fix: Paragraphs lost in table cells
closes OLN-262
2024-02-20 20:58:46 -05:00

31 lines
830 B
TypeScript

import TurndownService from "turndown";
import { inHtmlContext } from "./utils";
/**
* A turndown plugin for removing incompatible nodes from tables.
*
* @param turndownService The TurndownService instance.
*/
export default function sanitizeTables(turndownService: TurndownService) {
turndownService.addRule("headingsInTables", {
filter(node) {
return (
["H1", "H2", "H3", "H4", "H5", "H6"].includes(node.nodeName) &&
inHtmlContext(node, "table")
);
},
replacement(content) {
return `**${content.trim()}**`;
},
});
turndownService.addRule("paragraphsInCells", {
filter(node) {
return node.nodeName === "P" && inHtmlContext(node, "table");
},
replacement(content, node) {
return content.trim() + (node.nextSibling ? "\\n" : "");
},
});
}