/** 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 = /(? 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 = /(|||<[^>]+>)/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 += `${label}`; at = r.end; } return out + part.slice(at); }) .join(""); }