From 61d3772b68db4f35ef5abe99d598d1ff2ae0375f Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Thu, 17 Sep 2026 07:40:15 +0000 Subject: [PATCH 1/4] fix: reference tasks with short or bracket-leading bodies in snippets The snippet task-reference regex required the task body to start with two non-`[` characters, so a task whose body was one or two characters long, or that opened with a markdown link or an attribute, never got its `[[Page@pos]]` reference injected. Without that reference a task shown in a Linked Mentions widget cannot be toggled: the widget writes state back only through a parent wiki link containing `@`. Replace the two-character lookahead with a negative lookahead for `[[`, which is what "already has a page reference" actually means. --- plugs/index/snippet.test.ts | 46 +++++++++++++++++++++++++++++++++++++ plugs/index/snippet.ts | 7 ++++-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/plugs/index/snippet.test.ts b/plugs/index/snippet.test.ts index 78c9b0d4e3..40820a3716 100644 --- a/plugs/index/snippet.test.ts +++ b/plugs/index/snippet.test.ts @@ -77,3 +77,49 @@ test("extractSnippet neutralizes transclusions into plain links", () => { "[[API]]", ); }); + +test("extractSnippet references a task whose body is shorter than two characters", () => { + // A one- or two-character body is still a task, and without the injected + // `Page@pos` reference it cannot be toggled from a Linked Mentions widget. + const text1 = `* Parent [[Target]] + * [ ] Hi`; + const pos1 = text1.indexOf("* [ ]"); + expect(extractSnippet("test", text1, text1.indexOf("* Parent"))).toEqual( + `* Parent [[Target]]\n * [ ] [[test@${pos1}]] Hi`, + ); + + const text2 = `* Parent [[Target]] + * [ ] H`; + const pos2 = text2.indexOf("* [ ]"); + expect(extractSnippet("test", text2, text2.indexOf("* Parent"))).toEqual( + `* Parent [[Target]]\n * [ ] [[test@${pos2}]] H`, + ); +}); + +test("extractSnippet references a task whose body opens with a bracket", () => { + // Only a leading `[[` means "already referenced"; a markdown link or an + // attribute opens with a single `[` and must still get a reference. + const text1 = `* Parent [[Target]] + * [ ] [Google](https://google.com) look it up`; + const pos1 = text1.indexOf("* [ ]"); + expect(extractSnippet("test", text1, text1.indexOf("* Parent"))).toEqual( + `* Parent [[Target]]\n * [ ] [[test@${pos1}]] [Google](https://google.com) look it up`, + ); + + const text2 = `* Parent [[Target]] + * [ ] [due: 2026-01-01] pay rent`; + const pos2 = text2.indexOf("* [ ]"); + expect(extractSnippet("test", text2, text2.indexOf("* Parent"))).toEqual( + `* Parent [[Target]]\n * [ ] [[test@${pos2}]] [due: 2026-01-01] pay rent`, + ); +}); + +test("extractSnippet leaves an already-referenced task alone", () => { + // (control) A body that opens with `[[` is already a reference; adding a + // second one would point the widget's toggle at the wrong offset. + const text = `* Parent [[Target]] + * [ ] [[test@40]] Already referenced`; + expect(extractSnippet("test", text, text.indexOf("* Parent"))).toEqual( + "* Parent [[Target]]\n * [ ] [[test@40]] Already referenced", + ); +}); diff --git a/plugs/index/snippet.ts b/plugs/index/snippet.ts index 38919acb75..0918816f7d 100644 --- a/plugs/index/snippet.ts +++ b/plugs/index/snippet.ts @@ -88,8 +88,11 @@ export function extractSnippet( break; } - // Find tasks that don't have a page reference, and add one - const taskMatch = line.match(/^(\s*)([*-]\s+\[[^\]]+\]\s+)([^[][^[].+)$/); + // Find tasks that don't have a page reference, and add one. The body only + // counts as already-referenced when it opens with a `[[`; anything else + // (including a one-character body, or a body opening with a markdown link + // or an attribute) still needs a reference to stay toggleable. + const taskMatch = line.match(/^(\s*)([*-]\s+\[[^\]]+\]\s+)((?!\[\[).+)$/); if (taskMatch) { const pos = lineOffsets[i] + taskMatch[1].length; line = `${taskMatch[1] + taskMatch[2]}[[${pageName}@${pos}]] ${taskMatch[3]}`; From 37c30ceed2e3b5088bd18c59eebfb6861aa7f46c Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Thu, 17 Sep 2026 11:47:49 +0000 Subject: [PATCH 2/4] Tests: cover the remaining task-body boundaries in snippet extraction Adds the bracket-past-first-character, leading-transclusion, marker/state, deeper-indent, multi-task offset and whitespace-only body cases, plus three controls: a body opening with two brackets is still skipped, and a trailing carriage return still is. --- plugs/index/snippet.test.ts | 134 ++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) diff --git a/plugs/index/snippet.test.ts b/plugs/index/snippet.test.ts index 40820a3716..e48fbdfb41 100644 --- a/plugs/index/snippet.test.ts +++ b/plugs/index/snippet.test.ts @@ -114,6 +114,112 @@ test("extractSnippet references a task whose body opens with a bracket", () => { ); }); +test("extractSnippet references a task whose body has a bracket past the first character", () => { + // The old two-character guard also rejected bodies whose *second* character + // was a `[`, and short bodies that are nothing but brackets. + const text1 = `* Parent [[Target]] + * [ ] a[b: c] thing`; + const pos1 = text1.indexOf("* [ ]"); + expect(extractSnippet("test", text1, text1.indexOf("* Parent"))).toEqual( + `* Parent [[Target]]\n * [ ] [[test@${pos1}]] a[b: c] thing`, + ); + + const text2 = `* Parent [[Target]] + * [ ] [x`; + const pos2 = text2.indexOf("* [ ]"); + expect(extractSnippet("test", text2, text2.indexOf("* Parent"))).toEqual( + `* Parent [[Target]]\n * [ ] [[test@${pos2}]] [x`, + ); + + const text3 = `* Parent [[Target]] + * [ ] [`; + const pos3 = text3.indexOf("* [ ]"); + expect(extractSnippet("test", text3, text3.indexOf("* Parent"))).toEqual( + `* Parent [[Target]]\n * [ ] [[test@${pos3}]] [`, + ); +}); + +test("extractSnippet references a task whose body starts with a transclusion", () => { + // `![[Other]]` is neutralized into `[[Other]]` further down, which makes the + // line *look* referenced while carrying no `@pos` to write a toggle back to. + // The reference is what this asserts on, not that the line changed. + const text1 = `* Parent [[Target]] + * [ ] ![[Other]] see`; + const pos1 = text1.indexOf("* [ ]"); + expect(extractSnippet("test", text1, text1.indexOf("* Parent"))).toEqual( + `* Parent [[Target]]\n * [ ] [[test@${pos1}]] [[Other]] see`, + ); + + const text2 = `* Parent [[Target]] + * [ ] ![[Other]]`; + const pos2 = text2.indexOf("* [ ]"); + expect(extractSnippet("test", text2, text2.indexOf("* Parent"))).toEqual( + `* Parent [[Target]]\n * [ ] [[test@${pos2}]] [[Other]]`, + ); +}); + +test("extractSnippet references short task bodies for every marker and state", () => { + // The marker and state groups are untouched by the body guard, so a short + // body has to be referenced whatever marker or state it carries. + const dash = `* Parent [[Target]] + - [ ] Hi`; + expect(extractSnippet("test", dash, dash.indexOf("* Parent"))).toEqual( + `* Parent [[Target]]\n - [ ] [[test@${dash.indexOf("- [ ]")}]] Hi`, + ); + + const done = `* Parent [[Target]] + * [x] Hi`; + expect(extractSnippet("test", done, done.indexOf("* Parent"))).toEqual( + `* Parent [[Target]]\n * [x] [[test@${done.indexOf("* [x]")}]] Hi`, + ); + + const custom = `* Parent [[Target]] + * [TODO] Hi`; + expect(extractSnippet("test", custom, custom.indexOf("* Parent"))).toEqual( + `* Parent [[Target]]\n * [TODO] [[test@${custom.indexOf("* [TODO]")}]] Hi`, + ); +}); + +test("extractSnippet references a deeper indented task at its own offset", () => { + // The offset is `lineOffsets[i] + indent`: a wrong one silently toggles + // another task, so the extra indent has to show up in the reference. + const text = `* Parent [[Target]] + * [ ] Hi`; + const pos = text.indexOf("* [ ]"); + expect(pos).toEqual(24); + expect(extractSnippet("test", text, text.indexOf("* Parent"))).toEqual( + `* Parent [[Target]]\n * [ ] [[test@${pos}]] Hi`, + ); +}); + +test("extractSnippet keeps every task offset when it references several", () => { + // Offsets come from the original text, so referencing one line must not + // shift the reference of any line below it. + const text = `* Parent [[Target]] + * [ ] Hi + * [ ] Longer body here + * [ ] H`; + const first = text.indexOf("* [ ] Hi"); + const second = text.indexOf("* [ ] Longer"); + const third = text.lastIndexOf("* [ ] H"); + expect([first, second, third]).toEqual([22, 33, 58]); + expect(extractSnippet("test", text, text.indexOf("* Parent"))).toEqual( + `* Parent [[Target]]\n * [ ] [[test@${first}]] Hi\n * [ ] [[test@${second}]] Longer body here\n * [ ] [[test@${third}]] H`, + ); +}); + +test("extractSnippet references a task whose body is only whitespace", () => { + // A body of pure whitespace was already referenced when it was long enough + // for the old guard (four spaces or more); the body guard only decides how + // much of it is needed, so the short form has to behave the same way. + const text = `* Parent [[Target]] + * [ ]${" "}`; + const pos = text.indexOf("* [ ]"); + expect(extractSnippet("test", text, text.indexOf("* Parent"))).toEqual( + `* Parent [[Target]]\n * [ ] [[test@${pos}]] ${" "}`, + ); +}); + test("extractSnippet leaves an already-referenced task alone", () => { // (control) A body that opens with `[[` is already a reference; adding a // second one would point the widget's toggle at the wrong offset. @@ -123,3 +229,31 @@ test("extractSnippet leaves an already-referenced task alone", () => { "* Parent [[Target]]\n * [ ] [[test@40]] Already referenced", ); }); + +test("extractSnippet skips any body opening with two brackets", () => { + // (control) The guard is a prefix test, not a well-formed-link test: an + // unterminated `[[` and a `[[[` are skipped as well, on both the old and the + // new form. This is what pins that swapping the character class for a + // lookahead did not widen the set of bodies treated as unreferenced. + const text1 = `* Parent [[Target]] + * [ ] [[`; + expect(extractSnippet("test", text1, text1.indexOf("* Parent"))).toEqual( + "* Parent [[Target]]\n * [ ] [[", + ); + + const text2 = `* Parent [[Target]] + * [ ] [[[X]]`; + expect(extractSnippet("test", text2, text2.indexOf("* Parent"))).toEqual( + "* Parent [[Target]]\n * [ ] [[[X]]", + ); +}); + +test("extractSnippet leaves a task with a trailing carriage return alone", () => { + // (control) `.` never matches `\r`, so a CRLF file's task lines are not + // referenced — before or after this change. Pins that the new guard did not + // start matching them either. + const text = `* Parent [[Target]]\n * [ ] H\r`; + expect(extractSnippet("test", text, text.indexOf("* Parent"))).toEqual( + "* Parent [[Target]]\n * [ ] H\r", + ); +}); From e7c1b308b92e53776959cf09d3464b0c992b9c85 Mon Sep 17 00:00:00 2001 From: askalf Date: Fri, 18 Sep 2026 07:44:51 -0400 Subject: [PATCH 3/4] index: keep the task separator from handing a space to the body With more than one space before an existing `[[page@pos]]` reference the greedy separator gave one back, the body then started with a space rather than `[[`, and the task was referenced a second time. The already-referenced check now looks past leading spaces. --- plugs/index/snippet.test.ts | 17 ++++++++++++++--- plugs/index/snippet.ts | 11 ++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/plugs/index/snippet.test.ts b/plugs/index/snippet.test.ts index e48fbdfb41..1717c34fd7 100644 --- a/plugs/index/snippet.test.ts +++ b/plugs/index/snippet.test.ts @@ -78,7 +78,7 @@ test("extractSnippet neutralizes transclusions into plain links", () => { ); }); -test("extractSnippet references a task whose body is shorter than two characters", () => { +test("extractSnippet references one- and two-character task bodies", () => { // A one- or two-character body is still a task, and without the injected // `Page@pos` reference it cannot be toggled from a Linked Mentions widget. const text1 = `* Parent [[Target]] @@ -115,8 +115,8 @@ test("extractSnippet references a task whose body opens with a bracket", () => { }); test("extractSnippet references a task whose body has a bracket past the first character", () => { - // The old two-character guard also rejected bodies whose *second* character - // was a `[`, and short bodies that are nothing but brackets. + // A single bracket, including one after the first character, must not + // suppress the task reference. const text1 = `* Parent [[Target]] * [ ] a[b: c] thing`; const pos1 = text1.indexOf("* [ ]"); @@ -230,6 +230,17 @@ test("extractSnippet leaves an already-referenced task alone", () => { ); }); +test("extractSnippet leaves an already-referenced task alone behind extra spaces", () => { + // The separator is greedy, but it must not hand a space back to the body so + // that the body no longer opens with `[[`: the task would be referenced twice. + const text = `* Parent [[Target]] + * [ ] [[test@40]] Already referenced`; + expect(extractSnippet("test", text, text.indexOf("* Parent"))).toEqual( + "* Parent [[Target]] + * [ ] [[test@40]] Already referenced", + ); +}); + test("extractSnippet skips any body opening with two brackets", () => { // (control) The guard is a prefix test, not a well-formed-link test: an // unterminated `[[` and a `[[[` are skipped as well, on both the old and the diff --git a/plugs/index/snippet.ts b/plugs/index/snippet.ts index 0918816f7d..314e5ffb53 100644 --- a/plugs/index/snippet.ts +++ b/plugs/index/snippet.ts @@ -88,11 +88,12 @@ export function extractSnippet( break; } - // Find tasks that don't have a page reference, and add one. The body only - // counts as already-referenced when it opens with a `[[`; anything else - // (including a one-character body, or a body opening with a markdown link - // or an attribute) still needs a reference to stay toggleable. - const taskMatch = line.match(/^(\s*)([*-]\s+\[[^\]]+\]\s+)((?!\[\[).+)$/); + // Find tasks that don't have a page reference, and add one. A body is + // already referenced only when it opens with `[[`, after any run of + // spaces: the separator must not give a space back to the body to get + // past the check. A one-character body, a markdown link or an attribute + // still needs a reference to stay toggleable. + const taskMatch = line.match(/^(\s*)([*-]\s+\[[^\]]+\]\s+)((?!\s*\[\[).+)$/); if (taskMatch) { const pos = lineOffsets[i] + taskMatch[1].length; line = `${taskMatch[1] + taskMatch[2]}[[${pageName}@${pos}]] ${taskMatch[3]}`; From 4822a68179fd400230702365b1feb20561249938 Mon Sep 17 00:00:00 2001 From: askalf Date: Fri, 18 Sep 2026 07:51:15 -0400 Subject: [PATCH 4/4] index: fix the string literal in the extra-spaces test --- plugs/index/snippet.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugs/index/snippet.test.ts b/plugs/index/snippet.test.ts index 1717c34fd7..74ab37be44 100644 --- a/plugs/index/snippet.test.ts +++ b/plugs/index/snippet.test.ts @@ -236,8 +236,7 @@ test("extractSnippet leaves an already-referenced task alone behind extra spaces const text = `* Parent [[Target]] * [ ] [[test@40]] Already referenced`; expect(extractSnippet("test", text, text.indexOf("* Parent"))).toEqual( - "* Parent [[Target]] - * [ ] [[test@40]] Already referenced", + "* Parent [[Target]]\n * [ ] [[test@40]] Already referenced", ); });