From 539e3f1b6a664eb57ada2b187dd4753aeed5d6be Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Fri, 11 Apr 2025 19:25:22 +0300 Subject: [PATCH] feat(export/markdown: trim confusing whitespace in lists --- src/services/export/markdown.spec.ts | 26 ++++++++++++++++++++++++++ src/services/export/markdown.ts | 22 ++++++++++++++++++++++ src/services/import/markdown.spec.ts | 18 ++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/src/services/export/markdown.spec.ts b/src/services/export/markdown.spec.ts index 74c6a9600..a344d975f 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`\ + + `; + 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 e524a6274..c4c71eee2 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 f1ca4971d..429e61b67 100644 --- a/src/services/import/markdown.spec.ts +++ b/src/services/import/markdown.spec.ts @@ -208,4 +208,22 @@ second line 2
  1. Hello
  2. { + 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*/`

    🐞 Bugfixes

    `, + /*html*/`` + ].join(""); + expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected); + }); + });