@@ -6,6 +6,7 @@ import React from 'react'
6
6
import rtl from '@testing-library/react'
7
7
import leftPad from 'left-pad'
8
8
import { remarkMdxImages } from 'remark-mdx-images'
9
+ import { VFile } from 'vfile'
9
10
import { bundleMDX } from '../index.js'
10
11
import { getMDXComponent , getMDXExport } from '../client.js'
11
12
@@ -469,6 +470,61 @@ Local Content
469
470
)
470
471
} )
471
472
473
+ test ( 'should support mdx from VFile' , async ( ) => {
474
+ const mdxSource = `# Heading`
475
+
476
+ const vfile = new VFile ( { value : mdxSource , path : '/data/mdx/my-post.mdx' } )
477
+
478
+ const { code} = await bundleMDX ( { source : vfile } )
479
+
480
+ const Component = getMDXComponent ( code )
481
+
482
+ const { container} = render ( React . createElement ( Component ) )
483
+
484
+ assert . is ( container . innerHTML , '<h1>Heading</h1>' )
485
+ } )
486
+
487
+ test ( 'should support mdx from VFile without path' , async ( ) => {
488
+ const mdxSource = `# Heading`
489
+
490
+ const vfile = new VFile ( { value : mdxSource } )
491
+
492
+ const { code} = await bundleMDX ( { source : vfile } )
493
+
494
+ const Component = getMDXComponent ( code )
495
+
496
+ const { container} = render ( React . createElement ( Component ) )
497
+
498
+ assert . is ( container . innerHTML , '<h1>Heading</h1>' )
499
+ } )
500
+
501
+ test ( 'should provide VFile path to plugins' , async ( ) => {
502
+ const mdxSource = `# Heading`
503
+
504
+ const vfile = new VFile ( { value : mdxSource , path : '/data/mdx/my-post.mdx' } )
505
+
506
+ /** @type {import('unified').Plugin } */
507
+ function plugin ( ) {
508
+ return function transformer ( tree , file ) {
509
+ assert . is ( file . path , '/data/mdx/my-post.mdx' )
510
+ }
511
+ }
512
+
513
+ const { code} = await bundleMDX ( {
514
+ source : vfile ,
515
+ mdxOptions ( options ) {
516
+ options . remarkPlugins = [ plugin ]
517
+ return options
518
+ } ,
519
+ } )
520
+
521
+ const Component = getMDXComponent ( code )
522
+
523
+ const { container} = render ( React . createElement ( Component ) )
524
+
525
+ assert . is ( container . innerHTML , '<h1>Heading</h1>' )
526
+ } )
527
+
472
528
test ( 'should work with react-dom api' , async ( ) => {
473
529
const mdxSource = `
474
530
import Demo from './demo'
0 commit comments