Skip to content

Build your personal knowledge base with Trilium Notes

fix(export): format exported notes with many blocks

Elian Dorane1b178fef7c5
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 fa0919d524..f22f9abfb8 100644
--- a/packages/trilium-core/src/services/export/format_html.spec.ts
+++ b/packages/trilium-core/src/services/export/format_html.spec.ts
@@ -78,6 +78,14 @@ describe("formatHtml", () => {
.toBe(`<pre class="x"><code class="y"> <b >keep</b></code></pre>`);
});
+ it("formats a container with more blocks than a call takes arguments", () => {
+ const paragraphs = "<p>a</p>".repeat(200_000);
+ const lines = formatHtml(`<div>${paragraphs}</div>`).split("\n");
+
+ expect(lines).toHaveLength(200_002);
+ expect(lines.at(-2)).toBe(" <p>a</p>");
+ });
+
it("gives the same output when run on its own output", () => {
const html = `<html><head><title>T</title></head><body><div><p>a</p>\n\n <p>b</p></div></body></html>`;
const formatted = formatHtml(html);
diff --git a/packages/trilium-core/src/services/export/format_html.ts b/packages/trilium-core/src/services/export/format_html.ts
index 1f95eb82c1..f1a18d1e98 100644
--- a/packages/trilium-core/src/services/export/format_html.ts
+++ b/packages/trilium-core/src/services/export/format_html.ts
@@ -9,9 +9,7 @@ import { type HTMLElement, type Node, NodeType, parse } from "node-html-parser";
*/
export function formatHtml(html: string) {
const root = parse(html, { comment: true });
- for (const element of root.querySelectorAll("*")) {
- element.rawAttrs = normalizeAttributes(element.rawAttrs);
- }
+ normalizeAllAttributes(root);
return canSplit(root) ? formatChildren(root, 0).join("\n") : root.toString();
}
@@ -61,7 +59,9 @@ function formatChildren(element: HTMLElement, depth: number) {
for (const child of element.childNodes) {
if (isBlock(child)) {
flushInline();
- lines.push(...formatElement(child as HTMLElement, depth));
+ for (const line of formatElement(child as HTMLElement, depth)) {
+ lines.push(line);
+ }
} else {
inline += child.toString();
}
@@ -80,6 +80,23 @@ function isBlock(node: Node) {
return node.nodeType === NodeType.ELEMENT_NODE && BLOCK_TAGS.has((node as HTMLElement).rawTagName.toLowerCase());
}
+/**
+ * Normalizes the attributes of every element below `root`. Walks the tree by hand, since
+ * `querySelectorAll("*")` grows faster than quadratically with the element count.
+ */
+function normalizeAllAttributes(root: HTMLElement) {
+ const pending = [ ...root.childNodes ];
+ for (let node = pending.pop(); node; node = pending.pop()) {
+ if (node.nodeType === NodeType.ELEMENT_NODE) {
+ const element = node as HTMLElement;
+ element.rawAttrs = normalizeAttributes(element.rawAttrs);
+ for (const child of element.childNodes) {
+ pending.push(child);
+ }
+ }
+ }
+}
+
/** An attribute name, optionally followed by `=` and a quoted or unquoted value. */
const ATTRIBUTE = /[^\s"'<>/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"'<>=`]+))?/g;