Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions extensions/user-input-fold/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,28 @@ test("multiple fenced blocks fold per block while prose shares one budget", () =
);
});

test("nested fences require the matching marker length and support tildes", () => {
const message = [
"````markdown",
"before",
"```js",
"nested",
"```",
...Array.from({ length: 20 }, (_, i) => `line-${i}`),
"````",
"~~~text",
...Array.from({ length: 10 }, (_, i) => `tilde-${i}`),
"~~~",
].join("\n");
const out = foldUserMessage(message);
assert.ok(out.includes("```js"));
assert.ok(out.includes("nested"));
assert.ok(out.includes("````markdown"));
assert.ok(out.includes("~~~text"));
assert.ok(out.includes("~~~"));
assert.equal((out.match(/````/g) ?? []).length % 2, 0);
});

test("CRLF messages fold without losing their line endings", () => {
const message = Array.from(
{ length: 21 },
Expand Down
22 changes: 17 additions & 5 deletions extensions/user-input-fold/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ type Segment =
| { kind: "prose"; lines: string[] }
| { kind: "code"; open: string; content: string[]; close: string };

const FENCE_OPEN = /^ {0,3}`{3,}/;
const FENCE_CLOSE = /^ {0,3}`{3,}[ \t]*$/;
const FENCE_OPEN = /^ {0,3}(`{3,}|~{3,})/;
const FENCE_CLOSE = /^ {0,3}([`~]+)[ \t]*$/;

function countLines(markdown: string) {
const parts = markdown.split("\n");
Expand All @@ -60,7 +60,8 @@ function parseSegments(lines: string[]): Segment[] {
let prose: string[] = [];
let i = 0;
while (i < lines.length) {
if (!FENCE_OPEN.test(lines[i])) {
const opening = lines[i].match(FENCE_OPEN);
if (!opening) {
prose.push(lines[i]);
i += 1;
continue;
Expand All @@ -70,12 +71,23 @@ function parseSegments(lines: string[]): Segment[] {
prose = [];
}
const open = lines[i];
const fence = opening[1];
const fenceChar = fence[0];
const fenceLength = fence.length;
const content: string[] = [];
let close: string | undefined;
let j = i + 1;
while (j < lines.length && close === undefined) {
if (FENCE_CLOSE.test(lines[j])) close = lines[j];
else content.push(lines[j]);
const closing = lines[j].match(FENCE_CLOSE)?.[1];
if (
closing &&
closing[0] === fenceChar &&
closing.length >= fenceLength
) {
close = lines[j];
} else {
content.push(lines[j]);
}
j += 1;
}
if (close === undefined) {
Expand Down