Skip to content

Commit 2574280

Browse files
LysxiaBinderDavid
authored andcommitted
Fix breadcrumbs
Issue: The breadcrumbs should link to the home page, but they currently link to the current page. The cause is that the `breadcrumbsField` function looks up the `"nav"` version of the parents, and then `indexlessUrlField` looks up their URL using `getRoute`, but the `"nav"` versions of the pages have no route. The fix is to use the default version of the parents instead. List of changes: 1. Remove now unused `"nav"` versions of the pages. I am guessing that the original reason for adding `"nav"` versions was to avoid a circular dependency (so that the message pages can link to the homepage which links to the messages). The dependency was introduced by the `load` function. But we can get the URL and title for the breadcrumbs without `load`. 2. Remove the `breadcrumbFields` function. On top of creating the "parents" field for the breadcrumbs, it adds a `messageTitleField` which appends the [GHC-XXXXXX] identifier. This was used in the messages pages and also the home page, where it just leaves the title unchanged. But that title was already available in `defaultContext`. Instead we add `messageTitleField` only in the messages pages and in the breadcrumbs (just in case, this is currently unused). 3. Simplify `breadcrumbCtx` to only get the url and title. 4. Refactor `indexlessUrlField` by reusing `urlField`. 5. Refactor `indexless` with an auxiliary `stripSuffix`. 6. Remove unused `breadcrumbField` in the `messages/examples/` pages.
1 parent d6a7d82 commit 2574280

File tree

1 file changed

+16
-37
lines changed

1 file changed

+16
-37
lines changed

message-index/site.hs

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Data.Data (Typeable)
1212
import Data.Foldable (for_)
1313
import Data.Function (on)
1414
import Data.Functor ((<&>))
15-
import Data.List (find, isPrefixOf, lookup, nub, sort, sortBy)
15+
import Data.List (find, lookup, nub, sort, sortBy, stripPrefix)
1616
import Data.List.NonEmpty (NonEmpty (..))
1717
import qualified Data.List.NonEmpty as NE
1818
import qualified Data.Map.Strict as Map
@@ -54,10 +54,6 @@ main = hakyll $ do
5454
route idRoute
5555
compile copyFileCompiler
5656

57-
match "messages/*/*/index.md" $
58-
version "nav" $ do
59-
compile getResourceBody
60-
6157
match "messages/*/*/index.md" $ do
6258
route $ setExtension "html"
6359
compile $ do
@@ -66,7 +62,6 @@ main = hakyll $ do
6662
getUnderlying
6763
<&> \ident ->
6864
fromFilePath $ takeDirectory (takeDirectory (toFilePath ident)) </> "index.md"
69-
bread <- breadcrumbField ["index.html", thisMessage]
7065
pandocCompiler
7166
>>= loadAndApplyTemplate
7267
"templates/example.html"
@@ -91,15 +86,11 @@ main = hakyll $ do
9186
)
9287
>>= relativizeUrls
9388

94-
match "messages/*/index.md" $
95-
version "nav" $ do
96-
compile pandocCompiler
97-
9889
match "messages/*/index.md" $ do
9990
route $ setExtension "html"
10091
compile $ do
10192
examples <- getExamples
102-
bread <- breadcrumbField ["index.html"]
93+
let bread = breadcrumbCtx ["index.html"]
10394
pandocCompiler
10495
>>= loadAndApplyTemplate
10596
"templates/message.html"
@@ -111,7 +102,7 @@ main = hakyll $ do
111102
defaultContext
112103
]
113104
)
114-
>>= loadAndApplyTemplate "templates/default.html" (bread <> defaultContext)
105+
>>= loadAndApplyTemplate "templates/default.html" (bread <> messageTitleField <> defaultContext)
115106
>>= relativizeUrls
116107

117108
match "messages/index.md" $ do
@@ -121,22 +112,18 @@ main = hakyll $ do
121112
match "404.html" $ do
122113
route idRoute
123114
compile $ do
124-
bread <- breadcrumbField ["index.html"]
125-
let ctx = mconcat [constField "title" "Not Found", bread, defaultContext]
115+
let bread = breadcrumbCtx ["index.html"]
116+
ctx = mconcat [constField "title" "Not Found", bread, defaultContext]
126117
getResourceBody
127118
>>= applyAsTemplate ctx
128119
>>= loadAndApplyTemplate "templates/default.html" ctx
129120

130-
match "index.html" $
131-
version "nav" $ do
132-
compile getResourceBody
133-
134121
match "index.html" $ do
135122
route idRoute
136123
compile $ do
137124
messages <- loadAll ("messages/*/index.md" .&&. hasNoVersion)
138-
bread <- breadcrumbField []
139-
let indexCtx =
125+
let bread = breadcrumbCtx []
126+
indexCtx =
140127
mconcat
141128
[ listField "messages" (messageCtx <> defaultContext) (pure messages),
142129
bread,
@@ -161,21 +148,15 @@ main = hakyll $ do
161148
exampleExtensions :: NonEmpty String
162149
exampleExtensions = "hs" :| ["yaml", "cabal"]
163150

164-
breadcrumbField :: [Identifier] -> Compiler (Context String)
165-
breadcrumbField idents =
166-
(messageTitleField <>) . breadcrumbCtx <$> traverse (load @String . setVersion (Just "nav")) idents
167-
168-
breadcrumbCtx :: [Item String] -> Context String
151+
breadcrumbCtx :: [Identifier] -> Context String
169152
breadcrumbCtx parents =
170-
listField "parents" (mconcat [indexlessUrlField "url", messageTitleField, defaultContext]) (pure parents)
153+
let parents' = (`Item` ()) <$> parents
154+
in listField "parents" (indexlessUrlField "url" <> messageTitleField) (pure parents')
171155

172156
indexlessUrlField :: String -> Context a
173-
indexlessUrlField key = field key $ \i ->
174-
let id = itemIdentifier i
175-
empty' = fail $ "No route url found for item " ++ show id
176-
in maybe empty' (indexless . toUrl) <$> getRoute id
157+
indexlessUrlField = mapContext indexless . urlField
177158

178-
messageTitleField :: Context String
159+
messageTitleField :: Context a
179160
messageTitleField = field "title" getTitle
180161
where
181162
getTitle item = do
@@ -301,9 +282,7 @@ flagSetFields =
301282
]
302283

303284
indexless :: String -> String
304-
indexless url
305-
| reverse toDrop `isPrefixOf` lru = reverse $ drop (length toDrop) lru
306-
| otherwise = url
307-
where
308-
lru = reverse url
309-
toDrop = "index.html"
285+
indexless url = fromMaybe url (stripSuffix "index.html" url)
286+
287+
stripSuffix :: String -> String -> Maybe String
288+
stripSuffix suffix src = reverse <$> stripPrefix (reverse suffix) (reverse src)

0 commit comments

Comments
 (0)