From 6a067671200f896c2a9b10a31aab307f88943973 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Sat, 29 Oct 2022 19:38:19 +0530 Subject: [PATCH] Include optional parameter for htmlToString configuration. --- codegen/GenerateMain.elm | 18 +++++++++++++++--- examples/base-path/app/Api.elm | 2 +- examples/docs/app/Api.elm | 2 +- examples/end-to-end/app/Api.elm | 2 +- examples/escaping/src/Api.elm | 2 +- examples/hackernews/app/Api.elm | 2 +- examples/hello/app/Api.elm | 2 +- examples/pokedex/app/Api.elm | 2 +- examples/repos/src/Api.elm | 2 +- examples/routing/app/Api.elm | 2 +- examples/slides/src/Api.elm | 2 +- examples/smoothies/app/Api.elm | 2 +- examples/todos/app/Api.elm | 2 +- examples/trails/app/Api.elm | 2 +- generator/template/app/Api.elm | 2 +- src/HtmlPrinter.elm | 10 +++++++--- src/Pages/Internal/Platform/Cli.elm | 4 ++-- src/Pages/ProgramConfig.elm | 7 +++++-- 18 files changed, 43 insertions(+), 24 deletions(-) diff --git a/codegen/GenerateMain.elm b/codegen/GenerateMain.elm index e049c06a..d69e6e0d 100644 --- a/codegen/GenerateMain.elm +++ b/codegen/GenerateMain.elm @@ -1687,7 +1687,7 @@ otherFile routes phaseString = (Elm.Op.cons apiPatterns.reference (Elm.apply (Elm.value { name = "routes", importFrom = [ "Api" ], annotation = Nothing }) [ getStaticRoutes.reference - , fnIgnore (Elm.string "") + , Elm.fn2 ( "a", Nothing ) ( "b", Nothing ) (\_ _ -> Elm.string "") ] ) ) @@ -1713,7 +1713,7 @@ otherFile routes phaseString = (Elm.apply (Elm.value { name = "routes", importFrom = [ "Api" ], annotation = Nothing }) [ getStaticRoutes.reference - , fnIgnore (Elm.string "") + , Elm.fn2 ( "a", Nothing ) ( "b", Nothing ) (\_ _ -> Elm.string "") ] |> Gen.List.call_.map Gen.ApiRoute.values_.toJson ) @@ -1822,7 +1822,19 @@ otherFile routes phaseString = } globalHeadTags = Elm.Declare.fn "globalHeadTags" - ( "htmlToString", Type.function [ Gen.Html.annotation_.html Gen.Basics.annotation_.never ] Type.string |> Just ) + ( "htmlToString" + , Type.function + [ Type.maybe + (Type.record + [ ( "indent", Type.int ) + , ( "newLines", Type.bool ) + ] + ) + , Gen.Html.annotation_.html Gen.Basics.annotation_.never + ] + Type.string + |> Just + ) (\htmlToString -> Elm.Op.cons (Elm.value diff --git a/examples/base-path/app/Api.elm b/examples/base-path/app/Api.elm index cdcb9bbe..d58f313a 100644 --- a/examples/base-path/app/Api.elm +++ b/examples/base-path/app/Api.elm @@ -8,7 +8,7 @@ import Route exposing (Route) routes : DataSource (List Route) - -> (Html Never -> String) + -> (Maybe { indent : Int, newLines : Bool } -> Html Never -> String) -> List (ApiRoute.ApiRoute ApiRoute.Response) routes getStaticRoutes htmlToString = [] diff --git a/examples/docs/app/Api.elm b/examples/docs/app/Api.elm index 3508b245..da989937 100644 --- a/examples/docs/app/Api.elm +++ b/examples/docs/app/Api.elm @@ -21,7 +21,7 @@ import Time routes : DataSource (List Route) - -> (Html Never -> String) + -> (Maybe { indent : Int, newLines : Bool } -> Html Never -> String) -> List (ApiRoute.ApiRoute ApiRoute.Response) routes getStaticRoutes htmlToString = [ ApiRoute.succeed diff --git a/examples/end-to-end/app/Api.elm b/examples/end-to-end/app/Api.elm index ce9a086e..63d07ce8 100644 --- a/examples/end-to-end/app/Api.elm +++ b/examples/end-to-end/app/Api.elm @@ -22,7 +22,7 @@ import Xml.Decode routes : DataSource (List Route) - -> (Html Never -> String) + -> (Maybe { indent : Int, newLines : Bool } -> Html Never -> String) -> List (ApiRoute.ApiRoute ApiRoute.Response) routes getStaticRoutes htmlToString = let diff --git a/examples/escaping/src/Api.elm b/examples/escaping/src/Api.elm index cdcb9bbe..d58f313a 100644 --- a/examples/escaping/src/Api.elm +++ b/examples/escaping/src/Api.elm @@ -8,7 +8,7 @@ import Route exposing (Route) routes : DataSource (List Route) - -> (Html Never -> String) + -> (Maybe { indent : Int, newLines : Bool } -> Html Never -> String) -> List (ApiRoute.ApiRoute ApiRoute.Response) routes getStaticRoutes htmlToString = [] diff --git a/examples/hackernews/app/Api.elm b/examples/hackernews/app/Api.elm index f2bd750e..340328b5 100644 --- a/examples/hackernews/app/Api.elm +++ b/examples/hackernews/app/Api.elm @@ -10,7 +10,7 @@ import Site routes : DataSource (List Route) - -> (Html Never -> String) + -> (Maybe { indent : Int, newLines : Bool } -> Html Never -> String) -> List (ApiRoute.ApiRoute ApiRoute.Response) routes getStaticRoutes htmlToString = [ DataSource.succeed manifest |> Manifest.generator Site.canonicalUrl diff --git a/examples/hello/app/Api.elm b/examples/hello/app/Api.elm index 26b82d82..a58a3df2 100644 --- a/examples/hello/app/Api.elm +++ b/examples/hello/app/Api.elm @@ -9,7 +9,7 @@ import Route exposing (Route) routes : DataSource (List Route) - -> (Html Never -> String) + -> (Maybe { indent : Int, newLines : Bool } -> Html Never -> String) -> List (ApiRoute ApiRoute.Response) routes getStaticRoutes htmlToString = [] diff --git a/examples/pokedex/app/Api.elm b/examples/pokedex/app/Api.elm index 946d2177..3b2cc3bc 100644 --- a/examples/pokedex/app/Api.elm +++ b/examples/pokedex/app/Api.elm @@ -17,7 +17,7 @@ import Site routes : DataSource (List Route) - -> (Html Never -> String) + -> (Maybe { indent : Int, newLines : Bool } -> Html Never -> String) -> List (ApiRoute.ApiRoute ApiRoute.Response) routes getStaticRoutes htmlToString = [ --nonHybridRoute diff --git a/examples/repos/src/Api.elm b/examples/repos/src/Api.elm index cdcb9bbe..d58f313a 100644 --- a/examples/repos/src/Api.elm +++ b/examples/repos/src/Api.elm @@ -8,7 +8,7 @@ import Route exposing (Route) routes : DataSource (List Route) - -> (Html Never -> String) + -> (Maybe { indent : Int, newLines : Bool } -> Html Never -> String) -> List (ApiRoute.ApiRoute ApiRoute.Response) routes getStaticRoutes htmlToString = [] diff --git a/examples/routing/app/Api.elm b/examples/routing/app/Api.elm index cdcb9bbe..d58f313a 100644 --- a/examples/routing/app/Api.elm +++ b/examples/routing/app/Api.elm @@ -8,7 +8,7 @@ import Route exposing (Route) routes : DataSource (List Route) - -> (Html Never -> String) + -> (Maybe { indent : Int, newLines : Bool } -> Html Never -> String) -> List (ApiRoute.ApiRoute ApiRoute.Response) routes getStaticRoutes htmlToString = [] diff --git a/examples/slides/src/Api.elm b/examples/slides/src/Api.elm index cdcb9bbe..d58f313a 100644 --- a/examples/slides/src/Api.elm +++ b/examples/slides/src/Api.elm @@ -8,7 +8,7 @@ import Route exposing (Route) routes : DataSource (List Route) - -> (Html Never -> String) + -> (Maybe { indent : Int, newLines : Bool } -> Html Never -> String) -> List (ApiRoute.ApiRoute ApiRoute.Response) routes getStaticRoutes htmlToString = [] diff --git a/examples/smoothies/app/Api.elm b/examples/smoothies/app/Api.elm index 9c3b704d..1f25f8d0 100644 --- a/examples/smoothies/app/Api.elm +++ b/examples/smoothies/app/Api.elm @@ -17,7 +17,7 @@ import Site routes : DataSource (List Route) - -> (Html Never -> String) + -> (Maybe { indent : Int, newLines : Bool } -> Html Never -> String) -> List (ApiRoute.ApiRoute ApiRoute.Response) routes getStaticRoutes htmlToString = [ --nonHybridRoute diff --git a/examples/todos/app/Api.elm b/examples/todos/app/Api.elm index ec6a0157..91392893 100644 --- a/examples/todos/app/Api.elm +++ b/examples/todos/app/Api.elm @@ -10,7 +10,7 @@ import Site routes : DataSource (List Route) - -> (Html Never -> String) + -> (Maybe { indent : Int, newLines : Bool } -> Html Never -> String) -> List (ApiRoute.ApiRoute ApiRoute.Response) routes getStaticRoutes htmlToString = [ DataSource.succeed manifest |> Manifest.generator Site.canonicalUrl diff --git a/examples/trails/app/Api.elm b/examples/trails/app/Api.elm index 406f173b..28a67dab 100644 --- a/examples/trails/app/Api.elm +++ b/examples/trails/app/Api.elm @@ -17,7 +17,7 @@ import Site routes : DataSource (List Route) - -> (Html Never -> String) + -> (Maybe { indent : Int, newLines : Bool } -> Html Never -> String) -> List (ApiRoute.ApiRoute ApiRoute.Response) routes getStaticRoutes htmlToString = [ --nonHybridRoute diff --git a/generator/template/app/Api.elm b/generator/template/app/Api.elm index 26b82d82..a58a3df2 100644 --- a/generator/template/app/Api.elm +++ b/generator/template/app/Api.elm @@ -9,7 +9,7 @@ import Route exposing (Route) routes : DataSource (List Route) - -> (Html Never -> String) + -> (Maybe { indent : Int, newLines : Bool } -> Html Never -> String) -> List (ApiRoute ApiRoute.Response) routes getStaticRoutes htmlToString = [] diff --git a/src/HtmlPrinter.elm b/src/HtmlPrinter.elm index a89b36ed..ee0ad59f 100644 --- a/src/HtmlPrinter.elm +++ b/src/HtmlPrinter.elm @@ -8,15 +8,19 @@ import Test.Html.Internal.ElmHtml.ToString exposing (defaultFormatOptions, nodeT import VirtualDom -htmlToString : Html msg -> String -htmlToString viewHtml = +htmlToString : Maybe { indent : Int, newLines : Bool } -> Html msg -> String +htmlToString formatOptions viewHtml = case Decode.decodeValue (decodeElmHtml (\_ _ -> VirtualDom.Normal (Decode.succeed ()))) (asJsonView viewHtml) of Ok str -> - nodeToStringWithOptions defaultFormatOptions str + nodeToStringWithOptions + (formatOptions + |> Maybe.withDefault defaultFormatOptions + ) + str Err err -> "Error pre-rendering HTML in HtmlPrinter.elm: " ++ Decode.errorToString err diff --git a/src/Pages/Internal/Platform/Cli.elm b/src/Pages/Internal/Platform/Cli.elm index 350f1549..33721647 100644 --- a/src/Pages/Internal/Platform/Cli.elm +++ b/src/Pages/Internal/Platform/Cli.elm @@ -880,7 +880,7 @@ sendSinglePageProgress site contentJson config model info = , headers = record.headers } { head = config.view Dict.empty Dict.empty Nothing currentPage Nothing sharedData pageData Nothing |> .head - , view = viewValue.body |> List.map HtmlPrinter.htmlToString |> String.join "\n" + , view = viewValue.body |> List.map (HtmlPrinter.htmlToString Nothing) |> String.join "\n" , title = viewValue.title } ) @@ -1221,7 +1221,7 @@ render404Page config sharedData model path notFoundReason = bodyToString : List (Html msg) -> String bodyToString body = - body |> List.map HtmlPrinter.htmlToString |> String.join "\n" + body |> List.map (HtmlPrinter.htmlToString Nothing) |> String.join "\n" urlToRoute : ProgramConfig userMsg userModel route pageData actionData sharedData effect mappedMsg errorPage -> Url -> route diff --git a/src/Pages/ProgramConfig.elm b/src/Pages/ProgramConfig.elm index a13a92e5..cf2b0c45 100644 --- a/src/Pages/ProgramConfig.elm +++ b/src/Pages/ProgramConfig.elm @@ -88,7 +88,10 @@ type alias ProgramConfig userMsg userModel route pageData actionData sharedData } -> userMsg , apiRoutes : - (Html Never -> String) + (Maybe { indent : Int, newLines : Bool } + -> Html Never + -> String + ) -> List (ApiRoute.ApiRoute ApiRoute.Response) , pathPatterns : List RoutePattern , basePath : List String @@ -98,7 +101,7 @@ type alias ProgramConfig userMsg userModel route pageData actionData sharedData , encodeResponse : ResponseSketch pageData actionData sharedData -> Bytes.Encode.Encoder , encodeAction : actionData -> Bytes.Encode.Encoder , decodeResponse : Bytes.Decode.Decoder (ResponseSketch pageData actionData sharedData) - , globalHeadTags : Maybe ((Html Never -> String) -> DataSource (List Head.Tag)) + , globalHeadTags : Maybe ((Maybe { indent : Int, newLines : Bool } -> Html Never -> String) -> DataSource (List Head.Tag)) , cmdToEffect : Cmd userMsg -> effect , perform : { fetchRouteData :