1
0
Эх сурвалжийг харах

fix: db import of tagged blocks

Test this and and :tag-classes option. Part of LOG-3176
Gabriel Horner 1 жил өмнө
parent
commit
cc4d4588e3

+ 11 - 10
deps/graph-parser/src/logseq/graph_parser/exporter.cljs

@@ -46,17 +46,17 @@
 (defn- convert-tag-to-class
   "Converts a tag block with class or returns nil if this tag should be removed
    because it has been moved"
-  [tag-block tag-classes]
+  [db tag-block tag-classes]
   (if-let [new-class (:block.temp/new-class tag-block)]
     (sqlite-util/build-new-class
      {:block/original-name new-class
       :block/name (common-util/page-name-sanity-lc new-class)})
     (when (contains? tag-classes (:block/name tag-block))
-      (-> tag-block
-          add-missing-timestamps
-          ;; don't use build-new-class b/c of timestamps
-          (merge {:block/format :markdown
-                  :block/type "class"})))))
+      (-> (db-class/build-new-class db tag-block)
+          ;; override with imported timestamps
+          (dissoc :block/created-at :block/updated-at)
+          (merge (add-missing-timestamps
+                  (select-keys tag-block [:block/created-at :block/updated-at])))))))
 
 (defn- update-page-tags
   [block tag-classes names-uuids page-tags-uuid]
@@ -71,7 +71,8 @@
         true
         (update :block/tags
                 (fn [tags]
-                  (keep #(convert-tag-to-class % tag-classes) tags)))
+                  ;; FIXME: Add class support to pages
+                  (keep #(convert-tag-to-class nil % tag-classes) tags)))
         (seq page-tags)
         (update :block/properties merge {page-tags-uuid page-tags})))
     block))
@@ -97,7 +98,7 @@
    (string/trim)))
 
 (defn- update-block-tags
-  [block tag-classes page-names-to-uuids]
+  [block db tag-classes page-names-to-uuids]
   (if (seq (:block/tags block))
     (let [original-tags (remove :block.temp/new-class (:block/tags block))]
       (-> block
@@ -113,7 +114,7 @@
                        (map #(add-uuid-to-page-map % page-names-to-uuids))))
           (update :block/tags
                   (fn [tags]
-                    (keep #(convert-tag-to-class % tag-classes) tags)))))
+                    (keep #(convert-tag-to-class db % tag-classes) tags)))))
     block))
 
 (defn- update-block-marker
@@ -614,7 +615,7 @@
                    (fix-block-name-lookup-ref db page-names-to-uuids)
                    (update-block-macros db page-names-to-uuids)
                    (update-block-refs page-names-to-uuids options)
-                   (update-block-tags tag-classes page-names-to-uuids)
+                   (update-block-tags db tag-classes page-names-to-uuids)
                    (update-block-marker options)
                    (update-block-priority options)
                    add-missing-timestamps

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

@@ -15,13 +15,24 @@
             [logseq.db.frontend.property :as db-property]
             [logseq.db.frontend.property.type :as db-property-type]))
 
+;; Helpers
+;; =======
+;; some have been copied from db-import script
+
 (defn- find-block-by-content [db content]
-  (->> content
-       (d/q '[:find [(pull ?b [*]) ...]
-              :in $ ?content
-              :where [?b :block/content ?content]]
-            db)
-       first))
+  (if (instance? js/RegExp content)
+    (->> content
+         (d/q '[:find [(pull ?b [*]) ...]
+                :in $ ?pattern
+                :where [?b :block/content ?content] [(re-find ?pattern ?content)]]
+              db)
+         first)
+    (->> content
+         (d/q '[:find [(pull ?b [*]) ...]
+                :in $ ?content
+                :where [?b :block/content ?content]]
+              db)
+         first)))
 
 (defn- find-page-by-name [db name]
   (->> name
@@ -89,6 +100,13 @@
                      (dissoc :assets))]
     (gp-exporter/export-file-graph conn conn config-file *files options')))
 
+(defn- import-files-to-db
+  "Import specific doc files for dev purposes"
+  [files conn options]
+  (let [doc-options (gp-exporter/build-doc-options conn {:macros {}} (merge options default-export-options))
+        files' (mapv #(hash-map :path %) files)]
+    (gp-exporter/export-doc-files conn files' <read-file doc-options)))
+
 (defn- readable-properties
   [db query-ent]
   (->> (db-property/properties query-ent)
@@ -103,6 +121,9 @@
                  (db-property/ref->property-value-content db v))]))
        (into {})))
 
+;; Tests
+;; =====
+
 (deftest-async export-basic-graph
   ;; This graph will contain basic examples of different features to import
   (p/let [file-graph-dir "test/resources/exporter-test-graph"
@@ -122,9 +143,9 @@
 
     (testing "graph wide counts"
       ;; Includes 2 journals as property values for :logseq.task/deadline
-      (is (= 7 (count (d/q '[:find ?b :where [?b :block/type "journal"]] @conn))))
-      ;; Count includes Contents
-      (is (= 3
+      (is (= 8 (count (d/q '[:find ?b :where [?b :block/type "journal"]] @conn))))
+      ;; Count includes Contents and page references
+      (is (= 5
              (count (d/q '[:find (pull ?b [*]) :where [?b :block/original-name ?name] (not [?b :block/type])] @conn))))
       (is (= 1 (count @assets))))
 
@@ -182,9 +203,37 @@
       (is (= #{:logseq.task/status :block/tags}
              (set (keys (readable-properties @conn (find-block-by-content @conn "old todo block")))))
           "old task properties are ignored")
-      
+
       (is (= {:logseq.property/query-sort-by :user.property/prop-num
               :logseq.property/query-properties [:block :page :user.property/prop-string :user.property/prop-num]
               :logseq.property/query-table true}
              (readable-properties @conn (find-block-by-content @conn "{{query (property :prop-string)}}")))
-          "query block has correct query properties"))))
+          "query block has correct query properties"))
+
+    (testing "tags without tag options"
+      (let [block (find-block-by-content @conn #"Inception")
+            tag-page (find-page-by-name @conn "Movie")]
+        (is (string/starts-with? (str (:block/content block)) "Inception [[")
+            "block with tag converts tag to page ref")
+        (is (= [(:db/id tag-page)] (map :db/id (:block/refs block)))
+            "block with tag has correct refs")
+
+        (is (and tag-page (not (:block/type tag-page)))
+            "tag page is not a class")))))
+
+(deftest-async export-file-with-tag-classes-option
+  (p/let [file-graph-dir "test/resources/exporter-test-graph"
+          file (node-path/join file-graph-dir "journals/2024_02_07.md")
+          conn (d/create-conn db-schema/schema-for-db-based-graph)
+          _ (d/transact! conn (sqlite-create-graph/build-db-initial-data "{}"))
+          _ (import-files-to-db [file] conn {:tag-classes ["movie"]})]
+    (let [block (find-block-by-content @conn #"Inception")
+          tag-page (find-page-by-name @conn "Movie")
+          another-tag-page (find-page-by-name @conn "p0")]
+      (is (= (:block/content block) "Inception")
+          "block with :tag-classes tag strips tag from content")
+
+      (is (= ["class"] (:block/type tag-page))
+          "tag page in :tag-classes is a class")
+      (is (and another-tag-page (not (:block/type another-tag-page)))
+          "another tag page not in :tag-classes is not a class"))))

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

@@ -0,0 +1,2 @@
+- Inception #Movie
+- TODO do X #p0