Extract updated MarkdownCodec helper, and use the new HTML-free helper for Docs to fix Lamdera Wire encoding error.

This commit is contained in:
Dillon Kearns 2022-01-26 10:57:19 -08:00
parent fe60b7c812
commit 0ae2caaad8
3 changed files with 19 additions and 43 deletions

View File

@ -237,45 +237,12 @@ type alias Data =
data : RouteParams -> DataSource Data
data route =
withFrontmatter Data
MarkdownCodec.withFrontmatter Data
frontmatterDecoder
TailwindMarkdownRenderer.renderer
("content/blog/" ++ route.slug ++ ".md")
withFrontmatter :
(frontmatter -> List Markdown.Block.Block -> value)
-> Decoder frontmatter
-> Markdown.Renderer.Renderer view
-> String
-> DataSource value
withFrontmatter constructor frontmatterDecoder_ renderer filePath =
DataSource.map2 constructor
(DataSource.File.onlyFrontmatter
frontmatterDecoder_
filePath
)
(DataSource.File.bodyWithoutFrontmatter
filePath
|> DataSource.andThen
(\rawBody ->
rawBody
|> Markdown.Parser.parse
|> Result.mapError (\_ -> "Couldn't parse markdown.")
|> DataSource.fromResult
)
|> DataSource.andThen
(\blocks ->
blocks
|> Markdown.Renderer.render renderer
-- we don't want to encode the HTML since it contains functions so it's not serializable
-- but we can at least make sure there are no errors turning it into HTML before encoding it
|> Result.map (\_ -> blocks)
|> DataSource.fromResult
)
)
type alias ArticleMetadata =
{ title : String
, description : String

View File

@ -16,6 +16,7 @@ import Json.Decode.Extra
import List.Extra
import Markdown.Block as Block exposing (Block)
import Markdown.Parser
import Markdown.Renderer
import MarkdownCodec
import NextPrevious
import Page exposing (Page, PageWithState, StaticPayload)
@ -184,7 +185,7 @@ head static =
type alias Data =
{ body : List (Html Msg)
{ body : List Block
, titles : { title : String, previousAndNext : ( Maybe NextPrevious.Item, Maybe NextPrevious.Item ) }
, editUrl : String
, metadata : { title : String, description : String }
@ -241,7 +242,10 @@ view maybeUrl sharedModel static =
, Bp.xl [ Tw.pr_36 ]
]
]
(static.data.body
((static.data.body
|> Markdown.Renderer.render TailwindMarkdownRenderer.renderer
|> Result.withDefault []
)
++ [ NextPrevious.view static.data.titles.previousAndNext
, Html.hr [] []
, Html.footer
@ -285,7 +289,7 @@ filePathDataSource routeParams =
Glob.expectUniqueMatch (findBySlug slug)
pageBody : RouteParams -> DataSource (List (Html msg))
pageBody : RouteParams -> DataSource (List Block)
pageBody routeParams =
routeParams
|> filePathDataSource

View File

@ -166,7 +166,7 @@ titleFromFrontmatter filePath =
withoutFrontmatter :
Markdown.Renderer.Renderer view
-> String
-> DataSource (List view)
-> DataSource (List Block)
withoutFrontmatter renderer filePath =
(filePath
|> StaticFile.bodyWithoutFrontmatter
@ -182,23 +182,26 @@ withoutFrontmatter renderer filePath =
(\blocks ->
blocks
|> Markdown.Renderer.render renderer
-- we don't want to encode the HTML since it contains functions so it's not serializable
-- but we can at least make sure there are no errors turning it into HTML before encoding it
|> Result.map (\_ -> blocks)
|> DataSource.fromResult
)
withFrontmatter :
(frontmatter -> List view -> value)
(frontmatter -> List Block -> value)
-> Decoder frontmatter
-> Markdown.Renderer.Renderer view
-> String
-> DataSource value
withFrontmatter constructor frontmatterDecoder renderer filePath =
withFrontmatter constructor frontmatterDecoder_ renderer filePath =
DataSource.map2 constructor
(StaticFile.onlyFrontmatter
frontmatterDecoder
frontmatterDecoder_
filePath
)
((StaticFile.bodyWithoutFrontmatter
(StaticFile.bodyWithoutFrontmatter
filePath
|> DataSource.andThen
(\rawBody ->
@ -207,11 +210,13 @@ withFrontmatter constructor frontmatterDecoder renderer filePath =
|> Result.mapError (\_ -> "Couldn't parse markdown.")
|> DataSource.fromResult
)
)
|> DataSource.andThen
(\blocks ->
blocks
|> Markdown.Renderer.render renderer
-- we don't want to encode the HTML since it contains functions so it's not serializable
-- but we can at least make sure there are no errors turning it into HTML before encoding it
|> Result.map (\_ -> blocks)
|> DataSource.fromResult
)
)