Skip to content
Open
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
68 changes: 66 additions & 2 deletions frontend/src/lib/services/numericListFenceNesting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe("numeric list fence nesting", () => {
}
});

it("accepts CommonMark closing indentation after a column-zero opening fence", () => {
it("preserves bare fences past CommonMark closing indentation as content after a column-zero opening fence", () => {
for (const fenceMarker of ["```", "~~~"]) {
for (const closingOffset of [1, 2, 3, 4]) {
const closingIndentation = " ".repeat(3 + closingOffset);
Expand All @@ -117,7 +117,71 @@ describe("numeric list fence nesting", () => {

expect(nestedCodeBlocks).toHaveLength(1);
expect(nestedCodeBlocks[0].textContent).toContain("template body");
expect(nestedCodeBlocks[0].textContent?.includes(fenceMarker)).toBe(closingOffset === 4);
expect(nestedCodeBlocks[0].textContent?.includes(fenceMarker)).toBe(true);
expect(renderedContainer.querySelectorAll(":scope > pre")).toHaveLength(0);
}
}
}
});

it("keeps an indented bare fence used as content nested inside the list item", () => {
const markdown = ["1. Example of nested:", "```text", " ```", "more", "```"].join("\n");

for (const isStreaming of [false, true]) {
const renderedContainer = renderMarkdown(markdown, isStreaming);
const nestedCodeBlocks = renderedContainer.querySelectorAll("ol > li pre > code");

expect(nestedCodeBlocks).toHaveLength(1);
expect(nestedCodeBlocks[0].textContent).toContain("```\nmore");
expect(renderedContainer.querySelectorAll(":scope > pre")).toHaveLength(0);
expect(renderedContainer.querySelectorAll(":scope > p")).toHaveLength(0);
}
});

it("keeps indented bare-fence content nested for deep numeric markers", () => {
for (const numericListMarker of ["14.", "123."]) {
const requiredIndentation = " ".repeat(numericListMarker.length + 1);
const markdown = [
`${numericListMarker} Item`,
"```text",
`${requiredIndentation}\`\`\``,
"more",
"```",
].join("\n");

for (const isStreaming of [false, true]) {
const renderedContainer = renderMarkdown(markdown, isStreaming);
const nestedCodeBlocks = renderedContainer.querySelectorAll("ol > li pre > code");
const orderedLists = renderedContainer.querySelectorAll("ol");

expect(orderedLists).toHaveLength(1);
expect(nestedCodeBlocks).toHaveLength(1);
expect(nestedCodeBlocks[0].textContent).toContain("```\nmore");
expect(renderedContainer.querySelectorAll(":scope > pre")).toHaveLength(0);
expect(renderedContainer.querySelectorAll(":scope > p")).toHaveLength(0);
}
}
});

it("still closes the block at CommonMark-valid closing indentation (offset 0 to 3)", () => {
for (const fenceMarker of ["```", "~~~"]) {
for (const closingSpaces of [0, 1, 2, 3]) {
const markdown = [
"1. Template example",
`${fenceMarker}text`,
"template body",
`${" ".repeat(closingSpaces)}${fenceMarker}`,
].join("\n");

for (const isStreaming of [false, true]) {
const renderedContainer = renderMarkdown(markdown, isStreaming);
const nestedCodeBlocks = renderedContainer.querySelectorAll("ol > li pre > code");

expect(nestedCodeBlocks).toHaveLength(1);
expect(nestedCodeBlocks[0].textContent).toContain("template body");
expect(nestedCodeBlocks[0].textContent?.includes(fenceMarker)).toBe(false);
expect(renderedContainer.querySelectorAll(":scope > pre")).toHaveLength(0);
expect(renderedContainer.querySelectorAll(":scope > p")).toHaveLength(0);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/lib/services/numericListFenceNesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,9 @@ export function nestNumericListFences(
);
targetIndentation = awaitedContinuationIndentation;
maximumSourceIndentation =
awaitedContinuationIndentation + COMMONMARK_MAX_FENCE_INDENTATION;
awaitedContinuationIndentation +
COMMONMARK_MAX_FENCE_INDENTATION -
bodyIndentation.length;
fenceState.open(fenceCandidate.marker);
awaitedContinuationIndentation = null;
nestedLines.push(
Expand Down