Added URL filter which removes 'index.html'.

This commit is contained in:
Wen Kokke 2020-10-28 12:17:56 +01:00
parent 39c3de6a99
commit 9776956c5e
2 changed files with 21 additions and 7 deletions

View file

@ -4,9 +4,11 @@ module Hakyll.Web.Routes.Permalink
( convertPermalink ( convertPermalink
, permalinkRoute , permalinkRoute
, permalinkRouteWithDefault , permalinkRouteWithDefault
, stripIndexFile
) where ) where
import Data.List (stripPrefix) import Data.List (stripPrefix)
import Data.List.Extra (stripSuffix)
import Data.Maybe (fromMaybe) import Data.Maybe (fromMaybe)
import Hakyll import Hakyll
import System.FilePath ((</>)) import System.FilePath ((</>))
@ -23,3 +25,8 @@ permalinkRouteWithDefault :: Routes -> Routes
permalinkRouteWithDefault def = metadataRoute $ \metadata -> permalinkRouteWithDefault def = metadataRoute $ \metadata ->
maybe def (constRoute . convertPermalink) (lookupString "permalink" 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)

View file

@ -176,7 +176,7 @@ main = do
>>= saveSnapshot "content" >>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/page.html" siteSectionContext >>= loadAndApplyTemplate "templates/page.html" siteSectionContext
>>= loadAndApplyTemplate "templates/default.html" siteSectionContext >>= loadAndApplyTemplate "templates/default.html" siteSectionContext
>>= relativizeUrls >>= prettifyUrls
-- Build compiler for literate Agda pages -- Build compiler for literate Agda pages
let pageWithAgdaCompiler :: CommandLineOptions -> Compiler (Item String) let pageWithAgdaCompiler :: CommandLineOptions -> Compiler (Item String)
@ -192,7 +192,7 @@ main = do
>>= saveSnapshot "content" >>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/page.html" siteSectionContext >>= loadAndApplyTemplate "templates/page.html" siteSectionContext
>>= loadAndApplyTemplate "templates/default.html" siteSectionContext >>= loadAndApplyTemplate "templates/default.html" siteSectionContext
>>= relativizeUrls >>= prettifyUrls
-- Run Hakyll -- Run Hakyll
-- --
@ -241,7 +241,7 @@ main = do
<&> writeHTML5With siteWriterOptions <&> writeHTML5With siteWriterOptions
>>= loadAndApplyTemplate "templates/page.html" siteContext >>= loadAndApplyTemplate "templates/page.html" siteContext
>>= loadAndApplyTemplate "templates/default.html" siteContext >>= loadAndApplyTemplate "templates/default.html" siteContext
>>= relativizeUrls >>= prettifyUrls
match "src/**.metadata" $ match "src/**.metadata" $
compile getResourceBody compile getResourceBody
@ -256,7 +256,7 @@ main = do
>>= saveSnapshot "content" >>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/page.html" siteContext >>= loadAndApplyTemplate "templates/page.html" siteContext
>>= loadAndApplyTemplate "templates/default.html" siteContext >>= loadAndApplyTemplate "templates/default.html" siteContext
>>= relativizeUrls >>= prettifyUrls
match "authors/*.metadata" $ match "authors/*.metadata" $
compile getResourceBody compile getResourceBody
@ -271,7 +271,7 @@ main = do
>>= applyAsTemplate postListContext >>= applyAsTemplate postListContext
>>= loadAndApplyTemplate "templates/page.html" siteContext >>= loadAndApplyTemplate "templates/page.html" siteContext
>>= loadAndApplyTemplate "templates/default.html" siteContext >>= loadAndApplyTemplate "templates/default.html" siteContext
>>= relativizeUrls >>= prettifyUrls
match "posts/*" $ do match "posts/*" $ do
route $ setExtension "html" route $ setExtension "html"
@ -285,7 +285,7 @@ main = do
>>= saveSnapshot "content" >>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/post.html" postContext >>= loadAndApplyTemplate "templates/post.html" postContext
>>= loadAndApplyTemplate "templates/default.html" siteContext >>= loadAndApplyTemplate "templates/default.html" siteContext
>>= relativizeUrls >>= prettifyUrls
-- Compile sections using literate Agda -- Compile sections using literate Agda
match "src/**.lagda.md" $ do match "src/**.lagda.md" $ do
@ -355,7 +355,7 @@ main = do
match (fromGlob $ "versions" </> v </> "**.html") $ do match (fromGlob $ "versions" </> v </> "**.html") $ do
route $ gsubRoute "versions/" (const "") route $ gsubRoute "versions/" (const "")
compile $ getResourceBody compile $ getResourceBody
>>= relativizeUrls >>= prettifyUrls
-- Copy other files -- Copy other files
match (fromGlob $ "versions" </> v </> "**") $ do match (fromGlob $ "versions" </> v </> "**") $ do
@ -427,3 +427,10 @@ compilePandocTemplate i = do
contentField :: String -> Snapshot -> Context String contentField :: String -> Snapshot -> Context String
contentField key snapshot = field key $ \item -> contentField key snapshot = field key $ \item ->
itemBody <$> loadSnapshot (itemIdentifier item) snapshot itemBody <$> loadSnapshot (itemIdentifier item) snapshot
--------------------------------------------------------------------------------
-- Relativise URLs and strip "index.html" suffixes
--------------------------------------------------------------------------------
prettifyUrls :: Item String -> Compiler (Item String)
prettifyUrls = relativizeUrls <=< withItemBody (return . stripIndexFile)