dillonkearns/elm-pages/1.1.2

NOTE: the latest version is 12.3.0

Pages.Document

The Document represents all the ways to handle the frontmatter metadata and documents found in your content folder.

Frontmatter content is turned into JSON, so you can use the familiar Elm JSON decoding to get frontmatter content. And you'll get helpful error messages if any of your frontmatter is invalid (which will prevent an invalid production build from being created!).

It's up to you how you parse your metadata and content. Here's a simple example of a site that has two types of pages, blog posts and page (a regular page like /about or /).

content/index.md

---
type: page
title: Welcome to my site!
---
# Here's my site!

I built it with `elm-pages`! 🚀

content/blog/hello-world.md

---
type: blog
author: Dillon Kearns
title: Hello, World!
---
# Hello, World! 👋

## This will be parsed as markdown

Hello!!!
-- this example uses elm-explorations/markdown




import Html exposing (Html)
import Json.Decode as Decode exposing (Decoder)
import Markdown
import Pages.Document


type Metadata
    = Blog { title : String, description : String, author : String }
    | Page { title : String }


markdownDocument : ( String, Pages.Document.DocumentHandler Metadata (Html msg) )
markdownDocument =
    Pages.Document.parser
        { extension = "md"
        , metadata = frontmatterDecoder
        , body = Markdown.toHtml []
        }


frontmatterDecoder : Decoder Metadata
frontmatterDecoder =
    Decode.field "type" Decode.string
        |> Decode.andThen
            (\metadataType ->
                case metadataType of
                    "blog" ->
                        Decode.map3 (\title description author -> { title = title, description = description, author = author })
                            (Decode.field "title" Decode.string)
                            (Decode.field "description" Decode.string)
                            (Decode.field "author" Decode.string)


                    "page" ->
                        Decode.map (\title -> { title = title })
                            (Decode.field "title" Decode.string)
            )
type Document metadata view

Represents all of the DocumentHandlers. You register a handler for each extension that tells it how to parse frontmatter and content for that extension.

type DocumentHandler metadata view

How to parse the frontmatter and content for a given extension. Build one using Document.parser (see above for an example).

{ extension : String
, metadata : Decoder metadata
, body : String -> Result String view
}
-> ( String, DocumentHandler metadata view )

Create a Document Handler for the given extension.

Document metadata
-> Document view
-> ( String, DocumentHandler metadata view )

Register an elm-markup parser for your .emu files.

Functions for use by generated code

List ( String, DocumentHandler metadata view )
-> Document metadata view

Used by the generated Pages.elm module. There's no need to use this outside of the generated code.

get :
String
-> Document metadata view
-> Maybe
{ frontmatterParser : String -> Result String metadata
, contentParser : String -> Result String view
}

Used by the generated Pages.elm module. There's no need to use this outside of the generated code.