enhance: allow user macros for :url property values

and single line :default values
This commit is contained in:
Gabriel Horner
2024-02-22 14:13:59 -05:00
parent 531a8ef533
commit bfc5bf9ccc
6 changed files with 52 additions and 18 deletions

View File

@@ -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

View File

@@ -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))

View File

@@ -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?]

View File

@@ -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)

View File

@@ -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?]

View File

@@ -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")