mewhit/prismicio/1.0.0

Prismic.Field

Field types

You can create your own Elm types to represent your documents using the following components.

type alias Field =
Field

A field in the Document.

Structured Text

type alias StructuredText =
StructuredText

StructuredText can be rendered to HTML using structuredTextAsHtml.

type alias StructuredTextBlock =
StructuredTextBlock

An element of StructuredText.

Images

type alias ImageViews =
ImageViews

A collection of image views.

type alias ImageView =
ImageView

Properties for a single image view.

type alias ImageDimensions =
ImageDimensions

Dimensions of an image view.

Embeds

type alias Embed =
Embed

Embed elements.

type alias DocumentReference =
DocumentReference

A reference to a Prismic document.

Decoding fields

text : Decoder Field String

Decode a Text field.

structuredText : Decoder Field StructuredText

Decode a StructuredText field.

image : Decoder Field ImageViews

Decode an Image field.

date : Decoder Field Date

Decode a Date field.

Viewing fields

structuredTextAsHtml : LinkResolver msg -> StructuredText -> List (Html msg)

Render some StructuredText as HTML.

You must supply a LinkResolver to resolve any links in the StructuredText. If you don't care about this, you can use the defaultLinkResolver.

structuredTextBlockAsHtml : LinkResolver msg -> StructuredTextBlock -> Html msg

Render a single block of StructuredText as HTML.

imageAsHtml : ImageView -> Html msg
embedAsHtml : Embed -> Html msg
linkAsHtml : LinkResolver msg -> Link -> Html msg
type alias LinkResolver msg =
{ resolveDocumentReference :
DocumentReference -> List (Attribute msg)
, resolveUrl : String -> List (Attribute msg)
}

A LinkResolver simply converts a Prismic DocumentReference to a list of Html.Attributes. structuredTextAsHtml and friends add these attributes to links in the text.

For example, you can use this to add onClick handlers to links:

type Msg
    = NavigateTo DocumentReference


myLinkResolver : LinkResolver Msg
myLinkResolver docRef =
    [ Html.Attributes.href ""
    , Html.Events.onClick (NavigateTo docRef)
    ]


view : StructuredText -> Html Msg
view myStructuredText =
    structuredTextAsHtml myLinkResolver myStructuredText

Your update function would handle the NavigateTo message and perform the appropriate routing.

defaultLinkResolver : LinkResolver msg

Adds a default href attribute to links: [ href "documents/{doc.id}/{doc.slug}" ]

StructuredText helpers

getTitle : StructuredText -> Maybe StructuredTextBlock

Get the first title out of some StructuredText, if there is one.

getFirstImage : StructuredText -> Maybe ImageView

Get the first image out of some StructuredText, if there is one.

getFirstParagraph : StructuredText -> Maybe StructuredTextBlock

Get the first paragraph out of some StructuredText, if there is one.

getText : StructuredTextBlock -> String

Get the contents of a single StructuredText element as a String.

getTexts : StructuredText -> String

Get the contents of a some StructuredText as a String.