Skip to content

Commit cb4746b

Browse files
committed
Improve a725579
1 parent 5dd3199 commit cb4746b

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

packages/remend/__tests__/inline-code.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ describe("inline code formatting (`)", () => {
6464
});
6565
});
6666

67+
describe("escaped backticks in inline code", () => {
68+
it("should not treat escaped backticks as code delimiters", () => {
69+
// \` is not a real backtick delimiter, so **bold should still be completed
70+
expect(remend("\\`not code\\` **bold")).toBe("\\`not code\\` **bold**");
71+
});
72+
73+
it("should complete emphasis when only escaped backticks are present", () => {
74+
expect(remend("\\` *italic")).toBe("\\` *italic*");
75+
});
76+
});
77+
6778
describe("emphasis markers inside inline code spans should not leak", () => {
6879
it("should not complete bold/italic/strikethrough if they are inside inline code", () => {
6980
expect(remend("`**bold`")).toBe("`**bold`");

packages/remend/__tests__/mixed-formatting.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ describe("mixed formatting", () => {
4848
});
4949

5050
it("should handle deeply nested incomplete formatting", () => {
51-
// Formats are closed in the order they're processed
51+
// Formats are closed in the order they're processed.
52+
// The ~~ isn't closed because it ends up inside the completed inline code span (`code ~~strike`),
53+
// so remend correctly leaves it alone.
5254
expect(remend("**bold *italic `code ~~strike")).toBe(
5355
"**bold *italic `code ~~strike*`"
5456
);

packages/remend/src/code-block-utils.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ export const isInsideCodeBlock = (text: string, position: number): boolean => {
55
let inMultilineCode = false;
66

77
for (let i = 0; i < position; i += 1) {
8+
// Skip escaped backticks
9+
if (text[i] === "\\" && i + 1 < text.length && text[i + 1] === "`") {
10+
i += 1;
11+
continue;
12+
}
13+
814
// Check for triple backticks (multiline code blocks)
915
if (text.substring(i, i + 3) === "```") {
1016
inMultilineCode = !inMultilineCode;
@@ -30,10 +36,15 @@ export const isPartOfTripleBacktick = (text: string, i: number): boolean => {
3036
return isTripleStart || isTripleMiddle || isTripleEnd;
3137
};
3238

33-
// Counts single backticks that are not part of triple backticks
39+
// Counts single backticks that are not part of triple backticks or escaped
3440
export const countSingleBackticks = (text: string): number => {
3541
let count = 0;
3642
for (let i = 0; i < text.length; i += 1) {
43+
// Skip escaped backticks
44+
if (text[i] === "\\" && i + 1 < text.length && text[i + 1] === "`") {
45+
i += 1;
46+
continue;
47+
}
3748
if (text[i] === "`" && !isPartOfTripleBacktick(text, i)) {
3849
count += 1;
3950
}
@@ -52,6 +63,12 @@ export const isWithinCompleteInlineCode = (
5263
let inlineCodeStart = -1;
5364

5465
for (let i = 0; i < text.length; i += 1) {
66+
// Skip escaped backticks
67+
if (text[i] === "\\" && i + 1 < text.length && text[i + 1] === "`") {
68+
i += 1;
69+
continue;
70+
}
71+
5572
// Check for triple backticks (multiline code blocks)
5673
if (text.substring(i, i + 3) === "```") {
5774
inMultilineCode = !inMultilineCode;

packages/remend/src/comparison-operator-handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// AI models frequently generate list items like "- > 25: expensive" where
33
// the > is a "greater than" operator, not a blockquote marker.
44

5-
import { isWithinCodeBlock } from "./utils";
5+
import { isInsideCodeBlock } from "./code-block-utils";
66

77
// Match list items where > appears as a comparison operator followed by a digit
88
// Pattern: list marker (-, *, +, or 1.) followed by > then optional = and a digit
@@ -23,7 +23,7 @@ export const handleComparisonOperators = (text: string): string => {
2323
LIST_COMPARISON_PATTERN,
2424
(match, prefix, suffix, offset) => {
2525
// Don't escape inside code blocks
26-
if (isWithinCodeBlock(text, offset)) {
26+
if (isInsideCodeBlock(text, offset)) {
2727
return match;
2828
}
2929

packages/remend/src/emphasis-handlers.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,10 @@ export const handleIncompleteSingleUnderscoreItalic = (
677677
return text;
678678
}
679679

680-
if (isWithinCompleteInlineCode(text, firstSingleUnderscoreIndex)) {
680+
if (
681+
isInsideCodeBlock(text, firstSingleUnderscoreIndex) ||
682+
isWithinCompleteInlineCode(text, firstSingleUnderscoreIndex)
683+
) {
681684
return text;
682685
}
683686

0 commit comments

Comments
 (0)