@@ -4,6 +4,7 @@ import { expect } from 'chai';
4
4
import unified from 'unified' ;
5
5
import markdown from 'remark-parse' ;
6
6
import mdStringify from 'remark-html' ;
7
+ import gfm from 'remark-gfm' ;
7
8
8
9
import { remarkExtend } from '../src/remarkExtend.js' ;
9
10
@@ -22,6 +23,9 @@ async function expectThrowsAsync(method, { errorMatch, errorMessage } = {}) {
22
23
} catch ( err ) {
23
24
error = err ;
24
25
}
26
+
27
+ console . debug ( error ) ;
28
+
25
29
expect ( error ) . to . be . an ( 'Error' , 'No error was thrown' ) ;
26
30
if ( errorMatch ) {
27
31
expect ( error . message ) . to . match ( errorMatch ) ;
@@ -31,18 +35,40 @@ async function expectThrowsAsync(method, { errorMatch, errorMessage } = {}) {
31
35
}
32
36
}
33
37
34
- async function execute ( input , { globalReplaceFunction } = { } ) {
38
+ /**
39
+ *
40
+ * @param {string } input
41
+ * @param {{shouldStringify?: boolean; globalReplaceFunction?: Function;} } param1
42
+ */
43
+ async function execute ( input , { shouldStringify = true , globalReplaceFunction } = { } ) {
35
44
const parser = unified ( )
36
45
//
37
46
. use ( markdown )
47
+ . use ( gfm )
38
48
. use ( remarkExtend , {
39
49
rootDir : __dirname ,
40
50
page : { inputPath : 'test-file.md' } ,
41
51
globalReplaceFunction,
52
+ } ) ;
53
+
54
+ if ( shouldStringify ) {
55
+ parser . use ( mdStringify ) ;
56
+ const result = await parser . process ( input ) ;
57
+ return result . contents ;
58
+ }
59
+
60
+ let tree ;
61
+ parser
62
+ . use ( ( ) => _tree => {
63
+ tree = _tree ;
42
64
} )
43
- . use ( mdStringify ) ;
44
- const result = await parser . process ( input ) ;
45
- return result . contents ;
65
+ // @ts -expect-error
66
+ . use ( function plugin ( ) {
67
+ this . Compiler = ( ) => '' ;
68
+ } ) ;
69
+
70
+ await parser . process ( input ) ;
71
+ return tree ;
46
72
}
47
73
48
74
describe ( 'remarkExtend' , ( ) => {
@@ -196,7 +222,7 @@ describe('remarkExtend', () => {
196
222
"```js ::import('./fixtures/three-sections-red.md', 'heading:has([value=Does not exit])')\n```" ;
197
223
await expectThrowsAsync ( ( ) => execute ( input ) , {
198
224
errorMatch :
199
- / T h e s t a r t s e l e c t o r " h e a d i n g : h a s \( \[ v a l u e = D o e s n o t e x i t \] \) " c o u l d n o t f i n d a m a t c h i n g n o d e i n " .* " \. $ / ,
225
+ / T h e s t a r t s e l e c t o r " h e a d i n g : h a s \( \[ v a l u e = D o e s n o t e x i t \] \) " , i m p o r t e d i n " . * " , c o u l d n o t f i n d a m a t c h i n g n o d e i n " .* " \. $ / ,
200
226
} ) ;
201
227
} ) ;
202
228
@@ -205,7 +231,7 @@ describe('remarkExtend', () => {
205
231
"```js ::import('./fixtures/three-sections-red.md', 'heading:has([value=More Red])', 'heading:has([value=Does not exit])')\n```" ;
206
232
await expectThrowsAsync ( ( ) => execute ( input ) , {
207
233
errorMatch :
208
- / T h e e n d s e l e c t o r " h e a d i n g : h a s \( \[ v a l u e = D o e s n o t e x i t \] \) " c o u l d n o t f i n d a m a t c h i n g n o d e i n " .* " \. / ,
234
+ / T h e e n d s e l e c t o r " h e a d i n g : h a s \( \[ v a l u e = D o e s n o t e x i t \] \) " , i m p o r t e d i n " . * " , c o u l d n o t f i n d a m a t c h i n g n o d e i n " .* " \. / ,
209
235
} ) ;
210
236
} ) ;
211
237
@@ -404,4 +430,25 @@ describe('remarkExtend', () => {
404
430
] . join ( '\n' ) ,
405
431
) ;
406
432
} ) ;
433
+
434
+ it . only ( 'supports files with frontmatter' , async ( ) => {
435
+ const result = await execute (
436
+ [
437
+ //
438
+ '### Static Headline' ,
439
+ "```js ::importBlock('./fixtures/three-sections-red-with-frontmatter.md', '## More Red')" ,
440
+ '```' ,
441
+ ] . join ( '\n' ) ,
442
+ ) ;
443
+
444
+ expect ( result ) . to . equal (
445
+ [
446
+ //
447
+ '<h3>Static Headline</h3>' ,
448
+ '<h2>More Red</h2>' ,
449
+ '<p>the sun can get red</p>' ,
450
+ '' ,
451
+ ] . join ( '\n' ) ,
452
+ ) ;
453
+ } ) ;
407
454
} ) ;
0 commit comments