-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
chore: upgrade Mermaid to the latest stable version #5681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
asyncapi-bot
merged 8 commits into
asyncapi:master
from
princerajpoot20:fix-vulnerabilities
Aug 3, 2026
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3a0e804
upgrade mermaid
princerajpoot20 dcaec53
coderabbit suggestion
princerajpoot20 471dec4
changes
princerajpoot20 6f04383
minor change
princerajpoot20 2647a38
minor change
princerajpoot20 4f6680e
fix deno module issue
princerajpoot20 6505cf9
minor fix
princerajpoot20 b908eed
minor fix
princerajpoot20 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import { useEffect, useId, useState } from 'react'; | ||
|
|
||
| type MermaidTheme = 'light' | 'dark'; | ||
|
|
||
| const MERMAID_THEME_VARIABLES: Record<MermaidTheme, Record<string, string>> = { | ||
| light: { | ||
| primaryColor: '#EDFAFF', | ||
| primaryBorderColor: '#47BCEE', | ||
| secondaryColor: '#F4EFFC', | ||
| secondaryBorderColor: '#875AE2', | ||
| fontFamily: 'Inter, sans-serif', | ||
| fontSize: '18px', | ||
| primaryTextColor: '#242929', | ||
| tertiaryColor: '#F7F9FA', | ||
| tertiaryBorderColor: '#BFC6C7', | ||
| lineColor: '#BFC6C7', | ||
| mainBkg: '#EDFAFF', | ||
| secondBkg: '#F4EFFC', | ||
| tertiaryBkg: '#F7F9FA', | ||
| clusterBkg: '#F7F9FA', | ||
| clusterBorder: '#BFC6C7', | ||
| edgeLabelBackground: '#FFFFFF' | ||
| }, | ||
| dark: { | ||
| primaryColor: '#1E293B', | ||
| primaryBorderColor: '#38BDF8', | ||
| secondaryColor: '#2E2459', | ||
| secondaryBorderColor: '#A87EFC', | ||
| fontFamily: 'Inter, sans-serif', | ||
| fontSize: '18px', | ||
| primaryTextColor: '#F8FAFC', | ||
| tertiaryColor: '#121825', | ||
| tertiaryBorderColor: '#475569', | ||
| lineColor: '#94A3B8', | ||
| mainBkg: '#1E293B', | ||
| secondBkg: '#2E2459', | ||
| tertiaryBkg: '#121825', | ||
| clusterBkg: '#121825', | ||
| clusterBorder: '#475569', | ||
| edgeLabelBackground: '#1E293B' | ||
| } | ||
| }; | ||
|
|
||
| // Cache the theme Mermaid was initialized with across client-side page transitions. | ||
| let initializedMermaidTheme: MermaidTheme | null = null; | ||
|
|
||
| /** | ||
| * @description Returns the Mermaid theme that matches the current website theme. | ||
| */ | ||
| function getMermaidTheme(): MermaidTheme { | ||
| return document.documentElement.classList.contains('dark') ? 'dark' : 'light'; | ||
| } | ||
|
|
||
| interface MermaidDiagramProps { | ||
| graph: string; | ||
| } | ||
|
|
||
| /** | ||
| * @description This component renders Mermaid diagrams. | ||
| * | ||
| * Mermaid is loaded via a dynamic import inside the render effect so it is | ||
| * never included in the initial JS bundle — it is fetched on demand only | ||
| * when a page actually contains a diagram. | ||
| * | ||
| * @param {MermaidDiagramProps} props - The props for the MermaidDiagram component. | ||
| * @param {string} props.graph - The Mermaid graph definition to render. | ||
| */ | ||
| export default function MermaidDiagram({ graph }: Readonly<MermaidDiagramProps>) { | ||
| const [svg, setSvg] = useState<string | null>(null); | ||
| const [theme, setTheme] = useState<MermaidTheme>('light'); | ||
| const reactId = useId(); | ||
| // Produce a stable, DOM-safe ID for the SVG element mermaid generates. | ||
| const diagramId = `mermaid${reactId.replace(/:/g, '')}`; | ||
|
Check warning on line 73 in components/MDX/MermaidDiagram.tsx
|
||
|
|
||
| useEffect(() => { | ||
| setTheme(getMermaidTheme()); | ||
|
|
||
| const observer = new MutationObserver(() => { | ||
| setTheme(getMermaidTheme()); | ||
| }); | ||
|
|
||
| observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] }); | ||
|
|
||
| return () => observer.disconnect(); | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| if (!graph) { | ||
| setSvg(null); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| let mounted = true; | ||
|
|
||
| async function render() { | ||
| try { | ||
| const { default: mermaid } = await import('mermaid'); | ||
|
|
||
| if (initializedMermaidTheme !== theme) { | ||
| initializedMermaidTheme = theme; | ||
| mermaid.initialize({ | ||
| startOnLoad: false, | ||
| theme: 'base', | ||
| securityLevel: 'strict', | ||
| // Keep Mermaid styling fully controlled by MERMAID_THEME_VARIABLES. | ||
| themeVariables: MERMAID_THEME_VARIABLES[theme] | ||
| }); | ||
| } | ||
|
|
||
| const { svg: rendered } = await mermaid.render(diagramId, graph.trim()); | ||
|
|
||
| if (mounted) { | ||
| setSvg(rendered); | ||
| } | ||
| } catch (e) { | ||
| if (mounted) { | ||
| setSvg(null); | ||
| } | ||
| // eslint-disable-next-line no-console | ||
| console.error(e); | ||
| } | ||
| } | ||
|
|
||
| render(); | ||
|
|
||
| return () => { | ||
| mounted = false; | ||
| }; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }, [graph, theme, diagramId]); | ||
|
|
||
| return <div dangerouslySetInnerHTML={{ __html: svg ?? '' }} />; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.