Annotate Wikipedia articles with the same keys as nodes

An article's text is drawn from its sanitised HTML as selectable text
(articleText.ts): paragraphs, list items and quotes with their inline
elements; a formula is one unit; tables and preformatted blocks are shown
but carry no offsets. The selection ladder, Tab, Enter, e d y x c :, D and
the rest go through the same store paths as on a node; a note on article
words is a note (Enter steps into it; c, f, u, the editor and history work
on it as on any other) and shows wherever the article is opened, in the
library's own note style. Detached annotations are listed at the top of
their section with a link to the old revision. Keys that only make sense on
a node say why in the status line. Tab on a link now selects its words, so
Esc widens before h leaves, as in a node.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
This commit is contained in:
thalladaandClaude Opus 5.5 committed 2026-11-15 21:53:00 -05:00
1 parent f63fc10dc0
commit 2c1ee048a4
18 files changed
+1134 -126

No files matched your search

+30 -8
View File
@@ -9,7 +9,9 @@
* attributes survives, and every link either names an article (data-wiki) or opens a new tab.
*/
import type { Attribution, FullArticle as Article, Sense, Summary, TitleSuggestion as Suggestion } from "../../server/wiki";
import type { SectionText } from "../core/article";
import { wikiKey, wikiTitle } from "../core/ref";
import { buildText, type ArticleText } from "./articleText";
export type { Article, Attribution, Suggestion };
type WikiSection = Article["sections"][number];
@@ -196,18 +198,38 @@ export function cleanHtml(html: string): string {
return out;
}
/** The article links in a piece of article HTML, in reading order: what Tab cycles through. */
const linkLists = new Map<string, { title: string; text: string }[]>();
export function wikiLinks(html: string): { title: string; text: string }[] {
let out = linkLists.get(html);
/** A piece of article HTML as text to select in (articleText.ts). Remembered per string. */
const texts = new Map<string, ArticleText>();
export function articleText(html: string): ArticleText {
let out = texts.get(html);
if (out) return out;
const doc = new DOMParser().parseFromString(`<body>${cleanHtml(html)}</body>`, "text/html");
out = [...doc.querySelectorAll("a[data-wiki]")].map((a) => ({ title: a.getAttribute("data-wiki")!, text: a.textContent ?? "" }));
if (linkLists.size > 400) linkLists.delete(linkLists.keys().next().value as string);
linkLists.set(html, out);
out = buildText(new DOMParser().parseFromString(`<body>${cleanHtml(html)}</body>`, "text/html").body);
if (texts.size > 400) texts.delete(texts.keys().next().value as string);
texts.set(html, out);
return out;
}
/** The headings down to a section (0: the lead, []): how an annotation names where it is. */
export function sectionPath(a: Article, id: number): string[] {
const out: string[] = [];
for (let s = a.sections.find((x) => x.id === id); s; s = a.sections.find((x) => x.id === s!.parent)) out.unshift(s.heading);
return out;
}
/** Every section's text with its headings, the lead first: what annotations are found again in. Remembered per article. */
const sectionLists = new WeakMap<Article, { ids: number[]; secs: SectionText[] }>();
export function sectionTexts(a: Article): { ids: number[]; secs: SectionText[] } {
let out = sectionLists.get(a);
if (!out) {
out = { ids: [0, ...a.sections.map((s) => s.id)], secs: [{ path: [], text: articleText(a.lead).text }, ...a.sections.map((s) => ({ path: sectionPath(a, s.id), text: articleText(s.html).text }))] };
sectionLists.set(a, out);
}
return out;
}
/** The address of a past revision of an article. */
export const revisionUrl = (title: string, rev: number, lang = config.lang) => `https://${lang}.wikipedia.org/w/index.php?title=${encodeURIComponent(wikiTitle(title).replace(/ /g, "_"))}&oldid=${rev}`;
/**
* The sections under one (0: the top level) that have something to show: the sanitiser empties
* Notes and References, and a row that opens onto nothing is noise.