Skip to content

Commit

Permalink
Add post lists for the tags
Browse files Browse the repository at this point in the history
  • Loading branch information
jvanbruegge committed Mar 22, 2024
1 parent 7f6018f commit 74d917d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions blog.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ executable blog
, OverloadedStrings
, LambdaCase
build-depends: base ^>=4.18.0.0
, containers
, aeson
, slick
, shake
Expand Down
19 changes: 11 additions & 8 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import Data.Aeson.Types qualified as A
import Data.Text qualified as T
import Slick (markdownToHTML, compileTemplate', substitute)
import Slick.Utils (convert)
import qualified Data.Aeson.KeyMap as KM
import Data.Aeson.KeyMap qualified as KM
import Development.Shake.FilePath (dropDirectory1, (</>), dropExtension)
import Data.Functor (void)
import Data.Maybe (fromMaybe)
import Control.Monad (foldM)
import Data.Aeson.Key (fromText)
import Data.List (sortOn)
import Data.Ord (Down(..))
import Data.Set qualified as Set

outputFolder :: FilePath
outputFolder = "build/"
Expand Down Expand Up @@ -76,7 +77,9 @@ getRendered _ = error "Could not find content key in object"
buildRules :: Action ()
buildRules = do
allPosts <- mapP buildPost =<< getDirectoryFiles "." ["articles//*.md"]
buildPostList allPosts
buildPostList Nothing allPosts
let allTags = Set.toList $ foldr (Set.union . Set.fromList . tags) Set.empty allPosts
void . forP allTags $ \tag -> buildPostList (Just (tag, "tags" </> tag)) (filter (elem tag . tags) allPosts)
copyStaticFiles

buildPost :: FilePath -> Action Post
Expand All @@ -93,15 +96,15 @@ buildPost srcPath = cacheAction ("build" :: T.Text, srcPath) $ do
writeFile' (outputFolder </> T.unpack postUrl </> "index.html") . T.unpack $ rendered
convert $ A.Object postData'

buildPostList :: [Post] -> Action ()
buildPostList posts = do
buildPostList :: Maybe (String, FilePath) -> [Post] -> Action ()
buildPostList tag posts = do
let posts' = sortOn (Down . date) posts
let postData = A.Object $ KM.fromList
let postData = A.Object $ KM.fromList $
[ (fromText "posts", A.toJSON posts')
, (fromText "prefix", A.String ".")
]
, (fromText "prefix", A.String (maybe "." (const "../..") tag))
] <> maybe [] (\(t, _) -> [(fromText "tag", A.String (T.pack t))]) tag
rendered <- getRendered <$> renderTemplates postData ["postList.html", "shell.html"]
writeFile' (outputFolder </> "index.html") . T.unpack $ rendered
writeFile' (outputFolder <> maybe "" snd tag </> "index.html") . T.unpack $ rendered

copyStaticFiles :: Action ()
copyStaticFiles = do
Expand Down
5 changes: 2 additions & 3 deletions static/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,14 @@ nav > ul {
}

main {
margin-top: 1.5em;
height: auto;
padding-bottom: 1em;

line-height: 1.4em;
font-size: 18px;
}

article + article {
article {
margin-top: 2em;
}

Expand Down Expand Up @@ -126,7 +125,7 @@ article > header ul {
color: var(--secondary-text);
}

article > header li {
.tag {
border: 0.05em solid white;
border-radius: 0.2em;
padding: 0 0.2em;
Expand Down
2 changes: 1 addition & 1 deletion templates/postHeader.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ <h1><a href="{{prefix}}/{{url}}">{{title}}</a></h1>
<time datetime="{{date}}">{{readableDate}}</time>
<ul>
{{#tags}}
<li><a href="{{prefix}}/tags/{{.}}">{{.}}</a></li>
<li class="tag"><a href="{{prefix}}/tags/{{.}}">{{.}}</a></li>
{{/tags}}
</ul>
</header>
Expand Down
3 changes: 3 additions & 0 deletions templates/postList.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{{#tag}}
<span>All posts in <span class="tag">{{.}}</span>:</span>
{{/tag}}
{{#posts}}
<article>
{{> templates/postHeader.html}}
Expand Down

0 comments on commit 74d917d

Please sign in to comment.