Skip to content

Commit 2d928e4

Browse files
committed
fix(pandoc): recognize fence markers indented up to 3 spaces
markdownWithExtractedHeading scanned every notebook cell, not just code cells. Code cells always emit fences at column 0, but markdown cells carry arbitrary user-authored content, where a fenced block nested in a list item can have its fence markers indented while a line inside stays at column 0. That line was previously invisible to fence tracking and could still be mistaken for a heading.
1 parent 6b309f2 commit 2d928e4

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

src/core/pandoc/pandoc-partition.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ export function partitionMarkdown(markdown: string): PartitionedMarkdown {
7272
};
7373
}
7474

75-
// CommonMark also allows fences indented up to 3 spaces; intentionally
76-
// unsupported here since the fences this scanner sees (code cell source)
77-
// always start at column 0.
78-
const kFenceOpenRegex = /^(`{3,}|~{3,})/;
75+
// CommonMark allows fence markers indented up to 3 spaces (content lines
76+
// inside the fence can have any indentation, including none).
77+
const kFenceOpenRegex = /^ {0,3}(`{3,}|~{3,})/;
78+
const kFenceCloseRegex = /^ {0,3}(`{3,}|~{3,})\s*$/;
7979

8080
export function markdownWithExtractedHeading(markdown: string) {
8181
const mdLines: string[] = [];
@@ -90,7 +90,7 @@ export function markdownWithExtractedHeading(markdown: string) {
9090
// never mistaken for the document heading.
9191
if (fence) {
9292
mdLines.push(line);
93-
const closeMatch = line.match(/^(`{3,}|~{3,})\s*$/);
93+
const closeMatch = line.match(kFenceCloseRegex);
9494
if (
9595
closeMatch && closeMatch[1][0] === fence.char &&
9696
closeMatch[1].length >= fence.length

tests/unit/pandoc-partition.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,22 @@ unitTest(
145145
},
146146
);
147147

148+
// deno-lint-ignore require-await
149+
unitTest(
150+
"markdownWithExtractedHeading - ignores an ATX-heading-like line inside a fenced code block whose fence markers are indented up to 3 spaces",
151+
async () => {
152+
const markdown = [
153+
"- a list item with a nested code block",
154+
"",
155+
" ```python",
156+
"# not a heading",
157+
" ```",
158+
].join("\n");
159+
const result = markdownWithExtractedHeading(markdown);
160+
assertEquals(result.headingText, undefined);
161+
},
162+
);
163+
148164
// deno-lint-ignore require-await
149165
unitTest(
150166
"markdownWithExtractedHeading - extracts a setext-style heading",

0 commit comments

Comments
 (0)