diff --git a/compiler/src/Generate/Html.hs b/compiler/src/Generate/Html.hs index e6470068d..7417a0771 100644 --- a/compiler/src/Generate/Html.hs +++ b/compiler/src/Generate/Html.hs @@ -71,9 +71,12 @@ sandwich_ root moduleName javascript = customHead else "" <> name <> "" + + htmlTag = Lamdera.unsafe $ Lamdera.Live.lamderaHtmlLang root in - [r| - + "\n" + <> htmlTag + <> [r| diff --git a/extra/Lamdera/Live.hs b/extra/Lamdera/Live.hs index 7aa7e0d40..eef29e02b 100644 --- a/extra/Lamdera/Live.hs +++ b/extra/Lamdera/Live.hs @@ -6,7 +6,8 @@ module Lamdera.Live where import qualified Data.ByteString as BS import qualified Data.ByteString.Builder as B -import qualified Data.Text.Encoding as T +import qualified Data.Text as T +import qualified Data.Text.Encoding as TE import qualified System.Directory as Dir import System.FilePath as FP import Control.Exception (finally, throw) @@ -40,16 +41,16 @@ lamderaLiveSrc = overrideM <- readUtf8Text overridePathBuilt case overrideM of Just override -> do - pure (T.encodeUtf8Builder override) + pure (TE.encodeUtf8Builder override) Nothing -> do Lamdera.debug $ "Couldn't load override " ++ overridePath ++ ", using compiled lamderaLive" - pure (T.encodeUtf8Builder (T.decodeUtf8 lamderaLive)) + pure (TE.encodeUtf8Builder (TE.decodeUtf8 lamderaLive)) else do Lamdera.debug $ "Couldn't find override " ++ overridePath ++ ", using compiled lamderaLive" - pure (T.encodeUtf8Builder (T.decodeUtf8 lamderaLive)) + pure (TE.encodeUtf8Builder (TE.decodeUtf8 lamderaLive)) else do Lamdera.debug $ "🗿 Using compiled lamderaLive" - pure (T.encodeUtf8Builder (T.decodeUtf8 lamderaLive)) + pure (TE.encodeUtf8Builder (TE.decodeUtf8 lamderaLive)) -- @TODO means we have to restart live for any changes... how to improve that? @@ -58,12 +59,31 @@ lamderaLiveHead root = do headHtmlM <- readUtf8Text $ root "head.html" case headHtmlM of Just headHtml -> - pure (True, T.encodeUtf8Builder headHtml) + pure (True, TE.encodeUtf8Builder headHtml) Nothing -> pure (False, "") +lamderaHtmlLang :: FilePath -> IO B.Builder +lamderaHtmlLang root = do + langM <- readUtf8Text $ root "html-lang" + pure $ maybe "" toHtmlTag langM + where + toHtmlTag lang = + let trimmed = T.strip lang in + if T.null trimmed + then "" + else " TE.encodeUtf8Builder (escapeHtmlAttr trimmed) <> "\">" + + escapeHtmlAttr = T.concatMap $ \c -> case c of + '&' -> "&" + '"' -> """ + '<' -> "<" + '>' -> ">" + _ -> T.singleton c + + lamderaLive :: BS.ByteString lamderaLive = $(bsToExp =<< runIO (Lamdera.Relative.readByteString "extra/dist/live.js"))