|
1 | 1 | import assert from 'node:assert/strict'
|
2 | 2 | import test from 'node:test'
|
3 |
| -import {u} from 'unist-builder' |
4 | 3 | import {h} from 'hastscript'
|
5 |
| -import {removePosition} from 'unist-util-remove-position' |
6 | 4 | import {selectAll} from 'hast-util-select'
|
7 | 5 | import {fromMarkdown} from 'mdast-util-from-markdown'
|
8 |
| -import {mdxjs} from 'micromark-extension-mdxjs' |
9 | 6 | import {mdxFromMarkdown} from 'mdast-util-mdx'
|
10 | 7 | import {toHast} from 'mdast-util-to-hast'
|
| 8 | +import {mdxjs} from 'micromark-extension-mdxjs' |
| 9 | +import {u} from 'unist-builder' |
| 10 | +import {removePosition} from 'unist-util-remove-position' |
11 | 11 | import {excerpt} from './index.js'
|
12 |
| -import * as mod from './index.js' |
13 |
| - |
14 |
| -test('excerpt', () => { |
15 |
| - assert.deepEqual( |
16 |
| - Object.keys(mod).sort(), |
17 |
| - ['excerpt'], |
18 |
| - 'should expose the public api' |
19 |
| - ) |
20 |
| - |
21 |
| - assert.deepEqual( |
22 |
| - excerpt( |
23 |
| - h('div', [ |
24 |
| - h('p', 'Lorem ipsum dolor sit amet.'), |
25 |
| - u('comment', 'more'), |
26 |
| - h('p', 'Consectetur adipisicing elit.') |
27 |
| - ]) |
28 |
| - ), |
29 |
| - h('div', [h('p', 'Lorem ipsum dolor sit amet.')]), |
30 |
| - 'should copy to an excerpt comment' |
31 |
| - ) |
32 | 12 |
|
33 |
| - assert.deepEqual( |
34 |
| - excerpt( |
35 |
| - h('div', [ |
36 |
| - h('p', 'Lorem ipsum dolor sit amet.'), |
37 |
| - u('comment', 'more'), |
38 |
| - h('p', 'Consectetur adipisicing elit.') |
39 |
| - ]), |
40 |
| - {maxSearchSize: 24} |
41 |
| - ), |
42 |
| - undefined, |
43 |
| - 'should support `maxSearchSize`' |
44 |
| - ) |
| 13 | +test('excerpt', async function (t) { |
| 14 | + await t.test('should expose the public api', async function () { |
| 15 | + assert.deepEqual(Object.keys(await import('./index.js')).sort(), [ |
| 16 | + 'excerpt' |
| 17 | + ]) |
| 18 | + }) |
45 | 19 |
|
46 |
| - assert.deepEqual( |
47 |
| - excerpt( |
48 |
| - h('div', [ |
49 |
| - h('p', [ |
50 |
| - 'Lorem ipsum dolor sit amet.', |
| 20 | + await t.test('should copy to an excerpt comment', async function () { |
| 21 | + assert.deepEqual( |
| 22 | + excerpt( |
| 23 | + h('div', [ |
| 24 | + h('p', 'Lorem ipsum dolor sit amet.'), |
51 | 25 | u('comment', 'more'),
|
52 |
| - 'Consectetur adipisicing elit.' |
| 26 | + h('p', 'Consectetur adipisicing elit.') |
53 | 27 | ])
|
54 |
| - ]) |
55 |
| - ), |
56 |
| - h('div', [h('p', 'Lorem ipsum dolor sit amet.')]), |
57 |
| - 'should copy to an excerpt comment inside a paragraph' |
58 |
| - ) |
| 28 | + ), |
| 29 | + h('div', [h('p', 'Lorem ipsum dolor sit amet.')]) |
| 30 | + ) |
| 31 | + }) |
59 | 32 |
|
60 |
| - assert.deepEqual( |
61 |
| - excerpt( |
62 |
| - h('div', [ |
63 |
| - h('p', 'Lorem ipsum dolor sit amet.'), |
64 |
| - h('p', 'Consectetur adipisicing elit.') |
65 |
| - ]) |
66 |
| - ), |
67 |
| - undefined, |
68 |
| - 'should return `undefined` if there’s no comment' |
69 |
| - ) |
| 33 | + await t.test('should support `maxSearchSize`', async function () { |
| 34 | + assert.deepEqual( |
| 35 | + excerpt( |
| 36 | + h('div', [ |
| 37 | + h('p', 'Lorem ipsum dolor sit amet.'), |
| 38 | + u('comment', 'more'), |
| 39 | + h('p', 'Consectetur adipisicing elit.') |
| 40 | + ]), |
| 41 | + {maxSearchSize: 24} |
| 42 | + ), |
| 43 | + undefined |
| 44 | + ) |
| 45 | + }) |
70 | 46 |
|
71 |
| - assert.deepEqual( |
72 |
| - excerpt( |
73 |
| - h('div', [ |
74 |
| - h('p', 'Lorem ipsum dolor sit amet.'), |
75 |
| - u('comment', 'just a comment'), |
76 |
| - h('p', 'Consectetur adipisicing elit.') |
77 |
| - ]) |
78 |
| - ), |
79 |
| - undefined, |
80 |
| - 'should not match on comments with other values' |
| 47 | + await t.test( |
| 48 | + 'should copy to an excerpt comment inside a paragraph', |
| 49 | + async function () { |
| 50 | + assert.deepEqual( |
| 51 | + excerpt( |
| 52 | + h('div', [ |
| 53 | + h('p', [ |
| 54 | + 'Lorem ipsum dolor sit amet.', |
| 55 | + u('comment', 'more'), |
| 56 | + 'Consectetur adipisicing elit.' |
| 57 | + ]) |
| 58 | + ]) |
| 59 | + ), |
| 60 | + h('div', [h('p', 'Lorem ipsum dolor sit amet.')]) |
| 61 | + ) |
| 62 | + } |
81 | 63 | )
|
82 | 64 |
|
83 |
| - assert.deepEqual( |
84 |
| - excerpt( |
85 |
| - h('div', [ |
86 |
| - h('p', 'Lorem ipsum dolor sit amet.'), |
87 |
| - u('comment', '\r\n stop here\t\n'), |
88 |
| - h('p', 'Consectetur adipisicing elit.') |
89 |
| - ]), |
90 |
| - {comment: 'stop here'} |
91 |
| - ), |
92 |
| - h('div', [h('p', 'Lorem ipsum dolor sit amet.')]), |
93 |
| - 'should ignore trailing whitespace around comments' |
| 65 | + await t.test( |
| 66 | + 'should return `undefined` if there’s no comment', |
| 67 | + async function () { |
| 68 | + assert.deepEqual( |
| 69 | + excerpt( |
| 70 | + h('div', [ |
| 71 | + h('p', 'Lorem ipsum dolor sit amet.'), |
| 72 | + h('p', 'Consectetur adipisicing elit.') |
| 73 | + ]) |
| 74 | + ), |
| 75 | + undefined |
| 76 | + ) |
| 77 | + } |
94 | 78 | )
|
95 | 79 |
|
96 |
| - const tree = h('div', [ |
97 |
| - h('p', ['Lorem ipsum ', h('del', 'dolor'), ' sit amet.']), |
98 |
| - u('comment', 'more'), |
99 |
| - h('p', 'Consectetur adipisicing elit.') |
100 |
| - ]) |
| 80 | + await t.test( |
| 81 | + 'should not match on comments with other values', |
| 82 | + async function () { |
| 83 | + assert.deepEqual( |
| 84 | + excerpt( |
| 85 | + h('div', [ |
| 86 | + h('p', 'Lorem ipsum dolor sit amet.'), |
| 87 | + u('comment', 'just a comment'), |
| 88 | + h('p', 'Consectetur adipisicing elit.') |
| 89 | + ]) |
| 90 | + ), |
| 91 | + undefined |
| 92 | + ) |
| 93 | + } |
| 94 | + ) |
101 | 95 |
|
102 |
| - assert.deepEqual( |
103 |
| - excerpt(tree, {ignore: selectAll('del', tree)}), |
104 |
| - h('div', [h('p', ['Lorem ipsum ', ' sit amet.'])]), |
105 |
| - 'should support `ignore`' |
| 96 | + await t.test( |
| 97 | + 'should ignore trailing whitespace around comments', |
| 98 | + async function () { |
| 99 | + assert.deepEqual( |
| 100 | + excerpt( |
| 101 | + h('div', [ |
| 102 | + h('p', 'Lorem ipsum dolor sit amet.'), |
| 103 | + u('comment', '\r\n stop here\t\n'), |
| 104 | + h('p', 'Consectetur adipisicing elit.') |
| 105 | + ]), |
| 106 | + {comment: 'stop here'} |
| 107 | + ), |
| 108 | + h('div', [h('p', 'Lorem ipsum dolor sit amet.')]) |
| 109 | + ) |
| 110 | + } |
106 | 111 | )
|
107 | 112 |
|
108 |
| - const tree2 = fromMarkdown('Some text\n\n{/* more */}\n\nSome more text', { |
109 |
| - extensions: [mdxjs()], |
110 |
| - mdastExtensions: [mdxFromMarkdown()] |
| 113 | + await t.test('should support `ignore`', async function () { |
| 114 | + const tree = h('div', [ |
| 115 | + h('p', ['Lorem ipsum ', h('del', 'dolor'), ' sit amet.']), |
| 116 | + u('comment', 'more'), |
| 117 | + h('p', 'Consectetur adipisicing elit.') |
| 118 | + ]) |
| 119 | + |
| 120 | + assert.deepEqual( |
| 121 | + excerpt(tree, {ignore: selectAll('del', tree)}), |
| 122 | + h('div', [h('p', ['Lorem ipsum ', ' sit amet.'])]) |
| 123 | + ) |
111 | 124 | })
|
112 |
| - removePosition(tree2) |
113 | 125 |
|
114 |
| - assert.deepEqual( |
115 |
| - excerpt( |
116 |
| - toHast(tree2, { |
117 |
| - passThrough: ['mdxFlowExpression', 'mdxTextExpression', 'mdxjsEsm'] |
| 126 | + await t.test( |
| 127 | + 'should integrate w/ `mdast-util-mdx` (support `/* comments */`)', |
| 128 | + async function () { |
| 129 | + const tree = fromMarkdown('Some text\n\n{/* more */}\n\nSome more text', { |
| 130 | + extensions: [mdxjs()], |
| 131 | + mdastExtensions: [mdxFromMarkdown()] |
118 | 132 | })
|
119 |
| - ), |
120 |
| - u('root', [h('p', 'Some text'), u('text', '\n')]), |
121 |
| - 'should integrate w/ `mdast-util-mdx` (support `/* comments */`)' |
| 133 | + removePosition(tree) |
| 134 | + |
| 135 | + assert.deepEqual( |
| 136 | + excerpt( |
| 137 | + toHast(tree, { |
| 138 | + passThrough: ['mdxFlowExpression', 'mdxTextExpression', 'mdxjsEsm'] |
| 139 | + }) |
| 140 | + ), |
| 141 | + u('root', [h('p', 'Some text'), u('text', '\n')]) |
| 142 | + ) |
| 143 | + } |
122 | 144 | )
|
123 | 145 | })
|
0 commit comments