;; Extension — anchored-heading HTML render (functional TOC links).

(st-bootstrap-classes!)
(content/bootstrap!)
(content-bootstrap-section!)

(define
  d
  (doc-append
    (doc-append
      (doc-append (doc-empty "d") (mk-heading "intro" 1 "Intro"))
      (mk-text "p" "Body"))
    (mk-section
      "s"
      (list (mk-heading "sub" 2 "Sub") (mk-text "n" "nested")))))

;; ── headings get id anchors; other blocks unchanged ──
(content-test
  "anchored html"
  (content/html-anchored d)
  "<h1 id=\"intro\">Intro</h1><p>Body</p><section><h2 id=\"sub\">Sub</h2><p>nested</p></section>")

;; ── heading text escaped ──
(content-test
  "anchored escapes text"
  (content/html-anchored
    (doc-append (doc-empty "d") (mk-heading "h" 2 "A < B")))
  "<h2 id=\"h\">A &lt; B</h2>")

;; ── non-heading-only doc identical to asHTML ──
(define
  np
  (doc-append
    (doc-append (doc-empty "d") (mk-text "p" "x"))
    (mk-image "i" "/a.png" "alt")))
(content-test "no headings == asHTML" (content/html-anchored np) (asHTML np))

;; ── empty doc ──
(content-test "anchored empty" (content/html-anchored (doc-empty "e")) "")

;; ── anchors match TOC ids (end-to-end) ──
(content-test
  "anchor ids match toc"
  (map (fn (h) (get h :id)) (content/headings d))
  (list "intro" "sub"))

;; ── deep nesting ──
(define
  deep
  (doc-append
    (doc-empty "d")
    (mk-section
      "o"
      (list (mk-section "i" (list (mk-heading "deep" 3 "Deep")))))))
(content-test
  "deep anchored"
  (content/html-anchored deep)
  "<section><section><h3 id=\"deep\">Deep</h3></section></section>")
