瀏覽代碼

enhance: import advanced queries as query objects

Fixes LOG-3247. Handles migrating deprecated options and string and non-string titles
Gabriel Horner 1 年之前
父節點
當前提交
e74912c850

+ 28 - 3
deps/graph-parser/src/logseq/graph_parser/exporter.cljs

@@ -624,17 +624,29 @@
   "Does everything page properties does and updates a couple of block specific attributes"
   [{:block/keys [title] :as block*} db page-names-to-uuids refs {:keys [property-classes] :as options}]
   (let [{:keys [block properties-tx]} (handle-page-and-block-properties block* db page-names-to-uuids refs options)
+        advanced-query (some->> (second (re-find #"(?s)#\+BEGIN_QUERY(.*)#\+END_QUERY" title)) string/trim)
         additional-props (cond-> {}
+                           ;; Order matters as we ensure a simple query gets priority
                            (macro-util/query-macro? title)
                            (assoc :logseq.property/query
                                   (or (some->> (second (re-find #"\{\{query(.*)\}\}" title))
                                                string/trim)
-                                      title)))
+                                      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)))
         {:keys [block-properties pvalues-tx]}
         (when (seq additional-props)
           (build-properties-and-values additional-props db page-names-to-uuids
                                        (select-keys block [:block/properties-text-values :block/name :block/title :block/uuid])
-                                       options))]
+                                       options))
+        pvalues-tx' (if (and pvalues-tx (seq advanced-query))
+                      (concat pvalues-tx [{:block/uuid (second (:logseq.property/query block-properties))
+                                           :logseq.property.code/mode "clojure"
+                                           :logseq.property.node/display-type :code}])
+                      pvalues-tx)]
     {:block
      (cond-> block
        (seq block-properties)
@@ -646,10 +658,23 @@
                  ;; Put all non-query content in title. Could just be a blank string
                  {:block/title (string/trim (string/replace-first title #"\{\{query(.*)\}\}" ""))})))
 
+       (seq advanced-query)
+       ((fn [b]
+          (let [query-map (common-util/safe-read-map-string advanced-query)]
+            (cond-> (update b :block/tags (fnil conj []) :logseq.class/Query)
+              true
+              (assoc :block/title
+                     (or (when-let [title' (:title query-map)]
+                           (if (string? title') title' (pr-str title')))
+                         ;; Put all non-query content in title for now
+                         (string/trim (string/replace-first title #"(?s)#\+BEGIN_QUERY(.*)#\+END_QUERY" ""))))
+              (:collapsed? query-map)
+              (assoc :block/collapsed? true)))))
+
        (and (seq property-classes) (seq (:block/refs block*)))
        ;; remove unused, nonexistent property page
        (update :block/refs (fn [refs] (remove #(property-classes (keyword (:block/name %))) refs))))
-     :properties-tx (concat properties-tx (when pvalues-tx pvalues-tx))}))
+     :properties-tx (concat properties-tx (when pvalues-tx' pvalues-tx'))}))
 
 (defn- update-block-refs
   "Updates the attributes of a block ref as this is where a new page is defined. Also

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

@@ -176,7 +176,7 @@
       (is (= 18 (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 (= 2 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Query]] @conn))))
+      (is (= 3 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Query]] @conn))))
 
       ;; Don't count pages like url.md that have properties but no content
       (is (= 8
@@ -294,16 +294,26 @@
       (is (= #{"gpt"}
              (:block/alias (readable-properties @conn (find-page-by-name @conn "chat-gpt")))))
 
+      ;; Queries
       (is (= {:logseq.property.table/sorting [{:id :user.property/prop-num, :asc? false}]
               :logseq.property.view/type "Table View"
               :logseq.property.table/ordered-columns [:block/title :user.property/prop-string :user.property/prop-num]
               :logseq.property/query "(property :prop-string)"
               :block/tags [:logseq.class/Query]}
              (readable-properties @conn (find-block-by-property @conn :logseq.property/query "(property :prop-string)")))
-          "query block has correct query properties")
+          "simple query block has correct query properties")
       (is (= "For example, here's a query with title text:"
-           (:block/title (find-block-by-content @conn #"query with title text")))
-          "Text around a query block is set as a query's title"))
+             (:block/title (find-block-by-content @conn #"query with title text")))
+          "Text around a simple query block is set as a query's title")
+      (is (= {:logseq.property.view/type "List View"
+              :logseq.property/query "{:query (task todo doing)}"
+              :block/tags [:logseq.class/Query]
+              :logseq.property.table/ordered-columns [:block/title]}
+             (readable-properties @conn (find-block-by-content @conn #"tasks with")))
+          "Advanced query has correct query properties")
+      (is (= "tasks with todo and doing"
+             (:block/title (find-block-by-content @conn #"tasks with")))
+          "Advanced query has custom title migrated"))
 
     (testing "db attributes"
       (is (= true
@@ -371,9 +381,7 @@
     (testing "multiline blocks"
       (is (= "|markdown| table|\n|some|thing|" (:block/title (find-block-by-content @conn #"markdown.*table"))))
       (is (= "multiline block\na 2nd\nand a 3rd" (:block/title (find-block-by-content @conn #"multiline block"))))
-      (is (= "logbook block" (:block/title (find-block-by-content @conn #"logbook block"))))
-      (is (re-find #"(?s)^Text before\n#\+BEGIN_QUERY.*END_QUERY\nText after$"
-                   (:block/title (find-block-by-content @conn #":title \"tasks")))))
+      (is (= "logbook block" (:block/title (find-block-by-content @conn #"logbook block")))))
 
     (testing "block refs and path-refs"
       (let [block (find-block-by-content @conn "old todo block")]

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

@@ -20,4 +20,4 @@
   {:title "tasks with todo and doing"
   :query (task todo doing)}
   #+END_QUERY
-  Text after
+  Text after