5
5
import assert from 'node:assert/strict'
6
6
import test from 'node:test'
7
7
import { common } from 'lowlight'
8
- import { rehype } from 'rehype'
9
8
import rehypeHighlight from 'rehype-highlight'
9
+ import rehypeParse from 'rehype-parse'
10
+ import rehypeStringify from 'rehype-stringify'
11
+ import { unified } from 'unified'
10
12
11
13
test ( 'rehypeHighlight' , async function ( t ) {
12
14
await t . test ( 'should expose the public api' , async function ( ) {
@@ -16,9 +18,10 @@ test('rehypeHighlight', async function (t) {
16
18
} )
17
19
18
20
await t . test ( 'should work on empty code' , async function ( ) {
19
- const file = await rehype ( )
20
- . data ( 'settings' , { fragment : true } )
21
+ const file = await unified ( )
22
+ . use ( rehypeParse , { fragment : true } )
21
23
. use ( rehypeHighlight , { detect : true } )
24
+ . use ( rehypeStringify )
22
25
. process (
23
26
[ '<h1>Hello World!</h1>' , '' , '<pre><code></code></pre>' ] . join ( '\n' )
24
27
)
@@ -34,9 +37,10 @@ test('rehypeHighlight', async function (t) {
34
37
} )
35
38
36
39
await t . test ( 'should not highlight (no class)' , async function ( ) {
37
- const file = await rehype ( )
38
- . data ( 'settings' , { fragment : true } )
40
+ const file = await unified ( )
41
+ . use ( rehypeParse , { fragment : true } )
39
42
. use ( rehypeHighlight )
43
+ . use ( rehypeStringify )
40
44
. process (
41
45
[
42
46
'<h1>Hello World!</h1>' ,
@@ -56,9 +60,10 @@ test('rehypeHighlight', async function (t) {
56
60
} )
57
61
58
62
await t . test ( 'should highlight (`detect`, no class)' , async function ( ) {
59
- const file = await rehype ( )
60
- . data ( 'settings' , { fragment : true } )
63
+ const file = await unified ( )
64
+ . use ( rehypeParse , { fragment : true } )
61
65
. use ( rehypeHighlight , { detect : true } )
66
+ . use ( rehypeStringify )
62
67
. process (
63
68
[
64
69
'<h1>Hello World!</h1>' ,
@@ -80,9 +85,10 @@ test('rehypeHighlight', async function (t) {
80
85
await t . test (
81
86
'should highlight (detect, no class, subset)' ,
82
87
async function ( ) {
83
- const file = await rehype ( )
84
- . data ( 'settings' , { fragment : true } )
88
+ const file = await unified ( )
89
+ . use ( rehypeParse , { fragment : true } )
85
90
. use ( rehypeHighlight , { detect : true , subset : [ 'arduino' ] } )
91
+ . use ( rehypeStringify )
86
92
. process (
87
93
[
88
94
'<h1>Hello World!</h1>' ,
@@ -105,9 +111,10 @@ test('rehypeHighlight', async function (t) {
105
111
await t . test (
106
112
'should not highlight (`detect: false`, no class)' ,
107
113
async function ( ) {
108
- const file = await rehype ( )
109
- . data ( 'settings' , { fragment : true } )
114
+ const file = await unified ( )
115
+ . use ( rehypeParse , { fragment : true } )
110
116
. use ( rehypeHighlight , { detect : false } )
117
+ . use ( rehypeStringify )
111
118
. process (
112
119
[
113
120
'<h1>Hello World!</h1>' ,
@@ -128,9 +135,10 @@ test('rehypeHighlight', async function (t) {
128
135
)
129
136
130
137
await t . test ( 'should highlight (prefix without dash)' , async function ( ) {
131
- const file = await rehype ( )
132
- . data ( 'settings' , { fragment : true } )
138
+ const file = await unified ( )
139
+ . use ( rehypeParse , { fragment : true } )
133
140
. use ( rehypeHighlight , { detect : true , prefix : 'foo' } )
141
+ . use ( rehypeStringify )
134
142
. process (
135
143
[
136
144
'<h1>Hello World!</h1>' ,
@@ -150,9 +158,10 @@ test('rehypeHighlight', async function (t) {
150
158
} )
151
159
152
160
await t . test ( 'should highlight (prefix with dash)' , async function ( ) {
153
- const file = await rehype ( )
154
- . data ( 'settings' , { fragment : true } )
161
+ const file = await unified ( )
162
+ . use ( rehypeParse , { fragment : true } )
155
163
. use ( rehypeHighlight , { detect : true , prefix : 'foo-' } )
164
+ . use ( rehypeStringify )
156
165
. process (
157
166
[
158
167
'<h1>Hello World!</h1>' ,
@@ -172,9 +181,10 @@ test('rehypeHighlight', async function (t) {
172
181
} )
173
182
174
183
await t . test ( 'should highlight (lang class)' , async function ( ) {
175
- const file = await rehype ( )
176
- . data ( 'settings' , { fragment : true } )
184
+ const file = await unified ( )
185
+ . use ( rehypeParse , { fragment : true } )
177
186
. use ( rehypeHighlight )
187
+ . use ( rehypeStringify )
178
188
. process (
179
189
[
180
190
'<h1>Hello World!</h1>' ,
@@ -196,9 +206,10 @@ test('rehypeHighlight', async function (t) {
196
206
} )
197
207
198
208
await t . test ( 'should highlight (language class)' , async function ( ) {
199
- const file = await rehype ( )
200
- . data ( 'settings' , { fragment : true } )
209
+ const file = await unified ( )
210
+ . use ( rehypeParse , { fragment : true } )
201
211
. use ( rehypeHighlight )
212
+ . use ( rehypeStringify )
202
213
. process (
203
214
[
204
215
'<h1>Hello World!</h1>' ,
@@ -220,9 +231,10 @@ test('rehypeHighlight', async function (t) {
220
231
} )
221
232
222
233
await t . test ( 'should highlight (long name)' , async function ( ) {
223
- const file = await rehype ( )
224
- . data ( 'settings' , { fragment : true } )
234
+ const file = await unified ( )
235
+ . use ( rehypeParse , { fragment : true } )
225
236
. use ( rehypeHighlight )
237
+ . use ( rehypeStringify )
226
238
. process (
227
239
[
228
240
'<h1>Hello World!</h1>' ,
@@ -244,9 +256,10 @@ test('rehypeHighlight', async function (t) {
244
256
} )
245
257
246
258
await t . test ( 'should not highlight (`no-highlight`)' , async function ( ) {
247
- const file = await rehype ( )
248
- . data ( 'settings' , { fragment : true } )
259
+ const file = await unified ( )
260
+ . use ( rehypeParse , { fragment : true } )
249
261
. use ( rehypeHighlight )
262
+ . use ( rehypeStringify )
250
263
. process (
251
264
[
252
265
'<h1>Hello World!</h1>' ,
@@ -270,9 +283,10 @@ test('rehypeHighlight', async function (t) {
270
283
await t . test (
271
284
'should prefer `no-highlight` over a `language-*` class' ,
272
285
async function ( ) {
273
- const file = await rehype ( )
274
- . data ( 'settings' , { fragment : true } )
286
+ const file = await unified ( )
287
+ . use ( rehypeParse , { fragment : true } )
275
288
. use ( rehypeHighlight )
289
+ . use ( rehypeStringify )
276
290
. process (
277
291
'<h1>Hello World!</h1>\n<pre><code class="lang-js no-highlight">alert(1)</code></pre>'
278
292
)
@@ -285,9 +299,10 @@ test('rehypeHighlight', async function (t) {
285
299
)
286
300
287
301
await t . test ( 'should not highlight (`nohighlight`)' , async function ( ) {
288
- const file = await rehype ( )
289
- . data ( 'settings' , { fragment : true } )
302
+ const file = await unified ( )
303
+ . use ( rehypeParse , { fragment : true } )
290
304
. use ( rehypeHighlight )
305
+ . use ( rehypeStringify )
291
306
. process (
292
307
[
293
308
'<h1>Hello World!</h1>' ,
@@ -309,9 +324,10 @@ test('rehypeHighlight', async function (t) {
309
324
} )
310
325
311
326
await t . test ( 'should warn on missing languages' , async function ( ) {
312
- const file = await rehype ( )
313
- . data ( 'settings' , { fragment : true } )
327
+ const file = await unified ( )
328
+ . use ( rehypeParse , { fragment : true } )
314
329
. use ( rehypeHighlight )
330
+ . use ( rehypeStringify )
315
331
. process (
316
332
[
317
333
'<h1>Hello World!</h1>' ,
@@ -329,9 +345,10 @@ test('rehypeHighlight', async function (t) {
329
345
await t . test (
330
346
'should not highlight plainText-ed languages' ,
331
347
async function ( ) {
332
- const file = await rehype ( )
333
- . data ( 'settings' , { fragment : true } )
348
+ const file = await unified ( )
349
+ . use ( rehypeParse , { fragment : true } )
334
350
. use ( rehypeHighlight , { plainText : [ 'js' ] } )
351
+ . use ( rehypeStringify )
335
352
. process (
336
353
[
337
354
'<h1>Hello World!</h1>' ,
@@ -355,9 +372,10 @@ test('rehypeHighlight', async function (t) {
355
372
356
373
await t . test ( 'should not remove contents' , async function ( ) {
357
374
// For some reason this isn’t detected as c++.
358
- const file = await rehype ( )
359
- . data ( 'settings' , { fragment : true } )
375
+ const file = await unified ( )
376
+ . use ( rehypeParse , { fragment : true } )
360
377
. use ( rehypeHighlight , { detect : true , subset : [ 'cpp' ] } )
378
+ . use ( rehypeStringify )
361
379
. process ( `<pre><code>def add(a, b):\n return a + b</code></pre>` )
362
380
363
381
assert . equal (
@@ -367,9 +385,10 @@ test('rehypeHighlight', async function (t) {
367
385
} )
368
386
369
387
await t . test ( 'should support multiple `code`s in a `pre`' , async function ( ) {
370
- const file = await rehype ( )
371
- . data ( 'settings' , { fragment : true } )
372
- . use ( rehypeHighlight ) . process ( `<pre>
388
+ const file = await unified ( )
389
+ . use ( rehypeParse , { fragment : true } )
390
+ . use ( rehypeHighlight )
391
+ . use ( rehypeStringify ) . process ( `<pre>
373
392
<code class="language-javascript">const a = 1;</code>
374
393
<code class="language-python">printf("x")</code>
375
394
</pre>` )
@@ -381,9 +400,10 @@ test('rehypeHighlight', async function (t) {
381
400
} )
382
401
383
402
await t . test ( 'should reprocess exact' , async function ( ) {
384
- const file = await rehype ( )
385
- . data ( 'settings' , { fragment : true } )
403
+ const file = await unified ( )
404
+ . use ( rehypeParse , { fragment : true } )
386
405
. use ( rehypeHighlight )
406
+ . use ( rehypeStringify )
387
407
. process (
388
408
[
389
409
'<h1>Hello World!</h1>' ,
@@ -405,11 +425,10 @@ test('rehypeHighlight', async function (t) {
405
425
} )
406
426
407
427
await t . test ( 'should parse custom language' , async function ( ) {
408
- const file = await rehype ( )
409
- . data ( 'settings' , { fragment : true } )
410
- . use ( rehypeHighlight , {
411
- aliases : { javascript : [ 'funkyscript' ] }
412
- } )
428
+ const file = await unified ( )
429
+ . use ( rehypeParse , { fragment : true } )
430
+ . use ( rehypeHighlight , { aliases : { javascript : [ 'funkyscript' ] } } )
431
+ . use ( rehypeStringify )
413
432
. process (
414
433
'<pre><code class="lang-funkyscript">console.log(1)</code></pre>'
415
434
)
@@ -421,9 +440,10 @@ test('rehypeHighlight', async function (t) {
421
440
} )
422
441
423
442
await t . test ( 'should reprocess exact' , async function ( ) {
424
- const file = await rehype ( )
425
- . data ( 'settings' , { fragment : true } )
443
+ const file = await unified ( )
444
+ . use ( rehypeParse , { fragment : true } )
426
445
. use ( rehypeHighlight )
446
+ . use ( rehypeStringify )
427
447
. process (
428
448
[
429
449
'<h1>Hello World!</h1>' ,
@@ -445,9 +465,10 @@ test('rehypeHighlight', async function (t) {
445
465
} )
446
466
447
467
await t . test ( 'should ignore comments' , async function ( ) {
448
- const file = await rehype ( )
449
- . data ( 'settings' , { fragment : true } )
468
+ const file = await unified ( )
469
+ . use ( rehypeParse , { fragment : true } )
450
470
. use ( rehypeHighlight , { detect : true } )
471
+ . use ( rehypeStringify )
451
472
. process (
452
473
[
453
474
'<h1>Hello World!</h1>' ,
@@ -467,9 +488,10 @@ test('rehypeHighlight', async function (t) {
467
488
} )
468
489
469
490
await t . test ( 'should support `<br>` elements' , async function ( ) {
470
- const file = await rehype ( )
471
- . data ( 'settings' , { fragment : true } )
491
+ const file = await unified ( )
492
+ . use ( rehypeParse , { fragment : true } )
472
493
. use ( rehypeHighlight )
494
+ . use ( rehypeStringify )
473
495
. process (
474
496
[
475
497
'<h1>Hello World!</h1>' ,
@@ -490,9 +512,10 @@ test('rehypeHighlight', async function (t) {
490
512
} )
491
513
492
514
await t . test ( 'should register languages' , async function ( ) {
493
- const file = await rehype ( )
494
- . data ( 'settings' , { fragment : true } )
515
+ const file = await unified ( )
516
+ . use ( rehypeParse , { fragment : true } )
495
517
. use ( rehypeHighlight , { languages : { ...common , test : testLang } } )
518
+ . use ( rehypeStringify )
496
519
. process (
497
520
[
498
521
'<h1>Hello World!</h1>' ,
0 commit comments