Skip to content

Commit 6b79fef

Browse files
Copilotlarp0
andcommitted
Complete mermaid.js integration with reliable fallback handling
Co-authored-by: larp0 <[email protected]>
1 parent f575fce commit 6b79fef

File tree

4 files changed

+35
-1660
lines changed

4 files changed

+35
-1660
lines changed

docs.html

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
<!-- Marked.js for markdown parsing - try multiple CDNs for reliability -->
1010
<script src="https://unpkg.com/marked/marked.min.js"></script>
11-
<!-- Mermaid.js for diagram rendering - local fallback -->
12-
<script src="mermaid.min.js"></script>
11+
<!-- Mermaid.js for diagram rendering -->
12+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mermaid.min.js"></script>
1313
<script>
1414
// Fallback if marked.js fails to load
1515
if (typeof marked === 'undefined') {
@@ -440,6 +440,8 @@
440440
const mermaidDiv = document.createElement('div');
441441
mermaidDiv.className = 'mermaid';
442442
mermaidDiv.setAttribute('id', `mermaid-${index}`);
443+
// Preserve line breaks and whitespace properly
444+
mermaidDiv.style.whiteSpace = 'pre';
443445
mermaidDiv.textContent = text;
444446

445447
// Replace the code block with the mermaid div
@@ -450,31 +452,30 @@
450452
// Initialize mermaid if diagrams were found and library is available
451453
const mermaidDivs = document.querySelectorAll('.mermaid');
452454
if (mermaidDivs.length > 0) {
453-
// Simple approach - wait a bit for the library to load, then try once
454-
setTimeout(() => {
455+
// Initialize mermaid when library is available
456+
const initMermaid = () => {
455457
if (typeof mermaid !== 'undefined') {
456458
try {
457459
// Configure mermaid
458460
mermaid.initialize({
459-
startOnLoad: false,
461+
startOnLoad: true,
460462
theme: 'default',
461-
securityLevel: 'loose',
462-
fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Open Sans", "Helvetica Neue", sans-serif'
463+
securityLevel: 'loose'
463464
});
464-
465-
// Render all mermaid diagrams
466-
mermaid.run();
467465
} catch (error) {
468-
console.error('Error rendering mermaid diagrams:', error);
466+
console.error('Error initializing mermaid:', error);
469467
}
470468
} else {
471469
console.warn('Mermaid library not available');
472-
// If mermaid is not available, show a message in the mermaid divs
470+
// Show fallback content
473471
mermaidDivs.forEach(div => {
474-
div.innerHTML = '<p><em>Mermaid diagram (library not loaded)</em></p><pre><code>' + div.textContent + '</code></pre>';
472+
div.innerHTML = '<p><em>Mermaid diagram (requires mermaid.js)</em></p><pre style="background:#f4f4f4;padding:10px;border-radius:4px;"><code>' + div.textContent + '</code></pre>';
475473
});
476474
}
477-
}, 1000);
475+
};
476+
477+
// Try to initialize after a delay to ensure library is loaded
478+
setTimeout(initMermaid, 500);
478479
}
479480
}
480481

mermaid.min.js

Lines changed: 0 additions & 1646 deletions
This file was deleted.

test_mermaid.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<div class="mermaid">flowchart TD\n A --> B</div>
2+
<script src="mermaid.min.js"></script><script>mermaid.initialize({startOnLoad:true})</script>

test_mermaid2.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script src="mermaid.min.js"></script>
5+
</head>
6+
<body>
7+
<div class="mermaid">
8+
flowchart TD
9+
A --> B
10+
</div>
11+
<script>
12+
mermaid.initialize({
13+
startOnLoad: true,
14+
theme: 'default'
15+
});
16+
</script>
17+
</body>
18+
</html>

0 commit comments

Comments
 (0)