summaryrefslogtreecommitdiffhomepage
path: root/vhosts/blog/nuldoc-src/djot/to_html.ts
diff options
context:
space:
mode:
Diffstat (limited to 'vhosts/blog/nuldoc-src/djot/to_html.ts')
-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") {