forked from valeriangalliat/markdown-it-anchor
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
93 lines (76 loc) · 2.9 KB
/
test.js
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
const { equal } = require('assert')
const md = require('markdown-it')
const attrs = require('markdown-it-attrs')
const anchor = require('./')
equal(
md().use(anchor).render('# H1\n\n## H2'),
'<h1 id="h1">H1</h1>\n<h2 id="h2">H2</h2>\n'
)
equal(
md().use(attrs, anchor).render('# H1 {id=bubblegum}\n\n## H2 {id=shoelaces}'),
'<h1 id="bubblegum">H1</h1>\n<h2 id="shoelaces">H2</h2>\n'
)
equal(
md().use(anchor, { level: 2 }).render('# H1\n\n## H2'),
'<h1>H1</h1>\n<h2 id="h2">H2</h2>\n'
)
equal(
md().use(anchor, { level: [2, 4] }).render('# H1\n\n## H2\n\n### H3\n\n#### H4\n\n##### H5'),
'<h1>H1</h1>\n<h2 id="h2">H2</h2>\n<h3>H3</h3>\n<h4 id="h4">H4</h4>\n<h5>H5</h5>\n'
)
equal(
md().use(anchor, { permalink: true }).render('# H1'),
'<h1 id="h1">H1 <a class="header-anchor" href="#h1" aria-hidden="true">¶</a></h1>\n'
)
equal(
md().use(anchor, { permalink: true, permalinkClass: 'test' }).render('# H1'),
'<h1 id="h1">H1 <a class="test" href="#h1" aria-hidden="true">¶</a></h1>\n'
)
equal(
md().use(anchor, { permalink: true, permalinkSymbol: 'P' }).render('# H1'),
'<h1 id="h1">H1 <a class="header-anchor" href="#h1" aria-hidden="true">P</a></h1>\n'
)
equal(
md().use(anchor, { permalink: true, permalinkSymbol: '<i class="icon"></i>' }).render('# H1'),
'<h1 id="h1">H1 <a class="header-anchor" href="#h1" aria-hidden="true"><i class="icon"></i></a></h1>\n'
)
equal(
md().use(anchor).render('# Title\n\n## Title'),
'<h1 id="title">Title</h1>\n<h2 id="title-2">Title</h2>\n'
)
equal(
md().use(anchor, { permalink: true, permalinkBefore: true }).render('# H1'),
'<h1 id="h1"><a class="header-anchor" href="#h1" aria-hidden="true">¶</a> H1</h1>\n'
)
equal(
md().use(anchor, { level: 2, permalink: true }).render('# H1\n\n## H2'),
'<h1>H1</h1>\n<h2 id="h2">H2 <a class="header-anchor" href="#h2" aria-hidden="true">¶</a></h2>\n'
)
equal(
md({ html: true }).use(anchor, { permalink: true }).render('# <span>H1</span>'),
'<h1 id="h1"><span>H1</span> <a class="header-anchor" href="#h1" aria-hidden="true">¶</a></h1>\n'
)
equal(
md().use(anchor).render('#### `options`'),
'<h4 id="options"><code>options</code></h4>\n'
)
const calls = []
const callback = (token, info) => calls.push({ token, info })
equal(
md().use(anchor, { callback }).render('# First Heading\n\n## Second Heading'),
'<h1 id="first-heading">First Heading</h1>\n<h2 id="second-heading">Second Heading</h2>\n'
)
equal(
md().use(anchor, {
permalinkHref: (slug, state) => `${state.env.path}#${slug}`,
permalink: true
}).render('# H1', { path: 'file.html' }),
'<h1 id="h1">H1 <a class="header-anchor" href="file.html#h1" aria-hidden="true">¶</a></h1>\n'
)
equal(calls.length, 2)
equal(calls[0].token.tag, 'h1')
equal(calls[0].info.title, 'First Heading')
equal(calls[0].info.slug, 'first-heading')
equal(calls[1].token.tag, 'h2')
equal(calls[1].info.title, 'Second Heading')
equal(calls[1].info.slug, 'second-heading')