Skip to content

Instantly share code, notes, and snippets.

Created May 22, 2021 10:53
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Roam/Render component for showing the days until a date.
(ns com.luisthiamnye.roam-render.days-until
[roam.datascript :as d]
(defn inherited-uids [uid]
(d/q '[:find [?uids ...]
:in $ ?uid
[?b :block/uid ?uid]
(or-join [?b ?uids]
(and [?b :block/refs ?brefs]
[?brefs :block/uid ?uids])
(and [?b :block/parents ?p]
(or-join [?p ?uids]
(and [?p :block/refs ?prefs]
[?prefs :block/uid ?uids])
[?p :block/uid ?uids])))]
(defn get-date [uid]
(-> (inherited-uids uid)
(->>(into []
(map (fn [x]
(let [[_ m d y] (re-find #"(\d\d)-(\d\d)-(\d\d\d\d)" x)]
[y m d])))
(filter (fn [x]
(some some? x))))))
(as-> x (js/Date. (clojure.string/join \- x)))))
(defn days-between [d1 d2]
(.round js/Math (/ (- d2 d1) 1000 60 60 24)))
(defn _ [{:keys [block-uid]}]
(let [days-until (days-between (js/Date. (.toDateString (js/Date.)))
(get-date block-uid))]
[:span {:style {:background "gold"
:color "rgb(111,78,55)"
:padding "2px 5px"
:border-radius "3px"
:box-shadow "1px 1px 3px rgba(0 0 0 / 0.1)"}}
(when (pos? days-until) "In ")
[:span {:style {:color "rgb(64,40,22)"}}
(if (zero? days-until)
(.abs js/Math days-until))]
(when-not (zero? days-until) " Days")
(when (neg? days-until)
" ago")]]))
Copy link

LuisThiamNye commented May 22, 2021

Used like any other roam/render component: {{[[roam/render]]: ((block-ref-of-clojure-codeblock))}}

The component searches for the closest [[page link]] to a daily notes page throughout the block ancestry. If there are no page links, it can also use the date corresponding to the daily note page it is within.

Examples demonstrating how it works:

You can easily customise the colours by changing a few parts of the code.

Some ideas for improvement:

  • Change colour based on the time between now and the date.
  • Support specifying a time.x

Note: for custom page formats I use

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment