-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
145 lines (134 loc) · 3.76 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
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
import assert from 'node:assert/strict'
import test from 'node:test'
import {h} from 'hastscript'
import {excerpt} from 'hast-util-excerpt'
import {selectAll} from 'hast-util-select'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {mdxFromMarkdown} from 'mdast-util-mdx'
import {toHast} from 'mdast-util-to-hast'
import {mdxjs} from 'micromark-extension-mdxjs'
import {u} from 'unist-builder'
import {removePosition} from 'unist-util-remove-position'
test('excerpt', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('hast-util-excerpt')).sort(), [
'excerpt'
])
})
await t.test('should copy to an excerpt comment', async function () {
assert.deepEqual(
excerpt(
h('div', [
h('p', 'Lorem ipsum dolor sit amet.'),
u('comment', 'more'),
h('p', 'Consectetur adipisicing elit.')
])
),
h('div', [h('p', 'Lorem ipsum dolor sit amet.')])
)
})
await t.test('should support `maxSearchSize`', async function () {
assert.deepEqual(
excerpt(
h('div', [
h('p', 'Lorem ipsum dolor sit amet.'),
u('comment', 'more'),
h('p', 'Consectetur adipisicing elit.')
]),
{maxSearchSize: 24}
),
undefined
)
})
await t.test(
'should copy to an excerpt comment inside a paragraph',
async function () {
assert.deepEqual(
excerpt(
h('div', [
h('p', [
'Lorem ipsum dolor sit amet.',
u('comment', 'more'),
'Consectetur adipisicing elit.'
])
])
),
h('div', [h('p', 'Lorem ipsum dolor sit amet.')])
)
}
)
await t.test(
'should return `undefined` if there’s no comment',
async function () {
assert.deepEqual(
excerpt(
h('div', [
h('p', 'Lorem ipsum dolor sit amet.'),
h('p', 'Consectetur adipisicing elit.')
])
),
undefined
)
}
)
await t.test(
'should not match on comments with other values',
async function () {
assert.deepEqual(
excerpt(
h('div', [
h('p', 'Lorem ipsum dolor sit amet.'),
u('comment', 'just a comment'),
h('p', 'Consectetur adipisicing elit.')
])
),
undefined
)
}
)
await t.test(
'should ignore trailing whitespace around comments',
async function () {
assert.deepEqual(
excerpt(
h('div', [
h('p', 'Lorem ipsum dolor sit amet.'),
u('comment', '\r\n stop here\t\n'),
h('p', 'Consectetur adipisicing elit.')
]),
{comment: 'stop here'}
),
h('div', [h('p', 'Lorem ipsum dolor sit amet.')])
)
}
)
await t.test('should support `ignore`', async function () {
const tree = h('div', [
h('p', ['Lorem ipsum ', h('del', 'dolor'), ' sit amet.']),
u('comment', 'more'),
h('p', 'Consectetur adipisicing elit.')
])
assert.deepEqual(
excerpt(tree, {ignore: selectAll('del', tree)}),
h('div', [h('p', ['Lorem ipsum ', ' sit amet.'])])
)
})
await t.test(
'should integrate w/ `mdast-util-mdx` (support `/* comments */`)',
async function () {
const tree = fromMarkdown('Some text\n\n{/* more */}\n\nSome more text', {
extensions: [mdxjs()],
mdastExtensions: [mdxFromMarkdown()]
})
removePosition(tree)
assert.deepEqual(
excerpt(
toHast(tree, {
passThrough: ['mdxFlowExpression', 'mdxTextExpression', 'mdxjsEsm']
})
),
u('root', [h('p', 'Some text'), u('text', '\n')])
)
}
)
})