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
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:
45
lib/content/block-path.sx
Normal file
45
lib/content/block-path.sx
Normal 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)))))
|
||||
Reference in New Issue
Block a user