content: nested document outline (outline.sx) + 14 tests (635/635)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -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")
|
||||
|
||||
34
lib/content/outline.sx
Normal file
34
lib/content/outline.sx
Normal file
@@ -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)))
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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** |
|
||||
|
||||
78
lib/content/tests/outline.sx
Normal file
78
lib/content/tests/outline.sx
Normal file
@@ -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"))
|
||||
Reference in New Issue
Block a user