From 8c6bf623b6516257019a4f20ac902f93c7c91402 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Sat, 13 Nov 2021 01:04:15 +0100 Subject: feat(users/Profpatsch/blog: inject css into markdown files Change-Id: I72bba4bd8aa9a9fccab1fd3d618cfcfa7e0507c5 Reviewed-on: https://cl.tvl.fyi/c/depot/+/3844 Tested-by: BuildkiteCI Reviewed-by: Profpatsch --- users/Profpatsch/blog/default.nix | 74 ++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 24 deletions(-) (limited to 'users/Profpatsch/blog') diff --git a/users/Profpatsch/blog/default.nix b/users/Profpatsch/blog/default.nix index a272f0f446..4e24d097e3 100644 --- a/users/Profpatsch/blog/default.nix +++ b/users/Profpatsch/blog/default.nix @@ -9,11 +9,12 @@ let ; # / + # TODO: use toplevel = [ { route = [ "notes" ]; name = "Notes"; - page = router; + page = {cssFile}: router cssFile; } { route = [ "projects" ]; @@ -27,12 +28,20 @@ let { route = [ "notes" "preventing-oom" ]; name = "Preventing out-of-memory (OOM) errors on Linux"; - page = markdownToHtml "preventing-oom" ./notes/preventing-oom.md; + page = {cssFile}: markdownToHtml { + name = "preventing-oom"; + markdown = ./notes/preventing-oom.md; + inherit cssFile; + }; } { route = [ "notes" "rust-string-conversions" ]; name = "Converting between different String types in Rust"; - page = markdownToHtml "rust-string-conversions" ./notes/rust-string-conversions.md; + page = {cssFile}: markdownToHtml { + name = "rust-string-conversions"; + markdown = ./notes/rust-string-conversions.md; + inherit cssFile; + }; } ]; @@ -60,30 +69,48 @@ let title = "Ligature Emulation in Emacs"; subtitle = "It’s not pretty, but the results are"; description = "How to set up ligatures using prettify-symbols-mode and the Hasklig/FiraCode fonts."; - page = markdownToHtml "2017-05-04-ligature-emluation-in-emacs" ./posts/2017-05-04-ligature-emulation-in-emacs.md; + page = {cssFile}: markdownToHtml { + name = "2017-05-04-ligature-emluation-in-emacs"; + markdown = ./posts/2017-05-04-ligature-emulation-in-emacs.md; + inherit cssFile; + }; route = [ "posts" "2017-05-04-ligature-emluation-in-emacs" ]; tags = ["emacs"]; } ]; # convert a markdown file to html via lowdown - markdownToHtml = name: note: depot.nix.runExecline "${name}.html" {} [ - "importas" "out" "out" - bins.lowdown "-s" "-Thtml" "-o" "$out" note - ]; - - # all notes with `route` converted to an absolute path - notesFullRoute = lib.pipe notes [ - (map (x@{route, ...}: x // { route = mkRoute route; })) - ]; - - # all posts with `route` converted to an absolute path - postsFullRoute = lib.pipe posts [ + markdownToHtml = { + name, + # the file to convert + markdown, + # css file to add to the final result, as { route } + cssFile + }: + depot.nix.runExecline "${name}.html" {} ([ + "importas" "out" "out" + (depot.users.Profpatsch.lib.debugExec "") + bins.lowdown + "-s" "-Thtml" + ] ++ + (lib.optional (cssFile != null) (["-M" "css=${mkRoute cssFile.route}"])) + ++ [ + "-o" "$out" + markdown + ]); + + # takes a { route … } attrset and converts the route lists to an absolute path + fullRoute = attrs: lib.pipe attrs [ (map (x@{route, ...}: x // { route = mkRoute route; })) ]; # a cdb from route to a netencoded version of data for each route - router = lib.pipe (notesFullRoute ++ postsFullRoute) [ + router = cssFile: lib.pipe (notes ++ posts) [ + (map (r: with depot.users.Profpatsch.lens; + lib.pipe r [ + (over (field "route") mkRoute) + (over (field "page") (_ { inherit cssFile; })) + ])) (map (x: { name = x.route; value = depot.users.Profpatsch.netencode.gen.dwim x; @@ -105,8 +132,8 @@ let "https://code.tvl.fyi/tree/${relativePath}"; # look up a route by path ($1) - router-lookup = depot.nix.writeExecline "router-lookup" { readNArgs = 1; } [ - cdbLookup router "$1" + router-lookup = cssFile: depot.nix.writeExecline "router-lookup" { readNArgs = 1; } [ + cdbLookup (router cssFile) "$1" ]; runExeclineStdout = name: args: cmd: depot.nix.runExecline name args ([ @@ -115,7 +142,7 @@ let ] ++ cmd); notes-index-html = - let o = notesFullRoute; + let o = fullRoute notes; in ''