فهرست منبع

Fix title and other built-in properties incorrectly parsing refs

Fixes issues in #6970. Introduced built-in property types as there are
fewer to list as parsed than unparsed.  Also removed test case that was
incorrectly introduced in #5580
Gabriel Horner 3 سال پیش
والد
کامیت
c6a3eb2de0

+ 42 - 0
deps/graph-parser/src/logseq/graph_parser/property.cljs

@@ -62,6 +62,48 @@
      :todo :doing :now :later :done}
    @built-in-extended-properties))
 
+(def built-in-property-types
+  "Types for built-in properties. Built-in properties whose values are to be
+  parsed by gp-text/parse-non-string-property-value should be added here"
+  {:template-including-parent :boolean
+   :public :boolean
+   :exclude-from-graph-view :boolean
+   :heading :boolean
+   :collapsed :boolean
+   :created-at :integer
+   :created_at :integer
+   :updated-at :integer
+   :last-modified-at :integer
+   :last_modified_at :integer
+   :query-table :boolean
+   :query-sort-desc :boolean
+   :hl-page :integer
+   :hl-stamp :integer
+   :todo :integer
+   :doing :integer
+   :now :integer
+   :later :integer
+   :done :integer})
+
+(assert (set/subset? (set (keys built-in-property-types))
+                     (set/union (hidden-built-in-properties)
+                                (editable-built-in-properties)))
+        "Keys of built-in-property-types must be valid built-in properties")
+
+(defn unparsed-built-in-properties
+  "Properties whose values will not be parsed by gp-text/parse-property"
+  []
+  (set/difference (set/union (hidden-built-in-properties)
+                             (editable-built-in-properties))
+                  ;; Most of these need to be auto-parsed as integers so exclude
+                  ;; them until we have ones that must be unparsed
+                  @built-in-extended-properties
+                  ;; Refs need to be parsed
+                  editable-linkable-built-in-properties
+                  ;; All these should be parsed by gp-text/parse-non-string-property-value
+                  (set (keys built-in-property-types))))
+
+
 (defonce properties-start ":PROPERTIES:")
 (defonce properties-end ":END:")
 (defonce properties-end-pattern

+ 25 - 29
deps/graph-parser/src/logseq/graph_parser/text.cljs

@@ -78,9 +78,6 @@
        (not (string/starts-with? p "./"))
        (not (gp-util/url? p))))
 
-(defonce non-parsing-properties
-  (atom #{"background-color" "background_color"}))
-
 (defn parse-non-string-property-value
   "Return parsed non-string property value or nil if none is found"
   [v]
@@ -151,38 +148,37 @@
                     (map second))]
     (set (mapcat sep-by-comma plains))))
 
+(defn- parse-property-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)]
+    (if property-separated-by-commas?
+      (->> (extract-refs-by-commas v (get config-state :format :markdown))
+           (set/union refs))
+      refs)))
+
 (defn parse-property
-  "Property value parsing that takes into account built-in properties, and user config"
+  "Property value parsing that takes into account built-in properties, format
+  and user config"
   [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?
-                (->> (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 (string/trim (str v))
-        non-string-property (parse-non-string-property-value v)]
+  (let [v' (string/trim (str v))]
     (cond
       (contains? (set/union
-                  #{"filters" "macro"}
-                  (get config-state :ignored-page-references-keywords)) k)
-      v
+                  (set (map name (gp-property/unparsed-built-in-properties)))
+                  (get config-state :ignored-page-references-keywords))
+                 (name k))
+      v'
 
-      (@non-parsing-properties k)
-      v
-
-      (string/blank? v)
+      (string/blank? v')
       nil
 
-      (and (string? v) (gp-util/wrapped-by-quotes? v))
-      v
-
-      (seq refs')
-      refs'
-
-      (some? non-string-property)
-      non-string-property
+      (gp-util/wrapped-by-quotes? v')
+      v'
 
+      ;; parse property value as needed
       :else
-      v)))
+      (let [refs (parse-property-refs k v' mldoc-references-ast config-state)]
+        (if (seq refs)
+          refs
+          (if-some [new-val (parse-non-string-property-value v')]
+            new-val
+            v'))))))

+ 13 - 4
deps/graph-parser/test/logseq/graph_parser/text_test.cljs

@@ -80,8 +80,8 @@
 
   (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"}))
+         :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)
@@ -102,10 +102,19 @@
     (are [k v y] (= (parse-property k v {}) y)
          :tags "\"foo, bar\"" "\"foo, bar\""
          :tags "\"[[foo]], [[bar]]\"" "\"[[foo]], [[bar]]\""))
-  
+
   (testing "parse title property with square bracket"
     (are [k v y] (= (parse-property k v {}) y)
-      :title "[[Jan 11th, 2022]] 21:26" "\"[[Jan 11th, 2022]] 21:26\"")))
+         :title "[[Jan 11th, 2022]] 21:26" "[[Jan 11th, 2022]] 21:26"
+         :title "[[[[aldsfkd]] a.b/c.d]]" "[[[[aldsfkd]] a.b/c.d]]"))
+
+  (testing "built-in properties parse as expected"
+    (are [k v y] (= (parse-property k v {}) y)
+         :id "62e98716-9c0b-4253-83e7-7f8e8a23fe19" "62e98716-9c0b-4253-83e7-7f8e8a23fe19"
+         :filters "{\"product process\" true}" "{\"product process\" true}"
+         :collapsed "false" false
+         :created-at "1609233702047" 1609233702047
+         :background-color "#533e7d" "#533e7d")))
 
 
 #_(cljs.test/test-ns 'logseq.graph-parser.text-test)

+ 1 - 12
deps/graph-parser/test/logseq/graph_parser_test.cljs

@@ -65,18 +65,7 @@
                        @conn)
                   (map first)
                   (map :block/properties)))
-          "id as text has correct :block/properties"))
-
-    (let [conn (ldb/start-conn)]
-      (graph-parser/parse-file conn "foo.md" "- id:: [[628953c1-8d75-49fe-a648-f4c612109098]]" {})
-      (is (= [{:id #{"628953c1-8d75-49fe-a648-f4c612109098"}}]
-             (->> (d/q '[:find (pull ?b [*])
-                         :in $
-                         :where [?b :block/content] [(missing? $ ?b :block/name)]]
-                       @conn)
-                  (map first)
-                  (map :block/properties)))
-          "id as linked ref has correct :block/properties")))
+          "id as text has correct :block/properties")))
 
   (testing "unexpected failure during block extraction"
     (let [conn (ldb/start-conn)