Skip to content

Build your personal knowledge base with Trilium Notes

fix(export): split exported containers at their blocks

Elian Doran9aaa97c5e64c
diff --git a/packages/trilium-core/src/services/export/format_html.spec.ts b/packages/trilium-core/src/services/export/format_html.spec.ts
index bc68f9039c..fa0919d524 100644
--- a/packages/trilium-core/src/services/export/format_html.spec.ts
+++ b/packages/trilium-core/src/services/export/format_html.spec.ts
@@ -45,14 +45,21 @@ describe("formatHtml", () => {
].join("\n"));
});
- it("keeps a container verbatim when splitting it would add whitespace between its children", () => {
- expect(formatHtml(`<ul><li>text<ul><li>n</li></ul></li><li><span>a</span><span>b</span></li></ul>`)).toBe([
+ it("splits only at blocks, keeping each run of text and inline elements on one line", () => {
+ expect(formatHtml(`<ul><li><a href="x">X</a><ul><li>n</li></ul></li><li><span>a</span><span>b</span></li></ul>`)).toBe([
"<ul>",
- " <li>text<ul><li>n</li></ul></li>",
+ " <li>",
+ ` <a href="x">X</a>`,
+ " <ul>",
+ " <li>n</li>",
+ " </ul>",
+ " </li>",
" <li><span>a</span><span>b</span></li>",
"</ul>"
].join("\n"));
- expect(formatHtml(`text before <p>a block</p>`)).toBe(`text before <p>a block</p>`);
+ expect(formatHtml(`<div> a <b>b</b><!-- c --> c<p>p</p> d </div>`))
+ .toBe([ "<div>", " a <b>b</b><!-- c --> c", " <p>p</p>", "  d", "</div>" ].join("\n"));
+ expect(formatHtml(`text before <p>a block</p>`)).toBe("text before\n<p>a block</p>");
});
it("leaves the content of every element it does not split untouched", () => {
diff --git a/packages/trilium-core/src/services/export/format_html.ts b/packages/trilium-core/src/services/export/format_html.ts
index e894806d4b..1f95eb82c1 100644
--- a/packages/trilium-core/src/services/export/format_html.ts
+++ b/packages/trilium-core/src/services/export/format_html.ts
@@ -2,10 +2,10 @@ import { type HTMLElement, type Node, NodeType, parse } from "node-html-parser";
/**
* Formats exported HTML with one block per line and each container indented by its depth, so an
- * edit to one paragraph changes one line of a diff. An element is split across lines only when
- * every child is a block, because whitespace between inline children shows on the page; every
- * other element, and a fragment with text at its top level, keeps its content as parsed. Every tag
- * has its attributes separated by one space.
+ * edit to one paragraph changes one line of a diff. A container with a block child is split at its
+ * blocks, and each run of text and inline elements between them stays on one line: whitespace
+ * next to a block does not render, while whitespace between inline elements does. Every other
+ * element keeps its content as parsed, and every tag has its attributes separated by one space.
*/
export function formatHtml(html: string) {
const root = parse(html, { comment: true });
@@ -30,45 +30,54 @@ const BLOCK_TAGS = new Set([
"base", "link", "meta", "noscript", "script", "style", "title"
]);
-function formatNode(node: Node, depth: number): string[] {
+function formatElement(element: HTMLElement, depth: number): string[] {
const indent = INDENT.repeat(depth);
-
- if (node.nodeType === NodeType.ELEMENT_NODE && canSplit(node as HTMLElement)) {
- const element = node as HTMLElement;
- const attributes = element.rawAttrs ? ` ${element.rawAttrs}` : "";
- return [
- `${indent}<${element.rawTagName}${attributes}>`,
- ...formatChildren(element, depth + 1),
- `${indent}</${element.rawTagName}>`
- ];
+ if (!canSplit(element)) {
+ return [ indent + element.toString() ];
}
- return [ indent + node.toString().trim() ];
+ const attributes = element.rawAttrs ? ` ${element.rawAttrs}` : "";
+ return [
+ `${indent}<${element.rawTagName}${attributes}>`,
+ ...formatChildren(element, depth + 1),
+ `${indent}</${element.rawTagName}>`
+ ];
}
+/** Writes each block child of `element` on its own lines, and the inline content between them on one. */
function formatChildren(element: HTMLElement, depth: number) {
- return element.childNodes
- .filter((child) => !isWhitespace(child))
- .flatMap((child) => formatNode(child, depth));
+ const indent = INDENT.repeat(depth);
+ const lines: string[] = [];
+ let inline = "";
+
+ const flushInline = () => {
+ const text = inline.replace(/^[ \t\n\f\r]+|[ \t\n\f\r]+$/g, "");
+ if (text) {
+ lines.push(indent + text);
+ }
+ inline = "";
+ };
+
+ for (const child of element.childNodes) {
+ if (isBlock(child)) {
+ flushInline();
+ lines.push(...formatElement(child as HTMLElement, depth));
+ } else {
+ inline += child.toString();
+ }
+ }
+ flushInline();
+ return lines;
}
-/** Whether `element` (or the parsed root, which has no tag) can have its children on separate lines. */
+/** Whether `element` (or the parsed root, which has no tag) is a container with a block child. */
function canSplit(element: HTMLElement) {
- if (element.rawTagName && !CONTAINER_TAGS.has(element.rawTagName.toLowerCase())) {
- return false;
- }
+ const isContainer = !element.rawTagName || CONTAINER_TAGS.has(element.rawTagName.toLowerCase());
+ return isContainer && element.childNodes.some(isBlock);
+}
- const children = element.childNodes.filter((child) => !isWhitespace(child));
- return children.length > 0 && children.every((child) => {
- switch (child.nodeType) {
- case NodeType.ELEMENT_NODE:
- return BLOCK_TAGS.has((child as HTMLElement).rawTagName.toLowerCase());
- case NodeType.COMMENT_NODE:
- return true;
- default:
- return child.rawText.trimStart().startsWith("<!");
- }
- });
+function isBlock(node: Node) {
+ return node.nodeType === NodeType.ELEMENT_NODE && BLOCK_TAGS.has((node as HTMLElement).rawTagName.toLowerCase());
}
/** An attribute name, optionally followed by `=` and a quoted or unquoted value. */
@@ -79,8 +88,3 @@ function normalizeAttributes(rawAttrs: string) {
return Array.from(rawAttrs.matchAll(ATTRIBUTE), ([ attribute ]) => attribute.replace(/^([^=\s]+)\s*=\s*/, "$1="))
.join(" ");
}
-
-/** Whitespace as HTML defines it, which leaves out the significant U+00A0 that `trim()` removes. */
-function isWhitespace(node: Node) {
- return node.nodeType === NodeType.TEXT_NODE && /^[ \t\n\f\r]*$/.test(node.rawText);
-}
diff --git a/packages/trilium-core/src/services/export/zip/html.spec.ts b/packages/trilium-core/src/services/export/zip/html.spec.ts
index a77dc3b65b..149bf8ab1e 100644
--- a/packages/trilium-core/src/services/export/zip/html.spec.ts
+++ b/packages/trilium-core/src/services/export/zip/html.spec.ts
@@ -87,7 +87,7 @@ describe("HtmlExportProvider", () => {
expect(result).toContain("A &lt;b&gt; &amp; &quot;Title&quot;");
expect(result).not.toContain("<b> &");
expect(result).toContain('<base target="_parent">');
- // Content is pretty-printed, so the body markup is preserved (line layout may differ).
+ // formatHtml() changes only the line layout, so the body markup is preserved.
expect(result).toContain('<div class="ck-content">');
expect(result).toContain("<p>Body</p>");
expect(result).toContain('<h1 data-trilium-h1>');
@@ -96,10 +96,9 @@ describe("HtmlExportProvider", () => {
expect(rewriteFn.mock.calls[0][1]).toBe(noteMeta);
});
- it("keeps the space after an inline tag that the pretty-printer wraps inside of", () => {
+ it("writes a paragraph exactly as the note holds it", () => {
const { provider } = buildProvider({ zipExportOptions: { skipHtmlTemplate: true } });
const noteMeta: NoteMeta = { format: "html", notePath: ["root", "leaf"] };
- // Long enough that the 70-column wrap lands inside the third <code> tag.
const content = "<p>When given a value, it will sort by other criteria instead: a comma-separated "
+ "list of levels, each <code spellcheck=\"false\">title</code>, "
+ "<code spellcheck=\"false\">dateCreated</code>, "
@@ -107,11 +106,10 @@ describe("HtmlExportProvider", () => {
const result = provider.prepareContent("Sorting", content, noteMeta) as string;
- expect(result).not.toMatch(/<\/code>[A-Za-z]/);
- expect(result.replace(/\s+/g, " ")).toContain("</code> or the name of a label");
+ expect(result).toBe(content);
});
- it("wraps identically however long the href was before rewriteFn shortened it", () => {
+ it("exports the same file however long the href was before rewriteFn shortened it", () => {
// Both forms are written by Trilium for the same link: the editor stores a full note
// path, an import stores a bare ID. rewriteFn collapses each to the same target, so
// the exported file must not differ.
@@ -202,10 +200,10 @@ describe("HtmlExportProvider", () => {
expect(rewriteFn).not.toHaveBeenCalled();
});
- it("does not pretty-print very large content but still wraps and rewrites it", () => {
+ it("formats large content too, after wrapping and rewriting it", () => {
const rewriteFn = vi.fn((content: string) => content);
const { provider } = buildProvider({ rewriteFn });
- const big = "<p>x</p>".repeat(20000); // well over the 100k pretty-print threshold
+ const big = "<p>x</p>".repeat(20000);
const result = provider.prepareContent("T", big, {
format: "html",
@@ -213,7 +211,7 @@ describe("HtmlExportProvider", () => {
}) as string;
expect(result).toContain('<base target="_parent">');
- expect(result.length).toBeGreaterThan(100_000);
+ expect(result.split("\n <p>x</p>")).toHaveLength(20001);
expect(rewriteFn).toHaveBeenCalledTimes(1);
});
});
@@ -293,7 +291,7 @@ describe("HtmlExportProvider", () => {
expect(appendCalls).toHaveLength(0);
});
- it("does not pretty-print a very large navigation tree", () => {
+ it("formats a very large navigation tree too, one item per line", () => {
const { provider, appendCalls } = buildProvider();
provider.prepareMeta(buildMetaFile());
const rootMeta: NoteMeta = {
@@ -313,9 +311,7 @@ describe("HtmlExportProvider", () => {
const navigation = appendCalls.find((c) => c.name === "navigation.html")?.content;
expect(typeof navigation).toBe("string");
expect(String(navigation).length).toBeGreaterThan(100_000);
- // Emitted verbatim past the threshold, so the list still sits on the template's own line
- // instead of being broken up one tag per line by the pretty printer.
- expect(String(navigation)).toContain("<ul><li>");
+ expect(String(navigation).match(/^ +<li><a href="[^"]+" target="detail">Note number \d+<\/a><\/li>$/gm)).toHaveLength(2000);
});
it("throws when meta was never prepared", () => {