import visit from 'unist-util-visit' ; import type { Root } from 'mdast' ; export function FileTextMdxPlugin ( ) { return transformer ; function transformer ( tree , file ) { const addImports = [ ] ; visit ( tree , 'jsx' , ( child , index , parent ) => { const regexp = /<FileText\s+src=["'](.*?)["']\s+syntax=["'](.*?)["']/ ; const match = ( child . value as string ) . match ( regexp ) ; if ( match ) { const [ , src , syntax ] = match ; const nextIndex = addImports . length ; const varName = `_fileText${ nextIndex }` ; addImports . push ( `import ${ varName } from "${ src }?raw";` ) ; child . value = `<FileText text={${ varName }} syntax="${ syntax }" />` ; } else { const basicRegexp = /<FileText\s+(?:.+?\s)?src=/ ; const basicMatch = child . value . match ( basicRegexp ) ; if ( basicMatch ) { throw new Error ( `Invalid usage of <FileText />: ${ child . value }.
            Correct Syntax: <FileText src="./path/to/file" syntax="ts|tsx|text" />` ) ; } } } ) ; tree . children . unshift ( ... addImports . map ( ( importStr ) => { return { type : 'import' , value : importStr , } as any ; } ) ) ; } }