feat: add date parsing and sorting posts by date

This commit is contained in:
dusk 2021-05-16 03:57:35 +03:00
parent 6d61daa842
commit f94e21261c
Signed by: dusk
GPG Key ID: 1D8F8FAF2294D6EA
5 changed files with 29 additions and 14 deletions

View File

@ -33,18 +33,27 @@ in
mkServeFromSite = site: mkServePathScript (mkSitePath site);
mkSiteFrom = { src, templater }:
let
inherit (utils) readDir readFile fromTOML;
inherit (pkgs.lib) mapAttrs' nameValuePair head splitString;
inherit (utils) readDir readFile fromTOML mapAttrsToList sort elemAt;
inherit (pkgs.lib) nameValuePair head splitString pipe removeSuffix;
postsRendered =
let path = src + "/posts"; in
mapAttrs'
(name: _:
pipe (readDir path) [
(mapAttrsToList (name: _:
nameValuePair
(head (splitString "." name))
(parseMarkdown name (readFile (path + "/${name}")))
)
(readDir path);
))
(sort (p: op:
let
extractDate = name: splitString "-" (head (splitString "_" name));
getPart = name: el: removeSuffix "0" (elemAt (extractDate name) el);
d = getPart p.name;
od = getPart op.name;
in
!(((d 0) > (od 0)) && ((d 1) > (od 1)) && ((d 2) > (od 2)))
))
];
siteConfig = fromTOML (readFile (src + "/config.toml"));
context = {

View File

@ -1,7 +1,7 @@
{ utils, posts, pkgs, config, ... }@context:
let
inherit (utils) readFile mapAttrsToList tags fetchGit map;
inherit (pkgs.lib) flatten optional length;
inherit (utils) readFile mapAttrsToList tags fetchGit map elemAt;
inherit (pkgs.lib) optional length splitString;
stylesheets = map tags.mkStylesheet [
"https://unpkg.com/purecss@2.0.6/build/pure-min.css"
@ -9,11 +9,17 @@ let
"mine.css"
];
renderPost = name: value: with tags; article [
(a { href = "#${name}"; class = "postheader"; } (h3 { id = name; } ("## " + name)))
renderPost = { name, value }:
let
parts = splitString "_" name;
id = elemAt parts 1;
in
with tags; article [
(a { href = "#${id}"; class = "postheader"; } (h3 { inherit id; } ("## " + id)))
(h6 ("date: " + (elemAt parts 0)))
(readFile value)
];
allPosts = flatten (mapAttrsToList renderPost posts);
pages =
mapAttrsToList
(name: relPath: tags.div { class = "pure-u-1"; } (tags.a { href = "./${relPath}"; class = "postheader"; } name))
@ -21,7 +27,7 @@ let
postsSectionContent = with tags; [
(a { href = "#posts"; class = "postheader"; } (h1 "# posts"))
] ++ allPosts;
] ++ (map renderPost posts);
sidebarSection = optional ((length pages) > 0) (
with tags; nav { class = "sidebar"; } ([