Przeglądaj źródła

fix: simple queries without properties not importing

Also handle query title for simple queries. Also loosened page-tags to
allow for future logseq class importing. Fixes LOG-3243
Gabriel Horner 1 rok temu
rodzic
commit
9531811b68

+ 40 - 20
deps/graph-parser/src/logseq/graph_parser/exporter.cljs

@@ -82,14 +82,18 @@
       (throw (ex-info (str "No uuid found for page name " (pr-str page-name))
                       {:page-name page-name}))))
 
+(defn- logseq-class-ident?
+  [k]
+  (and (qualified-keyword? k) (= "logseq.class" (namespace k))))
+
 (defn- update-page-tags
   [block db tag-classes page-names-to-uuids all-idents]
   (if (seq (:block/tags block))
     (let [page-tags (->> (:block/tags block)
                          (remove #(or (:block.temp/new-class %)
                                       (contains? tag-classes (:block/name %))
-                                      ;; Ignore new class tags from extract
-                                      (= % :logseq.class/Journal)))
+                                      ;; Ignore new class tags from extract e.g. :logseq.class/Journal
+                                      (logseq-class-ident? %)))
                          (map #(vector :block/uuid (get-page-uuid page-names-to-uuids (:block/name %))))
                          set)]
       (cond-> block
@@ -97,7 +101,7 @@
         (update :block/tags
                 (fn [tags]
                   ;; Don't lazy load as this needs to build before the page does
-                  (vec (keep #(if (= % :logseq.class/Journal)
+                  (vec (keep #(if (logseq-class-ident? %)
                                 %
                                 (convert-tag-to-class db % page-names-to-uuids tag-classes all-idents)) tags))))
         (seq page-tags)
@@ -124,7 +128,10 @@
   [block db tag-classes page-names-to-uuids all-idents]
   (let [block'
         (if (seq (:block/tags block))
-          (let [original-tags (remove :block.temp/new-class (:block/tags block))]
+          (let [original-tags (remove #(or (:block.temp/new-class %)
+                                           ;; Filter out new classes already set on a block e.g. :logseq.class/Query
+                                           (logseq-class-ident? %))
+                                      (:block/tags block))]
             (-> block
                 (update :block/title
                         content-without-tags-ignore-case
@@ -138,14 +145,12 @@
                              (map #(add-uuid-to-page-map % page-names-to-uuids))))
                 (update :block/tags
                         (fn [tags]
-                          (vec (keep #(convert-tag-to-class db % page-names-to-uuids tag-classes all-idents) tags))))))
+                          (vec (keep #(if (logseq-class-ident? %)
+                                        %
+                                        (convert-tag-to-class db % page-names-to-uuids tag-classes all-idents))
+                                     tags))))))
           block)]
-    (cond-> block'
-      (macro-util/query-macro? (:block/title block))
-      ((fn [b]
-         (merge (update b :block/tags (fnil conj []) :logseq.class/Query)
-                ;; Blank title here as query property has already been set
-                {:block/title ""}))))))
+    block'))
 
 (defn- update-block-marker
   "If a block has a marker, convert it to a task object"
@@ -384,11 +389,6 @@
                          [(built-in-property-name-to-idents prop) prop-value]))))
              (into {}))]
     (cond-> m
-      (macro-util/query-macro? title)
-      (assoc :logseq.property/query
-             (or (some->> (second (re-find #"\{\{query(.*)\}\}" title))
-                          string/trim)
-                 title))
       (and (contains? props :query-sort-desc) (:query-sort-by props))
       (update :logseq.property.table/sorting
               (fn [v]
@@ -622,14 +622,34 @@
 
 (defn- handle-block-properties
   "Does everything page properties does and updates a couple of block specific attributes"
-  [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)]
+  [{: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)
+        additional-props (cond-> {}
+                           (macro-util/query-macro? title)
+                           (assoc :logseq.property/query
+                                  (or (some->> (second (re-find #"\{\{query(.*)\}\}" title))
+                                               string/trim)
+                                      title)))
+        {: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))]
     {:block
      (cond-> block
+       (seq block-properties)
+       (merge block-properties)
+
+       (macro-util/query-macro? title)
+       ((fn [b]
+          (merge (update b :block/tags (fnil conj []) :logseq.class/Query)
+                 ;; Put all non-query content in title. Could just be a blank string
+                 {:block/title (string/trim (string/replace-first title #"\{\{query(.*)\}\}" ""))})))
+
        (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 properties-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
@@ -683,7 +703,7 @@
 
 (defn- build-block-tx
   [db block* pre-blocks page-names-to-uuids {:keys [tag-classes import-state] :as options}]
-  ;; (prn ::block-in block)
+  ;; (prn ::block-in block*)
   (let [;; needs to come before update-block-refs to detect new property schemas
         {:keys [block properties-tx]}
         (handle-block-properties block* db page-names-to-uuids (:block/refs block*) options)

+ 5 - 2
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 (= 1 (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/Query]] @conn))))
 
       ;; Don't count pages like url.md that have properties but no content
       (is (= 8
@@ -300,7 +300,10 @@
               :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"))
+          "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"))
 
     (testing "db attributes"
       (is (= true

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

@@ -1,3 +1,5 @@
+- For example, here's a query with title text:
+{{query (property type book)}}
 - test multilines in this page
 - |markdown| table|
   |some|thing|