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); mkServeFromSite = site: mkServePathScript (mkSitePath site);
mkSiteFrom = { src, templater }: mkSiteFrom = { src, templater }:
let let
inherit (utils) readDir readFile fromTOML; inherit (utils) readDir readFile fromTOML mapAttrsToList sort elemAt;
inherit (pkgs.lib) mapAttrs' nameValuePair head splitString; inherit (pkgs.lib) nameValuePair head splitString pipe removeSuffix;
postsRendered = postsRendered =
let path = src + "/posts"; in let path = src + "/posts"; in
mapAttrs' pipe (readDir path) [
(name: _: (mapAttrsToList (name: _:
nameValuePair nameValuePair
(head (splitString "." name)) (head (splitString "." name))
(parseMarkdown name (readFile (path + "/${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")); siteConfig = fromTOML (readFile (src + "/config.toml"));
context = { context = {

View File

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