Skip to content

Build your personal knowledge base with Trilium Notes

fix(markdown): keep blank lines in code inside collapsibles and HTML tables

Elian Doran2bc4502a0641
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Themes.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Themes.html
index 4b9c439ac1..5d562bc5d3 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Themes.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Themes.html
@@ -77,6 +77,7 @@ body .CodeMirror {
}</code></pre>
<p>&nbsp;</p>
</details>
+<p>&nbsp;</p>
<h3>Activating Your Custom Theme</h3>
<p>Once you've created your custom theme:</p>
<ol>
diff --git a/docs/User Guide/User Guide/Basic Concepts and Features/Themes.md b/docs/User Guide/User Guide/Basic Concepts and Features/Themes.md
index 5d3bf0dbb7..c83f6c7136 100644
--- a/docs/User Guide/User Guide/Basic Concepts and Features/Themes.md
+++ b/docs/User Guide/User Guide/Basic Concepts and Features/Themes.md
@@ -22,8 +22,7 @@ Trilium supports custom user themes, allowing you to personalize the application
font-style: normal;
font-weight: 400;
src: url('/custom/fonts/raleway.woff2') format('woff2');
-}
-
+}&#10;
:root {
--main-font-family: 'Raleway' !important;
--main-font-size: normal;
@@ -31,8 +30,7 @@ Trilium supports custom user themes, allowing you to personalize the application
--tree-font-size: normal;
--detail-font-family: inherit;
--detail-font-size: normal;
- --detail-text-font-family: 'Garamond' !important;
-
+ --detail-text-font-family: 'Garamond' !important;&#10;
--main-background-color: #404552;
--main-text-color: #AFB8C6;
--main-border-color: #AFB8C6;
@@ -61,12 +59,10 @@ Trilium supports custom user themes, allowing you to personalize the application
--modal-background-color: #404552;
--modal-backdrop-color: black;
--scrollbar-border-color: rgba(175, 184, 198, 0.5);
-}
-
+}&#10;
body .note-detail-text {
font-size: 120%;
-}
-
+}&#10;
body .CodeMirror {
filter: invert(100%) hue-rotate(180deg);
}</code></pre>
diff --git a/docs/User Guide/User Guide/Theme development/Creating an icon pack.md b/docs/User Guide/User Guide/Theme development/Creating an icon pack.md
index 15eaf248c9..b151743fe3 100644
--- a/docs/User Guide/User Guide/Theme development/Creating an icon pack.md
+++ b/docs/User Guide/User Guide/Theme development/Creating an icon pack.md
@@ -106,30 +106,25 @@ Sample `selection.json` excerpt:
<summary>A tiny Node.js script to produce the manifest</summary>
<p>Place <code>selection.json</code> in the same directory and run with Node 20+):</p>
<pre><code class="language-text-javascript">import { join } from "node:path";
-import { readFileSync, writeFileSync } from "node:fs";
-
+import { readFileSync, writeFileSync } from "node:fs";&#10;
function processIconPack(packName) {
const path = join(packName);
const selectionMeta = JSON.parse(readFileSync(join(path, "selection.json"), "utf-8"));
- const icons = {};
-
+ const icons = {};&#10;
for (const icon of selectionMeta.icons) {
let name = icon.properties.name;
if (name.endsWith(`-${packName}`)) {
name = name.split("-").slice(0, -1).join("-");
- }
-
+ }&#10;
const id = `ph-${name}`;
icons[id] = {
glyph: `${String.fromCharCode(icon.properties.code)}`,
terms: [ name ]
};
- }
-
+ }&#10;
writeFileSync("manifest.json", JSON.stringify(icons, null, 2), "utf8");
console.log("manifest.json created");
-}
-
+}&#10;
processIconPack("light");</code></pre>
<p>What to do with the script:</p>
<ul>
diff --git a/packages/trilium-core/src/services/import/markdown.spec.ts b/packages/trilium-core/src/services/import/markdown.spec.ts
index f1be1ffc2a..1646512043 100644
--- a/packages/trilium-core/src/services/import/markdown.spec.ts
+++ b/packages/trilium-core/src/services/import/markdown.spec.ts
@@ -593,6 +593,20 @@ $$`;
.toStrictEqual(`<details class="trilium-collapsible">Just some <em>text</em> here</details>`);
});
+ it("round-trips a code block with blank lines inside a collapsible block or a table kept as HTML", () => {
+ const code = `<pre><code class="language-text-x-trilium-auto">a {\n}\n\n \n:root {\n --x: 1;\n}</code></pre>`;
+
+ for (const html of [
+ `<details class="trilium-collapsible"><summary>S</summary>${code}<p>After</p></details><p>Outside</p>`,
+ `<figure class="table"><table><tbody><tr><td>${code}</td><td>B</td></tr></tbody></table></figure><p>Outside</p>`
+ ]) {
+ const markdown = markdownExportService.toMarkdown(html);
+
+ expect(markdown).toContain("<pre>");
+ expect(markdownService.renderToHtml(markdown, "Title")).toStrictEqual(html);
+ }
+ });
+
it("leaves a <details> shown inside a fenced code block as escaped sample text", () => {
const input = trimIndentation`\
\`\`\`html
diff --git a/packages/turndown-plugin-gfm/src/lib/serialize-structural-html.ts b/packages/turndown-plugin-gfm/src/lib/serialize-structural-html.ts
index 152123fe95..39f68d092e 100644
--- a/packages/turndown-plugin-gfm/src/lib/serialize-structural-html.ts
+++ b/packages/turndown-plugin-gfm/src/lib/serialize-structural-html.ts
@@ -25,7 +25,7 @@ export function serializeStructuralHtml(node: Element, containerTags: string[],
if (containerTags.indexOf(node.nodeName) === -1 || hasDirectText) {
// Leaf element (td/th/caption/col, or a <details> body child): keep it on one line.
- return isVoid ? indent + openTag : indent + openTag + node.innerHTML.trim() + closeTag;
+ return isVoid ? indent + openTag : indent + openTag + withoutBlankLines(node.innerHTML.trim()) + closeTag;
}
const lines = [indent + openTag];
@@ -39,6 +39,13 @@ export function serializeStructuralHtml(node: Element, containerTags: string[],
return lines.join('\n');
}
+// A blank line ends a raw-HTML block in CommonMark, so one inside a leaf (an empty line of
+// <pre> code) is joined to the line before it through a `&#10;` reference. Each such line
+// break decodes to the same newline, so the text is unchanged.
+function withoutBlankLines(html: string): string {
+ return html.replace(/\n(?=[ \t]*(?:\n|$))/g, '&#10;');
+}
+
function hasSignificantTextChild(node: Element): boolean {
for (let i = 0; i < node.childNodes.length; i++) {
const child = node.childNodes[i];