Skip to content

Commit 34eb8b7

Browse files
committed
fix(mobile): match fence quote depth and validate definition destinations
Only a fence at the opener's quote depth closes it, so deeper quoted fences cannot end outer examples. Reference definitions require a valid destination, so headings after bare labels are not copied.
1 parent 5ea81e7 commit 34eb8b7

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

‎apps/mobile/src/lib/mermaidBlocks.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,34 @@ describe("splitMarkdownWithMermaid", () => {
160160
expect(tail.markdown).not.toContain("[guide]: https://example.com");
161161
}
162162
});
163+
164+
it("does not let a deeper quoted fence close an outer example", () => {
165+
const markdown = ["```md", "> ```mermaid", "> A --> B", "> ```", "extra", "```"].join("\n");
166+
const segments = splitMarkdownWithMermaid(markdown);
167+
expect(segments.map((segment) => segment.kind)).toEqual(["markdown"]);
168+
expect(segments[0]).toMatchObject({ kind: "markdown", markdown });
169+
});
170+
171+
it("does not copy a label followed by a heading as a definition", () => {
172+
const segments = splitMarkdownWithMermaid(
173+
["[note]:", "# heading", "", "```mermaid", "A --> B", "```", "", "text"].join("\n"),
174+
);
175+
const tail = segments[segments.length - 1];
176+
expect(tail).toMatchObject({ kind: "markdown" });
177+
if (tail?.kind === "markdown") {
178+
expect(tail.markdown).toContain("text");
179+
expect(tail.markdown).not.toContain("[note]:");
180+
}
181+
});
182+
183+
it("keeps single-line definitions with titles", () => {
184+
const segments = splitMarkdownWithMermaid(
185+
['[a]: /url "title"', "", "```mermaid", "A --> B", "```", "", "[Open][a]"].join("\n"),
186+
);
187+
const tail = segments[segments.length - 1];
188+
expect(tail).toMatchObject({ kind: "markdown" });
189+
if (tail?.kind === "markdown") {
190+
expect(tail.markdown).toContain('[a]: /url "title"');
191+
}
192+
});
163193
});

‎apps/mobile/src/lib/mermaidBlocks.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ export type MermaidMarkdownSegment =
1111

1212
const FENCE_RE = /^ {0,3}(`{3,}|~{3,})[ \t]*([^\n]*)\n?/;
1313
const QUOTE_RE = /^(?: {0,3}>[ \t]?)+/;
14-
const DEFINITION_RE = /^ {0,3}\[[^\]\n]+\]:[ \t]*\S.*$/;
14+
const DESTINATION_RE = "(?:<[^<>\\n]*>|[^\\s<>\\n]+)";
15+
const TITLE_RE = "(?:\"[^\"]*\"|'[^']*'|\\([^()\\n]*\\))";
16+
const DEFINITION_RE = new RegExp(
17+
`^ {0,3}\\[[^\\]\\n]+\\]:[ \\t]*${DESTINATION_RE}(?:[ \\t]+${TITLE_RE})?[ \\t]*$`,
18+
);
1519
const DEFINITION_LABEL_RE = /^ {0,3}\[[^\]\n]+\]:[ \t]*$/;
20+
const DESTINATION_ONLY_RE = new RegExp(`^[ \\t]*${DESTINATION_RE}(?:[ \\t]+${TITLE_RE})?[ \\t]*$`);
1621

1722
function stripQuote(line: string): string {
1823
return line.replace(QUOTE_RE, "");
@@ -46,7 +51,11 @@ function closerIndex(
4651
const char = fence[0] === "`" ? "`" : "~";
4752
const closeRe = new RegExp(`^ {0,3}${char}{${fence.length},}[ \\t]*$`);
4853
for (let scan = from; scan < lines.length; scan += 1) {
49-
if ((depths[scan] ?? 0) < minDepth) return -1;
54+
const depth = depths[scan] ?? 0;
55+
// A fenced block ends when its blockquote ends, and only a fence at the
56+
// opener's quote depth can close it.
57+
if (depth < minDepth) return -1;
58+
if (depth !== minDepth) continue;
5059
if (closeRe.test(lines[scan] ?? "")) return scan;
5160
}
5261
return -1;
@@ -82,7 +91,7 @@ function collectDefinitions(markdown: string): string[] {
8291
else if (
8392
DEFINITION_LABEL_RE.test(line) &&
8493
(depths[index + 1] ?? 0) === (depths[index] ?? 0) &&
85-
/^[ \t]*\S/.test(lines[index + 1] ?? "") &&
94+
DESTINATION_ONLY_RE.test(lines[index + 1] ?? "") &&
8695
!FENCE_RE.test(lines[index + 1] ?? "")
8796
) {
8897
found.push(`${line}\n${lines[index + 1]}`);

0 commit comments

Comments
 (0)