Repository navigation
fix(core): escape leading block markers when exporting Markdown - #3472
Conversation
A paragraph such as `>10% of orders failed` or `2024. Revenue grew` was exported as is, so reading the Markdown back gave a quote or a numbered list item. Escape a quote, ATX heading, list or thematic break marker at the start of a block's text, the way CommonMark allows.
|
@JohanesPeterV is attempting to deploy a commit to the TypeCell Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
@blocknote/ariakit
@blocknote/code-block
@blocknote/core
@blocknote/diagram-block
@blocknote/mantine
@blocknote/math-block
@blocknote/react
@blocknote/server-util
@blocknote/shadcn
@blocknote/xl-ai
@blocknote/xl-docx-exporter
@blocknote/xl-email-exporter
@blocknote/xl-multi-column
@blocknote/xl-odt-exporter
@blocknote/xl-pdf-exporter
@blocknote/xl-typst-exporter
commit: |
Summary
A paragraph that starts like a Markdown block changes type after
blocksToMarkdownLossyandtryParseMarkdownToBlocks.>10% of orders failedcomes back as a quote,2024. Revenue grewas a numbered list item.main>10% of orders failed\>10% of orders failed2024. Revenue grew2024\. Revenue grew# of orders: 12\# of orders: 12- 5 items\- 5 items---\---With this PR each one reads back as the same paragraph.
Rationale
escapeMarkdownTextescapes inline punctuation such as*,_and backticks, but the start of a block's text is written as is. So a leading>,#,-,+or2024.opens a quote, heading, list or divider when the Markdown is read back, by BlockNote's importer or any CommonMark parser.The same happens in quotes and in a list item's later paragraphs. On a list item's first line,
* >10% of orders failedreads back fine in BlockNote, but CommonMark reads it as a list item holding a quote.Changes
In
htmlToMarkdown.ts:escapeBlockStartchecks the start of a block's text with one anchored regex and adds a backslash the way CommonMark does:\>,\#,\-,\+,2024\.,2\), and\---for a thematic break. LikecouldStartMarkdownBlock, it does this at any indentation, since BlockNote's importer has no indented code and the export trims the first block's indentation.<p>), list and task item text, and the child paragraphs of a list or toggle item.couldStartMarkdownBlockalready writes<br>there.Impact
Only text that starts with one of these markers exports differently. Real quotes, headings and lists, and the same characters mid-line (
10 > 5,#hashtag,-5,2024.5,--- x), export as before. Headings and table cells are untouched, since their text can't open a block. No export or copy snapshot changes.Copying or dragging whole blocks writes this Markdown to
text/plaintoo, so a copied>10%paragraph pastes into a plain-text app as\>10%.text/plainstays the Markdown export on purpose (#3384).Testing
htmlToMarkdown.browser.test.ts: 10 cases (paragraph, quote, list item, and a list item's second paragraph) fail onmainand pass with the fix. 5 controls (real quote, heading and list, mid-line markers) pass on both.markdown.browser.test.ts:>10%and2024.paragraphs and a quote block- 5 items, exported and read back. Two more cases do the same for indented text: a first paragraph# reportindented 4 spaces, and a tab-indented2024. Revenue grewafter another paragraph. All three fail onmainand pass with the fix.vp test -c vite.config.browser.ts --mode browser-localintests): 13 fail onmain, 62/62 pass with the fix.runTests.browser.test.tsinformatConversion/exportandclipboard/copy), which CI runs in Docker: 299 and 23 pass in one local Chromium run with both added toLOCAL_BROWSER_FILES, no snapshot changes.vp lint --type-aware --type-check(frompackages/coreandtests) andvp fmt --checkon the changed files: clean.Screenshots/Video
A small page built from the "Converting Blocks to Markdown", "Parsing Markdown to Blocks" and "Displaying Document JSON" examples, same typing in both. Typing
2024.turns the block into a numbered list in the editor, so Backspace undoes that first, like a user would.Before (
main):before.mp4
After:
after.mp4
Checklist
Additional Notes
No docs change, it's an export fix.
Not a regression from #2624: 0.50.0 exports these the same way, since its remark-stringify setup wrote text nodes as is.
In other CommonMark parsers, a paragraph indented 4+ spaces is already an indented code block, on
maintoo, so the backslash shows in that code. I put BlockNote's own round trip first.BlockNote's importer also reads a no-break space after a marker as a space, so
2024.+ no-break space still comes back as a list. CommonMark only allows a space or tab there, so that one's for the parser. Happy to do it in a follow-up.#3450 changes the same lines in
serializeParagraphandserializeListContinuation, happy to rebase onto it if it lands first.Found, fixed and tested with Codex and Claude Code. I reviewed the change.
Summary by CodeRabbit