Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/social-bugs-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"streamdown": patch
---

fix multiple renders of the same mermaid diagram
2 changes: 1 addition & 1 deletion apps/website/app/components/mermaid.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Section } from './section';

export const MermaidDemo = () => {
const mermaidExample = `Interactive diagram rendering with manual control. Click the chart icon next to any Mermaid code block to render it!
const mermaidExample = `Interactive diagram rendering with manual control. Click the copy icon next to any Mermaid diagram to copy the code to your clipboard.

## Simple Flowchart

Expand Down
4 changes: 2 additions & 2 deletions packages/streamdown/lib/mermaid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ export const Mermaid = ({ chart, className }: MermaidProps) => {
// Initialize mermaid only once globally
const mermaid = await initializeMermaid();

// Use a stable ID based on chart content hash to prevent re-renders
// Use a stable ID based on chart content hash and timestamp to ensure uniqueness
const chartHash = chart.split('').reduce((acc, char) => {
// biome-ignore lint/suspicious/noBitwiseOperators: "Required for Mermaid"
return ((acc << 5) - acc + char.charCodeAt(0)) | 0;
}, 0);
const uniqueId = `mermaid-${Math.abs(chartHash)}`;
const uniqueId = `mermaid-${Math.abs(chartHash)}-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;

const { svg } = await mermaid.render(uniqueId, chart);

Expand Down