Repository navigation
Avoid blank PDF pages from Mermaid Preview’s code wrapper - #25621
Conversation
There was a problem hiding this comment.
ℹ️ No critical issues — one minor test-hygiene suggestion inline.
Reviewed changes
- Mermaid fence detection moves to the raw HAST node — the
prerenderer inuse-markdown-preview-components.tsxnow inspectsnode.childrenfor acodechild carrying alanguage-mermaidclass, instead of comparingchild.type === MermaidBlock. The old comparison never matched becauseprereceives thecodeelement (whosetypeis thecodecomponent), not the renderedMermaidBlock. CodeBlockCopyButtongainswrapInPre— defaults totrue, so ordinary code keeps its<pre>;falserenders the children directly, dropping the accidental outer<pre>around Mermaid diagrams. The copy button, clipboard extraction, and annotation wrapper are unchanged.- New happy-dom component test — exercises the real react-markdown pipeline with a mocked
MermaidBlock, asserting the diagram has no<pre>ancestor, source copy still works, and ordinary/invalid/inline code paths are unaffected.
deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏
| @@ -0,0 +1,147 @@ | |||
| // @vitest-environment happy-dom | |||
There was a problem hiding this comment.
This test drives React via act(...) + createRoot, but never sets globalThis.IS_REACT_ACT_ENVIRONMENT = true, so React logs The current testing environment is not configured to support act(...) on every render (visible as repeated stderr in the suite). Nearly every other createRoot + act renderer test in this repo sets the flag at module scope (e.g. EditorEditFileSurface.monaco-containment.test.tsx, combined-diff-file-tree-render.test.tsx). Setting it removes the noise and lets React actually enforce act, so un-awaited updates in this test surface instead of passing silently.
5c59c26 to
2c540e1
Compare
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 2 minutes. View limit details
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. |
Keep the existing copy button and annotation wrapper while checking the original HAST code class. This removes the extra empty page reproduced in Markdown Preview PDF export without shrinking labels. Credit gum798 for #21310 and #21377, innocarpe for #21328, and m13v for the label-readability review. The height-cap proposals were evaluated but this fix keeps the existing diagram sizing.
Trim the wrapper test to the regression cases and mock the runtime target hook main now uses.
2c540e1 to
25fe3b7
Compare

ELI5
Exporting a tall Mermaid diagram from Markdown Preview could insert an empty PDF page before the diagram. Preview now avoids that extra page while keeping the diagram’s copy button, source annotation range, and existing label size.
What Changed
Detect Mermaid fences from the checked Markdown syntax node. The existing check inspected the React child before its code component rendered, so it never recognized Mermaid. Reuse the existing copy-button wrapper with optional
<pre>wrapping; ordinary code keeps the same default wrapper. Two production files: +19/−8, net 11 lines.Why
The accidental outer code
<pre>adds another print boundary around the SVG. Removing that wrapper fixes the reproduced empty page without changing export CSS or fitting every diagram onto one page. React Markdown 10.1.0 supplies the original syntax node, and hast-util-to-jsx-runtime 2.3.6 creates the mapped child before React renders it. React Markdown source, HAST JSX source, CSS fragmentation.Credit @gum798 for the report and #21377, Wooseong Kim (@innocarpe) for #21328, and @m13v for raising label readability. Both height-cap proposals were tested through the actual export pipeline: an 80-node diagram’s text shrank to about a 1.6/1.55-point glyph bounding height, versus 13.934 points without a cap. This patch uses the existing intended wrapper behavior instead. Mermaid’s geometry and Electron’s A4/margin settings remain unchanged. Mermaid 11.17.2 source, Electron 43.7.5 printToPDF.
Linked Issue
Addresses the reproduced Preview wrapper case in #21310. Keep that broader issue open: heading-only pages and Chromium’s existing slicing of oversized SVGs, including boundary duplication, remain. This does not add automatic diagram pagination or guarantee readable wide diagrams.
Visual Proof
Same 20-node fixture, A4 PDF page 2, before and after. The empty code background is replaced by the diagram at its previous label size:
Before PDF, 5 pages · After PDF, 4 pages. Page 1 still contains only the heading; the diagram continues across pages at its original size.
Preview retains the copy button and supported Git annotation ranges. The earlier folder-file renderer controls lacked annotation wrappers; the corrected normal-folder check also has none:
Testing
ORCA_BACKGROUND_LAUNCH=1; actual export menu, export IPC, hidden PDF window, andprintToPDF. Only the save picker was replaced with an owned fixture path.pnpm tc, fullpnpm lint, changed-code quality against the stated base, standalone React Doctor 0.9.14 changed-lines gate, and a freshelectron-vite build --mode e2epassed.Six actual Git diagram exports cover normal, near-page-height, 20/80-node, wide, and sequence diagrams. Three earlier folder-file exports were renderer controls with an incomplete project-group fixture, not normal-folder acceptance. The 20-node export changes 5→4 pages, the 80-node export 11→10, removing the empty page without changing measured label size or losing distinct labels. Four additional Git exports and four earlier folder-file renderer controls preserve all 150 ordinary code lines, invalid diagram source, and a local 240×80 raster image alongside a diagram. Export strips copy/note controls. Clicking every source copy button sends exact authored code through the existing clipboard IPC, intercepted in the owned app so the OS clipboard stays untouched.
The required standalone React Doctor changed-lines gate has no findings. An additional broader scan exhausted its 4 GiB Node heap. Physical Windows displays and live SSH were not exercised; host routing, authored Markdown, image handling, PDF lifecycle, and remote wire are unchanged.
Corrected folder acceptance
A separate hidden Linux Electron 43.7.5 check registers a normal folder group with its real parent path at creation and verifies its catalog, local execution host, active workspace, and document owner. Seven fresh exports cover ordinary and 20-node diagrams, a safe
<br/>label, ordinary/150-line code, invalid Mermaid, and a local 240×80 raster image. The 20-node PDF has four pages, all 20 distinct labels, and a 13.406-point glyph bounding height; none of these exports has an empty text page. Heading-only pages and existing SVG slicing still occur. Ordinary code retains every line; invalid source and the embedded raster remain. All seven copy buttons send exact authored source through the intercepted clipboard IPC; export UI controls are absent. Folder annotation wrappers remain absent.This acceptance check uses the read-only combined build at
b7ac4927068c6074acda61334b097881551857d6, including this PR and the safe HTML-label work in #25554. Main and renderer build hashes match before/after; this PR's source and head are unchanged. Earlier name-only folder receipts are retained as renderer controls and superseded for normal-folder acceptance.Review
Notes
Risk is low to medium: removing Mermaid’s accidental code wrapper changes its Preview background/padding and print placement. Copy and annotations remain; ordinary code, invalid-source fallback, sanitizer/CSP, libraries, and export lifecycle retain existing behavior. Stacked on #25540 solely for the shared lint-support ancestor.
Rebased onto main (Oct 31)
<pre>, the code copy button landed on top of it, so on diagrams the copy button now sits just left of expand (.code-block-wrapper-diagram).<pre>wrapper (main's page 2 is the blank one). The expand and copy buttons stay out of the export.