From 1c5749d1cf99765fc17a1823a78cc1a6cc1148f5 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Mon, 9 Sep 2019 15:17:19 -0700 Subject: [PATCH] Add published metadata to blog posts. --- .../content/blog/types-over-conventions.md | 10 ++- examples/docs/content/index.md | 1 + examples/docs/content/markdown.md | 14 ---- examples/docs/elm.json | 1 + examples/docs/gen/PagesNew.elm | 14 +--- examples/docs/src/Main.elm | 65 ++++++++++++------- examples/docs/src/Metadata.elm | 48 ++------------ 7 files changed, 59 insertions(+), 94 deletions(-) delete mode 100644 examples/docs/content/markdown.md diff --git a/examples/docs/content/blog/types-over-conventions.md b/examples/docs/content/blog/types-over-conventions.md index f92d365a..dfa1e2f9 100644 --- a/examples/docs/content/blog/types-over-conventions.md +++ b/examples/docs/content/blog/types-over-conventions.md @@ -1,7 +1,11 @@ --- -author: Dillon Kearns -title: Types Over Conventions -description: TODO +{ + "type": "blog", + "author": "Dillon Kearns", + "title": "Types Over Conventions", + "description": "TODO", + "published": "2019-09-09", +} --- Rails started a movement of simplifying project setup with [a philosophy of "Convention Over Configuration"](https://rubyonrails.org/doctrine/#convention-over-configuration). This made for a very easy experience bootstrapping a new web server. The downside is that you have a lot of implicit rules that can be hard to follow. diff --git a/examples/docs/content/index.md b/examples/docs/content/index.md index 5eb8ac9a..a62e0e77 100644 --- a/examples/docs/content/index.md +++ b/examples/docs/content/index.md @@ -1,5 +1,6 @@ --- title: elm-pages - a statically typed site generator +type: page --- A **statically typed** site generator diff --git a/examples/docs/content/markdown.md b/examples/docs/content/markdown.md deleted file mode 100644 index 53c5d9ac..00000000 --- a/examples/docs/content/markdown.md +++ /dev/null @@ -1,14 +0,0 @@ ---- -title: Hello from markdown! 👋 ---- - -# Welcome to `elm-pages`! 🚀 - -`elm-pages` - -Looks like it's working! - -Here's a list: - -- This is an item -- And so is this diff --git a/examples/docs/elm.json b/examples/docs/elm.json index 8fef6a8f..447c6bcc 100644 --- a/examples/docs/elm.json +++ b/examples/docs/elm.json @@ -22,6 +22,7 @@ "elm-community/result-extra": "2.2.1", "elm-community/string-extra": "4.0.1", "elm-explorations/markdown": "1.0.0", + "justinmimbs/date": "3.1.2", "lukewestby/elm-string-interpolate": "1.0.3", "mdgriffith/elm-markup": "3.0.1", "mdgriffith/elm-ui": "1.1.4", diff --git a/examples/docs/gen/PagesNew.elm b/examples/docs/gen/PagesNew.elm index f393c810..34324e85 100644 --- a/examples/docs/gen/PagesNew.elm +++ b/examples/docs/gen/PagesNew.elm @@ -65,7 +65,6 @@ allPages = , (buildPage [ "docs", "directory-structure" ]) , (buildPage [ "docs" ]) , (buildPage [ ]) - , (buildPage [ "markdown" ]) ] pages = @@ -79,8 +78,7 @@ pages = , all = [ (buildPage [ "docs", "directory-structure" ]), (buildPage [ "docs" ]) ] } , index = (buildPage [ ]) - , markdown = (buildPage [ "markdown" ]) - , all = [ (buildPage [ ]), (buildPage [ "markdown" ]) ] + , all = [ (buildPage [ ]) ] } images = @@ -121,7 +119,7 @@ content : List ( List String, { extension: String, frontMatter : String, body : content = [ ( ["blog", "types-over-conventions"] - , { frontMatter = """{"author":"Dillon Kearns","title":"Types Over Conventions","description":"TODO"} + , { frontMatter = """{"type":"blog","author":"Dillon Kearns","title":"Types Over Conventions","description":"TODO","published":"2019-09-09"} """ , body = Nothing , extension = "md" } ) @@ -139,13 +137,7 @@ content = } ) , ( [] - , { frontMatter = """{"title":"elm-pages - a statically typed site generator"} -""" , body = Nothing - , extension = "md" - } ) - , - ( ["markdown"] - , { frontMatter = """{"title":"Hello from markdown! 👋"} + , { frontMatter = """{"title":"elm-pages - a statically typed site generator","type":"page"} """ , body = Nothing , extension = "md" } ) diff --git a/examples/docs/src/Main.elm b/examples/docs/src/Main.elm index 7968361d..9a6d3e22 100644 --- a/examples/docs/src/Main.elm +++ b/examples/docs/src/Main.elm @@ -1,6 +1,7 @@ module Main exposing (main) import Color +import Date import DocSidebar import DocumentSvg import Element exposing (Element) @@ -62,32 +63,48 @@ markdownDocument = Pages.Document.parser { extension = "md" , metadata = - Json.Decode.oneOf - [ Json.Decode.map3 - (\author title description -> - Metadata.Article - { author = author - , title = title - , description = description - } - ) - (Json.Decode.field "author" Json.Decode.string) - (Json.Decode.field "title" Json.Decode.string) - (Json.Decode.field "description" Json.Decode.string) - , Json.Decode.map2 - (\title maybeType -> - case maybeType of - Just "doc" -> - Metadata.Doc { title = title } + Json.Decode.field "type" Json.Decode.string + |> Json.Decode.andThen + (\pageType -> + case pageType of + "doc" -> + Json.Decode.field "title" Json.Decode.string + |> Json.Decode.map (\title -> Metadata.Doc { title = title }) + + "page" -> + Json.Decode.field "title" Json.Decode.string + |> Json.Decode.map (\title -> Metadata.Page { title = title }) + + "blog" -> + Json.Decode.map4 + (\author title description published -> + Metadata.Article + { author = author + , title = title + , description = description + , published = published + } + ) + (Json.Decode.field "author" Json.Decode.string) + (Json.Decode.field "title" Json.Decode.string) + (Json.Decode.field "description" Json.Decode.string) + (Json.Decode.field "published" + (Json.Decode.string + |> Json.Decode.andThen + (\isoString -> + case Date.fromIsoString isoString of + Ok date -> + Json.Decode.succeed date + + Err error -> + Json.Decode.fail error + ) + ) + ) _ -> - Metadata.Page { title = title } + Json.Decode.fail <| "Unexpected page type " ++ pageType ) - (Json.Decode.field "title" Json.Decode.string) - (Json.Decode.field "type" Json.Decode.string - |> Json.Decode.maybe - ) - ] , body = MarkdownRenderer.view } @@ -296,7 +313,7 @@ head metadata = |> Seo.article { tags = [] , section = Nothing - , publishedTime = Nothing + , publishedTime = Just "May 13" , modifiedTime = Nothing , expirationTime = Nothing } diff --git a/examples/docs/src/Metadata.elm b/examples/docs/src/Metadata.elm index e369676c..e8cd3718 100644 --- a/examples/docs/src/Metadata.elm +++ b/examples/docs/src/Metadata.elm @@ -1,5 +1,6 @@ module Metadata exposing (ArticleMetadata, DocMetadata, Metadata(..), PageMetadata) +import Date exposing (Date) import Dict exposing (Dict) import Element exposing (Element) import Element.Font as Font @@ -12,7 +13,11 @@ type Metadata msg type alias ArticleMetadata = - { author : String, title : String, description : String } + { author : String + , title : String + , description : String + , published : Date + } type alias DocMetadata = @@ -22,44 +27,3 @@ type alias DocMetadata = type alias PageMetadata = { title : String } - - -gather : List { styled : Element msg, raw : String } -> { styled : List (Element msg), raw : String } -gather myList = - let - styled = - myList - |> List.map .styled - - raw = - myList - |> List.map .raw - |> String.join " " - in - { styled = styled, raw = raw } - - -viewText : { a | bold : Bool, italic : Bool, strike : Bool } -> String -> Element msg -viewText styles string = - Element.el (stylesFor styles) (Element.text string) - - -stylesFor : { a | bold : Bool, italic : Bool, strike : Bool } -> List (Element.Attribute b) -stylesFor styles = - [ if styles.bold then - Just Font.bold - - else - Nothing - , if styles.italic then - Just Font.italic - - else - Nothing - , if styles.strike then - Just Font.strike - - else - Nothing - ] - |> List.filterMap identity