Skip to content

fix(core): escape markdown in text that ends with whitespace - #2528

Open
0z5a wants to merge 1 commit into
Milkdown:mainfrom
0z5a:escape-text-ending-with-space
Open

0z5a wants to merge 1 commit into
Milkdown:mainfrom
0z5a:escape-text-ending-with-space

Conversation

@0z5a

@0z5a 0z5a commented Nov 19, 2026

Copy link
Copy Markdown
  • I read the contributing guide
  • I agree to follow the code of conduct

Summary

Closes #2527

The text handler in remark-handlers.ts returns text that matches /^[^*_\\]*\s+$/ as it is, without state.safe. That branch came in with #2058 to stop a trailing space from turning into  . But text in front of inline code, a link or emphasis often ends with a space, so its escapes get dropped too. \# not a heading `code` comes back as a heading, and `boolean` \| `string` in a table cell comes back as two cells.

With this change the branch still keeps the leading and trailing whitespace raw, so #2058 keeps working. The text between them now goes through state.safe, with the whitespace passed as before/after so the unsafe patterns see the right context.

How I found it

I round-tripped 500 README files through an editor with commonmark + gfm and compared the markdown-it render of each source with the render of the output, block by block. Three files lost blocks this way: a props table with `number` \| `string` in its type column got extra cells, a wrapped paragraph with a line that starts with 1. turned into an ordered list, and wrapped lines that start with - inside a list item turned into a nested list. In each case the text ends with a space before a code span or **strong**.

Where this can show up in practice

Option tables in docs often have a type column like `boolean` \| `string`. Loading one of those into the editor and typing anything rewrites the table with an extra column. I haven't seen a field report.

How did you test this change?

  • New specs in preset-commonmark (15 tests) and preset-gfm (4 tests). They cover \#, 1\., \-, \>, \&, \~\~, a \| between code spans in a table cell (from markdown and from a doc built in code), and text before a link and before **strong**. All 19 fail on main and pass with the change. Each case checks the markdown output and that parsing it again gives the same doc.
  • Control: the same specs check that the texts "a b " and " a b " and the line a **b** c serialize exactly as on main, and that " # x " before a code span keeps both outer spaces.
  • vitest run in preset-commonmark (147 passed), preset-gfm (40 passed) and core (4 passed).
  • tsc -b packages/plugins/preset-gfm/tsconfig.json exits 0 (it includes core and commonmark). oxfmt --check is clean on the changed files. oxlint --deny-warnings reports nothing in the changed files; repo-wide it shows the same 76 warnings in other packages on main and with this change.
  • Same 500-file round trip: output changed for 23 files, 3 files went from lost blocks to a full match, none went the other way, and files where a second round trip changes the output again went from 11 to 3. The count of   in the output did not change.
  • In the playground (7.23.0, Chromium 156) I reproduced the table and heading cases from the issue by typing one character.
  • One output differs in whitespace: a single text node that contains \n (a newline then a space) now gets   for that space, which is what the non-fast path already did. The parser does not produce such a node (it turns the line break into a hardbreak), so I could only reach it from a doc built in code. On main that space is lost when the output is parsed again.
  • Not run: unit tests of other packages and the e2e suite.

Platform: macOS

Text that matched the trailing-space branch of the text handler skipped
`state.safe`, so escapes such as `\#`, `1\.` and a `\|` between code
spans in a table cell were lost on save. Keep the outer whitespace raw
and pass only the text between it through `state.safe`.

Closes Milkdown#2527
@vercel

vercel Bot commented Nov 19, 2026

Copy link
Copy Markdown

@0z5a is attempting to deploy a commit to the Milkdown Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot

changeset-bot Bot commented Nov 19, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 76d1eab

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

This branch has not been deployed

No deployments
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] Escapes in text that ends with a space are dropped on save (\#, \| in tables, 1\.)

1 participant