@@ -34,9 +34,6 @@ 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 ;
4037 const $dom = useRef ( null ) ;
4138 return (
4239 < Wrapper ref = { $dom } >
@@ -46,41 +43,49 @@ const Preview = ({ path, ...mdData }) => {
4643 // transformImageUri={transformImageUri}
4744 source = { mdData . source }
4845 rehypeRewrite = { ( node , index , parent ) => {
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 = [ ] ;
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' ;
5355 }
5456 }
5557 } }
5658 components = { {
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 >
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+ >
7483 < Child />
75- </ Preview >
76- < Toolbar text = { code } copied = { getBooleanValue ( param , 'copied' , true ) } >
77- { param . title || '示例' }
78- </ Toolbar >
79- < Code > { code } </ Code >
80- </ CodeLayout >
81- ) ;
84+ </ CodeLayout >
85+ ) ;
86+ }
8287 }
83- return < code { ...rest } /> ;
88+ return < div { ...props } /> ;
8489 } ,
8590 } }
8691 />
0 commit comments