View Source Sanity.Components.PortableText (sanity_components v0.14.1)

For rending Sanity CMS portable text.

Examples

Basic example

use Phoenix.Component

# ...

assigns = %{
  portable_text: [
    %{
      _key: "f71173c80e3a",
      _type: "block",
      children: [%{_key: "d6c419dcf485", _type: "span", marks: [], text: "Test paragraph."}],
      mark_defs: [],
      style: "normal"
    }
  ]
}

~H"<Sanity.Components.PortableText.portable_text value={@portable_text} />"

Custom rendering

defmodule CustomBlock do
  use Phoenix.Component
  use Sanity.Components.PortableText

  @impl true
  def block(%{value: %{style: "normal"}} = assigns) do
    ~H"""
    <div class="custom-normal"><%= render_slot(@inner_block) %></div>
    """
  end

  def block(assigns), do: super(assigns)
end

Then render the component like:

~H"<Sanity.Components.PortableText.portable_text mod={CustomBlock} value={@portable_text} />"

Similarly, marks and types can be customized by defining mark/1 and type/1 functions in the module.

Summary

Functions

Renders Sanity CMS portable text. See module doc for examples.

Converts list of blocks to plain text.

Functions

portable_text(assigns)

Renders Sanity CMS portable text. See module doc for examples.

Attributes

to_plain_text(blocks)

Converts list of blocks to plain text.