Browse Source

fix: extract property refs support mixed values

Tienson Qin 3 years ago
parent
commit
50db6e42a0

+ 1 - 1
deps/graph-parser/src/logseq/graph_parser/block.cljs

@@ -618,7 +618,7 @@
                   (recur headings (rest blocks) timestamps' properties body))
 
                 (gp-property/properties-ast? block)
-                (let [properties (extract-properties (second block) user-config)]
+                (let [properties (extract-properties (second block) (assoc user-config :format format))]
                   (recur headings (rest blocks) timestamps properties body))
 
                 (heading-block? block)

+ 1 - 1
deps/graph-parser/src/logseq/graph_parser/extract.cljc

@@ -179,7 +179,7 @@
                                                          (let [k (if (keyword? x)
                                                                    (subs (str x) 1)
                                                                    x)]
-                                                           [(string/lower-case k) (text/parse-property k y mldoc-ast user-config)])))
+                                                           [(string/lower-case k) (text/parse-property k y mldoc-ast (assoc user-config :format format))])))
                                                   (into {})
                                                   (walk/keywordize-keys)))]
                          (when (and properties (seq properties))

+ 16 - 7
deps/graph-parser/src/logseq/graph_parser/text.cljs

@@ -125,13 +125,13 @@
               (string/trim x)))
        (set)))
 
-(defn sep-by-comma
+(defn- sep-by-comma
   [s]
   {:pre (string? s)}
   (->>
    (string/split s #",")
-   (remove string/blank?)
    (map string/trim)
+   (remove string/blank?)
    (set)))
 
 (defn separated-by-commas?
@@ -141,14 +141,23 @@
                           (set (get config-state :property/separated-by-commas)))
                k')))
 
+(defn- extract-refs-by-commas
+  [v format]
+  (let [plains (->> (map first (gp-mldoc/->edn v (gp-mldoc/default-config format)))
+                    first
+                    second
+                    (filter #(and (vector? %) (= "Plain" (first %))))
+                    (map second))]
+    (set (mapcat sep-by-comma plains))))
+
 (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))
+  [k v mldoc-references-ast config-state]
+  (let [refs (extract-refs-from-mldoc-ast mldoc-references-ast)
+        property-separated-by-commas? (separated-by-commas? config-state k)
         refs' (if property-separated-by-commas?
-                (sep-by-comma v)
+                (->> (extract-refs-by-commas v (get config-state :format :markdown))
+                     (set/union refs))
                 refs)
         k (if (or (symbol? k) (keyword? k)) (subs (str k) 1) k)
         v (if (or (symbol? v) (keyword? v))

+ 6 - 0
deps/graph-parser/test/logseq/graph_parser/text_test.cljs

@@ -78,6 +78,11 @@
          :comma-prop "comma, separated" #{"comma" "separated"}
          :comma-prop "one, two, one" #{"one" "two"}))
 
+  (testing "for user comma separated properties with mixed values"
+    (are [k v y] (= (parse-property k v {:property/separated-by-commas #{:comma-prop}}) y)
+      :comma-prop "foo, #bar" #{"foo", "bar"}
+      :comma-prop "comma, separated, [[page ref]], [[nested [[page]]]], #[[nested [[tag]]]], end" #{"page ref" "nested [[page]]" "nested [[tag]]" "comma" "separated" "end"}))
+
   (testing "for normal properties"
     (are [k v y] (= (parse-property k v {}) y)
          :normal "[[foo]] [[bar]]" #{"foo" "bar"}
@@ -98,4 +103,5 @@
          :tags "\"foo, bar\"" "\"foo, bar\""
          :tags "\"[[foo]], [[bar]]\"" "\"[[foo]], [[bar]]\"")))
 
+
 #_(cljs.test/test-ns 'logseq.graph-parser.text-test)