Files
logseq/src/main/frontend/template.cljs
Gabriel Horner 1f0e22275d Add ns docstrings for most ns in src/main
- Added to CI now that it passes
- Added no-doc for docstrings that don't add any more than what's in the
ns name or for ones where I didn't know the ns that well
2022-09-27 13:55:16 +08:00

36 lines
1.5 KiB
Clojure

(ns frontend.template
"Provides template related functionality"
(:require [clojure.string :as string]
[frontend.date :as date]
[frontend.state :as state]
[logseq.graph-parser.util.page-ref :as page-ref]))
(defn- variable-rules
[]
{"today" (page-ref/->page-ref (date/today))
"yesterday" (page-ref/->page-ref (date/yesterday))
"tomorrow" (page-ref/->page-ref (date/tomorrow))
"time" (date/get-current-time)
"current page" (page-ref/->page-ref (or (state/get-current-page)
(date/today)))})
;; TODO: programmable
;; context information, date, current page
(defn resolve-dynamic-template!
[content]
(string/replace content #"<%([^%].*?)%>"
(fn [[_ match]]
(let [match (string/trim match)]
(cond
(string/blank? match)
""
(get (variable-rules) (string/lower-case match))
(get (variable-rules) (string/lower-case match))
:else
(if-let [nld (date/nld-parse match)]
(let [;; NOTE: This following cannot handle timezones
;; date (tc/to-local-date-time nld)
date (doto (goog.date.DateTime.) (.setTime (.getTime nld)))]
(page-ref/->page-ref (date/journal-name date)))
match))))))