2025-01-13 00:32:58 +01:00
|
|
|
import { describe, it, expect } from "vitest";
|
2025-01-10 22:04:04 +02:00
|
|
|
import markdownExportService from "./md.js";
|
|
|
|
import { trimIndentation } from "../../../spec/support/utils.js";
|
2024-12-17 23:35:08 +02:00
|
|
|
|
|
|
|
describe("Markdown export", () => {
|
|
|
|
|
2025-03-11 21:05:55 +02:00
|
|
|
it("exports correct language tag for known languages", () => {
|
|
|
|
const conversionTable = {
|
|
|
|
"language-text-x-nginx-conf": "nginx",
|
2025-03-11 21:11:23 +02:00
|
|
|
"language-text-x-diff": "diff",
|
2025-03-11 21:05:55 +02:00
|
|
|
"language-application-javascript-env-frontend": "javascript",
|
|
|
|
"language-application-javascript-env-backend": "javascript"
|
|
|
|
};
|
2024-12-17 23:35:08 +02:00
|
|
|
|
2025-03-11 21:11:23 +02:00
|
|
|
for (const [ input, output ] of Object.entries(conversionTable)) {
|
2025-03-11 21:05:55 +02:00
|
|
|
const html = trimIndentation`\
|
|
|
|
<p>A diff:</p>
|
2025-03-11 21:11:23 +02:00
|
|
|
<pre><code class="${input}">Hello
|
2025-03-11 21:05:55 +02:00
|
|
|
-world
|
|
|
|
+worldy
|
|
|
|
</code></pre>`;
|
|
|
|
const expected = trimIndentation`\
|
|
|
|
A diff:
|
2024-12-17 23:35:08 +02:00
|
|
|
|
2025-03-11 21:11:23 +02:00
|
|
|
\`\`\`${output}
|
2025-03-11 21:05:55 +02:00
|
|
|
Hello
|
|
|
|
-world
|
|
|
|
+worldy
|
2024-12-17 23:40:39 +02:00
|
|
|
|
2025-03-11 21:05:55 +02:00
|
|
|
\`\`\``;
|
2025-01-11 14:11:18 +02:00
|
|
|
|
2025-03-11 21:05:55 +02:00
|
|
|
expect(markdownExportService.toMarkdown(html)).toBe(expected);
|
|
|
|
}
|
2025-01-11 14:11:18 +02:00
|
|
|
});
|
|
|
|
|
2024-12-17 23:40:39 +02:00
|
|
|
it("removes auto tag for code blocks", () => {
|
|
|
|
const html = trimIndentation`\
|
|
|
|
<pre><code class="language-text-x-trilium-auto">Hello
|
|
|
|
-world
|
|
|
|
+worldy
|
|
|
|
</code></pre>`;
|
|
|
|
const expected = trimIndentation`\
|
|
|
|
\`\`\`
|
|
|
|
Hello
|
|
|
|
-world
|
|
|
|
+worldy
|
|
|
|
|
|
|
|
\`\`\``;
|
|
|
|
|
|
|
|
expect(markdownExportService.toMarkdown(html)).toBe(expected);
|
2024-12-17 23:42:31 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("supports code block with no language tag", () => {
|
|
|
|
const html = trimIndentation`\
|
|
|
|
<pre><code>Hello</code></pre>`;
|
|
|
|
const expected = trimIndentation`\
|
|
|
|
\`\`\`
|
|
|
|
Hello
|
|
|
|
\`\`\``;
|
|
|
|
|
|
|
|
expect(markdownExportService.toMarkdown(html)).toBe(expected);
|
|
|
|
});
|
2025-02-08 22:59:28 +02:00
|
|
|
|
|
|
|
it("exports strikethrough text correctly", () => {
|
|
|
|
const html = "<s>hello</s>Hello <s>world</s>";
|
|
|
|
const expected = "~~hello~~Hello ~~world~~";
|
|
|
|
expect(markdownExportService.toMarkdown(html)).toBe(expected);
|
|
|
|
});
|
2025-02-22 12:45:21 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
2025-03-11 17:12:48 +02:00
|
|
|
|
|
|
|
it("rewrites image URL with spaces", () => {
|
|
|
|
const html = `<img src="Hello world .png"/>`;
|
|
|
|
const expected = ``;
|
|
|
|
expect(markdownExportService.toMarkdown(html)).toBe(expected);
|
|
|
|
});
|
2024-12-22 15:45:54 +02:00
|
|
|
});
|