Skip to content

Build your personal knowledge base with Trilium Notes

feat(export/markdown: trim confusing whitespace in lists

Elian Doran539e3f1b6a66
diff --git a/src/services/export/markdown.spec.ts b/src/services/export/markdown.spec.ts
index 74c6a96001..a344d975f6 100644
--- a/src/services/export/markdown.spec.ts
+++ b/src/services/export/markdown.spec.ts
@@ -289,4 +289,30 @@ describe("Markdown export", () => {
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
+ it("does not generate additional spacing when exporting lists with paragraph", () => {
+ const html = trimIndentation`\
+ <ul>
+ <li><a href="https://github.com/JYC333">@JYC333</a> made their first contribution
+ in <a href="https://github.com/TriliumNext/Notes/pull/294">#294</a>
+
+ </li>
+ <li>
+ <p><a href="https://github.com/TriliumNext/Notes/issues/375">Note Tooltip isn't removed when clicking on internal trilium link in read-only mode</a>
+
+ </p>
+ </li>
+ <li>
+ <p><a href="https://github.com/TriliumNext/Notes/issues/384">Calendar dropdown won't close if click/right-click other button that open notes from launcher bar</a>
+
+ </p>
+ </li>
+ </ul>
+ `;
+ const expected = trimIndentation`\
+ * [@JYC333](https://github.com/JYC333) made their first contribution in [#294](https://github.com/TriliumNext/Notes/pull/294)
+ * [Note Tooltip isn't removed when clicking on internal trilium link in read-only mode](https://github.com/TriliumNext/Notes/issues/375)
+ * [Calendar dropdown won't close if click/right-click other button that open notes from launcher bar](https://github.com/TriliumNext/Notes/issues/384)`;
+ expect(markdownExportService.toMarkdown(html)).toBe(expected);
+ });
+
});
diff --git a/src/services/export/markdown.ts b/src/services/export/markdown.ts
index e524a62749..c4c71eee2d 100644
--- a/src/services/export/markdown.ts
+++ b/src/services/export/markdown.ts
@@ -46,6 +46,7 @@ function toMarkdown(content: string) {
instance.addRule("inlineLink", buildInlineLinkFilter());
instance.addRule("figure", buildFigureFilter());
instance.addRule("math", buildMathFilter());
+ instance.addRule("li-1", buildListItemFilter());
instance.use(gfm);
instance.keep([ "kbd" ]);
}
@@ -207,6 +208,27 @@ function buildFigureFilter(): Rule {
}
}
+// Keep in line with https://github.com/mixmark-io/turndown/blob/master/src/commonmark-rules.js.
+function buildListItemFilter(): Rule {
+ return {
+ filter: "li",
+ replacement(content, node, options) {
+ content = content
+ .trim()
+ .replace(/\n/gm, '\n ') // indent
+ let prefix = options.bulletListMarker + ' '
+ const parent = node.parentNode as HTMLElement;
+ if (parent.nodeName === 'OL') {
+ var start = parent.getAttribute('start')
+ var index = Array.prototype.indexOf.call(parent.children, node)
+ prefix = (start ? Number(start) + index : index + 1) + '. '
+ }
+ const result = prefix + content + (node.nextSibling && !/\n$/.test(content) ? '\n' : '');
+ return result;
+ }
+ }
+}
+
function buildMathFilter(): Rule {
const MATH_INLINE_PREFIX = "\\(";
const MATH_INLINE_SUFFIX = "\\)";
diff --git a/src/services/import/markdown.spec.ts b/src/services/import/markdown.spec.ts
index f1ca4971d9..429e61b67a 100644
--- a/src/services/import/markdown.spec.ts
+++ b/src/services/import/markdown.spec.ts
@@ -208,4 +208,22 @@ second line 2</code></pre><ul><li>Hello</li><li>world</li></ul><ol><li>Hello</li
expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);
});
+ it("does not generate additional spacing when importing lists", () => {
+ const input = trimIndentation`\
+ ### 🐞 Bugfixes
+
+ * [v0.90.4 docker does not read USER\_UID and USER\_GID from environment](https://github.com/TriliumNext/Notes/issues/331)
+ * [Invalid CSRF token on Android phone](https://github.com/TriliumNext/Notes/issues/318)
+ * [Excess spacing in lists](https://github.com/TriliumNext/Notes/issues/341)`;
+ const expected = [
+ /*html*/`<h3>🐞 Bugfixes</h3>`,
+ /*html*/`<ul>`,
+ /*html*/`<li><a href="https://github.com/TriliumNext/Notes/issues/331">v0.90.4 docker does not read USER_UID and USER_GID from environment</a></li>`,
+ /*html*/`<li><a href="https://github.com/TriliumNext/Notes/issues/318">Invalid CSRF token on Android phone</a></li>`,
+ /*html*/`<li><a href="https://github.com/TriliumNext/Notes/issues/341">Excess spacing in lists</a></li>`,
+ /*html*/`</ul>`
+ ].join("");
+ expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);
+ });
+
});