summaryrefslogtreecommitdiffhomepage
path: root/vhosts/blog/nuldoc-src/djot
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-06-15 13:12:46 +0900
committernsfisis <nsfisis@gmail.com>2025-06-15 13:13:24 +0900
commita65bb9609284d273f0aa232dbaf69597c87f5a12 (patch)
treebec63ad11f79fee55dd07c27625c5f692af2b7aa /vhosts/blog/nuldoc-src/djot
parentc2252e60d3ab192271e4241943dd165087567af8 (diff)
downloadnsfisis.dev-a65bb9609284d273f0aa232dbaf69597c87f5a12.tar.gz
nsfisis.dev-a65bb9609284d273f0aa232dbaf69597c87f5a12.tar.zst
nsfisis.dev-a65bb9609284d273f0aa232dbaf69597c87f5a12.zip
feat(blog/nuldoc): merge consecutive text nodes
Diffstat (limited to 'vhosts/blog/nuldoc-src/djot')
-rw-r--r--vhosts/blog/nuldoc-src/djot/to_html.ts39
1 files changed, 39 insertions, 0 deletions
diff --git a/vhosts/blog/nuldoc-src/djot/to_html.ts b/vhosts/blog/nuldoc-src/djot/to_html.ts
index 3a6c1e9f..5ea9b57d 100644
--- a/vhosts/blog/nuldoc-src/djot/to_html.ts
+++ b/vhosts/blog/nuldoc-src/djot/to_html.ts
@@ -13,6 +13,7 @@ import {
} from "../dom.ts";
export default async function toHtml(doc: Document): Promise<Document> {
+ mergeConsecutiveTextNodes(doc);
removeUnnecessaryTextNode(doc);
transformLinkLikeToAnchorElement(doc);
transformSectionIdAttribute(doc);
@@ -23,9 +24,47 @@ export default async function toHtml(doc: Document): Promise<Document> {
traverseFootnotes(doc);
removeUnnecessaryParagraphNode(doc);
await transformAndHighlightCodeBlockElement(doc);
+ mergeConsecutiveTextNodes(doc);
return doc;
}
+function mergeConsecutiveTextNodes(doc: Document) {
+ forEachChildRecursively(doc.root, (n) => {
+ if (n.kind !== "element") {
+ return;
+ }
+
+ const newChildren: Node[] = [];
+ let currentTextContent = "";
+
+ for (const child of n.children) {
+ if (child.kind === "text" && !child.raw) {
+ currentTextContent += child.content;
+ } else {
+ if (currentTextContent !== "") {
+ newChildren.push({
+ kind: "text",
+ content: currentTextContent,
+ raw: false,
+ });
+ currentTextContent = "";
+ }
+ newChildren.push(child);
+ }
+ }
+
+ if (currentTextContent !== "") {
+ newChildren.push({
+ kind: "text",
+ content: currentTextContent,
+ raw: false,
+ });
+ }
+
+ n.children = newChildren;
+ });
+}
+
function removeUnnecessaryTextNode(doc: Document) {
forEachChildRecursively(doc.root, (n) => {
if (n.kind !== "element") {