;; 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)))))