Skip to content

Commit e60e8f4

Browse files
fix(mobile): match carried reference labels with surrounding spaces
A label written as [ x ] did not match a fold's own [x] definition, so the outside definition was prepended and took over the fold's link. Trim labels and normalize bracket contents in the fragment the same way, as CommonMark does, before the mention and self-definition checks. AI-assisted; verified with the tests and tools listed above.
1 parent c3f0a22 commit e60e8f4

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ describe("splitMarkdownDetails", () => {
133133
0,
134134
{ "part > rest": "https://example.com/a", "part rest": "https://example.com/b" },
135135
],
136-
// A fold that defines a label keeps its own definition.
136+
// A fold that defines a label keeps its own definition, however the label is spaced.
137137
[
138-
"Top\n\n<details>\n<summary>S</summary>\n\nSee [x].\n\n[x]: https://second\n\n</details>\n\n[x]: https://first",
138+
"[ x ]: https://outside.example\n\n<details>\n<summary>S</summary>\n\nSee [ x ].\n\n[x]: https://inside.example\n\n</details>",
139139
1,
140-
{ x: "https://second" },
140+
{ x: "https://inside.example" },
141141
],
142142
] as const) {
143143
const segment = splitMarkdownDetails(markdown)[pick];

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ function referenceDefinition(written: string, quotes: number): MarkdownReference
295295
let end = 1;
296296
while (end < source.length && source[end] !== "]") end += source[end] === "\\" ? 2 : 1;
297297
if (source[0] !== "[" || end >= source.length) return null;
298-
return { label: source.slice(1, end).replace(/\s+/g, " ").toLowerCase(), source };
298+
return { label: source.slice(1, end).replace(/\s+/g, " ").trim().toLowerCase(), source };
299299
}
300300

301301
/** Read after HTML flattening, which exposes definitions a raw HTML block hides from the parse. */
@@ -324,7 +324,12 @@ export function withReferenceDefinitions(
324324
definitions: ReadonlyArray<MarkdownReferenceDefinition>,
325325
): string {
326326
if (definitions.length === 0 || !markdown.includes("[")) return markdown;
327-
const text = markdown.toLowerCase().replace(/\s+/g, " ");
327+
// Labels match as CommonMark matches them: case-insensitive, whitespace collapsed and trimmed.
328+
const text = markdown
329+
.toLowerCase()
330+
.replace(/\s+/g, " ")
331+
.replace(/\[ /g, "[")
332+
.replace(/ \]/g, "]");
328333
// A label the fragment defines itself keeps its own definition, as in the whole reply.
329334
const used = definitions.filter(
330335
(definition) =>

0 commit comments

Comments
 (0)