;; content-on-sx — block id remapping / clone.
;;
;; Deep-rewrite every block id in the tree (descending into sections) by applying
;; a function. Enables collision-free composition: prefix one document's ids
;; before concatenating it with another. Immutable; content is unchanged, only
;; ids.
;;
;; Requires (loaded by harness): doc.sx, section.sx (section? /
;; section-children / section-with-children).

(define
  block-remap-id
  (fn
    (b f)
    (let
      ((nb (blk-set b "id" (f (blk-id b)))))
      (if
        (section? nb)
        (section-with-children
          nb
          (map (fn (c) (block-remap-id c f)) (section-children nb)))
        nb))))

(define
  content/remap-ids
  (fn
    (doc f)
    (doc-with-blocks
      doc
      (map (fn (b) (block-remap-id b f)) (doc-blocks doc)))))

(define
  content/prefix-ids
  (fn (doc prefix) (content/remap-ids doc (fn (id) (str prefix id)))))
