about summary refs log tree commit diff
path: root/web/blog/fragments.nix
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2020-02-09T21·44+0000
committerVincent Ambo <tazjin@google.com>2020-02-09T21·44+0000
commit1e770f5d8844b01817247d6027b39eec6b4b46f8 (patch)
tree677dee5015d5ca6a6434db9991fa2bbd3d497f66 /web/blog/fragments.nix
parent0bc2f8995eb6ebcd13a33282520f724451630a36 (diff)
feat(web/blog): Add support for draft & unlisted posts r/521
Posts with either `draft = true;` or `listed = false;` will no longer
be included in index generation and will have a warning callout
inserted at the top of the page urging people not to share the links
to them.
Diffstat (limited to 'web/blog/fragments.nix')
-rw-r--r--web/blog/fragments.nix38
1 files changed, 31 insertions, 7 deletions
diff --git a/web/blog/fragments.nix b/web/blog/fragments.nix
index c910ac014e5e..b13ba20c6699 100644
--- a/web/blog/fragments.nix
+++ b/web/blog/fragments.nix
@@ -5,13 +5,17 @@
 # An entire post is rendered by `renderPost`, which assembles the
 # fragments together in a runCommand execution.
 #
-# The post overview is rendered by 'postList'.
+# The post index is generated by //web/homepage, not by this code.
 { pkgs, lib, ... }:
 
 let
   inherit (builtins) filter map hasAttr replaceStrings toFile;
   inherit (pkgs.third_party) runCommandNoCC writeText;
 
+  # Generate a post list for all listed, non-draft posts.
+  isDraft = post: (hasAttr "draft" post) && post.draft;
+  isUnlisted = post: (hasAttr "listed" post) && !post.listed;
+
   escape = replaceStrings [ "<" ">" "&" "'" ] [ "&lt;" "&gt;" "&amp;" "&#39;" ];
 
   header = title: ''
@@ -47,6 +51,22 @@ let
   </body>
   '';
 
+  draftWarning = toFile "draft.html" ''
+    <p class="cheddar-callout cheddar-warning">
+      <b>Note:</b> This post is a <b>draft</b>! Please do not share
+      the link to it without asking me first.
+    </p>
+    <hr>
+  '';
+
+  unlistedWarning = toFile "unlisted.html" ''
+    <p class="cheddar-callout cheddar-warning">
+      <b>Note:</b> This post is <b>unlisted</b>! Please do not share
+      the link to it without asking me first.
+    </p>
+    <hr>
+  '';
+
   renderPost = post: runCommandNoCC "${post.key}.html" {} ''
     cat ${toFile "header.html" (header post.title)} > $out
 
@@ -56,17 +76,21 @@ let
     date --date="@${toString post.date}" '+%Y-%m-%d' >> $out
     echo '</aside>' >> $out
 
+    ${
+      # Add a warning to draft/unlisted posts to make it clear that
+      # people should not share the post.
+
+      if (isDraft post) then "cat ${draftWarning} >> $out"
+      else if (isUnlisted post) then "cat ${unlistedWarning} >> $out"
+      else "# Your ads could be here?"
+    }
+
     # Write the actual post through cheddar's about-filter mechanism
     cat ${post.content} | ${pkgs.tools.cheddar}/bin/cheddar --about-filter ${post.content} >> $out
     echo '</article>' >> $out
 
     cat ${toFile "footer.html" footer} >> $out
   '';
-
-  # Generate a post list for all listed, non-draft posts.
-  isDraft = post: (hasAttr "draft" post) && post.draft;
-  isUnlisted = post: (hasAttr "listed" post) && !post.listed;
-  includePost = post: !(isDraft post) && !(isUnlisted post);
 in {
-  inherit renderPost;
+  inherit renderPost isDraft isUnlisted;
 }