-
-
Notifications
You must be signed in to change notification settings - Fork 335
fix mermaid diagrams #855
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
Merged
fix mermaid diagrams #855
Changes from all commits
Commits
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
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,131 @@ | ||
| 'use client' | ||
|
|
||
| import * as React from 'react' | ||
| import { twMerge } from 'tailwind-merge' | ||
| import { CodeBlockView } from './CodeBlockView' | ||
| import { buildPlainCodeBlockHtml } from './codeBlock.shared' | ||
|
|
||
| type MermaidRenderState = | ||
| | { | ||
| status: 'loading' | ||
| svg?: undefined | ||
| } | ||
| | { | ||
| status: 'rendered' | ||
| svg: string | ||
| } | ||
| | { | ||
| status: 'error' | ||
| svg?: undefined | ||
| } | ||
|
|
||
| function getIsDarkMode() { | ||
| return document.documentElement.classList.contains('dark') | ||
| } | ||
|
|
||
| function useIsDarkMode() { | ||
| const [isDark, setIsDark] = React.useState(false) | ||
|
|
||
| React.useEffect(() => { | ||
| const updateIsDark = () => setIsDark(getIsDarkMode()) | ||
| updateIsDark() | ||
|
|
||
| const observer = new MutationObserver(updateIsDark) | ||
| observer.observe(document.documentElement, { | ||
| attributeFilter: ['class'], | ||
| attributes: true, | ||
| }) | ||
|
|
||
| return () => observer.disconnect() | ||
| }, []) | ||
|
|
||
| return isDark | ||
| } | ||
|
|
||
| export function MermaidBlock({ | ||
| className, | ||
| code, | ||
| isEmbedded, | ||
| showTypeCopyButton, | ||
| style, | ||
| title, | ||
| }: { | ||
| className?: string | ||
| code: string | ||
| isEmbedded?: boolean | ||
| showTypeCopyButton?: boolean | ||
| style?: React.CSSProperties | ||
| title?: string | ||
| }) { | ||
| const isDark = useIsDarkMode() | ||
| const reactId = React.useId() | ||
| const mermaidId = React.useMemo( | ||
| () => `mermaid-${reactId.replace(/[^a-zA-Z0-9_-]/g, '')}`, | ||
| [reactId], | ||
| ) | ||
| const [renderState, setRenderState] = React.useState<MermaidRenderState>({ | ||
| status: 'loading', | ||
| }) | ||
|
|
||
| React.useEffect(() => { | ||
| let cancelled = false | ||
|
|
||
| async function renderMermaid() { | ||
| setRenderState({ status: 'loading' }) | ||
|
|
||
| try { | ||
| const mermaid = (await import('mermaid')).default | ||
|
|
||
| mermaid.initialize({ | ||
| securityLevel: 'strict', | ||
| startOnLoad: false, | ||
| theme: isDark ? 'dark' : 'default', | ||
| }) | ||
|
|
||
| const { svg } = await mermaid.render(mermaidId, code) | ||
|
|
||
| if (!cancelled) { | ||
| setRenderState({ status: 'rendered', svg }) | ||
| } | ||
| } catch { | ||
| if (!cancelled) { | ||
| setRenderState({ status: 'error' }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void renderMermaid() | ||
|
|
||
| return () => { | ||
| cancelled = true | ||
| } | ||
| }, [code, isDark, mermaidId]) | ||
|
|
||
| if (renderState.status !== 'rendered') { | ||
| return ( | ||
| <CodeBlockView | ||
| className={className} | ||
| copyText={code.trimEnd()} | ||
| htmlMarkup={buildPlainCodeBlockHtml(code)} | ||
| isEmbedded={isEmbedded} | ||
| lang="mermaid" | ||
| showTypeCopyButton={showTypeCopyButton} | ||
| style={style} | ||
| title={title} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| return ( | ||
| <div | ||
| className={twMerge( | ||
| 'mermaid-block not-prose w-full max-w-full overflow-x-auto rounded-md border border-gray-500/20 bg-white p-4 dark:bg-gray-950 [&_svg]:mx-auto [&_svg]:max-w-none', | ||
| className, | ||
| )} | ||
| style={style} | ||
| role="img" | ||
| aria-label={title ?? 'Mermaid diagram'} | ||
| dangerouslySetInnerHTML={{ __html: renderState.svg }} | ||
| /> | ||
| ) | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error state is indistinguishable from the loading state and silently hides render failures.
On
catch,renderStatebecomes'error'but the UI renders the exact sameCodeBlockViewwith the raw mermaid source as the'loading'state does. A malformed diagram will therefore appear as a normal code block with no feedback that rendering failed — both to the user and to anyone debugging. At minimum, log the caught error (mermaid throws informative parse errors) and render a distinct error surface so authors can tell the difference.🛠️ Suggested change
And branch the render so the error path is visible (e.g., render
CodeBlockViewwith an addedtitlelike`${title ?? ''} (mermaid render failed)`or a small inline error message above the fallback).🤖 Prompt for AI Agents