Skip to content

fix(desktop): wrap long code-block lines when exporting to PDF (#5307) - #5460

Merged
Jocs merged 1 commit into
developfrom
fix/5307-pdf-code-block-wrap
Sep 19, 2026
Merged

Jocs merged 1 commit into
developfrom
fix/5307-pdf-code-block-wrap

Conversation

@Jocs

@Jocs Jocs commented Sep 19, 2026

Copy link
Copy Markdown
Member

Fixes #5307.

Problem

A fenced code block with a long line is cut off in the exported PDF: the page shows the code block's scrollbar, but the rest of the line is simply not on the paper (and cannot be scrolled to or copied).

Reproduced on develop by exporting

```bash
dnf install -y wget openssl-devel bzip2-devel libffi-devel zlib-devel ncurses-devel sqlitedevel readline-devel tk-devel gdbm-devel xz-devel make gcc
```

through the real app (Playwright + File › Export › PDF), which produced exactly the PDF in the issue — the line ends at ncurses-deve with a scrollbar underneath.

Cause

printService.css wraps printed code blocks with

body article.markdown-body pre code.fenced-code-block { white-space: pre-wrap; … }

fenced-code-block is a class only the legacy muyajs renderer emitted. Exports now go through @muyajs/core, which renders fences with marked and emits <pre><code class="language-*"> — or a bare <code> when the fence has no language. The rule therefore matches nothing, and the element that actually carries the text keeps white-space: pre from github-markdown-css (.markdown-body pre > code) and from the prism theme (code[class*="language-"]). The engine's own .markdown-body pre { white-space: pre-wrap } cannot help, because white-space applies per element and the inner <code> overrides it; .markdown-body pre { overflow: auto } then renders the scrollbar seen in the PDF.

Fix

Select any pre code inside the printed article, which covers both highlighted fences and language-less ones. Wrapping stays unconditional for PDF/print — a printed page cannot be scrolled — matching the pre-migration behaviour.

Verification

  • New unit spec print-code-wrap.spec.ts renders a fence through the real @muyajs/core export path, feeds it to the real MarkdownPrint container, and asserts the @media print wrap selectors match the emitted <code> (both with and without a language). It fails on develop and passes with this change.
  • Real-app check: PDF exported from the running app before the change stops at ncurses-deve; after the change the full line wraps onto a second row ending in make gcc.
  • pnpm run lint (0 errors), pnpm run typecheck, pnpm run test:unit (822 passing).

🤖 Generated with Claude Code

The `@media print` rule that wraps code blocks selected
`pre code.fenced-code-block` — a class only the legacy muyajs renderer
emitted. @muyajs/core renders fences through marked, which emits
`<pre><code class="language-*">` (or no class at all), so the rule stopped
matching and the code text kept `white-space: pre` from github-markdown-css
(`pre > code`) and the prism theme (`code[class*="language-"]`). Long lines
then ran off the page: the exported PDF showed `pre`'s `overflow: auto`
scrollbar with the tail of the line unreachable.

Select any `pre code` inside the printed article instead, which covers both
the highlighted fences and the language-less ones.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

Build artifacts for PR #5460:

Run: https://github.com/marktext/marktext/actions/runs/35448360165

Artifact Size Link
marktext-windows-arm64 279.4 MB Download
marktext-macos-x64 297.8 MB Download
marktext-macos-arm64 287.5 MB Download
marktext-linux 638.2 MB Download
marktext-windows-x64 287.6 MB Download

@Jocs
Jocs merged commit 5f40269 into develop Sep 19, 2026
11 checks passed
@Jocs
Jocs deleted the fix/5307-pdf-code-block-wrap branch September 19, 2026 14:55
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.

[Bug] Lines in the code block are long, and they don't display fully when exported to PDF

1 participant