Skip to content

Commit d708864

Browse files
authored
fix: skip asterisks inside math blocks in remend (#306)
* fix: skip asterisks inside math blocks in remend * chore: add changeset for remend asterisk fix
1 parent 1275862 commit d708864

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

.changeset/tidy-cases-cut.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"remend": patch
3+
---
4+
5+
Fix asterisks inside math blocks being incorrectly treated as italic markers

packages/remend/__tests__/katex.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,26 @@ describe("math blocks with underscores", () => {
178178
expect(remend(str)).toBe("$$formula$$ and code `$$` and $$incomplete$$");
179179
});
180180
});
181+
182+
describe("math blocks with asterisks", () => {
183+
it("should not complete asterisks within block math", () => {
184+
const text = "$$\\mathbf{w}^{*}$$";
185+
expect(remend(text)).toBe(text);
186+
});
187+
188+
it("should not complete asterisks in complex math expressions", () => {
189+
const text =
190+
"$$\n\\mathbf{w}^{*} = \\underset{\\|\\mathbf{w}\\|=1}{\\arg\\max} \\;\\; \\mathbf{w}^T S \\mathbf{w}\n$$";
191+
expect(remend(text)).toBe(text);
192+
});
193+
194+
it("should handle asterisks outside math blocks normally", () => {
195+
const text = "Text with *italic* and math $$x^{*}$$";
196+
expect(remend(text)).toBe(text);
197+
});
198+
199+
it("should complete italic asterisk outside math but not inside", () => {
200+
const text = "Start *italic with $$x^{*}$$";
201+
expect(remend(text)).toBe("Start *italic with $$x^{*}$$*");
202+
});
203+
});

packages/remend/src/emphasis-handlers.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ const shouldSkipAsterisk = (
5757
return true;
5858
}
5959

60+
// Skip if within math block (only check if text has dollar signs)
61+
const hasMathBlocks = text.includes("$");
62+
if (hasMathBlocks && isWithinMathBlock(text, index)) {
63+
return true;
64+
}
65+
6066
// Special handling for *** sequences
6167
// If this is the first * in ***, don't skip it - it can close a single * italic
6268
// Example: **bold and *italic*** should count the first * of *** as closing the italic
@@ -313,7 +319,8 @@ const findFirstSingleAsteriskIndex = (text: string): number => {
313319
text[i] === "*" &&
314320
text[i - 1] !== "*" &&
315321
text[i + 1] !== "*" &&
316-
text[i - 1] !== "\\"
322+
text[i - 1] !== "\\" &&
323+
!isWithinMathBlock(text, i)
317324
) {
318325
// Check if asterisk is word-internal (between word characters)
319326
const prevChar = i > 0 ? text[i - 1] : "";

0 commit comments

Comments
 (0)