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>
85 lines
3.0 KiB
TypeScript
85 lines
3.0 KiB
TypeScript
import { marked, type RendererThis, type Tokens } from "marked";
|
|
import DOMPurify from "dompurify";
|
|
import { linkifyTags, linkifyWikilinks } from "@/utils/tags";
|
|
import { linkifyRecordRefs } from "@/utils/recordRefs";
|
|
|
|
function decodeEntities(text: string): string {
|
|
const textarea = document.createElement("textarea");
|
|
textarea.innerHTML = text;
|
|
return textarea.value;
|
|
}
|
|
|
|
export function slugify(text: string): string {
|
|
return text
|
|
.toLowerCase()
|
|
.replace(/<[^>]+>/g, "")
|
|
.replace(/[^\w\s-]/g, "")
|
|
.trim()
|
|
.replace(/\s+/g, "-");
|
|
}
|
|
|
|
export function stripFirstLineTags(text: string): string {
|
|
const match = text.match(/^[ \t]*((?:(?<!\w)#[\w]+(?:\/[\w]+)*[ \t]*)+)\n?/);
|
|
if (!match) return text;
|
|
// Verify every #-token is a tag, not a heading (headings have "# " with
|
|
// space) and not a record reference: a first line of "#4286 #4290" cites two
|
|
// records and stays in the body.
|
|
const tokens = match[1].trim().split(/\s+/);
|
|
const allTags = tokens.every((t) => /^#[\w]+(?:\/[\w]+)*$/.test(t) && !/^#\d+$/.test(t));
|
|
if (!allTags) return text;
|
|
return text.slice(match[0].length);
|
|
}
|
|
|
|
const headingRenderer = {
|
|
heading(this: RendererThis, { tokens, depth }: Tokens.Heading): string {
|
|
const text = this.parser.parseInline(tokens);
|
|
const id = slugify(text);
|
|
return `<h${depth} id="${id}">${text}</h${depth}>`;
|
|
},
|
|
};
|
|
|
|
marked.use({ renderer: headingRenderer });
|
|
|
|
const PURIFY_OPTS_FULL = {
|
|
ADD_ATTR: ["data-tag", "data-title", "data-task-index", "data-ref-space", "data-ref-id"],
|
|
FORCE_BODY: true,
|
|
};
|
|
|
|
const PURIFY_OPTS_PREVIEW = {
|
|
FORBID_TAGS: ["a", "img"],
|
|
FORCE_BODY: true,
|
|
};
|
|
|
|
export function renderMarkdown(text: string, { interactiveCheckboxes = false } = {}): string {
|
|
const stripped = stripFirstLineTags(text);
|
|
const decoded = decodeEntities(stripped);
|
|
let html = marked(decoded) as string;
|
|
|
|
if (interactiveCheckboxes) {
|
|
// marked renders task-list checkboxes as <input ... disabled="" ...>.
|
|
// Remove disabled and stamp a sequential data-task-index so the viewer
|
|
// can identify which item was toggled regardless of attribute order.
|
|
html = html.replace(/ disabled=""/g, "");
|
|
let taskIdx = 0;
|
|
html = html.replace(/<input ([^>]*type="checkbox"[^>]*)>/g, (_m, attrs) => {
|
|
return `<input data-task-index="${taskIdx++}" ${attrs}>`;
|
|
});
|
|
}
|
|
|
|
const withTags = linkifyTags(html);
|
|
const withLinks = linkifyRecordRefs(linkifyWikilinks(withTags));
|
|
const sanitized = DOMPurify.sanitize(withLinks, PURIFY_OPTS_FULL);
|
|
// marked escapes ' to ' — replace after sanitization to ensure clean rendering
|
|
return sanitized.replace(/'/g, "'");
|
|
}
|
|
|
|
export function renderPreview(text: string): string {
|
|
const stripped = stripFirstLineTags(text);
|
|
const decoded = decodeEntities(stripped);
|
|
const html = marked(decoded) as string;
|
|
const withTags = linkifyTags(html);
|
|
const withLinks = linkifyWikilinks(withTags);
|
|
const sanitized = DOMPurify.sanitize(withLinks, PURIFY_OPTS_PREVIEW);
|
|
return sanitized.replace(/'/g, "'");
|
|
}
|