Mermaid Diagrams Setup for GitHub Pages
Problem
The jekyll-mermaid
plugin is NOT supported by GitHub Pages. GitHub Pages only supports a limited set of whitelisted Jekyll plugins, and jekyll-mermaid
is not one of them.
Solution
We use a client-side JavaScript approach to render Mermaid diagrams. This works perfectly with GitHub Pages since it doesn’t require any server-side plugins.
How It Works
- JavaScript Library: We load Mermaid.js from CDN in
_includes/head.html
- Automatic Processing: JavaScript finds code blocks with
language-mermaid
class and converts them to Mermaid diagrams - Styling: Custom CSS ensures diagrams display properly
Usage
In Your Posts
Use standard markdown code blocks with mermaid
language identifier:
```mermaid
graph TD
A[Start] --> B[Process]
B --> C[End]
```
Layout Options
- Regular posts: Use
layout: post
(default) - Posts with enhanced Mermaid support: Use
layout: mermaid-post
Supported Diagram Types
- Flowcharts
- Sequence diagrams
- Class diagrams
- State diagrams
- Gantt charts
- Pie charts
- Git graphs
- User journey diagrams
Configuration
Mermaid Settings
The Mermaid configuration is in _includes/head.html
:
mermaid.initialize({
startOnLoad: true,
theme: 'default',
themeVariables: {
primaryColor: '#ff6b6b',
primaryTextColor: '#333',
primaryBorderColor: '#ff6b6b',
lineColor: '#333',
secondaryColor: '#4ecdc4',
tertiaryColor: '#ffe66d'
}
});
Customizing Themes
You can change the theme by modifying the theme
property:
'default'
'dark'
'forest'
'neutral'
Troubleshooting
Diagrams Not Rendering
- Check syntax: Ensure your Mermaid syntax is correct
- Clear cache: Hard refresh your browser (Ctrl+F5)
- Check console: Look for JavaScript errors in browser console
Performance Issues
- Large diagrams: Consider breaking complex diagrams into smaller ones
- Multiple diagrams: The page might take longer to load with many diagrams
Testing
Use the test post at _posts/2024-07-15-mermaid-test.md
to verify everything is working correctly.
Best Practices
- Keep diagrams simple: Complex diagrams may not render well on mobile
- Use descriptive labels: Make diagrams self-explanatory
- Test locally: Use Jekyll serve to test before pushing to GitHub
- Responsive design: Diagrams automatically scale on mobile devices
Files Modified
_config.yml
: Removed unsupported plugin references_includes/head.html
: Added Mermaid JavaScript and initializationstyle.scss
: Added Mermaid-specific CSS styling_layouts/mermaid-post.html
: Created enhanced layout for Mermaid posts
Alternative Solutions
If this approach doesn’t work for your needs, consider:
- GitHub Actions: Use custom build process with GitHub Actions
- Pre-rendered images: Convert diagrams to images before committing
- External hosting: Host Jekyll site elsewhere (Netlify, Vercel, etc.)