ContentfulRenderer (Contentful Renderer v0.5.0) View Source

Link to this section Summary

Functions

Behaves the same as render_document/2, but returns a Phoenix.HTML.Safe.t tuple.

Renders the content inside a node. Useful if you've overriden a renderer, but you don't want to replement how the renderer walks into the content.

Renders a Contentful node, in a tree of Maps and Lists, to HTML.

Link to this section Functions

Link to this function

render(content, options)

View Source

Behaves the same as render_document/2, but returns a Phoenix.HTML.Safe.t tuple.

Link to this function

render_content(node, options \\ [])

View Source

Renders the content inside a node. Useful if you've overriden a renderer, but you don't want to replement how the renderer walks into the content.

Link to this function

render_document(document, options \\ [])

View Source

Renders a Contentful node, in a tree of Maps and Lists, to HTML.

It accepts custom renderers for each node type, passed in as Keywords in the options parameter. To override the heading-1 renderer, pass in:

heading_1_node_renderer: fn node, options ->
  Phoenix.HTML.Tag.content_tag(:h1) do
    ContentfulRenderer.render_content(node, options)
  end
end

Renderers need to return a Phoenix.HTML.Safe.t() or they're treated as an unsafe string and HTML escaped.

By default it renders embedded-entry-inline and embedded-entry-block as blank, because these usually require local knowledge about the content model to render usefully. When this happens, Logger will warn.

Examples

iex> %{
...>   :content => [
...>     %{
...>       :content => [
...>         %{
...>           :data => %{},
...>           :marks => [],
...>           :nodeType => "text",
...>           :value => "Paragraph 1"
...>         }
...>       ],
...>       :data => %{},
...>       :nodeType => "paragraph"
...>     },
...>     %{
...>       :content => [
...>         %{
...>           :data => %{},
...>           :marks => [],
...>           :nodeType => "text",
...>           :value => "Paragraph 2"
...>         }
...>       ],
...>       :data => %{},
...>       :nodeType => "paragraph"
...>     }
...>   ],
...>   :data => %{},
...>   :nodeType => "document"
...> }
...> |> ContentfulRenderer.render_document()
"<p>Paragraph 1</p><p>Paragraph 2</p>"