-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.js
85 lines (73 loc) · 2.48 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
/**
* @typedef {import('mdast').Heading} Heading
*/
import assert from 'node:assert/strict'
import test from 'node:test'
import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import rehypeRemark from 'rehype-remark'
import remarkStringify from 'remark-stringify'
import {unified} from 'unified'
test('rehypeRemark', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('rehype-remark')).sort(), [
'default'
])
})
await t.test('should mutate', async function () {
const file = await unified()
.use(rehypeParse)
.use(rehypeRemark)
.use(remarkStringify)
.process('<h2>Hello, world!</h2>')
assert.equal(String(file), '## Hello, world!\n')
})
await t.test('should bridge', async function () {
const file = await unified()
.use(rehypeParse, {fragment: true})
// @ts-expect-error: TS currently barfs on overloads that result in mutate/bridges.
.use(rehypeRemark, unified())
.use(rehypeStringify)
.process('<h2>Hello, world!</h2>')
assert.equal(String(file), '<h2>Hello, world!</h2>')
})
await t.test('should support `document: false`', async function () {
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeRemark, {document: false})
.use(remarkStringify)
.process('<i>Hello</i>, <b>world</b>!')
assert.equal(String(file), '*Hello*, **world**!\n')
})
await t.test('should default to `document: true`', async function () {
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeRemark)
.use(remarkStringify)
.processSync('<i>Hello</i>, <b>world</b>!')
assert.equal(String(file), '*Hello*, **world**!\n')
})
await t.test('should support `options.handlers`', async function () {
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeRemark, {
handlers: {
div(state, node) {
if (node.tagName === 'div') {
/** @type {Heading} */
const result = {
type: 'heading',
depth: 1,
children: [{type: 'text', value: 'changed'}]
}
state.patch(node, result)
return result
}
}
}
})
.use(remarkStringify)
.process('<div>example</div>')
assert.equal(String(file), '# changed\n')
})
})