Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 411e3df

Browse files
committed
server(export): export Markdown using ATX heading syntax (closes #1251)
1 parent a1bfc6a commit 411e3df

3 files changed

Lines changed: 60 additions & 1 deletion

File tree

spec/support/utils.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
/**
2+
* Reads the level of indentation of the first line and trims the identation for all the text by that amount.
3+
*
4+
* For example, for:
5+
*
6+
* ```json
7+
* {
8+
* "hello": "world"
9+
* }
10+
* ```
11+
*
12+
* it results in:
13+
*
14+
* ```json
15+
* {
16+
* "hello": "world"
17+
* }
18+
* ```
19+
*
20+
* This is meant to be used as a template string, where it allows the indentation of the template without affecting whitespace changes.
21+
*
22+
* @example const html = trimIndentation`\
23+
* <h1>Heading 1</h1>
24+
* <h2>Heading 2</h2>
25+
* <h3>Heading 3</h3>
26+
* <h4>Heading 4</h4>
27+
* <h5>Heading 5</h5>
28+
* <h6>Heading 6</h6>
29+
* `;
30+
* @param strings
31+
* @returns
32+
*/
133
export function trimIndentation(strings: TemplateStringsArray) {
234
const str = strings.toString();
335

src/services/export/md.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,28 @@ describe("Markdown export", () => {
7474
const expected = "~~hello~~Hello ~~world~~";
7575
expect(markdownExportService.toMarkdown(html)).toBe(expected);
7676
});
77+
78+
it("exports headings properly", () => {
79+
const html = trimIndentation`\
80+
<h1>Heading 1</h1>
81+
<h2>Heading 2</h2>
82+
<h3>Heading 3</h3>
83+
<h4>Heading 4</h4>
84+
<h5>Heading 5</h5>
85+
<h6>Heading 6</h6>
86+
`;
87+
const expected = trimIndentation`\
88+
# Heading 1
89+
90+
## Heading 2
91+
92+
### Heading 3
93+
94+
#### Heading 4
95+
96+
##### Heading 5
97+
98+
###### Heading 6`;
99+
expect(markdownExportService.toMarkdown(html)).toBe(expected);
100+
});
77101
});

src/services/export/md.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ const fencedCodeBlockFilter: TurndownService.Rule = {
2424

2525
function toMarkdown(content: string) {
2626
if (instance === null) {
27-
instance = new TurndownService({ codeBlockStyle: "fenced" });
27+
instance = new TurndownService({
28+
headingStyle: "atx",
29+
codeBlockStyle: "fenced"
30+
});
2831
// Filter is heavily based on: https://github.com/mixmark-io/turndown/issues/274#issuecomment-458730974
2932
instance.addRule("fencedCodeBlock", fencedCodeBlockFilter);
3033
instance.use(turndownPluginGfm.gfm);

0 commit comments

Comments
 (0)