jgrenat/datocms-structured-text/1.0.0

NOTE: the latest version is 2.0.0

StructuredText.Decode

decoder : List ( ItemId, a ) -> Decoder (StructuredText a)

Decodes a DatoCMS DAST schema.

It requires a list of items that are used within blocks, inline items and/or item links nodes. If you don't use any of those nodes in your DAST document, you can provide an empty list as below. See itemDecoder for a more complex example with items.

myFieldDecoderWithoutItems : Decoder (StructuredText a)
myFieldDecoderWithoutItems =
    Decode.field "value" (StructuredText.Decode.decoder [])
itemDecoder : Decoder a -> Decoder ( ItemId, a )

Decodes a DatoCMS item with its item ID. Useful to decode items before passing them to the DAST decoder:

type alias ImageItem =
    { url : String
    , alt : String
    }


itemDecoder : Decoder ImageItem
itemDecoder =
    Decode.map2 ImageItem
        (Decode.field "url" Json.Decode.string)
        (Decode.field "alt" Json.Decode.String)


itemsListDecoder : Decoder (List ( ItemId, ImageItem ))
itemsListDecoder =
    StructuredText.Decode.itemDecoder itemDecoder
        |> Decode.list


myFieldDecoder : Decoder (StructuredText ImageItem)
myFieldDecoder =
    Decode.field "blocks" itemsListDecoder
        |> Decode.andThen
            (\items ->
                Decode.field "value" (StructuredText.Decode.decoder items)
            )