Skip to content

fix: Code block PDF export (BLO-987)#2725

Open
matthewlipski wants to merge 1 commit intomainfrom
code-block-pdf
Open

fix: Code block PDF export (BLO-987)#2725
matthewlipski wants to merge 1 commit intomainfrom
code-block-pdf

Conversation

@matthewlipski
Copy link
Copy Markdown
Collaborator

@matthewlipski matthewlipski commented May 7, 2026

Summary

This PR mainly changes the styling of code blocks when exported to PDF, replacing the black background for just a black border. The idea of this is to make it more suitable for print, which is a fairly common use case of exporting PDFs.

Additionally, it fixes an issue referenced in #2324, where only the first content with matching syntax highlighting is exported. The root cause of said issue is uncertain as syntax highlighting is applied via decorations, so I have no idea how it would ever show up in the editor content, but the fix should work all the same.

Closes #2324

Rationale

See above.

Changes

  • Updated code block styling in exported PDF.
  • Made all StyledText inline content get merged into a single string before exporting code block to PDF, instead of only the first StyledText text string being used.

Impact

N/A

Testing

N/A

Screenshots/Video

N/A

Checklist

  • Code follows the project's coding standards.
  • Unit tests covering the new feature have been added.
  • All existing tests pass.
  • The documentation has been updated to reflect the new feature

Additional Notes

N/A

Summary by CodeRabbit

  • Bug Fixes

    • Fixed code block rendering in PDF exports to display complete text content.
  • Style

    • Updated code block styling in PDF exports with a simplified border design.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 7, 2026

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

Project Deployment Actions Updated (UTC)
blocknote Ready Ready Preview May 7, 2026 6:27pm
blocknote-website Ready Ready Preview May 7, 2026 6:27pm

Request Review

@matthewlipski matthewlipski requested a review from nperez0111 May 7, 2026 18:27
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 7, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

The code block PDF exporter now extracts text by concatenating all styled text items from block content instead of selecting a single item, and adjusts the container styling from dark background with white text to a solid black border.

Changes

Code Block PDF Rendering

Layer / File(s) Summary
Text Content Extraction
packages/xl-pdf-exporter/src/pdf/defaultSchema/blocks.tsx
codeBlock renderer builds textContent by mapping over all StyledText elements in block.content and concatenating their .text values.
Container Styling
packages/xl-pdf-exporter/src/pdf/defaultSchema/blocks.tsx
Wrapper View styling changed from dark background and white text colors to a solid black 1px border.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • nperez0111

Poem

🐇 A code block's text now flows complete,
All styled lines in harmony meet,
Where borders frame with darkened lines,
The PDF export shines and refines! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The PR title 'fix: Code block PDF export (BLO-987)' clearly and concisely describes the main change: fixing code block PDF export behavior.
Description check ✅ Passed The PR description covers all essential template sections including Summary, Rationale, Changes, Impact, Testing, and Checklist with appropriate details for the scope of changes.
Linked Issues check ✅ Passed The PR successfully addresses issue #2324 by fixing the code block styling for PDF export and resolving the export content issue where only the first StyledText item was being used.
Out of Scope Changes check ✅ Passed All changes in the PR are directly related to the linked issue #2324: updating code block styling and fixing the content export bug. No extraneous modifications were introduced.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch code-block-pdf

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
packages/xl-pdf-exporter/src/pdf/defaultSchema/blocks.tsx (1)

120-122: ⚡ Quick win

Unsafe cast may silently produce "undefined" in output.

(block.content as StyledText<any>[]).map((item) => item.text) — if block.content ever contains a non-StyledText item (e.g., a Link, which has no top-level .text), then item.text is undefined, and .join("") silently emits the string "undefined" into the exported code block text.

The comment acknowledges code blocks should only contain StyledText, but the cast provides no runtime guard.

🛡️ Proposed defensive fix
-    const textContent = (block.content as StyledText<any>[])
-      .map((item) => item.text)
-      .join("");
+    const textContent = (block.content as StyledText<any>[])
+      .filter((item) => item.type === "text")
+      .map((item) => item.text)
+      .join("");
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/xl-pdf-exporter/src/pdf/defaultSchema/blocks.tsx` around lines 120 -
122, The current unsafe cast of block.content to StyledText<any>[] may produce
"undefined" if non-StyledText items (e.g., Link) appear; update the computation
of textContent to guard at runtime by treating block.content as an array of
unknown, filter or flatMap items that actually have a string .text property
(using a type guard or typeof check) before mapping and joining, and ensure
non-text items are skipped or replaced with a safe fallback (empty string) so
textContent never contains "undefined" while keeping references to block.content
and StyledText intact.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/xl-pdf-exporter/src/pdf/defaultSchema/blocks.tsx`:
- Around line 120-122: The current unsafe cast of block.content to
StyledText<any>[] may produce "undefined" if non-StyledText items (e.g., Link)
appear; update the computation of textContent to guard at runtime by treating
block.content as an array of unknown, filter or flatMap items that actually have
a string .text property (using a type guard or typeof check) before mapping and
joining, and ensure non-text items are skipped or replaced with a safe fallback
(empty string) so textContent never contains "undefined" while keeping
references to block.content and StyledText intact.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f1d7e4d0-8ec5-47b5-a6cf-c00bdc69a5b0

📥 Commits

Reviewing files that changed from the base of the PR and between 1b53232 and ddab4dc.

📒 Files selected for processing (1)
  • packages/xl-pdf-exporter/src/pdf/defaultSchema/blocks.tsx

Copy link
Copy Markdown
Contributor

@nperez0111 nperez0111 left a comment

Choose a reason for hiding this comment

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

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.

Difference between editor and export

2 participants