From f94e21261c066a97cbb3738f886b1626330225a6 Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Sun, 16 May 2021 03:57:35 +0300 Subject: [PATCH] feat: add date parsing and sorting posts by date --- .../posts/{looong.md => 2021-04-12_looong.md} | 0 .../{Testing more.md => 2021-07-01_asdf.md} | 0 .../posts/{test.md => 2021-10-30_test.md} | 0 pkgs-lib.nix | 21 +++++++++++++----- templaters/basic.nix | 22 ++++++++++++------- 5 files changed, 29 insertions(+), 14 deletions(-) rename examples/site/posts/{looong.md => 2021-04-12_looong.md} (100%) rename examples/site/posts/{Testing more.md => 2021-07-01_asdf.md} (100%) rename examples/site/posts/{test.md => 2021-10-30_test.md} (100%) diff --git a/examples/site/posts/looong.md b/examples/site/posts/2021-04-12_looong.md similarity index 100% rename from examples/site/posts/looong.md rename to examples/site/posts/2021-04-12_looong.md diff --git a/examples/site/posts/Testing more.md b/examples/site/posts/2021-07-01_asdf.md similarity index 100% rename from examples/site/posts/Testing more.md rename to examples/site/posts/2021-07-01_asdf.md diff --git a/examples/site/posts/test.md b/examples/site/posts/2021-10-30_test.md similarity index 100% rename from examples/site/posts/test.md rename to examples/site/posts/2021-10-30_test.md diff --git a/pkgs-lib.nix b/pkgs-lib.nix index 087bfd6..e616c9c 100644 --- a/pkgs-lib.nix +++ b/pkgs-lib.nix @@ -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 = { diff --git a/templaters/basic.nix b/templaters/basic.nix index e7e6d33..2d62f98 100644 --- a/templaters/basic.nix +++ b/templaters/basic.nix @@ -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))) - (readFile value) - ]; - allPosts = flatten (mapAttrsToList renderPost posts); + 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) + ]; + 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"; } ([