Added the Sanitize module.
authorsabadev <saba@sabadev.xyz>
Sat, 27 Mar 2021 15:04:23 +0000 (11:04 -0400)
committersabadev <dev@sabadev.xyz>
Tue, 13 Apr 2021 01:16:41 +0000 (21:16 -0400)
src/Html.hs
src/Sanitize.hs [new file with mode: 0644]

index 50cf7b0..2f5f80c 100644 (file)
@@ -5,6 +5,7 @@ import Control.Monad (void)
 import Control.Monad.IO.Class (MonadIO(..), liftIO)
 import Data.Maybe (fromMaybe)
 import Lucid
+import Sanitize
 import Servant
 import System.Directory (getDirectoryContents)
 import qualified Data.Text as T
@@ -13,7 +14,7 @@ import qualified Data.Text.IO as T
 htmlContainer :: (MonadIO m) => Maybe Theme -> Html a -> m (Html ())
 htmlContainer theme contents = do
   nav <- navigation theme
-  pure $ void $ with doctypehtml_ [lang_ "en"] $ do
+  pure $ sanitizeHtml $ void $ with doctypehtml_ [lang_ "en"] $ do
     head_ $ do
       title_ $ toHtml siteTitle
       meta_ [charset_ "utf8"]
diff --git a/src/Sanitize.hs b/src/Sanitize.hs
new file mode 100644 (file)
index 0000000..734939f
--- /dev/null
@@ -0,0 +1,11 @@
+module Sanitize where
+
+import Lucid (Html(..), renderText, toHtmlRaw)
+import Text.HTML.TagSoup.Tree (parseTree, renderTree, transformTree, TagTree(..))
+import qualified Data.Text.Lazy as T
+
+sanitizeHtml :: Html () -> Html ()
+sanitizeHtml = toHtmlRaw . renderTree . transformTree sanitizeTree . parseTree . renderText
+
+sanitizeTree :: TagTree T.Text -> [TagTree T.Text]
+sanitizeTree = pure