Browse Source

enhance: allow user macros for :url property values

and single line :default values
Gabriel Horner 1 year ago
parent
commit
bfc5bf9ccc

+ 1 - 0
.clj-kondo/config.edn

@@ -134,6 +134,7 @@
              logseq.common.util common-util
              logseq.common.util.page-ref page-ref
              logseq.common.util.block-ref block-ref
+             logseq.common.util.macro macro-util
              logseq.db ldb
              logseq.db.frontend.content db-content
              logseq.db.frontend.inputs db-inputs

+ 37 - 0
deps/common/src/logseq/common/util/macro.cljs

@@ -0,0 +1,37 @@
+(ns logseq.common.util.macro
+  (:require [clojure.string :as string]))
+
+(def left-braces "Opening characters for block-ref" "{{")
+(def right-braces "Closing characters for block-ref" "}}")
+
+(defn macro?
+  [*s]
+  (let [s (string/trim (str *s))]
+    (and (string/starts-with? s left-braces) (string/ends-with? s right-braces))))
+
+(defn macro-subs
+  [macro-content arguments]
+  (loop [s macro-content
+         args arguments
+         n 1]
+    (if (seq args)
+      (recur
+       (string/replace s (str "$" n) (first args))
+       (rest args)
+       (inc n))
+      s)))
+
+(defn- macro-expand-value
+  "Checks a string for a macro and expands it if there's a macro entry for it.
+   This is a slimmer version of macro-else-cp"
+  [val macros]
+  (if-let [[_ macro args] (and (string? val)
+                               (seq (re-matches #"\{\{(\S+)\s+(.*)\}\}" val)))]
+    (if-let [content (get macros macro)]
+      (macro-subs content (string/split args #"\s+"))
+      val)
+    val))
+
+(defn expand-value-if-macro
+  [s macros]
+  (if (macro? s) (macro-expand-value s macros) s))

+ 8 - 2
deps/db/src/logseq/db/frontend/property/type.cljs

@@ -2,7 +2,8 @@
   "Provides property types and related helper fns e.g. property value validation
   fns and their allowed schema attributes"
   (:require [datascript.core :as d]
-            [clojure.set :as set]))
+            [clojure.set :as set]
+            [logseq.common.util.macro :as macro-util]))
 
 ;; Config vars
 ;; ===========
@@ -54,6 +55,11 @@
          (catch :default _e
            false))))
 
+(defn macro-url?
+  [s]
+  ;; TODO: Confirm that macro expanded value is url when it's easier to pass data into validations
+  (macro-util/macro? s))
+
 (defn- logseq-page?
   [db id]
   (and (uuid? id)
@@ -107,7 +113,7 @@
    :checkbox boolean?
    :url      [:fn
               {:error/message "should be a URL"}
-              (some-fn url? uuid?)]
+              (some-fn url? uuid? macro-url?)]
    :page     [:fn
               {:error/message "should be a page"}
               logseq-page?]

+ 2 - 1
src/main/frontend/components/block.cljs

@@ -77,6 +77,7 @@
             [logseq.common.util :as common-util]
             [logseq.common.util.block-ref :as block-ref]
             [logseq.common.util.page-ref :as page-ref]
+            [logseq.common.util.macro :as macro-util]
             [logseq.shui.core :as shui]
             [medley.core :as medley]
             [promesa.core :as p]
@@ -1464,7 +1465,7 @@
                             macro-content)
 
                           (and (seq arguments) macro-content)
-                          (block/macro-subs macro-content arguments)
+                          (macro-util/macro-subs macro-content arguments)
 
                           :else
                           macro-content)

+ 4 - 3
src/main/frontend/components/property/value.cljs

@@ -21,7 +21,8 @@
             [frontend.handler.route :as route-handler]
             [frontend.handler.property.util :as pu]
             [promesa.core :as p]
-            [frontend.db.async :as db-async]))
+            [frontend.db.async :as db-async]
+            [logseq.common.util.macro :as macro-util]))
 
 (defn- select-type?
   [property type]
@@ -548,7 +549,7 @@
       [:span.number (str value)]
 
       :else
-      (inline-text {} :markdown (str value)))))
+      (inline-text {} :markdown (macro-util/expand-value-if-macro (str value) (state/get-macros))))))
 
 (rum/defc single-value-select
   [block property value value-f select-opts {:keys [editing?] :as opts}]
@@ -677,7 +678,7 @@
                     :block
                     (property-block-value value block property block-cp editor-box opts page-cp editor-id)
 
-                    (inline-text {} :markdown (str value)))))]))]))))
+                    (inline-text {} :markdown (macro-util/expand-value-if-macro (str value) (state/get-macros))))))]))]))))
 
 (rum/defc multiple-values < rum/reactive
   [block property v {:keys [on-chosen dropdown? editing?]

+ 0 - 12
src/main/frontend/format/block.cljs

@@ -144,18 +144,6 @@ and handles unexpected failure."
            (state/add-block-ast-cache! block-uuid content result)
            result))))))
 
-(defn macro-subs
-  [macro-content arguments]
-  (loop [s macro-content
-         args arguments
-         n 1]
-    (if (seq args)
-      (recur
-       (string/replace s (str "$" n) (first args))
-       (rest args)
-       (inc n))
-      s)))
-
 (defn break-line-paragraph?
   [[typ break-lines]]
   (and (= typ "Paragraph")