Skip to content

Add mermaid-scale option to resize Mermaid images - #15192

Open
faysou wants to merge 1 commit into
quarto-dev:mainfrom
faysou:feature/mermaid-scale
Open

faysou wants to merge 1 commit into
quarto-dev:mainfrom
faysou:feature/mermaid-scale

Conversation

@faysou

@faysou faysou commented Oct 23, 2026

Copy link
Copy Markdown

Description

Adds a mermaid-scale option that resizes Mermaid diagrams rendered as images by a percentage of their natural size. Proposed in #15069.

With mermaid-format: png or svg, Quarto sizes each diagram at its natural size at 96 dpi, so Mermaid's 16px text prints at 12pt and wide diagrams can run past the page in Typst and PDF output. fig-width scales each diagram by a different factor, so every diagram ends up with a different text size. mermaid-scale applies one factor to every diagram's natural size, which keeps diagram text the same size across a document.

---
format:
  typst:
    mermaid-format: png
    mermaid-scale: 75%
---

```{mermaid}
%%| mermaid-scale: 50%
flowchart LR
  A --> B
```
  • The value is a percentage and keeps the aspect ratio. A cell value overrides the document value.
  • An explicit fig-width or fig-height still takes precedence, so explicit sizes are respected as discussed in Typst: Unable to fit mermaid diagram into columns #8543.
  • It applies to the png and svg outputs, where Quarto computes the size in resolveSize. With mermaid-format: js, mermaid.js sizes the diagram in the browser, so the option has no effect.
  • The cell schema in the Mermaid handler and document-mermaid.yml both validate the value. A bare number or another string fails with a validation error instead of a TypeError.
  • The option is skipped when the cell's options are written to the output div, like mermaid-format.

The editor tooling files under src/resources/editor/tools/ were regenerated with quarto dev-call build-artifacts.

Tests

tests/smoke/render/render-mermaid-scale.test.ts renders three copies of a diagram to Typst: one at mermaid-scale: 100%, one inheriting the document's 50%, and one with fig-width: 3. It checks that the scaled image is half the size of the 100% one and that fig-width wins. It compares against the 100% copy instead of absolute sizes, so it does not depend on the fonts headless Chrome uses. With the scaling disabled the test fails (2.13in against an expected 1.065in).

The existing Mermaid smoke-all documents (mermaid-typst, mermaid-multi-diagram, mermaid-pdf-default, mermaid-svg-docx, 13661, mermaid-gfm-svg, mermaid-gfm-default, mermaid/backticks, mermaid-themes/3328) still pass.

Checklist

I have (if applicable):

  • referenced the GitHub issue this PR closes
  • updated the appropriate changelog in the PR
  • ensured the present test suite passes
  • added new tests
  • created a separate documentation PR in Quarto's website repo and linked it to this PR

Mermaid diagrams rendered as png or svg images are sized at their
natural size at 96 dpi, so their 16px text prints at 12pt and wide
diagrams can run past the page. fig-width scales each diagram by a
different factor, which gives every diagram a different text size.

mermaid-scale takes a percentage, such as 75%, and multiplies the
natural width and height by it, so all diagrams keep the same text
size. It can be set in the document front matter or on a single
cell, where it overrides the document value. An explicit fig-width
or fig-height still takes precedence. With mermaid-format: js the
browser sizes the diagram, so the option has no effect there.

Both the cell and front matter schemas validate the value, so a bare
number or other string fails with a clear error instead of a crash.
@faysou
faysou force-pushed the feature/mermaid-scale branch from 5645620 to 085d029 Compare October 23, 2026 20:50
@posit-snyk-bot

posit-snyk-bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

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