Skip to content

fix(markdown): center mermaid diagrams narrower than the content column - #470

Merged
gitfromwildan merged 1 commit into
mainfrom
fix/markdown-mermaid-center
Nov 5, 2026
Merged

gitfromwildan merged 1 commit into
mainfrom
fix/markdown-mermaid-center

Conversation

@gitfromwildan

Copy link
Copy Markdown
Member

Summary

Mermaid diagrams narrower than the content column now render centered instead of flush left.

Cause

MermaidMdx gives its <pre class="mermaid"> an inline width: fit-content and margin: 1em auto, so the <pre> was meant to shrink to the diagram and center itself. flame's globals.css sets pre { width: inherit !important }, which overrides that width. The <pre> spans the column, margin: auto does nothing, and the SVG sits at the start.

Fix

  • The <pre> now carries data-mermaid-align="center".
  • @docubook/markdown/styles.css centers the diagram's direct <svg> child: display: block; margin-inline: auto. Nested SVGs and the labels inside the diagram are untouched.
  • It sets no text-align, which Mermaid's HTML labels would inherit.
  • It does not make the <pre> a flex container. I tried that first. Mermaid sizes a Gantt chart from a temporary element it adds inside the <pre>, and as a flex item that element shrank the chart from 564px to the SVG default of 300px.
  • The Mermaid docs page notes the alignment.

Verification

Built flame's docs and measured each diagram's SVG inside its <pre> in Chromium. Gaps are left / right.

Diagram, 1280px viewport Before After
Flowchart 0 / 204px 102 / 102px
Class 0 / 243px 122 / 122px
State 0 / 236px 118 / 118px
ER 0 / 178px 89 / 89px
Sequence, Gantt, Pie full width full width, Gantt still 564px
  • At 390px every diagram is centered or full width, with no horizontal overflow.
  • After toggling the dark theme, the re-rendered diagram stays centered. In fullscreen it sits on the viewport center.
  • A new test in MermaidMdx.test.tsx reads the rule from the shipped styles.css. It checks that the rule matches the diagram's SVG but not a nested one, and that it uses neither text-align nor flex or grid on the <pre>.
  • 123 markdown tests and 801 flame tests pass. tsc, oxlint and the pre-push hook pass.

The diagram's <pre class="mermaid"> was meant to shrink to the diagram
and center itself with `width: fit-content` and `margin: auto`. flame's
global `pre { width: inherit !important }` overrides that width, so the
<pre> spans the column and every narrower diagram sat at the start.

- The <pre> now carries data-mermaid-align="center".
- styles.css centers the diagram's direct <svg> child as a block with
  auto inline margins. Nested SVGs and the labels inside are untouched.
- No text-align, which Mermaid's HTML labels would inherit. No flex on
  the <pre> either: Mermaid sizes a Gantt chart from a temporary element
  inside it, and a flex item shrank that chart from 564px to 300px.
- The Mermaid docs note the alignment.
@vercel

vercel Bot commented Nov 5, 2026

Copy link
Copy Markdown

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

1 Skipped Deployment
Project Deployment Actions Updated
docu Ignored Ignored Nov 5, 2026 3:59pm UTC

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ No new issues found.

Reviewed changes

This review covers the fix that centers Mermaid diagrams that are narrower than the content column. It was a standard review with no findings. The fix sits in the stylesheet and does not touch Mermaid's layout or measurement.

  • Centering hook: the <pre class="mermaid"> in MermaidMdx now has data-mermaid-align="center". This works around flame's @layer utilities { pre { width: inherit !important } }, which beats the inline width: fit-content.
  • Stylesheet rule: pre[data-mermaid-align="center"] > svg { display: block; margin-inline: auto; } in @docubook/markdown/styles.css.
    • The > svg combinator is correct. mermaid.run({ nodes: [node] }) replaces the contents of the <pre>, so the diagram SVG is a direct child of it.
    • Flame imports this sheet unlayered, so the rule wins over preflight's svg defaults.
    • The rule sets no text-align and does not make the <pre> a flex or grid container. That keeps Mermaid's HTML labels and its Gantt width measurement unchanged.
  • Regression test: the test reads the shipped stylesheet through styles.css?raw, the same pattern CodeBlockBleed.test.tsx uses (vitest.config.ts already sets css.include). It checks that the rule matches the diagram's own SVG but not a nested one, and that the <pre> does not become flex or grid.
  • Docs and changeset: the Mermaid docs page has a note on alignment, and the changeset is an @docubook/markdown patch.

Pullfrog  | View workflow run | Using claude-opus-5-5 | 𝕏

@gitfromwildan
gitfromwildan merged commit a0954b5 into main Nov 5, 2026
15 checks passed
@gitfromwildan
gitfromwildan deleted the fix/markdown-mermaid-center branch November 5, 2026 16:05
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.

1 participant