; lib/blogimport/tests/lexical.sx — lexical -> content block converter (st-bootstrap-classes!) (content-bootstrap-blocks!) (content-bootstrap-doc!) (content-bootstrap-callout!) (content-bootstrap-media!) ; ---- a representative lexical document (Ghost editor JSON, as SX dicts) ---- (define doc {:root {:children (list {:type "heading" :tag "h2" :children (list {:type "text" :text "Title"})} {:type "paragraph" :children (list {:type "text" :text "plain "} {:type "text" :text "bold" :format 1} {:type "text" :text " then "} {:type "link" :url "/x" :children (list {:type "text" :text "a link"})})} {:type "quote" :children (list {:type "text" :text "wise words"})} {:type "list" :listType "number" :children (list {:type "listitem" :children (list {:type "text" :text "one"})} {:type "listitem" :children (list {:type "text" :text "two"})})} {:type "codeblock" :language "python" :code "print(1)"} {:type "horizontalrule"} {:type "image" :src "/c.png" :alt "a cat"} {:type "callout" :backgroundColor "blue" :children (list {:type "text" :text "note!"})} {:type "twitter" :url "https://t/x"})}}) (define blocks (blogimport/lex-blocks doc)) ; ---- structure ---- (bi-test "block count" (len blocks) 9) (bi-test "ids by position" (map blk-id blocks) (list "b0" "b1" "b2" "b3" "b4" "b5" "b6" "b7" "b8")) (bi-test "types in order" (map blk-type blocks) (list "heading" "text" "quote" "list" "code" "divider" "image" "callout" "embed")) ; ---- heading ---- (bi-test "heading level" (blk-send (nth blocks 0) "level") 2) (bi-test "heading text" (str (blk-send (nth blocks 0) "text")) "Title") ; ---- paragraph with inline bold + link, flattened to plain concatenation ---- (bi-test "paragraph flattened text" (str (blk-send (nth blocks 1) "text")) "plain bold then a link") ; ---- quote ---- (bi-test "quote text" (str (blk-send (nth blocks 2) "text")) "wise words") ; ---- ordered list with items ---- (bi-test "list ordered" (blk-send (nth blocks 3) "ordered") true) (bi-test "list items" (blk-send (nth blocks 3) "items") (list "one" "two")) ; ---- code block ---- (bi-test "code language" (str (blk-send (nth blocks 4) "language")) "python") (bi-test "code text" (str (blk-send (nth blocks 4) "text")) "print(1)") ; ---- image ---- (bi-test "image src" (str (blk-send (nth blocks 6) "src")) "/c.png") (bi-test "image alt" (str (blk-send (nth blocks 6) "alt")) "a cat") ; ---- callout ---- (bi-test "callout kind" (str (blk-send (nth blocks 7) "kind")) "blue") (bi-test "callout text" (str (blk-send (nth blocks 7) "text")) "note!") ; ---- unknown card routed to embed, provider records original type ---- (bi-test "unknown -> embed provider" (str (blk-send (nth blocks 8) "provider")) "twitter") ; ---- heading level mapping ---- (bi-test "h1 level" (lex-heading-level "h1") 1) (bi-test "h4 level" (lex-heading-level "h4") 4) (bi-test "unknown tag default" (lex-heading-level "hx") 2) ; ---- bullet list ---- (define bdoc {:children (list {:type "list" :listType "bullet" :children (list {:type "listitem" :children (list {:type "text" :text "x"})})})}) (bi-test "bullet not ordered" (blk-send (nth (blogimport/lex-blocks bdoc) 0) "ordered") false) ; ---- empty doc ---- (bi-test "empty doc -> no blocks" (len (blogimport/lex-blocks {:root {:children (list)}})) 0) ; ---- bare-children doc (no :root wrapper) ---- (bi-test "bare children doc" (map blk-type (blogimport/lex-blocks {:children (list {:type "paragraph" :children (list {:type "text" :text "hi"})})})) (list "text")) ; ---- linebreak/tab in inline flattening ---- (bi-test "linebreak flatten" (str (blk-send (nth (blogimport/lex-blocks {:children (list {:type "paragraph" :children (list {:type "text" :text "a"} {:type "linebreak"} {:type "text" :text "b"})})}) 0) "text")) "a\nb")