-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathmarkdown.spec.js
More file actions
378 lines (351 loc) Β· 12.4 KB
/
Copy pathmarkdown.spec.js
File metadata and controls
378 lines (351 loc) Β· 12.4 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
* SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { createRichEditor } from '../EditorFactory.ts'
import { createMarkdownSerializer } from '../extensions/Markdown.js'
import { typesAvailable } from './../markdownit/callouts.js'
import markdownit from './../markdownit/index.js'
import {
markdownFromPaste,
markdownThroughEditor,
markdownThroughEditorHtml,
} from './testHelpers/markdown.js'
/*
* This file is for various markdown tests, mainly testing if input and output stays the same.
*
* Please add test belonging to some Node or Mark to the corresponding file in './nodes/` or `./marks/`
*/
describe('Markdown though editor', () => {
test('headlines', () => {
expect(markdownThroughEditor('# Test')).toBe('# Test')
expect(markdownThroughEditor('## Test')).toBe('## Test')
expect(markdownThroughEditor('### Test')).toBe('### Test')
expect(markdownThroughEditor('#### Test')).toBe('#### Test')
expect(markdownThroughEditor('##### Test')).toBe('##### Test')
})
test('hard breaks', () => {
expect(markdownThroughEditor('hard \nbreak')).toBe('hard \nbreak')
expect(markdownThroughEditor('hard\\\nbreak')).toBe('hard\\\nbreak')
expect(markdownThroughEditor('mixed\\\nhard \nbreak')).toBe(
'mixed\\\nhard \nbreak',
)
expect(markdownThroughEditor('hard<br>break')).toBe('hard<br />break')
expect(markdownThroughEditor('hard<br />break')).toBe('hard<br />break')
expect(markdownThroughEditor('soft\nbreak')).toBe('soft\nbreak')
})
test('inline format', () => {
expect(markdownThroughEditor('**Test**')).toBe('**Test**')
expect(markdownThroughEditor('__Test__')).toBe('__Test__')
expect(markdownThroughEditor('_Test_')).toBe('*Test*')
expect(markdownThroughEditor('~~Test~~')).toBe('~~Test~~')
expect(markdownThroughEditor('Have an `inline code` element')).toBe(
'Have an `inline code` element',
)
})
test('ul', () => {
expect(markdownThroughEditor('+ foo\n+ bar')).toBe('+ foo\n+ bar')
expect(markdownThroughEditor('* foo\n* bar')).toBe('* foo\n* bar')
expect(markdownThroughEditor('- foo\n- bar')).toBe('- foo\n- bar')
expect(markdownThroughEditor('- foo\n\n- bar')).toBe('- foo\n- bar')
expect(markdownThroughEditor('- foo\n\n\n- bar')).toBe('- foo\n- bar')
})
test('ol', () => {
expect(markdownThroughEditor('1. foo\n2. bar')).toBe('1. foo\n2. bar')
expect(markdownThroughEditor('0. foo\n1. bar')).toBe('0. foo\n1. bar')
// regression test for #4828
expect(markdownThroughEditor('42. foo\n43. bar')).toBe('42. foo\n43. bar')
// for now we enforce sequential order in ordered lists
expect(markdownThroughEditor('2. foo\n19. bar\n2. baz')).toBe(
'2. foo\n3. bar\n4. baz',
)
})
test('paragraph', () => {
// Test whitespace characters are untouched
expect(markdownThroughEditor('foo\nbar\n\nfoobar\nfoo\tbar')).toBe(
'foo\nbar\n\nfoobar\nfoo\tbar',
)
})
test('links', () => {
expect(markdownThroughEditor('[test](foo)')).toBe('[test](foo)')
expect(markdownThroughEditor('[test](foo "bar")')).toBe('[test](foo "bar")')
// Issue #2703
expect(markdownThroughEditor('[bar\\\\]: /uri\n\n[bar\\\\]')).toBe(
'[bar\\\\](/uri)',
)
// Issue #4900
expect(markdownThroughEditor('[`code`](foo)')).toBe('[`code`](foo)')
expect(markdownThroughEditor('[text with `code` inside](foo)')).toBe(
'[text with `code` inside](foo)',
)
// Wiki-style links (issue #4795)
expect(markdownThroughEditor('[[WikiLink]]')).toBe('[[WikiLink]]')
expect(markdownThroughEditor('text [[wikiLink]] more')).toBe(
'text [[wikiLink]] more',
)
})
test('images', () => {
// Inline images
expect(markdownThroughEditor('text  moretext')).toBe(
'text  moretext',
)
// regression introduced in #3282. See issue #3428.
expect(markdownThroughEditor('')).toBe('')
expect(markdownThroughEditor('Hello\n\n')).toBe(
'Hello\n\n',
)
// Wiki-style image links (issue #4795)
expect(markdownThroughEditor('![[wiki style image.png]]')).toBe(
'![[wiki style image.png]]',
)
expect(markdownThroughEditor('text ![[wiki style image.png]] more')).toBe(
'text ![[wiki style image.png]] more',
)
expect(markdownThroughEditor('Hello\n\n![[wiki style image.png]]')).toBe(
'Hello\n\n![[wiki style image.png]]',
)
})
test('special characters', () => {
expect(markdownThroughEditor('"\';&.-#><')).toBe('"\';&.-#><')
})
test('code block', () => {
expect(markdownThroughEditor('```\n<?php echo "Hello World";\n```')).toBe(
'```\n<?php echo "Hello World";\n```',
)
// Issue #3328
expect(markdownThroughEditor('```python\nprint("Hello World")\n```')).toBe(
'```python\nprint("Hello World")\n```',
)
// Issue #3739
expect(markdownThroughEditor('```\n```')).toBe('```\n```')
})
test('markdown untouched', () => {
// Issue #2703
expect(markdownThroughEditor('[bar\\\\]: /uri\n\n[bar\\\\]')).toBe(
'[bar\\\\](/uri)',
)
expect(markdownThroughEditor('## Test \\')).toBe('## Test \\')
expect(markdownThroughEditor('- [ [asd](sdf)')).toBe('- [ [asd](sdf)')
})
test('horizontal rule', () => {
expect(markdownThroughEditor('foo\n\n---\n\nfoobar')).toBe(
'foo\n\n---\n\nfoobar',
)
})
test('table', () => {
expect(markdownThroughEditor('| a | b |\n|---|---|\n| 1 | 2 |')).toBe(
'| a | b |\n|---|---|\n| 1 | 2 |\n',
)
})
test('escaping', () => {
const test =
'(Asdf [asdf asdf](asdf asdf) asdf asdf asdf asdf asdf asdf asdf asdf asdf)\n'
+ '\n'
+ '* [asdf asdf asdf/asdf](Asdf Asdf)\n'
+ '* asdf asdf asdf [a--f asdf asdf](a--f Asdf Asdf)\n'
+ '* [Asdf asdf asdf asdf asdf asdf](Asdf asdf)'
expect(markdownThroughEditor(test)).toBe(test)
expect(markdownThroughEditor('This is a [test] for escaping')).toBe(
'This is a [test] for escaping',
)
expect(markdownThroughEditor('This is a [test for escaping')).toBe(
'This is a [test for escaping',
)
})
test('callouts', () => {
typesAvailable.forEach((type) => {
const entry = `::: ${type}\n!${type}!\n\njust do it\n\n:::`
expect(markdownThroughEditor(entry)).toBe(entry)
})
})
test('preview with url only', () => {
const entry = '[https://example.org](https://example.org (preview))'
expect(markdownThroughEditor(entry)).toBe(entry)
})
test('preview with text', () => {
const entry = '[some other text](https://example.org (preview))'
expect(markdownThroughEditor(entry)).toBe(entry)
})
test('front matter', () => {
expect(markdownThroughEditor('---\nhello: world\n---')).toBe(
'---\nhello: world\n---',
)
expect(markdownThroughEditor('---\n---')).toBe('---\n---')
})
test('mentions', () => {
expect(markdownThroughEditor('@[username](mention://user/id)')).toBe(
'@[username](mention://user/id)',
)
expect(
markdownThroughEditor('pretext @[username](mention://user/id) posttext'),
).toBe('pretext @[username](mention://user/id) posttext')
})
test('details', () => {
expect(
markdownThroughEditor(
'<details>\n<summary>**summary**</summary>\n* list\n\n</details>\n',
),
).toBe('<details>\n<summary>**summary**</summary>\n* list\n\n</details>\n')
})
test('nested details', () => {
expect(
markdownThroughEditor(
'<details>\n<summary>summary</summary>\n* list\n\n<details>\n<summary>summary</summary>\ncontent\n\n</details>\n\n</details>\n',
),
).toBe(
'<details>\n<summary>summary</summary>\n* list\n\n<details>\n<summary>summary</summary>\ncontent\n\n</details>\n\n</details>\n',
)
})
})
describe('Markdown serializer from html', () => {
test('ul', () => {
expect(
markdownThroughEditorHtml(
'<ul data-bullet="*"><li>1</li><li>2</li></ul>',
),
).toBe('* 1\n* 2')
expect(
markdownThroughEditorHtml(
'<ul data-bullet="-"><li>1</li><li>2</li></ul>',
),
).toBe('- 1\n- 2')
})
test('paragraph', () => {
expect(markdownThroughEditorHtml('<p>hello</p><p>world</p>')).toBe(
'hello\n\nworld',
)
})
test('hard line breaks', () => {
expect(markdownThroughEditorHtml('<p>hard<br />break</p>')).toBe(
'hard \nbreak',
)
expect(markdownThroughEditorHtml('<p>hard<br>break</p>')).toBe(
'hard \nbreak',
)
expect(markdownThroughEditorHtml('<p>soft\nbreak</p>')).toBe('soft\nbreak')
})
test('links', () => {
expect(markdownThroughEditorHtml('<a href="foo">test</a>')).toBe(
'[test](foo)',
)
})
test('images', () => {
expect(
markdownThroughEditorHtml('<img src="image" alt="description" />'),
).toBe('')
expect(
markdownThroughEditorHtml(
'<p><img src="image" alt="description" /></p>',
),
).toBe('')
expect(
markdownThroughEditorHtml(
'<p>text<img src="image" alt="description" />moretext</p>',
),
).toBe('textmoretext')
})
test('callouts', () => {
typesAvailable.forEach((type) => {
expect(
markdownThroughEditorHtml(
`<div data-callout="${type}" class="callout callout-${type}"><p>!${type}!</p>just do it<p></p></div>`,
),
).toBe(`::: ${type}\n!${type}!\n\njust do it\n\n:::`)
expect(
markdownThroughEditorHtml(
`<p class="callout ${type}">!${type}!</p>`,
),
).toBe(`::: ${type}\n!${type}!\n\n:::`)
})
})
test('callouts with handbook classes', () => {
expect(
markdownThroughEditorHtml('<p class="callout warning">!warning!</p>'),
).toBe('::: warn\n!warning!\n\n:::')
})
test('table', () => {
expect(
markdownThroughEditorHtml(
'<table><tbody><tr><th>greetings</th></tr><tr><td>hello</td></tr></tbody></table>',
),
).toBe('| greetings |\n|-----------|\n| hello |\n')
})
test('table cell escaping', () => {
// while '|' has no special meaning in commonmark is has to be escaped for GFM tables
expect(
markdownThroughEditorHtml(
'<table><tr><th>greetings</th></tr><tr><td>hello | hallo</td></tr></table>',
),
).toBe('| greetings |\n|----------------|\n| hello \\| hallo |\n')
})
test('table pastes (#2708)', () => {
// while '|' has no special meaning in commonmark is has to be escaped for GFM tables
expect(
markdownFromPaste(
'<table><tbody><tr><th>greetings</th></tr><tr><td>hello</td></tr></tbody></table>',
),
).toBe('| greetings |\n|-----------|\n| hello |\n')
})
test('front matter', () => {
expect(
markdownThroughEditorHtml(
'<pre id="frontmatter"><code>some: value</code></pre><h1>Heading</h1>',
),
).toBe('---\nsome: value\n---\n\n# Heading')
// Test --- within front matter is allowed
expect(
markdownThroughEditorHtml(
'<pre id="frontmatter"><code>---</code></pre><h1>Heading</h1>',
),
).toBe('----\n---\n----\n\n# Heading')
})
test('mentions', () => {
expect(
markdownThroughEditorHtml(
'<span class="mention" data-label="username" data-type="user" data-id="id">username</span>',
),
).toBe('@[username](mention://user/id)')
expect(
markdownThroughEditorHtml(
'<span class="mention" data-label="whitespace user" data-type="user" data-id="whitespace user">whitespace user</span>',
),
).toBe('@[whitespace user](mention://user/whitespace%20user)')
expect(
markdownThroughEditorHtml(
'pretext <span class="mention" data-label="username" data-type="user" data-id="id">username</span> posttext',
),
).toBe('pretext @[username](mention://user/id) posttext')
})
test('details', () => {
expect(
markdownThroughEditorHtml(
'<details><summary><strong>summary</strong></summary><pre>code</pre></details>',
),
).toBe(
'<details>\n<summary>**summary**</summary>\n```\ncode\n```\n\n</details>\n',
)
})
test('math (content following math stays, #8654)', () => {
const test =
'Content above\n\n$$\n\\sum_{i=1}^n i = \\frac{n(n+1)}{2}\n$$\n\nContent below'
expect(markdownThroughEditor(test)).toBe(test)
})
})
describe('Trailing nodes', () => {
test('No extra transaction is added after loading', () => {
const source = '# My heading\n\n* test\n* test2'
const tiptap = createRichEditor()
tiptap.commands.setContent(markdownit.render(source))
const jsonBefore = tiptap.getJSON()
// Focus triggers a transaction which is adding the trailing node
// this pushes a step through the collaboration plugin
// Resulting markdown will not contain the trailing paragraph so everytime the tiptap instance is created from the html, this transaction gets dispatched
tiptap.commands.focus()
const jsonAfter = tiptap.getJSON()
expect(jsonAfter).toStrictEqual(jsonBefore)
const serializer = createMarkdownSerializer(tiptap.schema)
const md = serializer.serialize(tiptap.state.doc)
expect(md).toBe(source)
tiptap.destroy()
})
})