From a343f4ea60ab098e0bdd10193fd2e5e8dc68f83a Mon Sep 17 00:00:00 2001 From: giles Date: Sun, 7 Jun 2026 04:41:42 +0000 Subject: [PATCH] content: nested document outline (outline.sx) + 14 tests (635/635) Co-Authored-By: Claude Opus 4.8 (1M context) --- lib/content/conformance.sh | 3 +- lib/content/outline.sx | 34 ++++++++++++++++ lib/content/scoreboard.json | 5 ++- lib/content/scoreboard.md | 3 +- lib/content/tests/outline.sx | 78 ++++++++++++++++++++++++++++++++++++ plans/content-on-sx.md | 8 +++- 6 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 lib/content/outline.sx create mode 100644 lib/content/tests/outline.sx diff --git a/lib/content/conformance.sh b/lib/content/conformance.sh index 63d63af8..762a39fa 100755 --- a/lib/content/conformance.sh +++ b/lib/content/conformance.sh @@ -15,7 +15,7 @@ if [ ! -x "$SX_SERVER" ]; then fi fi -SUITES=(block doc render api meta page page-full markdown text section compose tree-edit clone query toc anchor transform normalize find-replace stats table data wire validate store snapshot crdt crdt-store sync md-import md-doc fed) +SUITES=(block doc render api meta page page-full markdown text section compose tree-edit clone query toc anchor outline transform normalize find-replace stats table data wire validate store snapshot crdt crdt-store sync md-import md-doc fed) OUT_JSON="lib/content/scoreboard.json" OUT_MD="lib/content/scoreboard.md" @@ -51,6 +51,7 @@ run_suite() { (load "lib/content/query.sx") (load "lib/content/toc.sx") (load "lib/content/anchor.sx") +(load "lib/content/outline.sx") (load "lib/content/transform.sx") (load "lib/content/normalize.sx") (load "lib/content/find-replace.sx") diff --git a/lib/content/outline.sx b/lib/content/outline.sx new file mode 100644 index 00000000..fe8eac27 --- /dev/null +++ b/lib/content/outline.sx @@ -0,0 +1,34 @@ +;; content-on-sx — nested document outline. +;; +;; Builds a hierarchical heading tree from content/headings: each node is +;; {:id :text :level :children}, where a heading nests under the nearest +;; preceding heading of a lower level. The structured companion to the flat TOC, +;; for rendering nested navigation. +;; +;; Requires (loaded by harness): query.sx (content/headings). + +;; consume a prefix of `hs` forming nodes whose level > minlevel; return +;; {:nodes ... :rest ...}. +(define + ol-forest + (fn + (hs minlevel) + (if + (= (len hs) 0) + {:rest (list) :nodes (list)} + (let + ((h (first hs))) + (if + (<= (get h :level) minlevel) + {:rest hs :nodes (list)} + (let + ((sub (ol-forest (rest hs) (get h :level)))) + (let + ((node {:id (get h :id) :text (get h :text) :children (get sub :nodes) :level (get h :level)})) + (let + ((more (ol-forest (get sub :rest) minlevel))) + {:rest (get more :rest) :nodes (cons node (get more :nodes))})))))))) + +(define + content/outline + (fn (doc) (get (ol-forest (content/headings doc) 0) :nodes))) diff --git a/lib/content/scoreboard.json b/lib/content/scoreboard.json index 75327131..8a800c42 100644 --- a/lib/content/scoreboard.json +++ b/lib/content/scoreboard.json @@ -16,6 +16,7 @@ "query": {"pass": 13, "fail": 0}, "toc": {"pass": 8, "fail": 0}, "anchor": {"pass": 6, "fail": 0}, + "outline": {"pass": 14, "fail": 0}, "transform": {"pass": 12, "fail": 0}, "normalize": {"pass": 11, "fail": 0}, "find-replace": {"pass": 10, "fail": 0}, @@ -33,7 +34,7 @@ "md-doc": {"pass": 12, "fail": 0}, "fed": {"pass": 20, "fail": 0} }, - "total_pass": 621, + "total_pass": 635, "total_fail": 0, - "total": 621 + "total": 635 } diff --git a/lib/content/scoreboard.md b/lib/content/scoreboard.md index 9c8a40c3..429e0235 100644 --- a/lib/content/scoreboard.md +++ b/lib/content/scoreboard.md @@ -20,6 +20,7 @@ _Generated by `lib/content/conformance.sh`_ | query | 13 | 0 | 13 | | toc | 8 | 0 | 8 | | anchor | 6 | 0 | 6 | +| outline | 14 | 0 | 14 | | transform | 12 | 0 | 12 | | normalize | 11 | 0 | 11 | | find-replace | 10 | 0 | 10 | @@ -36,4 +37,4 @@ _Generated by `lib/content/conformance.sh`_ | md-import | 38 | 0 | 38 | | md-doc | 12 | 0 | 12 | | fed | 20 | 0 | 20 | -| **Total** | **621** | **0** | **621** | +| **Total** | **635** | **0** | **635** | diff --git a/lib/content/tests/outline.sx b/lib/content/tests/outline.sx new file mode 100644 index 00000000..ad778fa0 --- /dev/null +++ b/lib/content/tests/outline.sx @@ -0,0 +1,78 @@ +;; Extension — nested document outline. + +(st-bootstrap-classes!) +(content/bootstrap!) +(content-bootstrap-section!) + +;; H1 / H2 H2 / H1 -> [h1{children: h2,h3}, h4] +(define + d + (doc-append + (doc-append + (doc-append + (doc-append (doc-empty "d") (mk-heading "a" 1 "A")) + (mk-heading "b" 2 "B")) + (mk-heading "c" 2 "C")) + (mk-heading "e" 1 "E"))) + +(define o (content/outline d)) +(content-test "outline top count" (len o) 2) +(content-test "outline first id" (get (first o) :id) "a") +(content-test + "outline first children ids" + (map (fn (n) (get n :id)) (get (first o) :children)) + (list "b" "c")) +(content-test "outline second top" (get (nth o 1) :id) "e") +(content-test + "outline second no children" + (get (nth o 1) :children) + (list)) + +;; ── deeper nesting: H1 / H2 / H3 ── +(define + d2 + (doc-append + (doc-append + (doc-append (doc-empty "d") (mk-heading "x" 1 "X")) + (mk-heading "y" 2 "Y")) + (mk-heading "z" 3 "Z"))) +(define o2 (content/outline d2)) +(content-test "deep top" (get (first o2) :id) "x") +(content-test + "deep child" + (get (first (get (first o2) :children)) :id) + "y") +(content-test + "deep grandchild" + (get (first (get (first (get (first o2) :children)) :children)) :id) + "z") + +;; ── node carries text + level ── +(content-test "node text" (get (first o) :text) "A") +(content-test "node level" (get (first o) :level) 1) + +;; ── empty / no headings ── +(content-test "outline empty" (content/outline (doc-empty "e")) (list)) +(content-test + "outline no headings" + (content/outline (doc-append (doc-empty "d") (mk-text "p" "x"))) + (list)) + +;; ── starting at H2 (no H1) still forms a forest ── +(define + d3 + (doc-append + (doc-append (doc-empty "d") (mk-heading "p" 2 "P")) + (mk-heading "q" 2 "Q"))) +(content-test "no-h1 forest count" (len (content/outline d3)) 2) + +;; ── headings nested inside sections are found (tree-wide via query) ── +(define + d4 + (doc-append + (doc-append (doc-empty "d") (mk-heading "top" 1 "Top")) + (mk-section "s" (list (mk-heading "in" 2 "In"))))) +(content-test + "section heading nested in outline" + (map (fn (n) (get n :id)) (get (first (content/outline d4)) :children)) + (list "in")) diff --git a/plans/content-on-sx.md b/plans/content-on-sx.md index b5209f93..865481b6 100644 --- a/plans/content-on-sx.md +++ b/plans/content-on-sx.md @@ -19,7 +19,7 @@ injected adapter, not core. ## Status (rolling) -`bash lib/content/conformance.sh` → **621/621** (Phases 1–4 COMPLETE + extensions: HTML/SX escaping, Markdown render + import/export incl. tables & frontmatter (full round-trip), CRDT replication, tree-aware validation, snapshot cache, doc metadata, plain-text render, nested block trees + deep tree editing, doc stats, table block, HTML page wrapper + SEO page, doc composition + id-remap, portable data + wire serialization, block query + transforms + find/replace, TOC rendering + anchored headings, normalization) +`bash lib/content/conformance.sh` → **635/635** (Phases 1–4 COMPLETE + extensions: HTML/SX escaping, Markdown render + import/export incl. tables & frontmatter (full round-trip), CRDT replication, tree-aware validation, snapshot cache, doc metadata, plain-text render, nested block trees + deep tree editing, doc stats, table block, HTML page wrapper + SEO page, doc composition + id-remap, portable data + wire serialization, block query + transforms + find/replace, TOC + anchored headings + outline, normalization) ## Ground rules @@ -98,6 +98,7 @@ lib/content/api.sx ── (content/edit) (content/render) (content/history) ─ - [x] block transforms (`transform.sx`: content/map-blocks/map-type/set-field-on) - [x] TOC rendering (`toc.sx`: content/toc-markdown + toc-html from headings) - [x] anchored-heading render (`anchor.sx`: content/html-anchored, functional TOC links) +- [x] document outline (`outline.sx`: content/outline, nested heading tree) - [x] document normalization (`normalize.sx`: content/normalize, drop empty blocks/sections) - [x] global find/replace (`find-replace.sx`: content/find-replace across text-bearing blocks) - [x] portable data serialization (`data.sx`: content/to-data + from-data, round-trips tree) @@ -105,6 +106,11 @@ lib/content/api.sx ── (content/edit) (content/render) (content/history) ─ ## Progress log +- 2026-06-07 — Extension: nested document outline (`outline.sx`). + `content/outline` builds a hierarchical heading tree from content/headings — + each node `{:id :text :level :children}`, headings nesting under the nearest + lower-level heading (recursive forest build). The structured companion to the + flat TOC for nested nav. 14 tests; suite 635/635. - 2026-06-07 — Extension: anchored-heading render (`anchor.sx`). `content/html-anchored` renders like asHTML but headings carry `id=""` (tree-wide, sections recurse, text escaped), so the TOC's `#id` links resolve —