@@ -6,6 +6,7 @@ import React from 'react'
66import rtl from '@testing-library/react'
77import leftPad from 'left-pad'
88import { remarkMdxImages } from 'remark-mdx-images'
9+ import { VFile } from 'vfile'
910import { bundleMDX } from '../index.js'
1011import { getMDXComponent , getMDXExport } from '../client.js'
1112
@@ -469,6 +470,61 @@ Local Content
469470 )
470471} )
471472
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+
472528test ( 'should work with react-dom api' , async ( ) => {
473529 const mdxSource = `
474530import Demo from './demo'
0 commit comments