Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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.
  • Loading branch information
askalf committed Sep 17, 2026
commit 61d3772b68db4f35ef5abe99d598d1ff2ae0375f
46 changes: 46 additions & 0 deletions plugs/index/snippet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
});
7 changes: 5 additions & 2 deletions plugs/index/snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]}`;
Expand Down