Skip to content

fix(comark): harden auto-close for code spans, math, escapes, and link text#233

Merged
farnabaz merged 2 commits into
comarkdown:mainfrom
antfubot:task/ct243j
Jun 10, 2026
Merged

fix(comark): harden auto-close for code spans, math, escapes, and link text#233
farnabaz merged 2 commits into
comarkdown:mainfrom
antfubot:task/ct243j

Conversation

@antfubot

@antfubot antfubot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

🔗 Linked issue

❓ Type of change

  • 📖 Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

Hardens autoCloseMarkdown() against several inline edge cases that mangled partial streaming output. The inline closer scans the last line counting markers (*, _, ~, `, $, …); it previously miscounted markers that are actually literal text, which corrupted marker parity and emitted stray closers.

1. Inline code spans and math are now literal regions

Emphasis markers inside an inline code span (`…`) or inline math ($…$) are literal text, not delimiters. They were being counted, throwing off bold/italic parity:

text `a*b` and **bold   →   text `a*b` and **bold*    ❌  (was)  — stray `*`
text `a*b` and **bold   →   text `a*b` and **bold**   ✅  (now)

$a * b$ and *italic     →   $a * b$ and *italic       ❌  (was)  — left open
$a * b$ and *italic     →   $a * b$ and *italic*      ✅  (now)

When a line ends inside an unclosed code/math region, only that region is closed and emphasis closers are suppressed, so nothing leaks into the span:

`x * y     →   `x * y*    ❌  (was)
`x * y     →   `x * y`    ✅  (now)

2. Escaped markers are no longer counted

A backslash escapes the following marker, so \* / \_ / \` are literal:

a \* b *it   →   a \* b *it    ❌  (was)  — left open
a \* b *it   →   a \* b *it*   ✅  (now)

3. Inline code inside unclosed link text (earlier commit)

A streaming [`foo skipped all inline markers inside link text, dropping the unclosed backtick and emitting only the bracket closer — landing ] inside the code span:

[`foo   →   [`foo]    ❌  (was)
[`foo   →   [`foo`]   ✅  (now)

Genuinely nested emphasis of mixed marker types (e.g. **_text) needs CommonMark flanking resolution and remains a documented limitation, tracked by it.todo cases (the downstream markdown-it parser does the real resolution). Complete links, attribute scopes, and block math ($$) are unaffected.

📝 Checklist

  • I have linked an issue or discussion.
  • I have run pnpm verify and it passes. (comark: 1055/1055 + 2 todo, lint + format clean; 2 pre-existing comark-vue typecheck errors are unrelated)
  • I have updated the documentation accordingly. (no docs change — internal streaming behavior)

This PR was created with the help of an agent.

…close

When auto-closing a partial stream where link text wraps inline code
(e.g. `[`foo`), the scanner skipped all markers inside link text and
dropped the unclosed backtick, producing `[`foo]` — the bracket closer
landed inside the unclosed code span and rendered as literal text.

Track backticks opened inside unclosed link text and close the code
span before the bracket when odd.
@vercel

vercel Bot commented Jun 9, 2026

Copy link
Copy Markdown

@antfubot is attempting to deploy a commit to the NuxtLabs Team on Vercel.

A member of the Team first needs to authorize it.

…lose

Inline emphasis markers (* _ ~) inside an inline code span or math
region are literal text, not delimiters, and a backslash escapes the
following marker. The counting-based inline closer previously counted
all of these, corrupting marker parity and emitting stray closers:

- asterisks inside a closed code span/math threw off bold/italic parity
- an open code span/math got a spurious emphasis closer appended
- escaped markers (\*) were counted as delimiters

Track code/math literal regions during the single O(n) scan and skip
escaped characters. When a line ends inside an open code/math region,
close only that region and suppress emphasis closers. Genuine nested
emphasis of mixed marker types remains a documented limitation.
@antfubot antfubot changed the title fix(comark): close inline code inside unclosed link text during auto-close fix(comark): harden auto-close for code spans, math, escapes, and link text Jun 10, 2026
@antfubot antfubot marked this pull request as ready for review June 10, 2026 02:35

@antfu antfu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed

@atinux atinux requested a review from farnabaz June 10, 2026 08:14
@vercel

vercel Bot commented Jun 10, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
comark-sveltekit Ready Ready Preview Jun 10, 2026 8:16am

@pkg-pr-new

pkg-pr-new Bot commented Jun 10, 2026

Copy link
Copy Markdown

Open in StackBlitz

comark

npm i https://pkg.pr.new/comark@233

@comark/ansi

npm i https://pkg.pr.new/@comark/ansi@233

@comark/html

npm i https://pkg.pr.new/@comark/html@233

@comark/nuxt

npm i https://pkg.pr.new/@comark/nuxt@233

@comark/react

npm i https://pkg.pr.new/@comark/react@233

@comark/svelte

npm i https://pkg.pr.new/@comark/svelte@233

@comark/vue

npm i https://pkg.pr.new/@comark/vue@233

commit: f56a714

@farnabaz farnabaz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@farnabaz farnabaz merged commit c810aab into comarkdown:main Jun 10, 2026
3 of 11 checks passed
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.

3 participants