@@ -19,16 +19,26 @@ const toolbar = (markmapInstance: Markmap) => {
1919 return toolbar . el
2020}
2121
22- const init = ( ) => document . querySelectorAll < HTMLElement > ( '.markmap-wrap' ) . forEach ( ( wrapper ) => {
23- const [ root , jsonOptions ] = Array . from ( wrapper . children , ( el ) => JSON . parse ( el . innerHTML ) )
24- wrapper . innerHTML = '<svg></svg>'
25- const svg = wrapper . querySelector ( 'svg' )
26- const markmapInstance = Markmap . create ( svg , deriveOptions ( jsonOptions ) , root )
27- if ( wrapper ?. dataset ?. toolbar !== 'false' ) {
28- wrapper . append ( toolbar ( markmapInstance ) )
29- }
30- resize . observe ( wrapper , debounce ( { delay : 100 } , ( ) => markmapInstance . fit ( ) ) )
31- } )
22+ const init = ( ) => {
23+ document . querySelectorAll < HTMLElement > ( '.markmap-wrap' ) . forEach ( ( wrapper ) => {
24+ if ( wrapper . children . length < 2 ) return
25+ const [ root , jsonOptions ] = Array . from ( wrapper . children , ( el ) => {
26+ try {
27+ return JSON . parse ( el . innerHTML )
28+ } catch {
29+ console . warn ( 'Failed to parse JSON:' , el . innerHTML )
30+ return null
31+ }
32+ } )
33+ if ( ! root || ! jsonOptions ) return
34+ const svg = document . createElementNS ( 'http://www.w3.org/2000/svg' , 'svg' )
35+ const elements : Element [ ] = [ svg ]
36+ const markmapInstance = Markmap . create ( svg , deriveOptions ( jsonOptions ) , root )
37+ if ( wrapper . dataset ?. toolbar !== 'false' ) elements . push ( toolbar ( markmapInstance ) )
38+ wrapper . replaceChildren ( ...elements )
39+ resize . observe ( wrapper , debounce ( { delay : 100 } , ( ) => markmapInstance . fit ( ) ) )
40+ } )
41+ }
3242
3343window . addEventListener ( 'vscode.markdown.updateContent' , init )
3444
0 commit comments