Skip to content

Revert "chore: replace mermaid with beautiful-mermaid"#43

Open
rsbh wants to merge 1 commit intomainfrom
revert-42-feat_use_beautiful_mermaid
Open

Revert "chore: replace mermaid with beautiful-mermaid"#43
rsbh wants to merge 1 commit intomainfrom
revert-42-feat_use_beautiful_mermaid

Conversation

@rsbh
Copy link
Copy Markdown
Member

@rsbh rsbh commented Apr 28, 2026

Reverts #42

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 28, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
chronicle Ready Ready Preview, Comment Apr 28, 2026 10:58am

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 28, 2026

📝 Walkthrough

Summary by CodeRabbit

  • Dependencies

    • Updated the diagram rendering library dependency to the latest version.
  • Refactor

    • Enhanced diagram rendering by adopting client-side processing for improved performance.

Walkthrough

The pull request updates the diagram rendering library from beautiful-mermaid to mermaid at version ^11.13.0, replacing synchronous SVG generation with client-side asynchronous rendering via a 'use client' directive and React effects.

Changes

Cohort / File(s) Summary
Dependency Update
packages/chronicle/package.json
Removed beautiful-mermaid and added mermaid ^11.13.0 as a dependency.
Mermaid Component Refactor
packages/chronicle/src/components/mdx/mermaid.tsx
Converted from synchronous rendering with useMemo and error display to client-side async rendering. Added 'use client' directive, dynamic mermaid import within useEffect, initialization with { startOnLoad: false, theme: 'default' }, state-based SVG storage, and removed error handling UI.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~15 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: reverting a previous commit that replaced mermaid with beautiful-mermaid, which aligns with the changeset modifications.
Description check ✅ Passed The description is directly related to the changeset, clearly stating it reverts #42, which matches the PR objectives.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch revert-42-feat_use_beautiful_mermaid

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/chronicle/src/components/mdx/mermaid.tsx (1)

19-19: Make Mermaid security settings explicit as a best practice.

While Mermaid v11.13.0 defaults to securityLevel: 'strict' (which is safe for dangerouslySetInnerHTML usage), explicitly setting this at Line 19 clarifies intent and guards against unexpected future version changes.

Suggested configuration
-      mermaid.initialize({ startOnLoad: false, theme: 'default' })
+      mermaid.initialize({
+        startOnLoad: false,
+        theme: 'default',
+        securityLevel: 'strict',
+      })
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/chronicle/src/components/mdx/mermaid.tsx` at line 19, The
mermaid.initialize call should explicitly set securityLevel to 'strict' to
harden against future defaults changing; update the mermaid.initialize
invocation (the mermaid.initialize({ startOnLoad: false, theme: 'default' })
call) to include securityLevel: 'strict' in its options so the component always
runs with strict security for any dangerouslySetInnerHTML usage.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/chronicle/src/components/mdx/mermaid.tsx`:
- Around line 14-29: The useEffect's inner async render function calls
mermaid.render without error handling, causing silent failures; wrap the
import/mermaid.initialize and mermaid.render calls inside a try/catch in the
render function (checking cancelled before updating) and on error call setSvg
with a sensible fallback (e.g., an error/SVG fallback or a user-visible message)
and log the error (console.error or process logger) so invalid diagram syntax or
other failures don't leave the component blank; keep references to mermaidId,
chart, cancelled and only update state via setSvg when not cancelled.

---

Nitpick comments:
In `@packages/chronicle/src/components/mdx/mermaid.tsx`:
- Line 19: The mermaid.initialize call should explicitly set securityLevel to
'strict' to harden against future defaults changing; update the
mermaid.initialize invocation (the mermaid.initialize({ startOnLoad: false,
theme: 'default' }) call) to include securityLevel: 'strict' in its options so
the component always runs with strict security for any dangerouslySetInnerHTML
usage.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: dc898b24-329d-4654-b76a-a90572a4daaa

📥 Commits

Reviewing files that changed from the base of the PR and between 6fce8f2 and cd0ce77.

