|
5 | 5 | onload=" |
6 | 6 | // Fix Goldmark's em/strong tag conversion in math blocks before rendering |
7 | 7 | // This is a workaround for Hugo < 0.132 which lacks passthrough extension |
| 8 | + // IMPORTANT: Protect Mermaid blocks from KaTeX processing |
| 9 | + // Save Mermaid blocks and replace with placeholders before DOM manipulation |
| 10 | + const mermaidPlaceholders = new Map(); |
| 11 | + document.querySelectorAll('.mermaid, .mermaid[data-mermaid-content]').forEach((el, idx) => { |
| 12 | + const placeholderId = 'mermaid-ph-' + idx; |
| 13 | + const placeholder = document.createElement('div'); |
| 14 | + placeholder.id = placeholderId; |
| 15 | + placeholder.className = 'mermaid-placeholder'; |
| 16 | + placeholder.style.display = 'none'; |
| 17 | + mermaidPlaceholders.set(placeholderId, el.outerHTML); |
| 18 | + if (el.parentNode) { |
| 19 | + el.parentNode.replaceChild(placeholder, el); |
| 20 | + } |
| 21 | + }); |
| 22 | + |
| 23 | + // Now do KaTeX processing on the modified DOM (Mermaid blocks are protected) |
8 | 24 | document.body.innerHTML = document.body.innerHTML |
9 | 25 | // Fix display math blocks ($$...$$) |
10 | 26 | .replace(/(\$\$[\s\S]*?\$\$)/g, function(match) { |
|
22 | 38 | .replace(/<\/?strong>/g, '**') + '$'; |
23 | 39 | }); |
24 | 40 | |
| 41 | + // Restore Mermaid blocks |
| 42 | + mermaidPlaceholders.forEach((html, id) => { |
| 43 | + const placeholder = document.getElementById(id); |
| 44 | + if (placeholder && placeholder.parentNode) { |
| 45 | + placeholder.outerHTML = html; |
| 46 | + } |
| 47 | + }); |
| 48 | + |
25 | 49 | renderMathInElement(document.body, { |
26 | 50 | delimiters: [ |
27 | 51 | {left: '$$', right: '$$', display: true}, |
|
32 | 56 | throwOnError: false, |
33 | 57 | trust: true, |
34 | 58 | strict: false, |
35 | | - // Ignore text in script, noscript, style, textarea, pre, code, option tags |
36 | | - ignoredTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code', 'option'] |
| 59 | + // Ignore text in script, noscript, style, textarea, pre, code, option, and mermaid tags |
| 60 | + ignoredTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code', 'option'], |
| 61 | + ignoredClasses: ['mermaid'] |
37 | 62 | }); |
38 | 63 | "></script> |
39 | 64 |
|
40 | 65 | <!-- Mermaid rendering --> |
41 | 66 | {{ if .Page.Store.Get "hasMermaid" }} |
42 | | - <script src="https://cdn.jsdelivr.net/npm/mermaid@11.4.0/dist/mermaid.min.js"></script> |
| 67 | + <script src="https://cdn.jsdelivr.net/npm/mermaid@11.10.0/dist/mermaid.min.js"></script> |
43 | 68 | <script> |
44 | | - mermaid.initialize({ startOnLoad: true }); |
| 69 | + mermaid.initialize({ |
| 70 | + startOnLoad: true, |
| 71 | + theme: 'default', |
| 72 | + securityLevel: 'loose', |
| 73 | + flowchart: { useMaxWidth: true }, |
| 74 | + classDiagram: { useMaxWidth: true }, |
| 75 | + logLevel: 'error' |
| 76 | + }); |
45 | 77 | </script> |
46 | 78 | {{ end }} |
47 | 79 |
|
|
0 commit comments