fix(7736): #N in a body links to the record it names, not to a tag
CI & Build / TypeScript typecheck (push) Successful in 35s
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 14s
CI & Build / Python tests (push) Failing after 1m37s
CI & Build / integration (push) Successful in 2m11s
CI & Build / Build & push image (push) Skipped
CI & Build / TypeScript typecheck (push) Successful in 35s
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 14s
CI & Build / Python tests (push) Failing after 1m37s
CI & Build / integration (push) Successful in 2m11s
CI & Build / Build & push image (push) Skipped
A purely numeric #N is a record reference everywhere now. The tag patterns (frontend utils/tags.ts, the editor decoration, the first-line tag strip, tag autocomplete, backend utils/tags.py) leave digits alone, so #3127 is no longer drawn as a tag chip linking to /notes?tag=3127. - utils/recordRefs.ts links #N, milestone N and rule N in rendered markdown, with the same patterns and other-numbering word list as services/record_refs.py. A drift test reads the TS source and fails when the two differ. - v-record-refs (directives/recordRefs.ts) asks POST /api/notes/resolve-refs once per body for what each id is. A link gets the route for its kind (task, lesson, snippet, note, milestone, rule) and its kind and title on hover. One the reader cannot open is drawn as a dead reference, and missing vs not shared read the same. Applied on every surface that renders markdown. - /milestones/:id finds the project and opens its board at that milestone (?milestone= expands it, scrolls to it and focuses it). - Backlinks include records that cite #N in a body or a work log, through readable_notes_clause instead of an owner-only filter, and route by kind. The graph draws #N citations as dashed reference edges. - create_note, create_task and add_task_log say which forms link. There were no numeric tags to clean up: tags are stored only from the explicit field, and none of the 200 most used is numeric. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
/** Record references in rendered prose: `#N`, `milestone N`, `rule N`.
|
||||
*
|
||||
* Three id spaces, three forms:
|
||||
* `#N` a note, task, lesson or snippet (one sequence, the notes table)
|
||||
* `milestone N` a milestone — the form a batch create writes for `{{ref:milestone}}`
|
||||
* `rule N` a rule; `preference N` too, since a preference is a rule
|
||||
* A `#N` after a word that names another numbering ("PR #198", "rule #34",
|
||||
* "step #3") is not a note, and stays plain text.
|
||||
*
|
||||
* The backend reads references with the same patterns and the same word list
|
||||
* (src/grimoire/services/record_refs.py: REF_RE, PROSE_REF_RE, _OTHER_SPACE),
|
||||
* so a backlink, a graph edge and a rendered link agree about what a body
|
||||
* cites. tests/test_record_ref_links.py fails when the two differ.
|
||||
*
|
||||
* linkifyRecordRefs only marks the references up. What each one IS — its
|
||||
* route, its title, whether the reader can open it — is filled in after render
|
||||
* by the v-record-refs directive (directives/recordRefs.ts), which asks the
|
||||
* server once per rendered body.
|
||||
*/
|
||||
|
||||
export const NOTE_REF_RE = /(?<![\w&#/])#(\d+)\b/g;
|
||||
export const PROSE_REF_RE = /(?<![\w/])\b(milestone|rule|preference)\s+#?(\d+)\b/gi;
|
||||
|
||||
export const OTHER_SPACE: readonly string[] = [
|
||||
"pr", "prs", "pull", "pulls", "request", "requests", "mr", "mrs",
|
||||
"run", "runs", "ci", "build", "builds", "job", "jobs", "pipeline",
|
||||
"action", "actions", "commit", "github", "gitlab", "gitea", "gh",
|
||||
"rule", "rules", "preference", "preferences", "milestone", "milestones",
|
||||
"system", "systems", "project", "projects", "log", "logs", "topic",
|
||||
"topics", "rulebook", "rulebooks", "step", "steps", "rank", "line",
|
||||
"lines", "page", "item", "option", "version", "port",
|
||||
];
|
||||
const OTHER_SPACE_SET = new Set(OTHER_SPACE);
|
||||
|
||||
const PREV_WORD_RE = /([A-Za-z]+)[\s"'(\[*_:]*$/;
|
||||
|
||||
export type RefSpace = "notes" | "milestones" | "rules";
|
||||
|
||||
export interface RefMatch {
|
||||
start: number;
|
||||
end: number;
|
||||
space: RefSpace;
|
||||
id: number;
|
||||
}
|
||||
|
||||
function prevWord(before: string): string {
|
||||
const m = PREV_WORD_RE.exec(before);
|
||||
return m ? m[1].toLowerCase() : "";
|
||||
}
|
||||
|
||||
/** Every reference in a run of plain text, in order, never overlapping.
|
||||
* `context` is the text that came before this run, for the word in front of
|
||||
* a `#N` that starts the run. */
|
||||
export function findRecordRefs(text: string, context = ""): RefMatch[] {
|
||||
const found: RefMatch[] = [];
|
||||
for (const m of text.matchAll(PROSE_REF_RE)) {
|
||||
const space: RefSpace = m[1].toLowerCase() === "milestone" ? "milestones" : "rules";
|
||||
found.push({ start: m.index!, end: m.index! + m[0].length, space, id: Number(m[2]) });
|
||||
}
|
||||
for (const m of text.matchAll(NOTE_REF_RE)) {
|
||||
const start = m.index!;
|
||||
const end = start + m[0].length;
|
||||
if (found.some((f) => start < f.end && end > f.start)) continue;
|
||||
if (OTHER_SPACE_SET.has(prevWord(context + text.slice(0, start)))) continue;
|
||||
found.push({ start, end, space: "notes", id: Number(m[1]) });
|
||||
}
|
||||
return found.sort((a, b) => a.start - b.start);
|
||||
}
|
||||
|
||||
/** Where a reference opens before the server has said what it is. A note id
|
||||
* may turn out to be a task, lesson or snippet; the directive corrects it. */
|
||||
export function provisionalHref(space: RefSpace, id: number): string {
|
||||
if (space === "milestones") return `/milestones/${id}`;
|
||||
if (space === "rules") return `/rules?rule=${id}`;
|
||||
return `/notes/${id}`;
|
||||
}
|
||||
|
||||
// Markup a reference is never drawn inside: an existing link (a wikilink, a
|
||||
// markdown link, a tag chip), code, and the tags themselves.
|
||||
const SKIP_RE = /(<a\b[\s\S]*?<\/a>|<code[\s\S]*?<\/code>|<pre[\s\S]*?<\/pre>|<[^>]+>)/gi;
|
||||
|
||||
/** Wrap each record reference in rendered HTML in an `a.record-ref`. */
|
||||
export function linkifyRecordRefs(html: string): string {
|
||||
const parts = html.split(SKIP_RE);
|
||||
let context = "";
|
||||
return parts
|
||||
.map((part, i) => {
|
||||
// Odd indices are markup the split kept; text between them is even.
|
||||
if (i % 2 === 1) return part;
|
||||
const refs = findRecordRefs(part, context);
|
||||
context = (context + part).slice(-80);
|
||||
if (!refs.length) return part;
|
||||
let out = "";
|
||||
let at = 0;
|
||||
for (const r of refs) {
|
||||
out += part.slice(at, r.start);
|
||||
const label = part.slice(r.start, r.end);
|
||||
out += `<a class="record-ref" data-ref-space="${r.space}" data-ref-id="${r.id}" href="${provisionalHref(r.space, r.id)}">${label}</a>`;
|
||||
at = r.end;
|
||||
}
|
||||
return out + part.slice(at);
|
||||
})
|
||||
.join("");
|
||||
}
|
||||
Reference in New Issue
Block a user