-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathmarkdown.spec.ts
292 lines (251 loc) · 11.1 KB
/
markdown.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import { describe, it, expect } from "vitest";
import markdownExportService from "./markdown.js";
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 = ``;
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);
});
it("exports admonitions properly", () => {
const html = trimIndentation`\
<p>
Before
</p>
<aside class="admonition note">
<p>
This is a note.
</p>
</aside>
<aside class="admonition tip">
<p>
This is a tip.
</p>
</aside>
<aside class="admonition important">
<p>
This is a very important information.
</p>
<figure class="table">
<table>
<tbody>
<tr>
<td>
1
</td>
<td>
2
</td>
</tr>
<tr>
<td>
3
</td>
<td>
4
</td>
</tr>
</tbody>
</table>
</figure>
</aside>
<aside class="admonition caution">
<p>
This is a caution.
</p>
</aside>
<aside class="admonition warning">
<h2>
Title goes here
</h2>
<p>
This is a warning.
</p>
</aside>
<p>
After
</p>
`;
const space = " "; // editor config trimming space.
const expected = trimIndentation`\
Before
> [!NOTE]
> This is a note.
> [!TIP]
> This is a tip.
> [!IMPORTANT]
> This is a very important information.
>${space}
> <figure class="table"><table><tbody><tr><td>1</td><td>2</td></tr><tr><td>3</td><td>4</td></tr></tbody></table></figure>
> [!CAUTION]
> This is a caution.
> [!WARNING]
> ## Title goes here
>${space}
> This is a warning.
After`;
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
it("exports code in tables properly", () => {
const html = trimIndentation`\
<table>
<tr>
<td>
Row 1
</td>
<td>
<p>Allows displaying the value of one or more attributes in the calendar
like this: </p>
<p>
<img src="13_Calendar View_image.png" alt="">
</p>
<pre><code class="language-text-x-trilium-auto">#weight="70"
#Mood="Good"
#calendar:displayedAttributes="weight,Mood"</code></pre>
<p>It can also be used with relations, case in which it will display the
title of the target note:</p><pre><code class="language-text-x-trilium-auto">~assignee=@My assignee
#calendar:displayedAttributes="assignee"</code></pre>
</td>
</tr>
</table>
`;
const expected = trimIndentation`\
<table><tbody><tr><td>Row 1</td><td><p>Allows displaying the value of one or more attributes in the calendar like this: </p><p><img src="13_Calendar View_image.png" alt=""></p><pre><code class="language-text-x-trilium-auto">#weight="70"
#Mood="Good"
#calendar:displayedAttributes="weight,Mood"</code></pre><p>It can also be used with relations, case in which it will display the title of the target note:</p><pre><code class="language-text-x-trilium-auto">~assignee=@My assignee
#calendar:displayedAttributes="assignee"</code></pre></td></tr></tbody></table>`;
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
it("converts to character", () => {
const html = /*html*/`<p>Hello world.</p>`;
const expected = `Hello\u00a0world.`;
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
it("preserves non-breaking space character", () => {
const html = /*html*/`<p>Hello\u00adworld.</p>`;
const expected = `Hello\u00adworld.`;
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
it("exports normal links verbatim", () => {
const html = /*html*/`<p><a href="https://www.google.com">Google</a></p>`;
const expected = `[Google](https://www.google.com)`;
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
it("exports reference links verbatim", () => {
const html = /*html*/`<p><a class="reference-link" href="../../Canvas.html">Canvas</a></p>`;
const expected = `<a class="reference-link" href="../../Canvas.html">Canvas</a>`;
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
it("converts image if it has no custom properties", () => {
const html = /*html*/`<p><img src="Include Note_image.png"></p>`;
const expected = ``;
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
it("preserves image verbatim if it has a width or height attribute", () => {
const scenarios = [
/*html*/`<img src="Include Note_image.png" width="16" height="16">`,
/*html*/`<img src="Include Note_image.png" width="16">`,
/*html*/`<img src="Include Note_image.png" height="16">`,
/*html*/`<img class="image_resized" style="aspect-ratio:853/315;width:50%;" src="6_File_image.png" width="853" height="315">`
];
for (const expected of scenarios) {
const html = /*html*/`<p>${expected}</p>`;
expect(markdownExportService.toMarkdown(html)).toBe(expected);
}
});
it("preserves figures", () => {
const html = /*html*/trimIndentation`\
<figure class="image" style="width:53.44%;">
<img style="aspect-ratio:991/403;" src="Jump to Note_image.png" width="991"
height="403">
</figure>
`;
const expected = `<figure class="image" style="width:53.44%;"><img style="aspect-ratio:991/403;" src="Jump to Note_image.png" width="991" height="403"></figure>`;
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
it("converts inline math expressions into proper Markdown syntax", () => {
const html = /*html*/String.raw`<span class="math-tex">\(H(X, Y) = \sum_{i=1}^{M} \sum_{j=1}^{L} p(x_i, y_j) \log_2 \frac{1}{p(x_i, y_j)} = - \sum_{i=1}^{M} \sum_{j=1}^{L} p(x_i, y_j) \log_2 p(x_i, y_j) \frac{\text{bits}}{\text{symbol}}\)</span></span>`;
const expected = String.raw`$H(X, Y) = \sum_{i=1}^{M} \sum_{j=1}^{L} p(x_i, y_j) \log_2 \frac{1}{p(x_i, y_j)} = - \sum_{i=1}^{M} \sum_{j=1}^{L} p(x_i, y_j) \log_2 p(x_i, y_j) \frac{\text{bits}}{\text{symbol}}$`;
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
it("converts display math expressions into proper Markdown syntax", () => {
const html = /*html*/String.raw`<span class="math-tex">\[H(X, Y) = \sum_{i=1}^{M} \sum_{j=1}^{L} p(x_i, y_j) \log_2 \frac{1}{p(x_i, y_j)} = - \sum_{i=1}^{M} \sum_{j=1}^{L} p(x_i, y_j) \log_2 p(x_i, y_j) \frac{\text{bits}}{\text{symbol}}\]</span></span>`;
const expected = String.raw`$$H(X, Y) = \sum_{i=1}^{M} \sum_{j=1}^{L} p(x_i, y_j) \log_2 \frac{1}{p(x_i, y_j)} = - \sum_{i=1}^{M} \sum_{j=1}^{L} p(x_i, y_j) \log_2 p(x_i, y_j) \frac{\text{bits}}{\text{symbol}}$$`;
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
});