forked from remarkjs/remark-slug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
234 lines (225 loc) · 5.54 KB
/
test.js
File metadata and controls
234 lines (225 loc) · 5.54 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
'use strict'
var test = require('tape')
var remark = require('remark')
var u = require('unist-builder')
var removePosition = require('unist-util-remove-position')
var slug = require('.')
test('remark-slug', function (t) {
t.deepEqual(
removePosition(
remark()
.use(slug)
.runSync(remark().parse('# Normal\n\n## Table of Contents\n\n# Baz\n')),
true
),
u('root', [
u(
'heading',
{depth: 1, data: {hProperties: {id: 'normal'}, id: 'normal'}},
[u('text', 'Normal')]
),
u(
'heading',
{
depth: 2,
data: {
hProperties: {id: 'table-of-contents'},
id: 'table-of-contents'
}
},
[u('text', 'Table of Contents')]
),
u('heading', {depth: 1, data: {hProperties: {id: 'baz'}, id: 'baz'}}, [
u('text', 'Baz')
])
]),
'should patch `id`s and `data.hProperties.id`'
)
t.deepEqual(
removePosition(
remark()
.use(function () {
return transform
function transform(tree) {
tree.children[0].data = {foo: 'bar'}
}
})
.use(slug)
.runSync(remark().parse('# Normal\n')),
true
),
u('root', [
u(
'heading',
{
depth: 1,
data: {foo: 'bar', hProperties: {id: 'normal'}, id: 'normal'}
},
[u('text', 'Normal')]
)
]),
'should not overwrite `data` on headings'
)
t.deepEqual(
removePosition(
remark()
.use(function () {
return transform
function transform(tree) {
tree.children[0].data = {hProperties: {className: ['foo']}}
}
})
.use(slug)
.runSync(remark().parse('# Normal\n')),
true
),
u('root', [
u(
'heading',
{
depth: 1,
data: {hProperties: {className: ['foo'], id: 'normal'}, id: 'normal'}
},
[u('text', 'Normal')]
)
]),
'should not overwrite `data.hProperties` on headings'
)
t.deepEqual(
removePosition(
remark()
.use(function () {
return transform
function transform(tree) {
tree.children[1].data = {hProperties: {id: 'here'}}
tree.children[3].data = {hProperties: {id: 'something'}}
}
})
.use(slug)
.runSync(
remark().parse(
[
'## Something',
'## Something here',
'## Something there',
'## Something also'
].join('\n\n') + '\n'
)
),
true
),
u('root', [
u(
'heading',
{
depth: 2,
data: {hProperties: {id: 'something'}, id: 'something'}
},
[u('text', 'Something')]
),
u(
'heading',
{
depth: 2,
data: {hProperties: {id: 'here'}, id: 'here'}
},
[u('text', 'Something here')]
),
u(
'heading',
{
depth: 2,
data: {hProperties: {id: 'something-there'}, id: 'something-there'}
},
[u('text', 'Something there')]
),
u(
'heading',
{
depth: 2,
data: {hProperties: {id: 'something-1'}, id: 'something-1'}
},
[u('text', 'Something also')]
)
]),
'should generate `id`s and `hProperties.id`s, based on `hProperties.id` if they exist'
)
t.deepEqual(
process(
[
'## I ♥ unicode',
'',
'## Dash-dash',
'',
'## en–dash',
'',
'## em–dash',
'',
'## 😄 unicode emoji',
'',
'## 😄-😄 unicode emoji',
'',
'## 😄_😄 unicode emoji',
'',
'##',
'',
'## ',
'',
'## Initial spaces',
'',
'## Final spaces ',
'',
'## Duplicate',
'',
'## Duplicate',
'',
'## :ok: No underscore',
'',
'## :ok_hand: Single',
'',
'## :ok_hand::hatched_chick: Two in a row with no spaces',
'',
'## :ok_hand: :hatched_chick: Two in a row',
''
].join('\n')
),
u('root', [
heading('I ♥ unicode', 'i--unicode'),
heading('Dash-dash', 'dash-dash'),
heading('en–dash', 'endash'),
heading('em–dash', 'emdash'),
heading('😄 unicode emoji', '-unicode-emoji'),
heading('😄-😄 unicode emoji', '--unicode-emoji'),
heading('😄_😄 unicode emoji', '_-unicode-emoji'),
heading(null, ''),
heading(null, '-1'),
heading('Initial spaces', 'initial-spaces'),
heading('Final spaces', 'final-spaces'),
heading('Duplicate', 'duplicate'),
heading('Duplicate', 'duplicate-1'),
heading(':ok: No underscore', 'ok-no-underscore'),
heading(':ok_hand: Single', 'ok_hand-single'),
heading(
':ok_hand::hatched_chick: Two in a row with no spaces',
'ok_handhatched_chick-two-in-a-row-with-no-spaces'
),
heading(
':ok_hand: :hatched_chick: Two in a row',
'ok_hand-hatched_chick-two-in-a-row'
)
]),
'should create GitHub slugs'
)
t.end()
})
function heading(label, id) {
return u(
'heading',
{depth: 2, data: {id: id, hProperties: {id: id}}},
label ? [u('text', label)] : []
)
}
function process(doc, options) {
var processor = remark().use(slug, options)
return removePosition(processor.runSync(processor.parse(doc)), true)
}