Use Json decoding instead of Yaml decoding for markdown frontmatter.

This commit is contained in:
Dillon Kearns 2019-08-05 15:17:18 -07:00
parent 69a5018ca8
commit 728fef16af
3 changed files with 10 additions and 16 deletions

View File

@ -15,7 +15,6 @@ import Pages.Parser exposing (PageOrPost)
import Platform.Sub exposing (Sub) import Platform.Sub exposing (Sub)
import Result.Extra import Result.Extra
import Url exposing (Url) import Url exposing (Url)
import Yaml.Decode
type alias Content = type alias Content =
@ -110,7 +109,7 @@ combineTupleResults input =
init : init :
(String -> view) (String -> view)
-> Yaml.Decode.Decoder metadata -> Json.Decode.Decoder metadata
-> (Json.Encode.Value -> Cmd (Msg userMsg)) -> (Json.Encode.Value -> Cmd (Msg userMsg))
-> (metadata -> List Head.Tag) -> (metadata -> List Head.Tag)
-> Parser metadata view -> Parser metadata view
@ -136,16 +135,11 @@ init markdownToHtml frontmatterParser toJsPort head parser content initUserModel
|> List.map |> List.map
(Tuple.mapSecond (Tuple.mapSecond
(\{ frontMatter, body } -> (\{ frontMatter, body } ->
Yaml.Decode.fromString frontmatterParser frontMatter Json.Decode.decodeString frontmatterParser frontMatter
|> Result.map (\parsedFrontmatter -> { parsedFrontmatter = parsedFrontmatter, body = body }) |> Result.map (\parsedFrontmatter -> { parsedFrontmatter = parsedFrontmatter, body = body })
|> Result.mapError |> Result.mapError
(\error -> (\error ->
case error of Html.text (Json.Decode.errorToString error)
Yaml.Decode.Parsing string ->
Html.text string
Yaml.Decode.Decoding string ->
Html.text string
) )
) )
) )
@ -268,7 +262,7 @@ application :
, content : Content , content : Content
, toJsPort : Json.Encode.Value -> Cmd (Msg userMsg) , toJsPort : Json.Encode.Value -> Cmd (Msg userMsg)
, head : metadata -> List Head.Tag , head : metadata -> List Head.Tag
, frontmatterParser : Yaml.Decode.Decoder metadata , frontmatterParser : Json.Decode.Decoder metadata
, markdownToHtml : String -> view , markdownToHtml : String -> view
} }
-> Program userFlags userModel userMsg metadata view -> Program userFlags userModel userMsg metadata view

View File

@ -13,7 +13,7 @@ content =
markdown : List ( List String, { frontMatter : String, body : String } ) markdown : List ( List String, { frontMatter : String, body : String } )
markdown = markdown =
[ ( ["markdown"] [ ( ["markdown"]
, { frontMatter = """title: This is a markdown article , { frontMatter = """{"title":"This is a markdown article"}
""" """
, body = """ , body = """
# Hey there 👋 # Hey there 👋

View File

@ -6,6 +6,7 @@ import Element exposing (Element)
import Element.Border import Element.Border
import Element.Font as Font import Element.Font as Font
import Html exposing (Html) import Html exposing (Html)
import Json.Decode
import Json.Encode import Json.Encode
import List.Extra import List.Extra
import Mark import Mark
@ -21,7 +22,6 @@ import Pages.Parser exposing (PageOrPost)
import RawContent import RawContent
import SocialMeta import SocialMeta
import Url exposing (Url) import Url exposing (Url)
import Yaml.Decode
port toJsPort : Json.Encode.Value -> Cmd msg port toJsPort : Json.Encode.Value -> Cmd msg
@ -60,11 +60,11 @@ markdownToHtml body =
|> Element.html |> Element.html
frontmatterParser : Yaml.Decode.Decoder (Metadata.Metadata msg) frontmatterParser : Json.Decode.Decoder (Metadata.Metadata msg)
frontmatterParser = frontmatterParser =
Yaml.Decode.field "title" Yaml.Decode.string Json.Decode.field "title" Json.Decode.string
|> Yaml.Decode.map Metadata.PageMetadata |> Json.Decode.map Metadata.PageMetadata
|> Yaml.Decode.map Metadata.Page |> Json.Decode.map Metadata.Page
type alias Model = type alias Model =