diff --git a/src/main/frontend/components/block.cljs b/src/main/frontend/components/block.cljs index ec1d9d9be0..09a523db05 100644 --- a/src/main/frontend/components/block.cljs +++ b/src/main/frontend/components/block.cljs @@ -47,7 +47,8 @@ [reitit.frontend.easy :as rfe] [frontend.commands :as commands] [lambdaisland.glogi :as log] - [frontend.context.i18n :as i18n])) + [frontend.context.i18n :as i18n] + [frontend.template :as template])) ;; TODO: remove rum/with-context because it'll make reactive queries not working @@ -841,12 +842,12 @@ (block/macro-subs macro-content arguments) macro-content) macro-content (when macro-content - (editor-handler/resolve-dynamic-template! macro-content))] + (template/resolve-dynamic-template! macro-content))] (render-macro config name arguments macro-content format)) (when-let [macro-txt (macro->text name arguments)] (let [macro-txt (when macro-txt - (editor-handler/resolve-dynamic-template! macro-txt)) + (template/resolve-dynamic-template! macro-txt)) format (get-in config [:block :block/format] :markdown)] (render-macro config name arguments macro-txt format)))))) diff --git a/src/main/frontend/components/editor.cljs b/src/main/frontend/components/editor.cljs index bc316e1104..ce9da50025 100644 --- a/src/main/frontend/components/editor.cljs +++ b/src/main/frontend/components/editor.cljs @@ -32,6 +32,7 @@ [medley.core :as medley] [cljs-drag-n-drop.core :as dnd] [frontend.text :as text] + [frontend.template :as template] ["/frontend/utils" :as utils])) (rum/defc commands < rum/reactive @@ -228,7 +229,7 @@ content (if (string/includes? (string/trim edit-content) "\n") content (text/remove-level-spaces content format)) - content (editor-handler/resolve-dynamic-template! content)] + content (template/resolve-dynamic-template! content)] (state/set-editor-show-template-search! false) (editor-handler/insert-command! id content diff --git a/src/main/frontend/db/query_dsl.cljs b/src/main/frontend/db/query_dsl.cljs index 8f7c12d01b..109103bdb2 100644 --- a/src/main/frontend/db/query_dsl.cljs +++ b/src/main/frontend/db/query_dsl.cljs @@ -14,7 +14,8 @@ [medley.core :as medley] [clojure.walk :as walk] [clojure.core] - [clojure.set :as set])) + [clojure.set :as set] + [frontend.template :as template])) ;; Query fields: @@ -377,8 +378,9 @@ (defn query [repo query-string] - (when query-string - (let [{:keys [query sort-by blocks?]} (parse repo query-string)] + (when (string? query-string) + (let [query-string (template/resolve-dynamic-template! query-string) + {:keys [query sort-by blocks?]} (parse repo query-string)] (when query (let [query (query-wrapper query blocks?)] (query-custom/react-query repo diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index 094a3e495b..079668a4b5 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -2257,31 +2257,3 @@ value (:block/content block) new-value (string/replace value full_text new-full-text)] (save-block-aux! block new-value (:block/format block) {}))) - -(defn variable-rules - [] - {"today" (util/format "[[%s]]" (date/today)) - "yesterday" (util/format "[[%s]]" (date/yesterday)) - "tomorrow" (util/format "[[%s]]" (date/tomorrow)) - "time" (date/get-current-time) - "current page" (util/format "[[%s]]" - (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 [date (tc/to-local-date-time nld)] - (util/format "[[%s]]" (date/journal-name date))) - match)))))) diff --git a/src/main/frontend/template.cljs b/src/main/frontend/template.cljs new file mode 100644 index 0000000000..96e3dd82ef --- /dev/null +++ b/src/main/frontend/template.cljs @@ -0,0 +1,34 @@ +(ns frontend.template + (:require [frontend.util :as util :refer-macros [profile]] + [frontend.date :as date] + [clojure.string :as string] + [cljs-time.coerce :as tc] + [frontend.state :as state])) + +(defn- variable-rules + [] + {"today" (util/format "[[%s]]" (date/today)) + "yesterday" (util/format "[[%s]]" (date/yesterday)) + "tomorrow" (util/format "[[%s]]" (date/tomorrow)) + "time" (date/get-current-time) + "current page" (util/format "[[%s]]" + (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 [date (tc/to-local-date-time nld)] + (util/format "[[%s]]" (date/journal-name date))) + match))))))