;; Extension — Markdown document export (frontmatter + body), round-trips with
;; md/import including metadata.

(st-bootstrap-classes!)
(content/bootstrap!)
(content-bootstrap-markdown!)
(content-bootstrap-table!)

(define nl (str "\n"))

;; ── no metadata → plain markdown (no frontmatter) ──
(define plain (doc-append (doc-empty "d") (mk-heading "h" 1 "Hi")))
(content-test
  "no-meta == asMarkdown"
  (content/markdown-doc plain)
  (asMarkdown plain))
(content-test "no-meta no frontmatter" (content/markdown-doc plain) "# Hi")

;; ── full metadata frontmatter ──
(define
  d
  (doc-with-meta
    (doc-append (doc-empty "post") (mk-heading "h" 1 "Hi"))
    {:slug "my-post" :title "My Post" :tags (list "a" "b")}))
(content-test
  "frontmatter export"
  (content/markdown-doc d)
  (str
    "---"
    nl
    "title: My Post"
    nl
    "slug: my-post"
    nl
    "tags: a, b"
    nl
    "---"
    nl
    nl
    "# Hi"))

;; ── title only ──
(content-test
  "title-only frontmatter"
  (content/markdown-doc
    (doc-with-title (doc-append (doc-empty "p") (mk-text "x" "body")) "T"))
  (str "---" nl "title: T" nl "---" nl nl "body"))

;; ── round-trip: import . export keeps metadata + blocks ──
(define rt (md/import (content/markdown-doc d) "post"))
(content-test "round-trip title" (doc-title rt) "My Post")
(content-test "round-trip slug" (doc-slug rt) "my-post")
(content-test "round-trip tags" (doc-tags rt) (list "a" "b"))
(content-test "round-trip body" (doc-types rt) (list "heading"))
(content-test
  "round-trip body text"
  (str (blk-send (doc-find rt "b0") "text"))
  "Hi")

;; ── round-trip a richer doc ──
(define
  d2
  (doc-with-meta
    (doc-append
      (doc-append (doc-empty "p") (mk-heading "h" 2 "Title"))
      (mk-text "p" "para text"))
    {:title "Big" :tags (list "x")}))
(define rt2 (md/import (content/markdown-doc d2) "p"))
(content-test "rt2 title" (doc-title rt2) "Big")
(content-test "rt2 tags" (doc-tags rt2) (list "x"))
(content-test "rt2 types" (doc-types rt2) (list "heading" "text"))
