Skip to content

Build your personal knowledge base with Trilium Notes

server(export): export Markdown using ATX heading syntax (closes #1251)

Elian Doran411e3dfa0eb7
diff --git a/spec/support/utils.ts b/spec/support/utils.ts
index 8735c96517..a616c0078f 100644
--- a/spec/support/utils.ts
+++ b/spec/support/utils.ts
@@ -1,3 +1,35 @@
+/**
+ * Reads the level of indentation of the first line and trims the identation for all the text by that amount.
+ *
+ * For example, for:
+ *
+ * ```json
+ * {
+ * "hello": "world"
+ * }
+ * ```
+ *
+ * it results in:
+ *
+ * ```json
+ * {
+ * "hello": "world"
+ * }
+ * ```
+ *
+ * This is meant to be used as a template string, where it allows the indentation of the template without affecting whitespace changes.
+ *
+ * @example const html = trimIndentation`\
+ * <h1>Heading 1</h1>
+ * <h2>Heading 2</h2>
+ * <h3>Heading 3</h3>
+ * <h4>Heading 4</h4>
+ * <h5>Heading 5</h5>
+ * <h6>Heading 6</h6>
+ * `;
+ * @param strings
+ * @returns
+ */
export function trimIndentation(strings: TemplateStringsArray) {
const str = strings.toString();
diff --git a/src/services/export/md.spec.ts b/src/services/export/md.spec.ts
index 6facdcba21..400ddd4ccc 100644
--- a/src/services/export/md.spec.ts
+++ b/src/services/export/md.spec.ts
@@ -74,4 +74,28 @@ describe("Markdown export", () => {
const expected = "~~hello~~Hello ~~world~~";
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
+
+ it("exports headings properly", () => {
+ const html = trimIndentation`\
+ <h1>Heading 1</h1>
+ <h2>Heading 2</h2>
+ <h3>Heading 3</h3>
+ <h4>Heading 4</h4>
+ <h5>Heading 5</h5>
+ <h6>Heading 6</h6>
+ `;
+ const expected = trimIndentation`\
+ # Heading 1
+
+ ## Heading 2
+
+ ### Heading 3
+
+ #### Heading 4
+
+ ##### Heading 5
+
+ ###### Heading 6`;
+ expect(markdownExportService.toMarkdown(html)).toBe(expected);
+ });
});
diff --git a/src/services/export/md.ts b/src/services/export/md.ts
index 5ba3d6e03f..669871ff9c 100644
--- a/src/services/export/md.ts
+++ b/src/services/export/md.ts
@@ -24,7 +24,10 @@ const fencedCodeBlockFilter: TurndownService.Rule = {
function toMarkdown(content: string) {
if (instance === null) {
- instance = new TurndownService({ codeBlockStyle: "fenced" });
+ instance = new TurndownService({
+ headingStyle: "atx",
+ codeBlockStyle: "fenced"
+ });
// Filter is heavily based on: https://github.com/mixmark-io/turndown/issues/274#issuecomment-458730974
instance.addRule("fencedCodeBlock", fencedCodeBlockFilter);
instance.use(turndownPluginGfm.gfm);