diff --git a/src/esa-utils/comment-remover.ts b/src/esa-utils/comment-remover.ts new file mode 100644 index 0000000..3608f8a --- /dev/null +++ b/src/esa-utils/comment-remover.ts @@ -0,0 +1,22 @@ +import type { Root, RootContent } from "hast"; +import { isComment } from "./nodes"; +import { visitParents } from "unist-util-visit-parents"; + +export function removeComments() { + return (tree: Root) => { + const targets: { parent: { children: RootContent[] }; idx: number }[] = []; + + visitParents(tree, (node, ancestors) => { + if (isComment(node)) { + const parent = ancestors[ancestors.length - 1]; + if (!parent) return; + const idx = parent.children.indexOf(node); + if (idx !== -1) targets.push({ parent, idx }); + } + }); + + for (const { parent, idx } of targets.sort((a, b) => b.idx - a.idx)) { + parent.children.splice(idx, 1); + } + }; +} diff --git a/src/esa-utils/faq-parser.ts b/src/esa-utils/faq-parser.ts index 8621935..46493d3 100644 --- a/src/esa-utils/faq-parser.ts +++ b/src/esa-utils/faq-parser.ts @@ -5,6 +5,7 @@ import remarkRehype from "remark-rehype"; import rehypeStringify from "rehype-stringify"; import { imageReplacer } from "./image-replacer"; import { externalLinkReplacer } from "./external-link-replacer"; +import { removeComments } from "./comment-remover"; function isH2(node: RootContent): node is Heading { return node.type === "heading" && node.depth === 2; @@ -51,6 +52,7 @@ export async function parseFaqs(ast: Root): Promise { .use(remarkRehype, { allowDangerousHtml: true }) .use(imageReplacer) .use(externalLinkReplacer) + .use(removeComments) .run(answerRoot); const answerHtml = unified() .use(rehypeStringify, { allowDangerousHtml: true }) diff --git a/src/esa-utils/nodes.ts b/src/esa-utils/nodes.ts index 9787b9b..6c0f695 100644 --- a/src/esa-utils/nodes.ts +++ b/src/esa-utils/nodes.ts @@ -80,3 +80,18 @@ export function isAnchor(node: Node | undefined): node is AnchorNode { } return true; } + +export interface CommentNode extends Node { + type: "raw"; + value: string; +} + +export function isComment(node: Node | undefined): node is CommentNode { + if (node === undefined) return false; + if ( + !(node.type === "raw" && "value" in node && typeof node.value === "string") + ) + return false; + if (!node.value.trimStart().startsWith("