-
Notifications
You must be signed in to change notification settings - Fork 420
feat: support mermaid charts in markdown renderer #1889
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
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5ac90c0
feat(renderer): add mermaid diagram renderer
jschwxrz 8d433ce
feat(renderer): render mermaid code fences
jschwxrz 6accb5d
fix: review remarks
jschwxrz 85ddbaf
Merge branch 'main' into feat-render-mermaid-charts-7z2y1
jschwxrz 47d85fa
fix: formatting
jschwxrz 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,88 @@ | ||
| import React, { useEffect, useMemo, useState } from 'react'; | ||
| import { cn } from '@renderer/utils/utils'; | ||
| import { createMermaidRenderId, renderMermaidDiagram } from './mermaid-renderer'; | ||
|
|
||
| interface MermaidDiagramProps { | ||
| chart: string; | ||
| isDark: boolean; | ||
| compact?: boolean; | ||
| } | ||
|
|
||
| type RenderState = | ||
| | { kind: 'rendered'; key: string; svg: string } | ||
| | { kind: 'error'; key: string; message: string }; | ||
|
|
||
| function errorMessage(error: unknown): string { | ||
| if (error instanceof Error && error.message) return error.message; | ||
| return 'Unable to render Mermaid diagram.'; | ||
| } | ||
|
|
||
| export const MermaidDiagram: React.FC<MermaidDiagramProps> = ({ chart, isDark, compact }) => { | ||
| const id = useMemo(() => createMermaidRenderId(), []); | ||
| const theme = isDark ? 'dark' : 'default'; | ||
| const renderKey = `${theme}:${chart}`; | ||
| const [state, setState] = useState<RenderState | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| let cancelled = false; | ||
|
|
||
| renderMermaidDiagram({ id, chart, theme }) | ||
| .then((svg) => { | ||
| if (!cancelled) setState({ kind: 'rendered', key: renderKey, svg }); | ||
| }) | ||
| .catch((error: unknown) => { | ||
| if (!cancelled) setState({ kind: 'error', key: renderKey, message: errorMessage(error) }); | ||
| }); | ||
|
|
||
| return () => { | ||
| cancelled = true; | ||
| }; | ||
| }, [chart, id, renderKey, theme]); | ||
|
|
||
| const visibleState = state?.key === renderKey ? state : null; | ||
|
|
||
| if (visibleState?.kind === 'error') { | ||
| return ( | ||
| <div | ||
| className={cn( | ||
| 'my-3 rounded-md border border-destructive/30 bg-destructive/5 p-3 text-xs text-destructive', | ||
| compact && 'my-2 p-2 text-[11px]' | ||
| )} | ||
| role="alert" | ||
| > | ||
| <div className="font-medium">Unable to render Mermaid diagram.</div> | ||
| <div className="mt-1 text-muted-foreground">{visibleState.message}</div> | ||
| <pre className="mt-2 overflow-x-auto rounded bg-muted/60 p-2 text-muted-foreground"> | ||
| <code>{chart}</code> | ||
| </pre> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| if (!visibleState) { | ||
| return ( | ||
| <div | ||
| className={cn( | ||
| 'my-3 rounded-md border border-border bg-muted/20 p-3 text-xs text-muted-foreground', | ||
| compact && 'my-2 p-2 text-[11px]' | ||
| )} | ||
| > | ||
| Rendering diagram... | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <div | ||
| className={cn( | ||
| 'my-3 overflow-x-auto rounded-md border border-border bg-background p-3', | ||
| compact && 'my-2 p-2' | ||
| )} | ||
| > | ||
| <div | ||
| className="min-w-fit text-foreground [&_svg]:h-auto [&_svg]:max-w-full" | ||
| dangerouslySetInnerHTML={{ __html: visibleState.svg }} | ||
| /> | ||
|
jschwxrz marked this conversation as resolved.
|
||
| </div> | ||
| ); | ||
| }; | ||
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,44 @@ | ||
| import type { MermaidConfig } from 'mermaid'; | ||
|
|
||
| let idCounter = 0; | ||
| let renderQueue: Promise<void> = Promise.resolve(); | ||
|
|
||
| type MermaidTheme = NonNullable<MermaidConfig['theme']>; | ||
|
|
||
| interface MermaidRenderRequest { | ||
| id: string; | ||
| chart: string; | ||
| theme: MermaidTheme; | ||
| } | ||
|
|
||
| export function createMermaidRenderId(): string { | ||
| idCounter += 1; | ||
| return `emdash-mermaid-${idCounter}`; | ||
| } | ||
|
|
||
| export async function renderMermaidDiagram({ | ||
| id, | ||
| chart, | ||
| theme, | ||
| }: MermaidRenderRequest): Promise<string> { | ||
| const render = async () => { | ||
| const mermaid = (await import('mermaid')).default; | ||
| const config: MermaidConfig = { | ||
| startOnLoad: false, | ||
| securityLevel: 'strict', | ||
| suppressErrorRendering: true, | ||
| theme, | ||
| }; | ||
|
|
||
| mermaid.initialize(config); | ||
| const { svg } = await mermaid.render(id, chart); | ||
| return svg; | ||
|
jschwxrz marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| const result = renderQueue.then(render, render); | ||
| renderQueue = result.then( | ||
| () => undefined, | ||
| () => undefined | ||
| ); | ||
| return result; | ||
| } | ||
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.