RichTextHtmlRenderer (rich_text_html_renderer v0.1.0)

This package aims to help use the responses from the Contentful API, specifically processing their rich-text field types.

Contentful is one of several headless CMS solutions and one of the only ones that provides a somewhat usable API.

This package relies on Floki: it converts the Contentful abstract syntax tree (AST) into the Floki AST format, and then converts that into HTML. Unlike Floki's, the Contentful data representation is not consistent, so some workarounds are required.

Limitations

The way Contentful links to other Entries and Assets makes it nearly impossible to convert a response into functional HTML. To find an asset's URL, you have to make a separate request to look up the data for a single asset. That requires more than the simple parsing that this package provides.

See Contentful Asset docs

If you are using Contentful to create your content, consider disabling links to Assets or Entries and instead define a fixed field(s) to hold optional assets. That way at least you know what you're in for when you try to convert their responses into usable HTML.

Contentful's UI also seems to support customizations via Javascript somehow. This package doesn't deal with any of that nonsense.

Summary

Functions

Converts the Contentful rich text document structure to HTML. Depending on how you have defined your Contentful "content types", your Entries may have multiple rich-text fields. Each rich text field requires conversion.

Functions

convert(contentful_doc, opts \\ [])

@spec convert(contentful_doc :: map(), opts :: keyword()) :: String.t()

Converts the Contentful rich text document structure to HTML. Depending on how you have defined your Contentful "content types", your Entries may have multiple rich-text fields. Each rich text field requires conversion.

Examples

iex> doc = %{
  "content" => [
    %{
      "content" => [%{"nodeType" => "text", "value" => "Hello, world!"}],
      "nodeType" => "paragraph"
    }
  ],
  "nodeType" => "document"
}

iex> RichTextHtmlRenderer.convert(doc)
"<p>Hello, world!</p>"

Options

Passed directly to Floki.raw_html/2