Add starting point for function to extract StaticRequests for each page.

This commit is contained in:
Dillon Kearns 2019-10-19 10:02:28 -07:00
parent 40089da983
commit 899f872223
3 changed files with 105 additions and 21 deletions

View File

@ -155,14 +155,17 @@ pageViewOrError pathKey viewFn model cache =
} }
Err error -> Err error ->
{ title = "Parsing error" Debug.todo "asdf"
, body = Html.text error
}
ContentCache.NeedContent extension _ -> -- { title = "Parsing error"
-- , body = Html.text error
-- }
ContentCache.NeedContent extension a ->
{ title = "", body = Html.text "" } { title = "", body = Html.text "" }
ContentCache.Unparsed extension _ _ -> -- Debug.todo (Debug.toString a)
ContentCache.Unparsed extension a b ->
-- Debug.todo (Debug.toString b)
{ title = "", body = Html.text "" } { title = "", body = Html.text "" }
Nothing -> Nothing ->
@ -335,7 +338,8 @@ init pathKey canonicalSiteUrl document toJsPort viewFn content initUserModel fla
-- Debug.todo error -- Debug.todo error
-- Cmd.none -- Cmd.none
_ -> _ ->
Cmd.none -- Cmd.none
Debug.todo "Error"
( maybePagePath, maybeMetadata ) = ( maybePagePath, maybeMetadata ) =
case ContentCache.lookupMetadata pathKey (Ok okCache) url of case ContentCache.lookupMetadata pathKey (Ok okCache) url of
@ -401,7 +405,7 @@ update :
-> ModelDetails userModel metadata view -> ModelDetails userModel metadata view
-> ( ModelDetails userModel metadata view, Cmd (Msg userMsg metadata view) ) -> ( ModelDetails userModel metadata view, Cmd (Msg userMsg metadata view) )
update pathKey onPageChangeMsg toJsPort document userUpdate msg model = update pathKey onPageChangeMsg toJsPort document userUpdate msg model =
case msg of case msg |> Debug.log "msg" of
LinkClicked urlRequest -> LinkClicked urlRequest ->
case urlRequest of case urlRequest of
Browser.Internal url -> Browser.Internal url ->
@ -590,6 +594,13 @@ cliApplication config =
Platform.worker Platform.worker
{ init = { init =
\flags -> \flags ->
let
okCache =
Debug.todo ""
_ =
Debug.log "@@@@@@@@@@@@@@@@@@ log from cli"
in
( CliModel ( CliModel
, case contentCache of , case contentCache of
Ok _ -> Ok _ ->
@ -599,6 +610,7 @@ cliApplication config =
(Json.Encode.object (Json.Encode.object
[ ( "errors", encodeErrors pageErrors ) [ ( "errors", encodeErrors pageErrors )
, ( "manifest", Manifest.toJson config.manifest ) , ( "manifest", Manifest.toJson config.manifest )
, ( "pages", Json.Encode.string "pages!" )
] ]
) )
@ -606,6 +618,7 @@ cliApplication config =
config.toJsPort config.toJsPort
(Json.Encode.object (Json.Encode.object
[ ( "manifest", Manifest.toJson config.manifest ) [ ( "manifest", Manifest.toJson config.manifest )
, ( "pages", Json.Encode.string "pages!" )
] ]
) )
@ -622,6 +635,78 @@ cliApplication config =
} }
staticResponseForPage :
List ( PagePath pathKey, metadata )
->
(List ( PagePath pathKey, metadata )
->
{ path : PagePath pathKey
, frontmatter : metadata
}
->
( StaticHttp.Request
, Decode.Value
->
Result String
{ view :
userModel
-> view
->
{ title : String
, body : Html userMsg
}
, head : List (Head.Tag pathKey)
}
)
)
-> pathKey
-> Result (List String) (List ( PagePath pathKey, String ))
staticResponseForPage siteMetadata viewFn pathKey =
siteMetadata
|> List.map
(\( pagePath, frontmatter ) ->
let
thing =
viewFn siteMetadata
{ path = pagePath
, frontmatter = frontmatter
}
in
Ok ( pagePath, "" )
)
|> combine
combine : List (Result error ( key, success )) -> Result (List error) (List ( key, success ))
combine list =
list
|> List.foldr resultFolder (Ok [])
resultFolder : Result error a -> Result (List error) (List a) -> Result (List error) (List a)
resultFolder current soFarResult =
case soFarResult of
Ok soFarOk ->
case current of
Ok currentOk ->
currentOk
:: soFarOk
|> Ok
Err error ->
Err [ error ]
Err soFarErr ->
case current of
Ok currentOk ->
Err soFarErr
Err error ->
error
:: soFarErr
|> Err
encodeErrors errors = encodeErrors errors =
errors errors
|> Json.Encode.dict |> Json.Encode.dict

View File

@ -130,14 +130,14 @@ view siteMetadata page =
case page.frontmatter of case page.frontmatter of
Metadata.Page metadata -> Metadata.Page metadata ->
StaticHttp.withData "" StaticHttp.withData ""
(Decode.succeed "abcdefg") (Decode.field "stargazers_count" Decode.int)
(\staticData -> (\staticData ->
{ view = { view =
\model viewForPage -> \model viewForPage ->
{ title = metadata.title { title = metadata.title
, body = , body =
"The value is: " "elm-pages 's: "
++ staticData ++ String.fromInt staticData
|> Element.text |> Element.text
|> wrapBody |> wrapBody
} }

View File

@ -1,5 +1,5 @@
module.exports = function(markdown, markup, includeBody) { module.exports = function(markdown, markup, includeBody) {
return `content : List ( List String, { extension: String, frontMatter : String, body : Maybe String, staticData : Maybe String } ) return `content : List ( List String, { extension: String, frontMatter : String, body : Maybe String } )
content = content =
[ ${markdown.concat(markup).map(entry => toEntry(entry, includeBody))} [ ${markdown.concat(markup).map(entry => toEntry(entry, includeBody))}
]`; ]`;
@ -17,7 +17,6 @@ function toEntry(entry, includeBody) {
( [${fullPath.join(", ")}] ( [${fullPath.join(", ")}]
, { frontMatter = """${entry.metadata} , { frontMatter = """${entry.metadata}
""" , body = ${body(entry, includeBody)} """ , body = ${body(entry, includeBody)}
, staticData = ${staticData(entry, includeBody)}
, extension = "${entry.extension}" , extension = "${entry.extension}"
} ) } )
`; `;
@ -33,12 +32,12 @@ function body(entry, includeBody) {
} }
} }
function staticData(entry, includeBody) { // function staticData(entry, includeBody) {
if (includeBody) { // if (includeBody) {
return `Just """${entry.staticData.replace(/\\/g, "\\\\")} // return `Just """${entry.staticData.replace(/\\/g, "\\\\")}
""" // """
`; // `;
} else { // } else {
return `Nothing`; // return `Nothing`;
} // }
} // }