diff options
author | gwern <gwern@gwern.net> | 2017-07-05 04:07:50 -0400 |
---|---|---|
committer | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2017-07-05 10:07:50 +0200 |
commit | 3fcd4b77c0a283946f60c66f82aa0e1c44139c10 (patch) | |
tree | 99de3c0e2d18662ad0a5c08f9271534e4939bd52 /lib/Hakyll/Web | |
parent | 012ee7facfe2e17a14523ebbed1459129fd4b390 (diff) | |
download | hakyll-3fcd4b77c0a283946f60c66f82aa0e1c44139c10.tar.gz |
Redirect: add checks for non-overlapping definitions
Diffstat (limited to 'lib/Hakyll/Web')
-rw-r--r-- | lib/Hakyll/Web/Redirect.hs | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/lib/Hakyll/Web/Redirect.hs b/lib/Hakyll/Web/Redirect.hs index 4760cff..283d8bf 100644 --- a/lib/Hakyll/Web/Redirect.hs +++ b/lib/Hakyll/Web/Redirect.hs @@ -7,8 +7,9 @@ module Hakyll.Web.Redirect ) where import Control.Applicative ((<$>)) -import Control.Monad (forM_) +import Control.Monad (forM_, when) import Data.Binary (Binary (..)) +import Data.List (nub) import Hakyll.Core.Compiler import Hakyll.Core.Identifier import Hakyll.Core.Routes @@ -55,6 +56,10 @@ import Hakyll.Core.Writable (Writable (..)) -- See also <https://groups.google.com/d/msg/hakyll/sWc6zxfh-uM/fUpZPsFNDgAJ>. createRedirects :: [(Identifier, String)] -> Rules () createRedirects redirects = + do -- redirects are many-to-fewer; keys must be unique, and must point somewhere else: + let keys = map fst redirects + when (length keys /= length (nub keys)) $ error "Overlapping 301 redirects; check your mapping for redundant keys" + when (or $ map (\(r,t) -> toFilePath r == t) redirects) $ error "Self-redirect detected: at least one redirect's target points to itself." forM_ redirects $ \(ident, to) -> create [ident] $ do route idRoute @@ -81,6 +86,7 @@ instance Writable Redirect where redirectToHtml :: Redirect -> String redirectToHtml (Redirect working) = "<!DOCTYPE html><html><head><meta charset=\"utf-8\"/><meta name=\"generator\" content=\"hakyll\"/>" ++ + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">" ++ "<meta http-equiv=\"refresh\" content=\"0; url=" ++ working ++ "\"><link rel=\"canonical\" href=\"" ++ working ++ "\"><title>Permanent Redirect</title></head><body><p>The page has moved to: <a href=\"" ++ working ++ |