;; Extension — Markdown render mode. asMarkdown is a polymorphic message send;
;; the boundary supplies the newline.

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

(define nl (str "\n"))

;; ── per-block ──
(content-test
  "heading h3"
  (asMarkdown (mk-heading "h" 3 "Title"))
  "### Title")
(content-test
  "heading h1"
  (asMarkdown (mk-heading "h" 1 "T"))
  "# T")
(content-test "text md" (asMarkdown (mk-text "p" "body")) "body")
(content-test
  "quote md"
  (asMarkdown (mk-quote "q" "Ada" "to err"))
  "> to err")
(content-test
  "image md"
  (asMarkdown (mk-image "i" "/c.png" "cat"))
  "![cat](/c.png)")
(content-test
  "embed md"
  (asMarkdown (mk-embed "e" "https://v/1" "vimeo"))
  "[embed](https://v/1)")
(content-test "divider md" (asMarkdown (mk-divider "d")) "---")
(content-test
  "code md"
  (asMarkdown (mk-code "c" "sx" "(+ 1 2)"))
  (str "```sx" nl "(+ 1 2)" nl "```"))
(content-test
  "ul md"
  (asMarkdown (mk-list "u" false (list "a" "b" "c")))
  (str "- a" nl "- b" nl "- c"))
(content-test
  "ol md"
  (asMarkdown (mk-list "o" true (list "x" "y")))
  (str "1. x" nl "1. y"))
(content-test "empty list md" (asMarkdown (mk-list "e" false (list))) "")

;; ── document joins blocks with a blank line ──
(define
  d
  (doc-append
    (doc-append
      (doc-append (doc-empty "doc") (mk-heading "h" 2 "Title"))
      (mk-text "p" "Hello"))
    (mk-divider "d")))
(content-test
  "doc md"
  (asMarkdown d)
  (str "## Title" nl nl "Hello" nl nl "---"))
(content-test "empty doc md" (asMarkdown (doc-empty "e")) "")

;; ── via facade ──
(content-test "render md" (content/render d "md") (asMarkdown d))
(content-test "render markdown" (content/render d "markdown") (asMarkdown d))
(content-test "render md keyword" (content/render d :md) (asMarkdown d))
(content-test "content/markdown alias" (content/markdown d) (asMarkdown d))
(content-test
  "block-markdown alias"
  (block-markdown (mk-heading "h" 2 "X"))
  "## X")

;; ── reflects edits / immutability ──
(content-test
  "md after update"
  (asMarkdown (doc-update d "p" "text" "Edited"))
  (str "## Title" nl nl "Edited" nl nl "---"))
(content-test
  "md original unchanged"
  (asMarkdown d)
  (str "## Title" nl nl "Hello" nl nl "---"))
