Notes/src/services/export/markdown.spec.ts

107 lines
3.1 KiB
TypeScript
Raw Normal View History

2025-01-13 00:32:58 +01:00
import { describe, it, expect } from "vitest";
import markdownExportService from "./markdown.js";
2025-01-10 22:04:04 +02:00
import { trimIndentation } from "../../../spec/support/utils.js";
describe("Markdown export", () => {
it("exports correct language tag for known languages", () => {
const conversionTable = {
"language-text-x-nginx-conf": "nginx",
"language-text-x-diff": "diff",
"language-application-javascript-env-frontend": "javascript",
"language-application-javascript-env-backend": "javascript",
"language-text-x-asm-mips": "mips"
};
for (const [ input, output ] of Object.entries(conversionTable)) {
const html = trimIndentation`\
<p>A diff:</p>
<pre><code class="${input}">Hello
-world
+worldy
</code></pre>`;
const expected = trimIndentation`\
A diff:
\`\`\`${output}
Hello
-world
+worldy
\`\`\``;
expect(markdownExportService.toMarkdown(html)).toBe(expected);
}
});
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);
});
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);
});
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);
});
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);
});
it("rewrites image URL with spaces", () => {
const html = `<img src="Hello world .png"/>`;
const expected = `![](Hello%20world%20%20.png)`;
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
it("supports keyboard shortcuts", () => {
const html = "<kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>Delete</kbd>";
expect(markdownExportService.toMarkdown(html)).toBe(html);
});
2024-12-22 15:45:54 +02:00
});