Bug Description
remend recognizes $...$ and $$...$$ as math contexts when deciding whether * or _ should be ignored by emphasis completion. It does not currently recognize LaTeX paren-style inline math (\(...\)) or bracket-style display math (\[...\]).
As a result, emphasis markers inside these math expressions can be counted as Markdown emphasis delimiters. This can cause remend to append a spurious _ or *, or to leave surrounding emphasis incomplete, even though the marker was part of a math expression.
This commonly appears with subscripts such as x_{t+1}, where the _ is not word-internal because it is followed by {.
Steps to Reproduce
import remend from "remend";
const text = [
String.raw`> Given tokens \(x_1, ..., x_t\), maximize \(P(x_{t+1} | x_1, ..., x_t)\)`,
"",
"**2. Instruction Tuning (SFT)**",
"- Dataset size is much smaller than pre-training",
].join("\n");
// Simulates a streaming snapshot before the heading has fully arrived.
const partial = text.slice(0, text.indexOf("(SFT)") + "(SFT".length);
console.log(remend(partial));
Expected Behavior
> Given tokens \(x_1, ..., x_t\), maximize \(P(x_{t+1} | x_1, ..., x_t)\)
**2. Instruction Tuning (SFT**
The incomplete bold span is completed cleanly, and math subscripts inside \(...\) are ignored by emphasis completion.
Actual Behavior
> Given tokens \(x_1, ..., x_t\), maximize \(P(x_{t+1} | x_1, ..., x_t)\)
**2. Instruction Tuning (SFT**_
A spurious _ is appended because the _ in x_{t+1} is counted as an unclosed single-underscore italic delimiter.
The same issue also affects complete documents:
console.log(remend(String.raw`\(P(x_{t+1})\)
plain trailing text.`));
Expected:
\(P(x_{t+1})\)
plain trailing text.
Actual:
\(P(x_{t+1})\)
plain trailing text._
And bracket-style math has the same behavior:
console.log(remend(String.raw`\[
P(x_{t+1} | x_t)
\]`));
Expected:
Actual:
Impact
This affects streamed Markdown that contains LaTeX math using \(...\) or \[...\], which is common in model outputs. The visible result is extra Markdown syntax appearing in unrelated text after the math expression.
Root Cause
isWithinMathBlock currently detects $...$ and $$...$$, but not \(...\) or \[...\].
The emphasis handlers rely on this math-context detection to skip * and _ inside math. Since LaTeX paren/bracket math is not recognized as math, markers inside those expressions are treated as normal Markdown emphasis markers.
Environment
remend: 1.3.0
- Observed via
streamdown: 2.5.0
Proposed Fix
Recognize LaTeX paren-style and bracket-style math delimiters as math contexts:
\( opens inline math
\) closes inline math
\[ opens display math
\] closes display math
The emphasis marker skip logic should also treat these delimiters as a reason to check math context, not only dollar-based math.
Bug Description
remendrecognizes$...$and$$...$$as math contexts when deciding whether*or_should be ignored by emphasis completion. It does not currently recognize LaTeX paren-style inline math (\(...\)) or bracket-style display math (\[...\]).As a result, emphasis markers inside these math expressions can be counted as Markdown emphasis delimiters. This can cause
remendto append a spurious_or*, or to leave surrounding emphasis incomplete, even though the marker was part of a math expression.This commonly appears with subscripts such as
x_{t+1}, where the_is not word-internal because it is followed by{.Steps to Reproduce
Expected Behavior
The incomplete bold span is completed cleanly, and math subscripts inside
\(...\)are ignored by emphasis completion.Actual Behavior
A spurious
_is appended because the_inx_{t+1}is counted as an unclosed single-underscore italic delimiter.The same issue also affects complete documents:
Expected:
Actual:
And bracket-style math has the same behavior:
Expected:
Actual:
Impact
This affects streamed Markdown that contains LaTeX math using
\(...\)or\[...\], which is common in model outputs. The visible result is extra Markdown syntax appearing in unrelated text after the math expression.Root Cause
isWithinMathBlockcurrently detects$...$and$$...$$, but not\(...\)or\[...\].The emphasis handlers rely on this math-context detection to skip
*and_inside math. Since LaTeX paren/bracket math is not recognized as math, markers inside those expressions are treated as normal Markdown emphasis markers.Environment
remend: 1.3.0streamdown: 2.5.0Proposed Fix
Recognize LaTeX paren-style and bracket-style math delimiters as math contexts:
\(opens inline math\)closes inline math\[opens display math\]closes display mathThe emphasis marker skip logic should also treat these delimiters as a reason to check math context, not only dollar-based math.