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,149 @@
|
||||
import type { ObjectDirective } from "vue";
|
||||
import { apiPost } from "@/api/client";
|
||||
import router from "@/router";
|
||||
import { recordHref } from "@/utils/recordHref";
|
||||
import type { RefSpace } from "@/utils/recordRefs";
|
||||
|
||||
/** v-record-refs — makes the `a.record-ref` links in a v-html body real.
|
||||
*
|
||||
* Put it on the element that holds rendered markdown:
|
||||
* <div class="prose" v-html="renderMarkdown(body)" v-record-refs />
|
||||
*
|
||||
* After each render it asks the server, once for the whole body, what every
|
||||
* referenced id is. Each link then gets the route its kind opens at (a `#N`
|
||||
* may be a task, lesson or snippet), the record's kind and title as a tooltip
|
||||
* and accessible name, and a "leads nowhere" style when the reader cannot open
|
||||
* it: missing, trashed and not-shared-with-you look the same, on purpose.
|
||||
* A plain click navigates in the app; a modified click (new tab) keeps the
|
||||
* browser's own behaviour.
|
||||
*/
|
||||
|
||||
interface NoteInfo { id: number; title: string; is_task: boolean; note_type: string; status: string | null; task_kind: string | null }
|
||||
interface MilestoneInfo { id: number; title: string; project_id: number | null; status: string }
|
||||
interface RuleInfo { id: number; title: string; kind: string }
|
||||
type Resolved = { notes: Record<string, NoteInfo>; milestones: Record<string, MilestoneInfo>; rules: Record<string, RuleInfo> };
|
||||
type Info = { href: string; label: string } | null;
|
||||
|
||||
// A short-lived cache: the same body re-renders on every keystroke in a
|
||||
// preview, and a page of work logs cites the same few records again and again.
|
||||
const CACHE_MS = 60_000;
|
||||
const cache = new Map<string, { at: number; info: Info }>();
|
||||
|
||||
const TASK_KIND_LABEL: Record<string, string> = { issue: "Issue", spike: "Spike" };
|
||||
|
||||
function noteInfo(n: NoteInfo): Info {
|
||||
let kind = "Note";
|
||||
if (n.is_task) kind = TASK_KIND_LABEL[n.task_kind ?? ""] ?? "Task";
|
||||
else if (n.note_type === "lesson") kind = "Lesson";
|
||||
else if (n.note_type === "snippet") kind = "Snippet";
|
||||
const state = n.is_task && n.status ? ` (${n.status.replace(/_/g, " ")})` : "";
|
||||
return { href: recordHref(n), label: `${kind} · ${n.title || "Untitled"}${state}` };
|
||||
}
|
||||
|
||||
function milestoneInfo(m: MilestoneInfo): Info {
|
||||
if (m.project_id == null) return null;
|
||||
return {
|
||||
href: `/projects/${m.project_id}?milestone=${m.id}`,
|
||||
label: `Milestone · ${m.title || "Untitled"}${m.status === "done" ? " (done)" : ""}`,
|
||||
};
|
||||
}
|
||||
|
||||
function ruleInfo(r: RuleInfo): Info {
|
||||
const kind = r.kind === "preference" ? "Preference" : "Rule";
|
||||
return { href: `/rules?rule=${r.id}`, label: `${kind} · ${r.title || "Untitled"}` };
|
||||
}
|
||||
|
||||
function refKey(space: RefSpace, id: number): string {
|
||||
return `${space}:${id}`;
|
||||
}
|
||||
|
||||
/** What each of these references is, from the cache or one server call. */
|
||||
export async function resolveRecordRefs(wanted: { space: RefSpace; id: number }[]): Promise<Map<string, Info>> {
|
||||
const now = Date.now();
|
||||
const out = new Map<string, Info>();
|
||||
const ask: Record<RefSpace, number[]> = { notes: [], milestones: [], rules: [] };
|
||||
for (const w of wanted) {
|
||||
const k = refKey(w.space, w.id);
|
||||
if (out.has(k)) continue;
|
||||
const hit = cache.get(k);
|
||||
if (hit && now - hit.at < CACHE_MS) out.set(k, hit.info);
|
||||
else if (!ask[w.space].includes(w.id)) ask[w.space].push(w.id);
|
||||
}
|
||||
if (ask.notes.length + ask.milestones.length + ask.rules.length === 0) return out;
|
||||
const res = await apiPost<Resolved>("/api/notes/resolve-refs", ask);
|
||||
const store = (space: RefSpace, id: number, info: Info) => {
|
||||
cache.set(refKey(space, id), { at: now, info });
|
||||
out.set(refKey(space, id), info);
|
||||
};
|
||||
for (const id of ask.notes) store("notes", id, res.notes[id] ? noteInfo(res.notes[id]) : null);
|
||||
for (const id of ask.milestones) store("milestones", id, res.milestones[id] ? milestoneInfo(res.milestones[id]) : null);
|
||||
for (const id of ask.rules) store("rules", id, res.rules[id] ? ruleInfo(res.rules[id]) : null);
|
||||
return out;
|
||||
}
|
||||
|
||||
function unresolvedLinks(el: HTMLElement): HTMLAnchorElement[] {
|
||||
return Array.from(el.querySelectorAll<HTMLAnchorElement>("a.record-ref:not([data-ref-state])"));
|
||||
}
|
||||
|
||||
async function decorate(el: HTMLElement) {
|
||||
const links = unresolvedLinks(el);
|
||||
if (!links.length) return;
|
||||
const wanted = links.map((a) => ({ space: a.dataset.refSpace as RefSpace, id: Number(a.dataset.refId) }));
|
||||
let resolved: Map<string, Info>;
|
||||
try {
|
||||
resolved = await resolveRecordRefs(wanted);
|
||||
} catch {
|
||||
// The links keep their provisional routes, which open the right view for
|
||||
// notes, rules and milestones alike; only the titles are missing.
|
||||
return;
|
||||
}
|
||||
for (const a of links) {
|
||||
if (!a.isConnected) continue;
|
||||
const info = resolved.get(refKey(a.dataset.refSpace as RefSpace, Number(a.dataset.refId)));
|
||||
if (info === undefined) continue;
|
||||
if (info === null) {
|
||||
a.dataset.refState = "missing";
|
||||
a.classList.add("record-ref--missing");
|
||||
a.removeAttribute("href");
|
||||
a.setAttribute("aria-disabled", "true");
|
||||
a.title = `${a.textContent} — no record you can open`;
|
||||
continue;
|
||||
}
|
||||
a.dataset.refState = "ok";
|
||||
a.href = info.href;
|
||||
a.title = info.label;
|
||||
a.setAttribute("aria-label", `${a.textContent}: ${info.label}`);
|
||||
}
|
||||
}
|
||||
|
||||
const clickHandlers = new WeakMap<HTMLElement, (e: MouseEvent) => void>();
|
||||
|
||||
function onClick(e: MouseEvent) {
|
||||
const a = (e.target as HTMLElement).closest("a.record-ref") as HTMLAnchorElement | null;
|
||||
if (!a) return;
|
||||
if (a.dataset.refState === "missing") {
|
||||
e.preventDefault();
|
||||
return;
|
||||
}
|
||||
if (e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
|
||||
const href = a.getAttribute("href");
|
||||
if (!href) return;
|
||||
e.preventDefault();
|
||||
router.push(href);
|
||||
}
|
||||
|
||||
export const vRecordRefs: ObjectDirective<HTMLElement> = {
|
||||
mounted(el) {
|
||||
clickHandlers.set(el, onClick);
|
||||
el.addEventListener("click", onClick);
|
||||
void decorate(el);
|
||||
},
|
||||
updated(el) {
|
||||
void decorate(el);
|
||||
},
|
||||
beforeUnmount(el) {
|
||||
const h = clickHandlers.get(el);
|
||||
if (h) el.removeEventListener("click", h);
|
||||
clickHandlers.delete(el);
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user