@@ -34,6 +34,9 @@ const getBooleanValue = (param, field, defaultValue) => {
3434} ;
3535
3636const Preview = ( { path, ...mdData } ) => {
37+ const Preview = CodeLayout . Preview ;
38+ const Code = CodeLayout . Code ;
39+ const Toolbar = CodeLayout . Toolbar ;
3740 const $dom = useRef ( null ) ;
3841 return (
3942 < Wrapper ref = { $dom } >
@@ -43,49 +46,43 @@ const Preview = ({ path, ...mdData }) => {
4346 // transformImageUri={transformImageUri}
4447 source = { mdData . source }
4548 rehypeRewrite = { ( node , index , parent ) => {
46- if ( node . type === 'element' && node . tagName === 'pre' && node . children [ 0 ] . data ?. meta ) {
47- const meta = node . children [ 0 ] . data ?. meta ;
48- if ( isMeta ( meta ) ) {
49- node . tagName = 'div' ;
50- if ( ! node . properties ) {
51- node . properties = { } ;
52- }
53- node . properties [ 'data-md' ] = meta ;
54- node . properties [ 'data-meta' ] = 'preview' ;
49+ if ( node . type === 'element' && parent && parent . type === 'root' && / h ( 1 | 2 | 3 | 4 | 5 | 6 ) / . test ( node . tagName ) ) {
50+ const child = node . children && node . children [ 0 ] ;
51+ if ( child && child . properties && child . properties . ariaHidden === 'true' ) {
52+ child . children = [ ] ;
5553 }
5654 }
5755 } }
5856 components = { {
59- div : ( { node, ...props } ) => {
60- const { 'data-meta' : meta , 'data-md' : metaData } = props ;
61- if ( meta === 'preview' ) {
62- const line = node . position ?. start . line ;
63- const metaId = getMetaId ( meta ) || String ( line ) ;
64- const Child = mdData . components [ `${ metaId } ` ] ;
65- if ( metaId && typeof Child === 'function' ) {
66- const code = mdData . data [ metaId ] . value || '' ;
67- const param = getURLParameters ( metaData ) ;
68- return (
69- < CodeLayout
70- disableCheckered = { getBooleanValue ( param , 'disableCheckered' , true ) }
71- disableToolbar = { getBooleanValue ( param , 'disableToolbar' , false ) }
72- disableCode = { getBooleanValue ( param , 'disableCode' , false ) }
73- disablePreview = { getBooleanValue ( param , 'disablePreview' , false ) }
74- bordered = { getBooleanValue ( param , 'bordered' , true ) }
75- copied = { getBooleanValue ( param , 'copied' , true ) }
76- background = { param . background }
77- toolbar = { param . title || '示例' }
78- codeProps = { { style : { padding : 0 } } }
79- style = { { padding : 0 } }
80- code = { < pre { ...props } /> }
81- text = { code }
82- >
57+ code : ( { inline, node, ...props } ) => {
58+ const { 'data-meta' : meta , ...rest } = props ;
59+ if ( inline || ! isMeta ( meta ) ) {
60+ return < code { ...props } /> ;
61+ }
62+ const line = node . position ?. start . line ;
63+ const metaId = getMetaId ( meta ) || String ( line ) ;
64+ const Child = mdData . components [ `${ metaId } ` ] ;
65+ if ( metaId && typeof Child === 'function' ) {
66+ const code = mdData . data [ metaId ] . value || '' ;
67+ const param = getURLParameters ( meta ) ;
68+ return (
69+ < CodeLayout
70+ disableCheckered = { getBooleanValue ( param , 'disableCheckered' , true ) }
71+ bordered = { getBooleanValue ( param , 'bordered' , true ) }
72+ >
73+ < Preview >
8374 < Child />
84- </ CodeLayout >
85- ) ;
86- }
75+ </ Preview >
76+ < Toolbar text = { code } copied = { getBooleanValue ( param , 'copied' , true ) } >
77+ { param . title || '示例' }
78+ </ Toolbar >
79+ < Code >
80+ < code { ...rest } />
81+ </ Code >
82+ </ CodeLayout >
83+ ) ;
8784 }
88- return < div { ...props } /> ;
85+ return < code { ...rest } /> ;
8986 } ,
9087 } }
9188 />
0 commit comments