warp icon indicating copy to clipboard operation
warp copied to clipboard

fix(mermaid): strip Mermaid frontmatter before rendering (#10676)

Open maxmilian opened this issue 6 months ago • 11 comments

Summary

Fixes #10676. Mermaid diagrams that start with a YAML frontmatter / config block (a --- ... --- block at the very top, supported by Mermaid 11+) never render in Warp — the UI stays on "Rendering Mermaid diagram" indefinitely on older builds, and shows the generic failure callout on newer ones. Issue's exact sample:

---
config:
  theme: default
---
xychart-beta
  title "Évolution des prix des appartements à Versailles"
  ...

Visual evidence

Built WarpOss from this branch, opened a Personal Notebook, and pasted the issue's mermaid-with-frontmatter source. Same notebook, same markdown — just toggling the Mermaid block's view mode.

warp-pr11249-ui-comparison
  • Raw view (left) — ---\nconfig:\n theme: default\n--- frontmatter is preserved in the source (this is what was breaking master).
  • Diagram view (right) — strip_mermaid_frontmatter strips the leading block before mermaid_to_svg, the flowchart renders.

Pipeline-level proof using the same mermaid_to_svg rev pinned in this repo:

warp-pr11249-pipeline-proof
  • Before — raw source with frontmatter → Parse error at line 1: Expected 'graph' or 'flowchart' declaration.
  • After — stripped source → SVG rendered.

Root cause

The pinned mermaid_to_svg renderer's first_diagram_type_token treats the first non-empty, non-%%-prefixed line as the diagram token. With frontmatter present that token is --- rather than xychart-beta / flowchart / etc., so the renderer fails to dispatch to the right parser and never produces a usable SVG.

Both call sites — crates/editor/src/content/mermaid_diagram.rs:39 (mermaid_asset_source, used by markdown notebook layout) and app/src/notebooks/editor/model.rs:157 (render_mermaid_clipboard_html, the clipboard SVG embed) — pass the raw source through unmodified.

Fix

Introduce strip_mermaid_frontmatter in crates/editor/src/content/mermaid_diagram.rs (returns Cow<'_, str> — Borrowed when no frontmatter, Owned otherwise) and apply it at both call sites. The asset cache key is derived from the post-strip source, so the same logical diagram with and without frontmatter shares a cache entry.

Strip is lossless: Warp passes a hardcoded MermaidTheme::light() to render_mermaid_to_svg and does not consult any of the frontmatter config keys, so nothing about today's rendered output changes when the leading --- block is removed.

Line trimming before delimiter comparison intentionally mirrors mermaid_to_svg's own first_diagram_type_token behavior — being stricter (e.g. requiring --- at column 0) would leave a class of broken sources still broken on the renderer side.

Tests

13 new unit tests in crates/editor/src/content/mermaid_diagram_tests.rs:

  • strip_frontmatter_removes_leading_config_block — the issue's exact xychart sample.
  • strip_frontmatter_preserves_source_without_frontmatter — no-op path returns Cow::Borrowed (no spurious allocation).
  • strip_frontmatter_skips_leading_blank_lines_before_open_delimiter — blanks/whitespace before --- are not load-bearing.
  • strip_frontmatter_handles_crlf_line_endings — Windows line endings.
  • strip_frontmatter_leaves_text_starting_with_three_dashes_then_content — --- title-like lines are not delimiters.
  • strip_frontmatter_leaves_unterminated_block_for_renderer_to_surface — missing close --- → leave intact so the renderer's own error surfaces.
  • strip_frontmatter_handles_empty_input — empty string.
  • strip_frontmatter_handles_only_frontmatter_no_body — frontmatter with no body.
  • strip_frontmatter_handles_frontmatter_without_trailing_newline — closing --- on final line.
  • strip_frontmatter_handles_open_delimiter_only_with_newline — ---\n alone.
  • strip_frontmatter_handles_open_delimiter_only_without_newline — --- alone (no trailing newline).
  • strip_frontmatter_treats_indented_dashes_as_delimiter — documented intentional choice that mirrors the renderer's line-trim behavior.
  • strip_frontmatter_does_not_treat_inner_dashes_line_as_delimiter — --- lines mid-diagram are not delimiters.
  • mermaid_asset_source_hashes_post_strip_for_cache_key_stability — same cache key with/without frontmatter.

All 16 module tests pass locally (cargo test -p warp_editor --lib content::mermaid_diagram::). cargo fmt --check, cargo clippy -p warp_editor --lib, cargo clippy -p warp --lib are clean.

Scope

Deliberately not changed:

  • mermaid_to_svg (the warpdotdev fork) is not patched. Fixing the renderer to recognize frontmatter would be more general but requires a separate PR to that repo plus a dep bump. The Warp-side strip is a single PR that fully solves the user-visible bug with minimal blast radius.
  • Frontmatter theme / config.* keys remain ignored. Honoring them would mean parsing YAML and overriding the hardcoded light theme — a feature, not a fix. The current behavior matches what shipped before frontmatter was attempted at all.

Follow-up

  • Patch the mermaid_to_svg fork to recognize --- frontmatter natively, then remove this Warp-side helper. Would also fix the bug for any other consumer of that crate.
  • Honor frontmatter theme: dark / theme: forest etc. by parsing the YAML and passing the appropriate MermaidTheme instead of hardcoded light().
  • The MermaidTheme::light() hardcode itself is worth revisiting — it doesn't honor Warp's app theme.

maxmilian avatar May 19 '26 00:05 maxmilian

@maxmilian

I'm starting a first review of this pull request.

You can view the conversation on Warp.

I completed the review and no human review was requested for this pull request.

Comment /oz-review on this pull request to retrigger a review (up to 3 times on the same pull request).

Powered by Oz

warp-for-oss[bot] avatar May 19 '26 00:05 warp-for-oss[bot]

Visual evidence (per Oz review request)

Built WarpOss from this branch, opened a Personal Notebook, and pasted the issue's mermaid-with-frontmatter source. Same notebook, same markdown — just toggling the Mermaid block's view mode.

warp-pr11249-ui-comparison

  • Raw view (left) — ---\nconfig:\n theme: default\n--- frontmatter is preserved in the source (this is what was breaking master).
  • Diagram view (right) — strip_mermaid_frontmatter strips the leading block before mermaid_to_svg, the flowchart renders.

Pipeline-level proof using the same mermaid_to_svg rev pinned in this repo:

warp-pr11249-pipeline-proof

  • Before — raw source with frontmatter → Parse error at line 1: Expected 'graph' or 'flowchart' declaration.
  • After — stripped source → SVG rendered.

maxmilian avatar May 19 '26 03:05 maxmilian

/oz-review

maxmilian avatar May 19 '26 03:05 maxmilian

@maxmilian

I'm re-reviewing this pull request in response to a review request.

You can view the conversation on Warp.

I completed the review and no human review was requested for this pull request.

Comment /oz-review on this pull request to retrigger a review (up to 3 times on the same pull request).

Powered by Oz

warp-for-oss[bot] avatar May 19 '26 03:05 warp-for-oss[bot]

https://github.com/user-attachments/assets/f3243972-195d-4af5-882b-a393bf796a25

Visual evidence — end-to-end recording (per Oz review request)

Side-by-side comparison of the exact same Notebook + mermaid source on stable vs this PR's local OSS build:

  • Before (red card, 0–2s) — v0.2026.05.18.05.32.stable_02. Mermaid block shows Error rendering Mermaid diagram. Please check syntax.
  • A.mov section (2–16s) — same notebook, same input, stable build cannot render the diagram. Raw view confirms the ---\nconfig:\n theme: default\n--- frontmatter is present in source.
  • After (green card, 16–18s) — local build from fix/10676-mermaid-frontmatter.
  • B.mov section (18–28s) — same notebook, same input, strip_mermaid_frontmatter is applied, the flowchart renders. Toggling to raw view confirms the frontmatter is still present in source (the source is not mutated).

Source markdown used in both:

---
config:
  theme: default
---
flowchart LR
    A[Markdown source] --> B{Has frontmatter?}
    B -- yes --> C[strip_mermaid_frontmatter]
    B -- no --> D[pass through]
    C --> E[mermaid_to_svg]
    D --> E
    E --> F[Rendered SVG]

/oz-review

maxmilian avatar May 19 '26 15:05 maxmilian

@maxmilian

I'm re-reviewing this pull request in response to a review request.

You can view the conversation on Warp.

I reviewed this pull request and requested human review from: @kevinyang372.

Comment /oz-review on this pull request to retrigger a review (up to 3 times on the same pull request).

Powered by Oz

warp-for-oss[bot] avatar May 19 '26 15:05 warp-for-oss[bot]

@zachlloyd built the mermaid diagram support! Do you mind taking a look?

kevinyang372 avatar May 21 '26 21:05 kevinyang372

taking a look. thanks for the fix

zachlloyd avatar May 26 '26 17:05 zachlloyd

@oz-agent what are the elements of the mermaid formatter that we are currently ignoring? i'm wondering if this fix should be in mermaid_to_svg rather than in warp

zachlloyd avatar May 26 '26 17:05 zachlloyd

@zachlloyd

I'm working on changes requested in this PR (responding to a PR conversation comment).

You can view the conversation on Warp.

I pushed changes to this PR based on the comment.

Next steps:

  • Review the changes pushed to this PR.
  • Follow up with another comment if further adjustments are needed.

Powered by Oz

warp-for-oss[bot] avatar May 26 '26 17:05 warp-for-oss[bot]

Haven't forgotten this - just exploring if the right fix might be in mermaid_to_svg

zachlloyd avatar May 28 '26 01:05 zachlloyd

The fix within mermaid_to_svg is here: https://github.com/warpdotdev/mermaid-to-svg/pull/15

Closing this PR

zachlloyd avatar May 30 '26 21:05 zachlloyd