mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-09-18 01:01:42 +08:00
feat(export/markdown: trim confusing whitespace in lists
This commit is contained in:
parent
eba4c1a545
commit
539e3f1b6a
@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
|
@ -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 = "\\)";
|
||||
|
@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user