Skip to content

Build your personal knowledge base with Trilium Notes

fix(export): rewrite links before pretty-printing exported HTML

Elian Doranbfa1cca2b48e
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 0fad213fc0..a77dc3b65b 100644
--- a/packages/trilium-core/src/services/export/zip/html.spec.ts
+++ b/packages/trilium-core/src/services/export/zip/html.spec.ts
@@ -111,6 +111,31 @@ describe("HtmlExportProvider", () => {
expect(result.replace(/\s+/g, " ")).toContain("</code> or the name of a label");
});
+ it("wraps identically 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.
+ const wrapFor = (href: string) => {
+ const { provider } = buildProvider({
+ zipExportOptions: { skipHtmlTemplate: true },
+ rewriteFn: (content) => content.replace(/href="[^"]*"/g, 'href="#root/_help_eIg8jdvaoNNd"')
+ });
+ const content = "<p>To search the whole database while hoisted, use the full "
+ + `<a class="reference-link" href="${href}">Search</a> and leave `
+ + "<em>Ancestor</em> field empty.</p>";
+ return provider.prepareContent("Quick search", content, {
+ format: "html",
+ notePath: ["root", "leaf"]
+ }) as string;
+ };
+
+ const fromImport = wrapFor("#root/eIg8jdvaoNNd");
+ const fromEditor = wrapFor("#root/pOsGYCXsbNQG/BFs8mudNFgCS/BCkXAVs63Ttv/eIg8jdvaoNNd");
+
+ expect(fromEditor).toBe(fromImport);
+ expect(fromImport).toContain('href="#root/_help_eIg8jdvaoNNd"');
+ });
+
it("uses a bare style.css path for a top-level note (notePath length 1)", () => {
const { provider } = buildProvider();
const result = provider.prepareContent("T", "<p>x</p>", {
diff --git a/packages/trilium-core/src/services/export/zip/html.ts b/packages/trilium-core/src/services/export/zip/html.ts
index d5c165d23e..ec68f830b3 100644
--- a/packages/trilium-core/src/services/export/zip/html.ts
+++ b/packages/trilium-core/src/services/export/zip/html.ts
@@ -71,10 +71,15 @@ export default class HtmlExportProvider extends ZipExportProvider {
</html>`;
}
+ // Rewrite before pretty-printing: `html.prettyPrint` wraps at 70 columns and counts
+ // attribute text toward that budget, so printing first would place line breaks against
+ // hrefs that `rewriteFn` is about to replace. The same note then exports different
+ // line breaks depending on whether the editor last wrote a link as a full note path or
+ // an import wrote it as a bare ID.
+ content = this.rewriteFn(content, noteMeta);
if (content.length < 100_000) {
content = html.prettyPrint(content, { indent_size: 2 });
}
- content = this.rewriteFn(content as string, noteMeta);
return content;
}
return content;