浏览代码

fix: whitespace on advanced query string import

Fixes https://github.com/logseq/db-test/issues/193
Gabriel Horner 6 月之前
父节点
当前提交
f34a8d57a0

+ 1 - 0
deps/graph-parser/nbb.edn

@@ -6,4 +6,5 @@
   {:local/root "../db"}
   io.github.nextjournal/nbb-test-runner
   {:git/sha "60ed57aa04bca8d604f5ba6b28848bd887109347"}
+  borkdude/rewrite-edn {:mvn/version "0.4.9"}
   io.github.pez/baldr {:mvn/version "1.0.9"}}}

+ 22 - 7
deps/graph-parser/src/logseq/graph_parser/exporter.cljs

@@ -1,7 +1,8 @@
 (ns logseq.graph-parser.exporter
   "Exports a file graph to DB graph. Used by the File to DB graph importer and
   by nbb-logseq CLIs"
-  (:require [cljs-time.coerce :as tc]
+  (:require [borkdude.rewrite-edn :as rewrite]
+            [cljs-time.coerce :as tc]
             [cljs.pprint]
             [clojure.edn :as edn]
             [clojure.set :as set]
@@ -17,6 +18,7 @@
             [logseq.common.uuid :as common-uuid]
             [logseq.db :as ldb]
             [logseq.db.common.order :as db-order]
+            [logseq.db.common.property-util :as db-property-util]
             [logseq.db.frontend.class :as db-class]
             [logseq.db.frontend.content :as db-content]
             [logseq.db.frontend.db-ident :as db-ident]
@@ -29,8 +31,7 @@
             [logseq.graph-parser.block :as gp-block]
             [logseq.graph-parser.extract :as extract]
             [logseq.graph-parser.property :as gp-property]
-            [promesa.core :as p]
-            [logseq.db.common.property-util :as db-property-util]))
+            [promesa.core :as p]))
 
 (defn- add-missing-timestamps
   "Add updated-at or created-at timestamps if they doesn't exist"
@@ -762,6 +763,23 @@
         block'' (replace-namespace-with-parent block' page-names-to-uuids)]
     {:block block'' :properties-tx properties-tx}))
 
+(defn- pretty-print-dissoc
+  [s dissoc-keys]
+  (-> (reduce rewrite/dissoc
+              (rewrite/parse-string s)
+              dissoc-keys)
+      str))
+
+(defn- migrate-advanced-query-string [query-str]
+  (try
+    (pretty-print-dissoc query-str [:title :group-by-page? :collapsed?])
+    (catch :default _e
+      ;; rewrite/parse-string can fail on some queries in Advanced Queries in docs graph
+      (js/console.error "Failed to parse advanced query string. Falling back to full query string: " (pr-str query-str))
+      (if-let [query-map (not-empty (common-util/safe-read-map-string query-str))]
+        (pr-str (dissoc query-map :title :group-by-page? :collapsed?))
+        query-str))))
+
 (defn- handle-block-properties
   "Does everything page properties does and updates a couple of block specific attributes"
   [{:block/keys [title] :as block*}
@@ -777,10 +795,7 @@
                                                string/trim)
                                       title))
                            (seq advanced-query)
-                           (assoc :logseq.property/query
-                                  (if-let [query-map (not-empty (common-util/safe-read-map-string advanced-query))]
-                                    (pr-str (dissoc query-map :title :group-by-page? :collapsed?))
-                                    advanced-query)))
+                           (assoc :logseq.property/query (migrate-advanced-query-string advanced-query)))
         {:keys [block-properties pvalues-tx]}
         (when (seq additional-props)
           (build-properties-and-values additional-props db page-names-to-uuids

+ 2 - 2
deps/graph-parser/test/logseq/graph_parser/exporter_test.cljs

@@ -173,7 +173,7 @@
       (is (= 25 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Journal]] @conn))))
 
       (is (= 4 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Task]] @conn))))
-      (is (= 3 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Query]] @conn))))
+      (is (= 4 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Query]] @conn))))
       (is (= 2 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Card]] @conn))))
 
       ;; Properties and tags aren't included in this count as they aren't a Page
@@ -520,7 +520,7 @@
         "Correct number of user classes")
 
     (is (= 4 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Task]] @conn))))
-    (is (= 3 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Query]] @conn))))
+    (is (= 4 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Query]] @conn))))
     (is (= 2 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Card]] @conn))))
 
     (testing "replacing refs in :block/title when :remove-inline-tags? set"

+ 22 - 1
deps/graph-parser/test/resources/exporter-test-graph/journals/2024_08_07.md

@@ -1,5 +1,5 @@
 - For example, here's a query with title text:
-{{query (property type book)}}
+  {{query (property type book)}}
 - test multilines in this page
 - |markdown| table|
   |some|thing|
@@ -21,3 +21,24 @@
   :query (task todo doing)}
   #+END_QUERY
   Text after
+- collapsed:: true
+  #+BEGIN_QUERY
+  {:title "⌛ Pretty print advanced query test"
+    :query [:find (pull ?b [*])
+       :in $ ?today
+       :where
+       [?b :block/page ?p]
+       [?p :page/journal? true]
+       [?p :page/journal-day ?jd]
+       [(str ?jd) ?jds]
+       [(subs ?jds 4 8) ?md1]
+       [(str ?today) ?td]
+       [(subs ?td 4 8) ?md2]
+       [(= ?md1 ?md2)]
+       [(< ?jd ?today)]
+    ]
+    :inputs [:today]
+    :breadcrumb-show? true
+    :collapsed? False
+  }
+  #+END_QUERY