1+ // Mermaid configuration for faster loading and better rendering
2+ document . addEventListener ( "DOMContentLoaded" , function ( ) {
3+ // Configure Mermaid to prevent FOUC (Flash of Unstyled Content)
4+ if ( typeof mermaid !== 'undefined' ) {
5+ mermaid . initialize ( {
6+ startOnLoad : false ,
7+ theme : 'default' ,
8+ themeVariables : {
9+ primaryColor : '#2196F3' ,
10+ primaryTextColor : '#fff' ,
11+ primaryBorderColor : '#1976D2' ,
12+ lineColor : '#757575' ,
13+ secondaryColor : '#E3F2FD' ,
14+ tertiaryColor : '#f5f5f5'
15+ } ,
16+ flowchart : {
17+ useMaxWidth : true ,
18+ htmlLabels : true ,
19+ curve : 'basis'
20+ } ,
21+ sequence : {
22+ diagramMarginX : 50 ,
23+ diagramMarginY : 10 ,
24+ actorMargin : 50 ,
25+ width : 150 ,
26+ height : 65 ,
27+ boxMargin : 10 ,
28+ boxTextMargin : 5 ,
29+ noteMargin : 10 ,
30+ messageMargin : 35 ,
31+ mirrorActors : true ,
32+ bottomMarginAdj : 1 ,
33+ useMaxWidth : true
34+ } ,
35+ gitgraph : {
36+ mainBranchName : 'main' ,
37+ showCommitLabel : true ,
38+ showBranches : true ,
39+ rotateCommitLabel : true
40+ }
41+ } ) ;
42+
43+ // Wait for the page to be fully loaded before rendering
44+ const renderMermaidDiagrams = ( ) => {
45+ const diagrams = document . querySelectorAll ( '.mermaid' ) ;
46+ diagrams . forEach ( ( diagram , index ) => {
47+ if ( ! diagram . hasAttribute ( 'data-processed' ) ) {
48+ // Hide the diagram initially to prevent FOUC
49+ diagram . style . visibility = 'hidden' ;
50+ diagram . style . opacity = '0' ;
51+
52+ // Process the diagram
53+ mermaid . init ( undefined , diagram ) . then ( ( ) => {
54+ // Show the diagram after processing
55+ diagram . style . transition = 'opacity 0.3s ease-in-out' ;
56+ diagram . style . visibility = 'visible' ;
57+ diagram . style . opacity = '1' ;
58+ } ) . catch ( ( error ) => {
59+ console . error ( 'Mermaid rendering error:' , error ) ;
60+ // Show the original text if rendering fails
61+ diagram . style . visibility = 'visible' ;
62+ diagram . style . opacity = '1' ;
63+ } ) ;
64+ }
65+ } ) ;
66+ } ;
67+
68+ // Render diagrams when page loads
69+ renderMermaidDiagrams ( ) ;
70+
71+ // Re-render diagrams when navigating (for SPA-like behavior)
72+ const observer = new MutationObserver ( ( mutations ) => {
73+ mutations . forEach ( ( mutation ) => {
74+ if ( mutation . type === 'childList' && mutation . addedNodes . length > 0 ) {
75+ // Check if new mermaid diagrams were added
76+ const hasMermaid = Array . from ( mutation . addedNodes ) . some ( node =>
77+ node . nodeType === Node . ELEMENT_NODE &&
78+ ( node . classList . contains ( 'mermaid' ) || node . querySelector ( '.mermaid' ) )
79+ ) ;
80+ if ( hasMermaid ) {
81+ setTimeout ( renderMermaidDiagrams , 100 ) ;
82+ }
83+ }
84+ } ) ;
85+ } ) ;
86+
87+ observer . observe ( document . body , {
88+ childList : true ,
89+ subtree : true
90+ } ) ;
91+ }
92+ } ) ;
93+
94+ // Theme switching support for Mermaid
95+ document . addEventListener ( 'DOMContentLoaded' , function ( ) {
96+ const themeToggleButtons = document . querySelectorAll ( '[data-md-color-scheme]' ) ;
97+
98+ themeToggleButtons . forEach ( button => {
99+ button . addEventListener ( 'click' , function ( ) {
100+ setTimeout ( ( ) => {
101+ const isDark = document . body . getAttribute ( 'data-md-color-scheme' ) === 'slate' ;
102+ if ( typeof mermaid !== 'undefined' ) {
103+ mermaid . initialize ( {
104+ theme : isDark ? 'dark' : 'default' ,
105+ themeVariables : isDark ? {
106+ primaryColor : '#BB86FC' ,
107+ primaryTextColor : '#000' ,
108+ primaryBorderColor : '#3700B3' ,
109+ lineColor : '#CF6679' ,
110+ secondaryColor : '#03DAC6' ,
111+ tertiaryColor : '#121212'
112+ } : {
113+ primaryColor : '#2196F3' ,
114+ primaryTextColor : '#fff' ,
115+ primaryBorderColor : '#1976D2' ,
116+ lineColor : '#757575' ,
117+ secondaryColor : '#E3F2FD' ,
118+ tertiaryColor : '#f5f5f5'
119+ }
120+ } ) ;
121+
122+ // Re-render all diagrams with new theme
123+ const diagrams = document . querySelectorAll ( '.mermaid[data-processed="true"]' ) ;
124+ diagrams . forEach ( diagram => {
125+ diagram . removeAttribute ( 'data-processed' ) ;
126+ const originalContent = diagram . getAttribute ( 'data-original' ) || diagram . textContent ;
127+ diagram . innerHTML = originalContent ;
128+ mermaid . init ( undefined , diagram ) ;
129+ } ) ;
130+ }
131+ } , 100 ) ;
132+ } ) ;
133+ } ) ;
134+ } ) ;
0 commit comments