@@ -16,17 +16,23 @@ import { GlossaryContext } from './components/GlossaryItem';
1616import BaseUrlContext from './contexts/BaseUrl' ;
1717import { options } from './options' ;
1818
19- import transformers from './processor/transform' ;
19+ import transformers , { readmeComponentsTransformer } from './processor/transform' ;
2020import compilers from './processor/compile' ;
2121import MdxSyntaxError from './errors/mdx-syntax-error' ;
2222
2323const unimplemented = debug ( 'mdx:unimplemented' ) ;
2424
25+ type ComponentOpts = Record < string , ( ) => React . ReactNode > ;
26+
2527type RunOpts = Omit < RunOptions , 'Fragment' > & {
26- components ?: Record < string , ( ) => React . ReactNode > ;
28+ components ?: ComponentOpts ;
2729 imports ?: Record < string , unknown > ;
2830} ;
2931
32+ type MdastOpts = {
33+ components ?: ComponentOpts ;
34+ } ;
35+
3036export { Components } ;
3137
3238export const utils = {
@@ -46,6 +52,7 @@ const makeUseMDXComponents = (more: RunOpts['components']) => {
4652 ...more ,
4753 ...Components ,
4854 Variable,
55+ code : Components . Code ,
4956 'code-tabs' : Components . CodeTabs ,
5057 'html-block' : Components . HTMLBlock ,
5158 img : Components . Image ,
@@ -72,7 +79,8 @@ export const compile = (text: string, opts = {}) => {
7279 } ) ,
7380 ) . replace ( / a w a i t i m p o r t \( _ r e s o l v e D y n a m i c M d x S p e c i f i e r \( ' r e a c t ' \) \) / , 'arguments[0].imports.React' ) ;
7481 } catch ( error ) {
75- throw new MdxSyntaxError ( error , text ) ;
82+ console . error ( error ) ;
83+ throw error . line ? new MdxSyntaxError ( error , text ) : error ;
7684 }
7785} ;
7886
@@ -104,9 +112,14 @@ export const html = (text: string, opts = {}) => {
104112 unimplemented ( 'html export' ) ;
105113} ;
106114
107- const astProcessor = ( opts = { } ) => remark ( ) . use ( remarkMdx ) . use ( remarkFrontmatter ) . use ( remarkPlugins ) ;
115+ const astProcessor = ( opts = { } ) =>
116+ remark ( )
117+ . use ( remarkMdx )
118+ . use ( remarkFrontmatter )
119+ . use ( remarkPlugins )
120+ . use ( readmeComponentsTransformer , { components : opts . components } ) ;
108121
109- export const mdast : any = ( text : string , opts = { } ) => {
122+ export const mdast : any = ( text : string , opts : MdastOpts = { } ) => {
110123 const processor = astProcessor ( opts ) ;
111124
112125 const tree = processor . parse ( text ) ;
0 commit comments