content: content/block-path + block-depth — locate a block in the tree (825/825)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 1m1s

Read-side companion to doc-find-deep + reparent: ancestor-section chain
(root-first) for a block id, nil if absent (distinct from () top-level path);
block-depth is the path length. For breadcrumbs / scoping. New suite +13.
Probe this pass found no bugs: clone/remap tree-wide, all block types have
asMarkdown:.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-07 19:45:29 +00:00
parent e9316b37c2
commit 160d0f2dd0
6 changed files with 124 additions and 5 deletions

45
lib/content/block-path.sx Normal file
View File

@@ -0,0 +1,45 @@
;; content-on-sx — locate a block in the tree (ancestor section path).
;;
;; The read-side companion to doc-find-deep (which returns the block) and the
;; move/reparent ops (which relocate it): content/block-path returns the list of
;; ancestor section ids, root-first, leading to a block id — i.e. where the
;; block sits in the tree. A top-level block has an empty path; a block one
;; section deep has a one-element path; a missing id returns nil (distinct from
;; the empty-list path of a present top-level block). content/block-depth is the
;; path length (0 = top level, -1 = absent). Useful for breadcrumbs and for
;; scoping an edit to a block's enclosing section. Pure traversal; descends into
;; any block carrying a children list, like the rest of the tree helpers.
;;
;; Requires (loaded by harness): block.sx, doc.sx.
(define
bp-in-blocks
(fn
(blocks id trail)
(if
(= (len blocks) 0)
nil
(let
((b (first blocks)))
(if
(= (blk-id b) id)
trail
(let
((ch (st-iv-get b "children")))
(let
((found (if (list? ch) (bp-in-blocks ch id (append trail (list (blk-id b)))) nil)))
(if (= found nil) (bp-in-blocks (rest blocks) id trail) found))))))))
;; ancestor section ids (root-first) for `id`, or nil if the block is absent.
(define
content/block-path
(fn (doc id) (bp-in-blocks (doc-blocks doc) id (list))))
;; depth of `id`: 0 at top level, n nested n sections deep, -1 if absent.
(define
content/block-depth
(fn
(doc id)
(let
((p (content/block-path doc id)))
(if (= p nil) -1 (len p)))))

View File

@@ -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 move clone query toc anchor outline flatten transform normalize find-replace stats summary index table callout media data wire validate sanitize store snapshot crdt crdt-tree crdt-blocks crdt-store sync md-import md-doc fed)
SUITES=(block doc render api meta page page-full markdown text section compose tree-edit move block-path clone query toc anchor outline flatten transform normalize find-replace stats summary index table callout media data wire validate sanitize store snapshot crdt crdt-tree crdt-blocks crdt-store sync md-import md-doc fed)
OUT_JSON="lib/content/scoreboard.json"
OUT_MD="lib/content/scoreboard.md"
@@ -48,6 +48,7 @@ run_suite() {
(load "lib/content/compose.sx")
(load "lib/content/tree-edit.sx")
(load "lib/content/move.sx")
(load "lib/content/block-path.sx")
(load "lib/content/clone.sx")
(load "lib/content/query.sx")
(load "lib/content/toc.sx")

View File

@@ -13,6 +13,7 @@
"compose": {"pass": 17, "fail": 0},
"tree-edit": {"pass": 17, "fail": 0},
"move": {"pass": 24, "fail": 0},
"block-path": {"pass": 13, "fail": 0},
"clone": {"pass": 10, "fail": 0},
"query": {"pass": 20, "fail": 0},
"toc": {"pass": 8, "fail": 0},
@@ -43,7 +44,7 @@
"md-doc": {"pass": 12, "fail": 0},
"fed": {"pass": 20, "fail": 0}
},
"total_pass": 812,
"total_pass": 825,
"total_fail": 0,
"total": 812
"total": 825
}

View File

@@ -17,6 +17,7 @@ _Generated by `lib/content/conformance.sh`_
| compose | 17 | 0 | 17 |
| tree-edit | 17 | 0 | 17 |
| move | 24 | 0 | 24 |
| block-path | 13 | 0 | 13 |
| clone | 10 | 0 | 10 |
| query | 20 | 0 | 20 |
| toc | 8 | 0 | 8 |
@@ -46,4 +47,4 @@ _Generated by `lib/content/conformance.sh`_
| md-import | 38 | 0 | 38 |
| md-doc | 12 | 0 | 12 |
| fed | 20 | 0 | 20 |
| **Total** | **812** | **0** | **812** |
| **Total** | **825** | **0** | **825** |

View File

@@ -0,0 +1,59 @@
;; Extension — locate a block in the tree (ancestor section path).
(st-bootstrap-classes!)
(content/bootstrap!)
(content-bootstrap-section!)
;; doc: top-level "a", section "s" containing "x" and nested section "i"
;; containing "z".
(define
d
(doc-append
(doc-append (doc-empty "d") (mk-text "a" "A"))
(mk-section
"s"
(list (mk-text "x" "X") (mk-section "i" (list (mk-text "z" "Z")))))))
;; ── block-path ──
(content-test
"top-level block has empty path"
(content/block-path d "a")
(list))
(content-test "one-deep block path" (content/block-path d "x") (list "s"))
(content-test
"two-deep block path"
(content/block-path d "z")
(list "s" "i"))
(content-test "section's own path" (content/block-path d "i") (list "s"))
(content-test "missing id path nil" (content/block-path d "zzz") nil)
;; nil (absent) is distinct from () (present top-level)
(content-test
"absent vs top-level distinguishable"
(if (= (content/block-path d "a") nil) "nil" "list")
"list")
;; ── block-depth ──
(content-test "depth top-level" (content/block-depth d "a") 0)
(content-test "depth one" (content/block-depth d "x") 1)
(content-test "depth two" (content/block-depth d "z") 2)
(content-test "depth section" (content/block-depth d "i") 1)
(content-test "depth absent" (content/block-depth d "zzz") -1)
;; ── path tracks reparenting (composes with move.sx) ──
;; (rebuild expectation directly; move tested elsewhere)
(define
flat
(doc-append
(doc-append (doc-empty "d") (mk-section "sec" (list)))
(mk-text "p" "P")))
(content-test
"before: p at top level"
(content/block-depth flat "p")
0)
;; ── empty doc ──
(content-test
"empty doc path nil"
(content/block-path (doc-empty "e") "x")
nil)