Skip to content

Avoid blank PDF pages from Mermaid Preview’s code wrapper - #25621

Merged
nwparker merged 2 commits into
mainfrom
nwparker/editor-pdf-export-breaks
Nov 1, 2026
Merged

nwparker merged 2 commits into
mainfrom
nwparker/editor-pdf-export-breaks

Conversation

@nwparker

@nwparker nwparker commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​114 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​114
Prod 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​27 $\color{#cf222e}{\Huge{\mathbf{−}}}$​9 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​18

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 After
Blank PDF page before diagram Diagram on the former blank page

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:

Before After
Preview before wrapper fix Preview with source copy retained

Testing

  • Manually tested compiled Electron on macOS in hidden renderers with ORCA_BACKGROUND_LAUNCH=1; actual export menu, export IPC, hidden PDF window, and printToPDF. Only the save picker was replaced with an owned fixture path.
  • 56 tests in 9 suites, including a regression red before the fix, real React Markdown component mapping, ordinary code, inline code, invalid Mermaid, copy source, annotations, and PDF lifecycle controls.
  • pnpm tc, full pnpm lint, changed-code quality against the stated base, standalone React Doctor 0.9.14 changed-lines gate, and a fresh electron-vite build --mode e2e passed.

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.

Registered folder tall diagram, PDF page 2
Registered folder safe HTML label, PDF page 1

Review

  • Agent skill upstream boundary: not applicable.
  • Small focused change; self-reviewed and independently source-reviewed.
  • User-facing behavior, mechanism, alternatives, and before/after proof provided.
  • Cross-platform, folder workspace, and SSH impact considered.

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)

  • Main now gives each diagram an expand button in its top-right corner. Without the <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).
  • Re-verified the export: the rendered Preview DOM printed through Chromium at the export's A4 size and margins gives 3 pages, against 4 with main's <pre> wrapper (main's page 2 is the blank one). The expand and copy buttons stay out of the export.
  • Trimmed the wrapper test to the two regression cases; the diagram case fails on main.

@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 critical issues — one minor test-hygiene suggestion inline.

Reviewed changes

  • Mermaid fence detection moves to the raw HAST node — the pre renderer in use-markdown-preview-components.tsx now inspects node.children for a code child carrying a language-mermaid class, instead of comparing child.type === MermaidBlock. The old comparison never matched because pre receives the code element (whose type is the code component), not the rendered MermaidBlock.
  • CodeBlockCopyButton gains wrapInPre — defaults to true, so ordinary code keeps its <pre>; false renders 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.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@@ -0,0 +1,147 @@
// @vitest-environment happy-dom

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.

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.

@nwparker
nwparker force-pushed the nwparker/editor-pdf-export-breaks branch from 5c59c26 to 2c540e1 Compare October 31, 2026 23:23
@nwparker
nwparker changed the base branch from nwparker/editor-mobile-import-cycle-lint to main October 31, 2026 23:30
@coderabbitai

coderabbitai Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: fa1f9da9-7f0c-4407-a939-383fd1888a14
Commits

Reviewing files that changed from the base of the PR and between 9483524 and 25fe3b7.

Files selected for processing: 4
  • src/renderer/src/assets/markdown-preview.css
  • src/renderer/src/components/editor/CodeBlockCopyButton.tsx
  • src/renderer/src/components/editor/MarkdownPreview.mermaid-wrapper.test.tsx
  • src/renderer/src/components/editor/use-markdown-preview-components.tsx
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

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.
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