Browse Source

Add :property/separated-by-commas option

Tienson Qin 3 years ago
parent
commit
b516c1db5f

+ 24 - 2
deps/graph-parser/src/logseq/graph_parser/text.cljs

@@ -124,10 +124,32 @@
               (string/trim x)))
        (set)))
 
+(defn sep-by-comma
+  [s]
+  (when s
+    (some->>
+     (string/split s #"[\,|,]{1}")
+     (remove string/blank?)
+     (map string/trim))))
+
+(defn separated-by-commas?
+  [config-state k]
+  (let [k' (if (keyword? k) k (keyword k))]
+    (contains? (set/union #{:alias :tags}
+                          (set (get config-state :property/separated-by-commas)))
+               k')))
+
 (defn parse-property
   "Property value parsing that takes into account built-in properties, and user config"
   [k v mldoc-ast config-state]
   (let [refs (extract-refs-from-mldoc-ast mldoc-ast)
+        property-separated-by-commas? (and (separated-by-commas? config-state k)
+                                           (empty? refs))
+        refs' (if property-separated-by-commas?
+                (if (string/includes? v ",")
+                  (distinct (sep-by-comma v))
+                  [(string/trim v)])
+                refs)
         k (if (or (symbol? k) (keyword? k)) (subs (str k) 1) k)
         v (if (or (symbol? v) (keyword? v))
             (subs (str v) 1)
@@ -149,8 +171,8 @@
       (and (string? v) (gp-util/wrapped-by-quotes? v))
       v
 
-      (seq refs)
-      refs
+      (seq refs')
+      refs'
 
       (some? non-string-property)
       non-string-property

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

@@ -1904,17 +1904,25 @@
          [[:span.opacity-50 "Click here to start writing, type '/' to see all the commands."]])
        [tags])))))
 
+(rum/defc span-comma
+  []
+  [:span ", "])
+
 (rum/defc property-cp
   [config block k value]
   (let [date (and (= k :date) (date/get-locale-string (str value)))
         user-config (state/get-config)
         ;; When value is a set of refs, display full property text
         ;; because :block/properties value only contains refs but user wants to see text
+        property-separated-by-commas? (text/separated-by-commas? (state/get-config) k)
         v (if (and (coll? value) (seq value)
-                   (not (contains? gp-property/editable-linkable-built-in-properties k)))
+                   (not (contains? gp-property/editable-linkable-built-in-properties k))
+                   (not property-separated-by-commas?))
             (gp-property/property-value-from-content (name k) (:block/content block))
             value)
         property-pages-enabled? (contains? #{true nil} (:property-pages/enabled? user-config))]
+    (prn {:k k
+          :property-separated-by-commas? property-separated-by-commas?})
     [:div
      (if property-pages-enabled?
        (page-cp (assoc config :property? true) {:block/name (subs (str k) 1)})
@@ -1933,6 +1941,15 @@
        (and (string? v) (gp-util/wrapped-by-quotes? v))
        (gp-util/unquote-string v)
 
+       (and property-separated-by-commas? (coll? v))
+       (let [v (->> (remove string/blank? v)
+                    (filter string?))
+             vals (for [v-item v]
+                    (page-cp config {:block/name v-item}))
+             elems (interpose (span-comma) vals)]
+         (for [elem elems]
+           (rum/with-key elem (str (random-uuid)))))
+
        :else
        (inline-text config (:block/format block) (str v)))]))
 

+ 5 - 0
templates/config.edn

@@ -232,6 +232,11 @@
  ;; E.g.:property-pages/excludelist #{:duration :author}
  ;; :property-pages/excludelist
 
+ ;; By default, property value separated by commas will not be treated as
+ ;; page references. You can add properties to enable it.
+ ;; E.g. :property/separated-by-commas #{:alias :tags}
+ ;; :property/separated-by-commas []
+
  ;; logbook setup
  ;; :logbook/settings
  ;; {:with-second-support? false ;limit logbook to minutes, seconds will be eliminated