mewhit / prismicio / Prismic.Field

Field types

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


type alias Field =
Prismic.Internal.Field

A field in the Document.

Structured Text


type alias StructuredText =
Prismic.Internal.StructuredText

StructuredText can be rendered to HTML using structuredTextAsHtml.


type alias StructuredTextBlock =
Prismic.Internal.StructuredTextBlock

An element of StructuredText.

Images


type alias ImageViews =
Prismic.Internal.ImageViews

A collection of image views.


type alias ImageView =
Prismic.Internal.ImageView

Properties for a single image view.


type alias ImageDimensions =
Prismic.Internal.ImageDimensions

Dimensions of an image view.

Embeds


type alias Embed =
Prismic.Internal.Embed

Embed elements.

Links


type alias Link =
Prismic.Internal.Link

Links to other documents or to the web.


type alias DocumentReference =
Prismic.Internal.DocumentReference

A reference to a Prismic document.

Decoding fields

text : Prismic.Internal.Decoder Field String

Decode a Text field.

structuredText : Prismic.Internal.Decoder Field StructuredText

Decode a StructuredText field.

image : Prismic.Internal.Decoder Field ImageViews

Decode an Image field.

date : Prismic.Internal.Decoder Field Date

Decode a Date field.

link : Prismic.Internal.Decoder Field Link

Decode a Link 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 (Html.Attribute msg)
, resolveUrl : String -> List (Html.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}" ]

resolveLink : LinkResolver msg -> Link -> List (Html.Attribute msg)

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.