/
parse-markdown-links.unit.test.ts
120 lines (110 loc) · 3.73 KB
/
parse-markdown-links.unit.test.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
import { parseMarkdownLinks } from './parse-markdown-links'
describe('Parse Markdown Links should', () => {
test('parse a link only', async () => {
const parsed = parseMarkdownLinks({
body: '[link only](1)',
linkMap: {},
})
expect(parsed).toEqual([{ ref: '1', text: 'link only', type: 'link' }])
})
test('parse a link without a reference', async () => {
const parsed = parseMarkdownLinks({
body: '[link only]',
linkMap: {},
})
expect(parsed).toEqual([
{ ref: undefined, text: 'link only', type: 'link' },
])
})
test('parse a link without a reference with linkMap', async () => {
const parsed = parseMarkdownLinks({
body: '[link only]',
linkMap: { 1: '/link-here' },
})
expect(parsed).toEqual([
{ ref: '/link-here', text: 'link only', type: 'link' },
])
})
test('parse a link without a reference with linkMap and two links', async () => {
const parsed = parseMarkdownLinks({
body: '[link one] [link two]',
linkMap: { 1: '/link-here', 2: '/and-here' },
})
expect(parsed).toEqual([
{ ref: '/link-here', text: 'link one', type: 'link' },
{ ref: '/and-here', text: 'link two', type: 'link' },
])
})
test('parse a link without a reference with linkMap, two links and text', async () => {
const parsed = parseMarkdownLinks({
body: '[link one] middle [link two]',
linkMap: { 1: '/link-here', 2: '/and-here' },
})
expect(parsed).toEqual([
{ ref: '/link-here', text: 'link one', type: 'link' },
{ text: 'middle', type: 'text' },
{ ref: '/and-here', text: 'link two', type: 'link' },
])
})
test('parse a link without a reference with linkMap, two links and text around', async () => {
const parsed = parseMarkdownLinks({
body: 'start [link one] middle [link two] end',
linkMap: { 1: '/link-here', 2: '/and-here' },
})
expect(parsed).toEqual([
{ text: 'start', type: 'text' },
{ ref: '/link-here', text: 'link one', type: 'link' },
{ text: 'middle', type: 'text' },
{ ref: '/and-here', text: 'link two', type: 'link' },
{ text: 'end', type: 'text' },
])
})
test('parse text only', async () => {
const parsed = parseMarkdownLinks({
body: 'text only',
linkMap: {},
})
expect(parsed).toEqual([{ text: 'text only', type: 'text' }])
})
test('parse text before a link', async () => {
const parsed = parseMarkdownLinks({
body: 'text before [a link](1)',
linkMap: {},
})
expect(parsed).toEqual([
{ text: 'text before', type: 'text' },
{ ref: '1', text: 'a link', type: 'link' },
])
})
test('parse text after a link', async () => {
const parsed = parseMarkdownLinks({
body: '[a link](1) text after',
linkMap: {},
})
expect(parsed).toEqual([
{ ref: '1', text: 'a link', type: 'link' },
{ text: 'text after', type: 'text' },
])
})
test('parse a mixed example', async () => {
const linkMap = {
1: '/home',
2: '/somewhere',
}
const parsed = parseMarkdownLinks({
body: 'Dit is [een link](1) met [twee link] en dan nog [drie link](2) en nog wat tekst. Meer links [kunnen](https://www.ok.nl) ook.',
linkMap,
})
expect(parsed).toEqual([
{ type: 'text', text: 'Dit is' },
{ type: 'link', text: 'een link', ref: '/home' },
{ type: 'text', text: 'met' },
{ type: 'link', text: 'twee link', ref: '/somewhere' },
{ type: 'text', text: 'en dan nog' },
{ type: 'link', text: 'drie link', ref: '/somewhere' },
{ type: 'text', text: 'en nog wat tekst. Meer links' },
{ type: 'link', text: 'kunnen', ref: 'https://www.ok.nl' },
{ type: 'text', text: 'ook.' },
])
})
})