⛔ Files ignored due to path filters (1)
  • bun.lock is excluded by !**/*.lock
📒 Files selected for processing (2)
  • packages/chronicle/package.json
  • packages/chronicle/src/components/mdx/mermaid.tsx

Comment on lines +14 to 29
useEffect(() => {
let cancelled = false

async function render() {
const { default: mermaid } = await import('mermaid')
mermaid.initialize({ startOnLoad: false, theme: 'default' })
const { svg: rendered } = await mermaid.render(
mermaidId,
chart
)
if (!cancelled) setSvg(rendered)
}

render()
return () => { cancelled = true }
}, [chart])
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🌐 Web query:

For Mermaid version 11.13.0, does mermaid.render reject/throw on invalid diagram syntax, and what is the recommended React useEffect error-handling pattern?

💡 Result:

For Mermaid 11.13.0: yes—mermaid.render rejects/throws on invalid diagram syntax. If you call the async variant (the recommended API in v11), invalid Mermaid text is surfaced as a rejected promise you should catch (mermaid’s “render failed” handling is documented, and the docs explicitly show wrapping mermaid.render in try/catch). Also, multiple Mermaid issues describe “Syntax error in text” occurring for invalid diagram text when using the render API, consistent with render failing rather than silently succeeding. Recommended React useEffect error-handling pattern: - Don’t rely on the synchronous call/“callback style” to capture errors. - Use an async effect (or define an async function inside the effect) and wrap the await in try/catch. - Track a local “cancelled” flag in the effect cleanup so you don’t call setState after unmount. - Optionally handle parseability separately with mermaid.parse before calling render, or rely on the promise rejection from render. Example pattern: useEffect( => { let cancelled = false; const run = async => { try { const { svg } = await mermaid.render('diagram-id', diagramText); if (!cancelled) setSvg(svg); } catch (err) { if (!cancelled) setError(err instanceof Error ? err.message : String(err)); } }; run; return => { cancelled = true; }; }, [diagramText]); This matches Mermaid’s documented approach to handling errors from render (try/catch around await) and the general React pattern of preventing state updates after unmount.

Citations:


Handle async Mermaid failures to avoid silent blank output.

At Line 27, render() is called without error handling. Mermaid v11.13.0's render() method throws on invalid diagram syntax and other failures. Without a try/catch block, the component fails silently with no fallback state.

Proposed fix
   useEffect(() => {
     let cancelled = false

     async function render() {
-      const { default: mermaid } = await import('mermaid')
-      mermaid.initialize({ startOnLoad: false, theme: 'default' })
-      const { svg: rendered } = await mermaid.render(
-        mermaidId,
-        chart
-      )
-      if (!cancelled) setSvg(rendered)
+      try {
+        const { default: mermaid } = await import('mermaid')
+        mermaid.initialize({ startOnLoad: false, theme: 'default' })
+        const { svg: rendered } = await mermaid.render(mermaidId, chart)
+        if (!cancelled) setSvg(rendered)
+      } catch {
+        if (!cancelled) setSvg('')
+      }
     }

-    render()
+    void render()
     return () => { cancelled = true }
   }, [chart])
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
useEffect(() => {
let cancelled = false
async function render() {
const { default: mermaid } = await import('mermaid')
mermaid.initialize({ startOnLoad: false, theme: 'default' })
const { svg: rendered } = await mermaid.render(
mermaidId,
chart
)
if (!cancelled) setSvg(rendered)
}
render()
return () => { cancelled = true }
}, [chart])
useEffect(() => {
let cancelled = false
async function render() {
try {
const { default: mermaid } = await import('mermaid')
mermaid.initialize({ startOnLoad: false, theme: 'default' })
const { svg: rendered } = await mermaid.render(mermaidId, chart)
if (!cancelled) setSvg(rendered)
} catch {
if (!cancelled) setSvg('')
}
}
void render()
return () => { cancelled = true }
}, [chart])
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/chronicle/src/components/mdx/mermaid.tsx` around lines 14 - 29, The
useEffect's inner async render function calls mermaid.render without error
handling, causing silent failures; wrap the import/mermaid.initialize and
mermaid.render calls inside a try/catch in the render function (checking
cancelled before updating) and on error call setSvg with a sensible fallback
(e.g., an error/SVG fallback or a user-visible message) and log the error
(console.error or process logger) so invalid diagram syntax or other failures
don't leave the component blank; keep references to mermaidId, chart, cancelled
and only update state via setSvg when not cancelled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants