A Clojure library for rendering the PortableText rich text format content (used in Sanity) to hiccup or HTML.
Passes most of the official tests, except for some JavaScript library-specific features, and some image transforms (to be implemented).
(require '[portable-text.html :as pt])
(def content-blocks
[{:_key "R5FvMrjo"
:_type "block"
:children
[{:_key "cZUQGmh4"
:_type "span"
:marks []
:text "Plain text."}]
:markDefs []
:style "normal"}])
(pt/render content-blocks)
;;=> "<p>Plain text.</p>"
(pt/to-hiccup content-blocks)
;;=> [:p {} ("Plain text.")]
You can optionally pass an options map to both to-hiccup
and render
to
specify details about your Sanity installation, which is necessary to render
images correctly:
(pt/render
[{:_type "image",
:_key "d234a4fa317a",
:asset
{:_type "reference",
:_ref "image-YiOKD0O6AdjKPaK24WtbOEv0-3456x2304-jpg"}}]
{:sanity/project-id "3do82whm"
:sanity/dataset "production"})
;;=> "<figure><img src=\"https://cdn.sanity.io/images/3do82whm/production/YiOKD0O6AdjKPaK24WtbOEv0-3456x2304.jpg\"/></figure>"
Images are rendered from Sanity's CDN by default, but you can override it:
(sut/to-hiccup
[{:_type "image",
:_key "d234a4fa317a",
:asset
{:_type "reference",
:_ref "image-yoda-3456x2304-jpg"}}]
{:sanity/project-id "abcd"
:sanity/dataset "prod"
:cdn-url "https://cdn.mysite.com"})
;;=> [:figure {}
;; [:img
;; {:src "https://cdn.mysite.com/images/abcd/prod/yoda-3456x2304.jpg"}]]
Image transforms such as crop and hotspot are not (yet) supported.
make test # ...or
make autotest
Copyright © 2020 Christian Johansen
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close