`autoCloseMarkdown` appends a stray `**` when emphasis contains a space
Environment
- Operating System:
Windows 11 - Node Version:
v24.19.0 - Package Manager:
pnpm@10 - Comark Version:
v0.9.2 - Framework:
None(autoCloseMarkdowndirectly; also reproduced through@comark/vue)
Comark Version
v0.9.2 (latest)
Reproduction
import { autoCloseMarkdown } from 'comark'
autoCloseMarkdown('**a b**c')
// expected: '**a b**c'
// actual: '**a b**c**'
Through the parser, with default options:
import { parseMarkdown } from 'comark'
await parseMarkdown('**a b**c')
// [['p', {}, ['strong', {}, 'a b'], 'c**']]
await parseMarkdown('**a b**c', { autoClose: false })
// [['p', {}, ['strong', {}, 'a b'], 'c']]
The CJK case that surfaced this:
autoCloseMarkdown('这是**中 文。**后续文字')
// actual: '这是**中 文。**后续文字**'
The same input without the space (这是**中文。**后续文字, #493) is left alone, so the trigger here is the whitespace, not CJK punctuation.
Description
When an emphasis run contains whitespace and its closing ** is immediately followed by a word character, healRegion fails to close the open F_STAR frame and closers() appends a stray ** at EOF:
| input | output |
|---|---|
**a b** c |
unchanged (closer followed by a space) |
**a b** |
unchanged (end of input) |
**a b**c |
**a b**c** |
x **a b**c |
x **a b**c** |
**a b**c **d e**f |
**a b**c **d e**f**** |
Root cause, in packages/comark/src/auto-close.ts (healRegion):
- any space/tab/CR resets
starInWord = false(the newline branch does the same); - at the second run of
**a b**c,prev = 'b'andnext = 'c', soinWordistrue; - because the space reset
starInWord,inWord && !starInWordrecords the run instarSkippedLenand skips the closing branch; - the frame opened at position 0 stays open, and
closers()appends**when the region ends.
The parser then renders a b bold and leaves a literal ** after c. In longer paragraphs the stray marker is very visible at the end of the text.
Possible fixes (either direction):
- let a run close an open
F_STARframe before applying theinWord && !starInWordskip; - or keep
starInWordtrue across whitespace while anF_STARframe is open.
Regression tests worth adding: **a b**c, x **a b**c, **a b**c **d e**f, plus the existing no-space CJK cases from #493.
This is the same symptom family as #276, #493 and #566, but those were punctuation-driven; this one is triggered by whitespace inside the emphasis, and is still present on main (the whitespace branch still sets starInWord = false).
This is not covered by #498. That change only handles a run that was left literal for being word-internal (the first run of 这是**中文。**后续文字 opens no frame), then pairs a later attached run with it via attachedLeft && !inWord && starSkippedLen === n. In **a b**c the first run does open a frame, and the closing run has inWord === true (prev b, next c), so it never reaches that branch; it is misclassified as word-internal only because the space reset starInWord. The open frame is then flushed at EOF.
Additional context
autoClose: falseparses the same input correctly, so the damage happens entirely in the healer.autoClosedefaults totrue, so this also affects complete, non-streaming documents, not just streaming tails.- #405 ("heal incomplete markdown only while streaming") would also avoid the damage for finished documents, but it was closed without merging.
Logs