Fix #3100: Hoist whitespace outside markdown emphasis markers#3343
Open
JiuqingSong wants to merge 2 commits into
Open
Fix #3100: Hoist whitespace outside markdown emphasis markers#3343JiuqingSong wants to merge 2 commits into
JiuqingSong wants to merge 2 commits into
Conversation
The markdown emitter wrapped segment text with **/*/~~ verbatim, so a text segment ending or starting with whitespace produced invalid markdown (e.g. "**world **how" instead of "**world** how"). Move leading/trailing whitespace outside the emphasis markers before wrapping, and skip wrapping for whitespace-only segments. An existing test in createMarkdownBlockgroupTest was asserting the buggy "*text *[link](...)" output; updated to the correct "*text* [link](...)". Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes issue #3100 where the markdown emitter could produce invalid CommonMark by wrapping segments that contain leading or trailing whitespace inside emphasis markers (e.g. **world **how). The textProcessor in createMarkdownParagraph now hoists surrounding whitespace outside the **/*/~~ markers and returns whitespace-only segments untouched. An existing test that asserted the buggy output was updated, and five new unit tests cover the fix.
Changes:
- Refactor
textProcessorto extract leading/trailing whitespace via regex and apply emphasis markers only around the non-whitespace core, including when a link is present. - Update the previously incorrect
> *text *[link](...)assertion to> *text* [link](...). - Add unit tests for bold trailing whitespace, italic leading whitespace, strikethrough surrounding whitespace, combined bold+italic, and whitespace-only segments.
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/roosterjs-content-model-markdown/lib/modelToMarkdown/creators/createMarkdownParagraph.ts | Reworked textProcessor to hoist whitespace outside emphasis markers and skip wrapping whitespace-only segments. |
| packages/roosterjs-content-model-markdown/test/modelToMarkdown/convertContentModelToMarkdownTest.ts | Added five regression tests covering the new whitespace-hoisting behavior. |
| packages/roosterjs-content-model-markdown/test/modelToMarkdown/creators/createMarkdownBlockgroupTest.ts | Updated an existing assertion that encoded the buggy markdown output. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const [, leading, core, trailing] = match ? match : ['', '', text.text, '']; | ||
|
|
||
| if (!core) { | ||
| return text.text; |
Comment on lines
+60
to
+61
| const match = /^(\s*)([\s\S]*?)(\s*)$/.exec(text.text); | ||
| const [, leading, core, trailing] = match ? match : ['', '', text.text, '']; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
**/*/~~verbatim, so a text segment with surrounding whitespace produced invalid CommonMark (e.g.**world **howinstead of**world** how).textProcessorincreateMarkdownParagraph.tsnow hoists leading/trailing whitespace outside the emphasis markers and skips wrapping for whitespace-only segments.createMarkdownBlockgroupTestwas asserting the buggy> *text *[link](...)output — updated to the correct> *text* [link](...).Test plan
yarn test:fast --testPathPattern=roosterjs-content-model-markdown— 199/199 passyarn eslint— clean🤖 Generated with Claude Code