@@ -31,6 +31,29 @@ function extractSource(code) {
3131 return code . textContent . trim ( ) ;
3232}
3333
34+ function assetExistsInHead ( asset , attr ) {
35+ const value = asset . getAttribute ( attr ) ;
36+ if ( ! value ) return false ;
37+
38+ return Array . from ( document . head . querySelectorAll ( asset . tagName . toLowerCase ( ) ) )
39+ . some ( existing => existing . getAttribute ( attr ) === value ) ;
40+ }
41+
42+ function preserveExpressiveCodeAssets ( container ) {
43+ if ( ! container ?. classList ?. contains ( 'expressive-code' ) ) return ;
44+
45+ const assets = container . querySelectorAll ( ':scope > link[rel="stylesheet"], :scope > script[type="module"]' ) ;
46+ for ( const asset of assets ) {
47+ const attr = asset . tagName === 'LINK' ? 'href' : 'src' ;
48+ if ( assetExistsInHead ( asset , attr ) ) {
49+ asset . remove ( ) ;
50+ continue ;
51+ }
52+
53+ document . head . appendChild ( asset ) ;
54+ }
55+ }
56+
3457async function renderMermaid ( ) {
3558 const codeBlocks = document . querySelectorAll ( 'pre[data-language="mermaid"] code' ) ;
3659 if ( ! codeBlocks . length ) return ;
@@ -41,6 +64,7 @@ async function renderMermaid() {
4164 const src = extractSource ( code ) ;
4265 // Replace the whole expressive-code wrapper, falling back to the <pre>
4366 const container = code . closest ( '.expressive-code' ) || code . closest ( 'pre' ) ;
67+ preserveExpressiveCodeAssets ( container ) ;
4468 const wrapper = document . createElement ( 'div' ) ;
4569 wrapper . dataset . mermaidSrc = src ;
4670 wrapper . className = 'mermaid-diagram not-content' ;
0 commit comments