fix(markdown): respect fenced block closers

This commit is contained in:
scotttong committed 2026-08-07 18:06:37 -07:00
1 parent 8f84a23aa5
commit 945b4102f3
2 files changed
+11 -2

No files matched your search

@@ -62,6 +62,11 @@ describe("normalizeMidLineHeadings", () => {
expect(normalizeMidLineHeadings(body)).toBe(body);
});
it("does not close a fence marker that has trailing content", () => {
const body = "```markdown\n``` still code\ncode ## comment\n```";
expect(normalizeMidLineHeadings(body)).toBe(body);
});
it("is a no-op when there is no `#` at all", () => {
const body = "Just some prose with no markers.";
expect(normalizeMidLineHeadings(body)).toBe(body);
+6 -2
View File
@@ -7,7 +7,7 @@ export function normalizeEscapedLineBreaks(value: string): string {
.replace(/\\r/g, "\n");
}
const FENCE_LINE = /^(\s{0,3})(```+|~~~+)/;
const FENCE_LINE = /^(\s{0,3})(```+|~~~+)(.*)$/;
// A mid-line ATX heading marker: 1–6 `#`, a space, then heading text. The
// capture keeps the char immediately before the run so we can tell a real
// (collapsed) heading from an inline `#` token like `C#`/`F#`.
@@ -53,7 +53,11 @@ export function normalizeMidLineHeadings(value: string): string {
inFence = true;
fenceChar = marker[0]!;
fenceLength = marker.length;
} else if (marker[0] === fenceChar && marker.length >= fenceLength) {
} else if (
marker[0] === fenceChar
&& marker.length >= fenceLength
&& fenceMatch[3]!.trim().length === 0
) {
inFence = false;
fenceChar = "";
fenceLength = 0;