comark icon indicating copy to clipboard operation
comark copied to clipboard

`autoCloseMarkdown` appends a stray `**` when emphasis contains a space

Open libondev opened this issue 6 days ago • 0 comments

Environment

  • Operating System: Windows 11
  • Node Version: v24.19.0
  • Package Manager: pnpm@10
  • Comark Version: v0.9.2
  • Framework: None (autoCloseMarkdown directly; 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' and next = 'c', so inWord is true;
  • because the space reset starInWord, inWord && !starInWord records the run in starSkippedLen and 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_STAR frame before applying the inWord && !starInWord skip;
  • or keep starInWord true across whitespace while an F_STAR frame 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: false parses the same input correctly, so the damage happens entirely in the healer.
  • autoClose defaults to true, 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


libondev avatar Nov 30 '26 09:11 libondev