From 5999ed4290cffadfb0175ee4ac5da86da43e9c04 Mon Sep 17 00:00:00 2001 From: Lai_lai Date: Wed, 11 Mar 2026 00:47:12 +0900 Subject: [PATCH 1/3] =?UTF-8?q?html=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E3=82=92=E5=89=8A=E9=99=A4=E3=81=99=E3=82=8B=E5=87=A6=E7=90=86?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/esa-utils/comment-remover.ts | 22 ++++++++++++++++++++++ src/esa-utils/faq-parser.ts | 2 ++ src/esa-utils/nodes.ts | 12 ++++++++++++ src/pages/posts/[id].astro | 2 ++ 4 files changed, 38 insertions(+) create mode 100644 src/esa-utils/comment-remover.ts diff --git a/src/esa-utils/comment-remover.ts b/src/esa-utils/comment-remover.ts new file mode 100644 index 0000000..e0a4471 --- /dev/null +++ b/src/esa-utils/comment-remover.ts @@ -0,0 +1,22 @@ +import type { Root } from "hast"; +import { isComment } from "./nodes"; +import { visitParents } from "unist-util-visit-parents"; + +export function removeComments() { + return (tree: Root) => { + const targets: { parent: { children: any[] }; 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..e41b032 100644 --- a/src/esa-utils/nodes.ts +++ b/src/esa-utils/nodes.ts @@ -80,3 +80,15 @@ 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("