From 045a8459da0ce8629274f7dbd44f1fcc89f59ff4 Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Tue, 17 Nov 2026 02:25:25 -0500 Subject: [PATCH] Core for several libraries: graft carries the Wikipedia flag, keeps a top's anchors when asked and maps old ids to new; a library's title at creation; addresses under /l/ Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab --- src/core/address.ts | 32 +++++++++++++++++++++++++++++--- src/core/model.ts | 20 +++++++++++++++----- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/core/address.ts b/src/core/address.ts index a934b7d..bb6cf80 100644 --- a/src/core/address.ts +++ b/src/core/address.ts @@ -1,15 +1,41 @@ /** - * Where a node is, as the page's address: `/` is the library root, `/n/` any other node. - * The id is all there is to it, so an address survives renames and moves. + * Where a node is, as the page's address. Every address starts with its library: `/l/` + * is the library's root, `/l//n/` any other node of it, and + * `/l//w//` an article as a page of its own. The node id is all there is to + * a node's part, so an address survives renames and moves. An address without a library (`/`, or + * one from before libraries, `/n/…`) is read in the library last used. */ import type { NodeId, Snap } from "./model"; +/** A library's id: what `Doc.library` makes (base 36), also a directory name on the server. */ +export const LIBRARY_ID = /^[0-9a-z]{1,32}$/; + +/** The part of an address within its library: `/` for the root, `/n/<node id>` for any other node. */ export function address(snap: Snap, id: NodeId): string { if (!snap.nodes[id] || id === snap.root) return "/"; return `/n/${encodeURIComponent(id)}`; } -/** The node a path names, or why there is none. */ +/** A whole address: the library's prefix, then the part within it. */ +export function inLibrary(library: string, part: string): string { + const base = `/l/${encodeURIComponent(library)}`; + return part === "/" || part === "" ? base : base + part; +} + +/** An address split into its library and the part within it; no library if it names none. */ +export function splitLibrary(path: string): { library: string | null; part: string } { + const m = /^\/l\/([^/]+)(\/.*)?$/.exec(path); + if (!m) return { library: null, part: path || "/" }; + let id = m[1]; + try { + id = decodeURIComponent(id); + } catch { + /* kept as it came; it will name no library */ + } + return { library: id, part: m[2] || "/" }; +} + +/** The node a path within a library names, or why there is none. */ export function resolve(snap: Snap, path: string): { node: NodeId } | { missing: string } { let parts: string[]; try { diff --git a/src/core/model.ts b/src/core/model.ts index e441360..d387152 100644 --- a/src/core/model.ts +++ b/src/core/model.ts @@ -222,13 +222,13 @@ export class Doc { } /** An empty library. Its id is kept in the database, so a replica of another library is never mistaken for it. */ - static library(author: string, id = randomPeer().toString(36)): Doc { + static library(author: string, id = randomPeer().toString(36), title = LIBRARY_TITLE): Doc { const loro = new LoroDoc(); loro.setPeerId(randomPeer()); const doc = new Doc(loro, author); doc.begin("Create the library", "human"); const root = loro.getTree("tree").createNode(); - initRoot(root, LIBRARY_TITLE, "", "human"); + initRoot(root, title, "", "human"); root.data.set("library", true); loro.getMap("meta").set("library", id); doc.commit("Create the library", "human"); @@ -622,14 +622,22 @@ export class Doc { /** * Write a copy of a subtree (from this document or another) under `parent`, as it stands: - * content, authorship, pins and the hashes it was last reconciled with. Must run inside a commit. + * content, authorship, locks, targets, declined phrases, the Wikipedia flags and the hashes it was + * last reconciled with. Must run inside a commit. + * + * The top's anchors are offsets into its parent's text, so they are kept only where that text is + * the same: when the copy lands beside the original, or when the caller says the new parent's + * text is a copy of the old one's (`keepAnchors`). `ids`, if given, is filled with each copied + * node's new id by its old one. Targets are copied as they are; a caller copying between + * libraries rewrites the node targets through `ids`. */ - graft(src: Snap, from: NodeId, parent: NodeId, opts: { index?: number; title?: string } = {}): NodeId { + graft(src: Snap, from: NodeId, parent: NodeId, opts: { index?: number; title?: string; keepAnchors?: boolean; ids?: Map<NodeId, NodeId> } = {}): NodeId { const { index, title } = opts; const into = this.node(parent); const visit = (id: NodeId, under: LoroTreeNode, at?: number): NodeId => { const s = src.nodes[id]; const n = at === undefined ? under.createNode() : under.createNode(at); + opts.ids?.set(id, n.id); n.data.set("kind", s.kind); n.data.set("title", id === from && title !== undefined ? title : s.title); (n.data.setContainer("body", new LoroText()) as LoroText).insert(0, s.body); @@ -639,12 +647,14 @@ export class Doc { if (s.lens) n.data.set("lens", s.lens); if (s.ref) n.data.set("ref", s.ref as never); if (s.wanchor) n.data.set("wanchor", s.wanchor as never); + // Only a section of the library root can be the Wikipedia container, as `create` has it. + if (s.articles && under.data.get("library")) n.data.set("articles", true); if (s.nolink?.length) setNolink(n, s.nolink); n.data.set("up", s.seenUp); n.data.set("down", s.seenDown); // The copy's anchors are offsets into its parent's body, which is a copy too; except at the // top, where they would be offsets into a different text unless it lands beside the original. - const kept = id !== from || parent === s.parent ? s.anchors.filter((a) => a.end > a.start) : []; + const kept = id !== from || parent === s.parent || opts.keepAnchors ? s.anchors.filter((a) => a.end > a.start) : []; if (kept.length) n.data.set("anchors", this.makeAnchors(under, kept.map((a) => [a.start, a.end]), kept.map((a) => a.quote)) as never); this.mark(n.id); for (const c of s.children) visit(c, n);