Skip to content
Merged
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
45 changes: 45 additions & 0 deletions packages/markdown-core/src/fences.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Tests fenced-code-block span scanning used to keep chunk breaks out of code blocks.
import { describe, expect, it } from "vitest";
import { isSafeFenceBreak, parseFenceSpans } from "./fences.js";

describe("parseFenceSpans closing-fence rules", () => {
it("treats a marker line with trailing text as code content, not a closing fence", () => {
// CommonMark: a closing fence may be followed only by whitespace, so "``` not a close" is code
// content and the block stays open until the real closing fence. Reporting an interior offset
// as a safe break would let a chunker split inside the code block.
const text = "```\ncode\n``` not a close\nmore code\n```\n";
const spans = parseFenceSpans(text);

expect(spans).toHaveLength(1);
expect(isSafeFenceBreak(spans, text.indexOf("more code") + 1)).toBe(false);
});

it("does not close on non-space/tab whitespace", () => {
for (const suffix of ["\u00a0", "\v", "\f"]) {
const text = `\`\`\`\ncode\n\`\`\`${suffix}\nmore code\n`;
const spans = parseFenceSpans(text);

expect(spans).toHaveLength(1);
expect(spans[0]?.end).toBe(text.length);
expect(isSafeFenceBreak(spans, text.indexOf("more code") + 1)).toBe(false);
}
});

it("closes fences with CRLF line endings", () => {
const text = "```\r\ncode\r\n```\r\nafter\r\n";
const spans = parseFenceSpans(text);

expect(spans).toHaveLength(1);
expect(isSafeFenceBreak(spans, text.indexOf("after") + 1)).toBe(true);
});

it("still closes on a bare fence, a longer same-marker fence, and keeps an opener info string", () => {
expect(parseFenceSpans("```\ncode\n```\nafter\n")).toHaveLength(1);
expect(parseFenceSpans("```\ncode\n````` \nafter\n")).toHaveLength(1);
expect(parseFenceSpans("```python\nx = 1\n```\n")).toHaveLength(1);

const closed = "```\ncode\n```\nafter\n";
const spans = parseFenceSpans(closed);
expect(isSafeFenceBreak(spans, closed.indexOf("after") + 1)).toBe(true);
});
});
12 changes: 8 additions & 4 deletions packages/markdown-core/src/fences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function scanFenceSpans(
while (offset <= buffer.length) {
const nextNewline = buffer.indexOf("\n", offset);
const lineEnd = nextNewline === -1 ? buffer.length : nextNewline;
const line = buffer.slice(offset, lineEnd);
const line = buffer.slice(offset, lineEnd).replace(/\r$/, "");

const match = line.match(/^( {0,3})(`{3,}|~{3,})(.*)$/);
if (match && (offset > 0 || startsAtLineStart)) {
Expand All @@ -58,9 +58,13 @@ export function scanFenceSpans(
marker,
indent,
};
} else if (open.markerChar === markerChar && markerLen >= open.markerLen) {
// CommonMark allows a closing fence to be longer than the opener, but
// it must use the same marker character to avoid crossing fence kinds.
} else if (
open.markerChar === markerChar &&
markerLen >= open.markerLen &&
/^[ \t]*$/.test(match[3])
) {
// CommonMark permits only spaces or tabs after a closing fence. A marker line carrying
// other trailing text is code content, not a close, so it must not end the block.
const end = lineEnd;
spans.push({
start: open.start,
Expand Down
Loading