From 9776956c5e7ba63b354e7374b1d2bd9c0a27ca58 Mon Sep 17 00:00:00 2001 From: Wen Kokke Date: Wed, 28 Oct 2020 12:17:56 +0100 Subject: [PATCH] Added URL filter which removes 'index.html'. --- hs/Hakyll/Web/Routes/Permalink.hs | 7 +++++++ hs/Main.hs | 21 ++++++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/hs/Hakyll/Web/Routes/Permalink.hs b/hs/Hakyll/Web/Routes/Permalink.hs index 68a89807..6f36603f 100644 --- a/hs/Hakyll/Web/Routes/Permalink.hs +++ b/hs/Hakyll/Web/Routes/Permalink.hs @@ -4,9 +4,11 @@ module Hakyll.Web.Routes.Permalink ( convertPermalink , permalinkRoute , permalinkRouteWithDefault + , stripIndexFile ) where import Data.List (stripPrefix) +import Data.List.Extra (stripSuffix) import Data.Maybe (fromMaybe) import Hakyll import System.FilePath (()) @@ -23,3 +25,8 @@ permalinkRouteWithDefault :: Routes -> Routes permalinkRouteWithDefault def = metadataRoute $ \metadata -> maybe def (constRoute . convertPermalink) (lookupString "permalink" metadata) +-- Removes "index.html" from URLs. +stripIndexFile :: String -> String +stripIndexFile = withUrls dir + where + dir link = fromMaybe link (stripSuffix "index.html" link) diff --git a/hs/Main.hs b/hs/Main.hs index 9ee9c91d..ed3ad905 100644 --- a/hs/Main.hs +++ b/hs/Main.hs @@ -176,7 +176,7 @@ main = do >>= saveSnapshot "content" >>= loadAndApplyTemplate "templates/page.html" siteSectionContext >>= loadAndApplyTemplate "templates/default.html" siteSectionContext - >>= relativizeUrls + >>= prettifyUrls -- Build compiler for literate Agda pages let pageWithAgdaCompiler :: CommandLineOptions -> Compiler (Item String) @@ -192,7 +192,7 @@ main = do >>= saveSnapshot "content" >>= loadAndApplyTemplate "templates/page.html" siteSectionContext >>= loadAndApplyTemplate "templates/default.html" siteSectionContext - >>= relativizeUrls + >>= prettifyUrls -- Run Hakyll -- @@ -241,7 +241,7 @@ main = do <&> writeHTML5With siteWriterOptions >>= loadAndApplyTemplate "templates/page.html" siteContext >>= loadAndApplyTemplate "templates/default.html" siteContext - >>= relativizeUrls + >>= prettifyUrls match "src/**.metadata" $ compile getResourceBody @@ -256,7 +256,7 @@ main = do >>= saveSnapshot "content" >>= loadAndApplyTemplate "templates/page.html" siteContext >>= loadAndApplyTemplate "templates/default.html" siteContext - >>= relativizeUrls + >>= prettifyUrls match "authors/*.metadata" $ compile getResourceBody @@ -271,7 +271,7 @@ main = do >>= applyAsTemplate postListContext >>= loadAndApplyTemplate "templates/page.html" siteContext >>= loadAndApplyTemplate "templates/default.html" siteContext - >>= relativizeUrls + >>= prettifyUrls match "posts/*" $ do route $ setExtension "html" @@ -285,7 +285,7 @@ main = do >>= saveSnapshot "content" >>= loadAndApplyTemplate "templates/post.html" postContext >>= loadAndApplyTemplate "templates/default.html" siteContext - >>= relativizeUrls + >>= prettifyUrls -- Compile sections using literate Agda match "src/**.lagda.md" $ do @@ -355,7 +355,7 @@ main = do match (fromGlob $ "versions" v "**.html") $ do route $ gsubRoute "versions/" (const "") compile $ getResourceBody - >>= relativizeUrls + >>= prettifyUrls -- Copy other files match (fromGlob $ "versions" v "**") $ do @@ -427,3 +427,10 @@ compilePandocTemplate i = do contentField :: String -> Snapshot -> Context String contentField key snapshot = field key $ \item -> itemBody <$> loadSnapshot (itemIdentifier item) snapshot + +-------------------------------------------------------------------------------- +-- Relativise URLs and strip "index.html" suffixes +-------------------------------------------------------------------------------- + +prettifyUrls :: Item String -> Compiler (Item String) +prettifyUrls = relativizeUrls <=< withItemBody (return . stripIndexFile)