Skip to content

Commit 45e1d3b

Browse files
committed
fix: preserve escaped disclosure tags
1 parent be1f7c7 commit 45e1d3b

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎src/infra/outbound/markdown-details.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,9 @@ describe("flattenMarkdownDetails", () => {
112112
const markdown = "<details-widget>body</details-widget>";
113113
expect(flattenMarkdownDetails(markdown)).toBe(markdown);
114114
});
115+
116+
it("leaves backslash-escaped disclosure tags unchanged", () => {
117+
const markdown = "\\<details>literal\\</details>";
118+
expect(flattenMarkdownDetails(markdown)).toBe(markdown);
119+
});
115120
});

‎src/infra/outbound/markdown-details.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ type DetailsNode =
1111

1212
const DETAILS_TAG_RE = /<\s*(\/?)\s*(details|summary)(?=\s|>)[^>]*>/gi;
1313

14+
function isEscapedMarkdownCharacter(text: string, index: number): boolean {
15+
let backslashes = 0;
16+
for (let cursor = index - 1; cursor >= 0 && text.charAt(cursor) === "\\"; cursor -= 1) {
17+
backslashes += 1;
18+
}
19+
return backslashes % 2 === 1;
20+
}
21+
1422
function appendNode(target: DetailsNode[], node: DetailsNode): void {
1523
const previous = target.at(-1);
1624
if (typeof previous === "string" && typeof node === "string") {
@@ -156,7 +164,7 @@ export function flattenMarkdownDetails(text: string): string {
156164
let cursor = 0;
157165
for (const match of text.matchAll(DETAILS_TAG_RE)) {
158166
const start = match.index ?? 0;
159-
if (isInsideCode(start, codeRegions)) {
167+
if (isEscapedMarkdownCharacter(text, start) || isInsideCode(start, codeRegions)) {
160168
continue;
161169
}
162170

0 commit comments

Comments
 (0)