From f888525808844449ada7a9a9542b86159e4fc25c Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Tue, 17 Nov 2026 02:53:58 -0500 Subject: [PATCH] A library's size counts what is under its top-level nodes, the same in /api/libraries and the root's byline (a root alone is 0 nodes) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab --- server/libraries.ts | 5 +++-- src/core/model.ts | 3 +++ src/ui/Node.tsx | 4 ++-- src/ui/api.ts | 1 + tests/libraries.test.ts | 8 ++++---- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/server/libraries.ts b/server/libraries.ts index e891dc8..3a2a99e 100644 --- a/server/libraries.ts +++ b/server/libraries.ts @@ -24,7 +24,7 @@ import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, statSync import { join } from "node:path"; import { LIBRARY_ID } from "../src/core/address"; import { FormatError } from "../src/core/format"; -import { Doc, retitleOps } from "../src/core/model"; +import { Doc, libraryNodeCount, retitleOps } from "../src/core/model"; import { writeText } from "../src/core/seed"; import { LibraryStore } from "./library"; import { HttpError, writeAtomic } from "./util"; @@ -48,6 +48,7 @@ export interface LibraryInfo { id: string; title: string; created: string; + /** Nodes under its top-level nodes (the root and the Wikipedia container not counted). */ nodes: number; branches: string[]; } @@ -189,7 +190,7 @@ export class Libraries { e.title = title; renamed = true; } - out.push({ id: e.id, title, created: e.created, nodes: Object.keys(s.main.snap().nodes).length, branches: [...s.branches.keys()] }); + out.push({ id: e.id, title, created: e.created, nodes: libraryNodeCount(s.main.snap()), branches: [...s.branches.keys()] }); } if (renamed) this.saveIndex(); return out; diff --git a/src/core/model.ts b/src/core/model.ts index d387152..4d12c34 100644 --- a/src/core/model.ts +++ b/src/core/model.ts @@ -897,6 +897,9 @@ export function tops(snap: Snap): NodeId[] { export const isLibraryRoot = (snap: Snap, id: NodeId) => snap.library && id === snap.root; +/** How many nodes a library holds: everything under its top-level nodes, the root and the Wikipedia container left out (0 for a root alone). */ +export const libraryNodeCount = (snap: Snap) => tops(snap).reduce((n, d) => n + 1 + descendants(snap, d).length, 0); + /** * The top of a node's branch: its ancestor (or itself) among the root's children. A relation, not * a kind of node. None for the library root itself; in a single tree, the root. diff --git a/src/ui/Node.tsx b/src/ui/Node.tsx index 12070d9..f78620f 100644 --- a/src/ui/Node.tsx +++ b/src/ui/Node.tsx @@ -1,7 +1,7 @@ /** Rendering shared by both page layouts: a node's body with its notes, the outline rows, and the top of the page. */ import { Component, type ComponentChildren } from "preact"; import { useLayoutEffect, useRef, useState } from "preact/hooks"; -import { anchorLost, ancestors, descendants, isLibraryRoot, label, notesOf, sections, staleness, type NodeId, type Snap } from "../core/model"; +import { anchorLost, ancestors, descendants, isLibraryRoot, label, libraryNodeCount, notesOf, sections, staleness, type NodeId, type Snap } from "../core/model"; import type { Ref } from "../core/ref"; import { clip, overlaps, paragraphs, plain, type Form, type Range } from "../core/text"; import { streamView } from "../core/marks"; @@ -462,7 +462,7 @@ export function TopMatter({ id }: { id: NodeId }) { const editing = store.edit?.node === id && store.edit.field === "title"; const library = isLibraryRoot(snap, id); // The library root's count leaves out the Wikipedia container, which is nobody's section. - const count = library ? kids.reduce((n, d) => n + 1 + descendants(snap, d).length, 0) : descendants(snap, id).length; + const count = library ? libraryNodeCount(snap) : descendants(snap, id).length; return ( <> {crumbs.length > 0 && ( diff --git a/src/ui/api.ts b/src/ui/api.ts index ee29995..3b34b77 100644 --- a/src/ui/api.ts +++ b/src/ui/api.ts @@ -28,6 +28,7 @@ export interface LibraryInfo { id: string; title: string; created: string; + /** Nodes under its top-level nodes (the root and the Wikipedia container not counted). */ nodes: number; branches: string[]; } diff --git a/tests/libraries.test.ts b/tests/libraries.test.ts index e938807..52c271f 100644 --- a/tests/libraries.test.ts +++ b/tests/libraries.test.ts @@ -36,8 +36,8 @@ describe("the libraries on disk", () => { expect(work.titles()).toEqual([]); expect(work.id).not.toBe(first.id); expect(libs.list().map((l) => [l.title, l.nodes])).toEqual([ - ["Library", 2], - ["Work notes", 1], + ["Library", 1], + ["Work notes", 0], ]); // An op on one never touches the other: separate databases, separate files. const hash = first.main.stateHash(); @@ -150,7 +150,7 @@ describe("the server with several libraries", () => { const made = await send("POST", "/api/libraries", { title: "Second" }); expect(made.status).toBe(201); const b = (await made.json()).library; - expect(b).toMatchObject({ title: "Second", nodes: 1 }); + expect(b).toMatchObject({ title: "Second", nodes: 0 }); const state = await get(`/api/l/${b.id}/state`); expect(state).toMatchObject({ library: b.id, title: "Second" }); expect(state.libraries.map((l: { id: string }) => l.id)).toEqual([a.id, b.id]); @@ -228,7 +228,7 @@ describe("the server with several libraries", () => { const again = serve(data, starters); await up(); const listed = (await get("/api/libraries")).libraries; - expect(listed.map((l: { id: string; nodes: number }) => [l.id, l.nodes])).toEqual([[a.id, Object.keys(treeA.nodes).length]]); + expect(listed.map((l: { id: string; nodes: number }) => [l.id, l.nodes])).toEqual([[a.id, Object.keys(treeA.nodes).length - 1]]); again.child.kill("SIGTERM"); await again.exited; }, 60_000);