Skip to content

Commit 42acfb6

Browse files
authored
Merge pull request #75 from openSVM/copilot/fix-74
Add Mermaid diagram rendering support to documentation system
2 parents 8ffacaf + 2766dde commit 42acfb6

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

docs.html

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +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 -->
12+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mermaid.min.js"></script>
1113
<script>
1214
// Fallback if marked.js fails to load
1315
if (typeof marked === 'undefined') {
@@ -260,6 +262,16 @@
260262
font-size: 1.1rem;
261263
}
262264

265+
/* Mermaid diagram styling */
266+
.mermaid {
267+
text-align: center;
268+
margin: 20px 0;
269+
background: white;
270+
border-radius: 8px;
271+
padding: 20px;
272+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
273+
}
274+
263275
.breadcrumb {
264276
background: #f8f9fa;
265277
padding: 15px 40px;
@@ -393,6 +405,80 @@
393405
document.title = `${title} - OSVM CLI Documentation`;
394406
}
395407

408+
// Process mermaid diagrams in the loaded content
409+
function processMermaidDiagrams() {
410+
// Find all code blocks that contain mermaid
411+
const codeBlocks = document.querySelectorAll('pre code');
412+
413+
codeBlocks.forEach((codeBlock, index) => {
414+
let text = codeBlock.textContent.trim();
415+
const parentPre = codeBlock.parentElement;
416+
417+
// Check if this is a mermaid diagram - look for mermaid language or text starting with flowchart/graph/sequenceDiagram etc.
418+
const isMermaidLang = codeBlock.className.includes('language-mermaid') ||
419+
parentPre.querySelector('code[class*="language-mermaid"]');
420+
const isMermaidContent = text.startsWith('flowchart') ||
421+
text.startsWith('graph') ||
422+
text.startsWith('sequenceDiagram') ||
423+
text.startsWith('classDiagram') ||
424+
text.startsWith('stateDiagram') ||
425+
text.startsWith('pie') ||
426+
text.startsWith('gantt') ||
427+
text.startsWith('journey') ||
428+
text.startsWith('gitgraph') ||
429+
text.includes('subgraph');
430+
431+
if (isMermaidLang || isMermaidContent) {
432+
// Clean up the text - remove "mermaid" prefix if it exists
433+
if (text.startsWith('mermaid\n') || text.startsWith('mermaid\r\n')) {
434+
text = text.substring(text.indexOf('\n') + 1).trim();
435+
} else if (text.startsWith('mermaidflowchart')) {
436+
text = text.substring(7); // Remove "mermaid" prefix
437+
}
438+
439+
// Create a new div for the mermaid diagram
440+
const mermaidDiv = document.createElement('div');
441+
mermaidDiv.className = 'mermaid';
442+
mermaidDiv.setAttribute('id', `mermaid-${index}`);
443+
// Preserve line breaks and whitespace properly
444+
mermaidDiv.style.whiteSpace = 'pre';
445+
mermaidDiv.textContent = text;
446+
447+
// Replace the code block with the mermaid div
448+
parentPre.parentElement.replaceChild(mermaidDiv, parentPre);
449+
}
450+
});
451+
452+
// Initialize mermaid if diagrams were found and library is available
453+
const mermaidDivs = document.querySelectorAll('.mermaid');
454+
if (mermaidDivs.length > 0) {
455+
// Initialize mermaid when library is available
456+
const initMermaid = () => {
457+
if (typeof mermaid !== 'undefined') {
458+
try {
459+
// Configure mermaid
460+
mermaid.initialize({
461+
startOnLoad: true,
462+
theme: 'default',
463+
securityLevel: 'loose'
464+
});
465+
} catch (error) {
466+
console.error('Error initializing mermaid:', error);
467+
}
468+
} else {
469+
console.warn('Mermaid library not available');
470+
// Show fallback content
471+
mermaidDivs.forEach(div => {
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>';
473+
});
474+
}
475+
};
476+
477+
// Try to initialize after a delay to ensure library is loaded
478+
setTimeout(initMermaid, 500);
479+
}
480+
}
481+
396482
// Load and render markdown content
397483
async function loadDocument(docName) {
398484
const contentElement = document.getElementById('markdown-content');
@@ -430,6 +516,9 @@
430516

431517
contentElement.innerHTML = processedHtml;
432518

519+
// Process mermaid diagrams
520+
processMermaidDiagrams();
521+
433522
// Scroll to top
434523
window.scrollTo(0, 0);
435524

0 commit comments

Comments
 (0)