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

fix: db import of tagged page with pageTags

Part of LOG-3176
Gabriel Horner 1 жил өмнө
parent
commit
f0ecf03c4a

+ 1 - 1
deps/graph-parser/script/db_import.cljs

@@ -92,7 +92,7 @@
 (defn- import-files-to-db
   "Import specific doc files for dev purposes"
   [file conn {:keys [files] :as options}]
-  (let [doc-options (gp-exporter/build-doc-options conn {:macros {}} (merge options default-export-options))
+  (let [doc-options (gp-exporter/build-doc-options {:macros {}} (merge options default-export-options))
         files' (mapv #(hash-map :path %)
                      (into [file] (map resolve-path files)))]
     (gp-exporter/export-doc-files conn files' <read-file doc-options)))

+ 13 - 14
deps/graph-parser/src/logseq/graph_parser/exporter.cljs

@@ -59,13 +59,14 @@
                   (select-keys tag-block [:block/created-at :block/updated-at])))))))
 
 (defn- update-page-tags
-  [block tag-classes names-uuids page-tags-uuid]
+  [block tag-classes names-uuids]
   (if (seq (:block/tags block))
     (let [page-tags (->> (:block/tags block)
                          (remove #(or (:block.temp/new-class %) (contains? tag-classes (:block/name %))))
-                         (map #(or (get names-uuids (:block/name %))
-                                   (throw (ex-info (str "No uuid found for tag " (pr-str (:block/name %)))
-                                                   {:tag %}))))
+                         (map #(vector :block/uuid
+                                       (or (get names-uuids (:block/name %))
+                                           (throw (ex-info (str "No uuid found for tag " (pr-str (:block/name %)))
+                                                           {:tag %})))))
                          set)]
       (cond-> block
         true
@@ -74,7 +75,7 @@
                   ;; 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})))
+        (merge {:logseq.property/page-tags page-tags})))
     block))
 
 (defn- add-uuid-to-page-map [m page-names-to-uuids]
@@ -626,7 +627,7 @@
     (concat properties-tx deadline-properties-tx [block'])))
 
 (defn- build-new-page
-  [m tag-classes page-names-to-uuids page-tags-uuid]
+  [m tag-classes page-names-to-uuids]
   (-> m
       ;; Fix pages missing :block/original-name. Shouldn't happen
       ((fn [m']
@@ -637,13 +638,13 @@
       ;; TODO: org-mode content needs to be handled
       (assoc :block/format :markdown)
       (dissoc :block/whiteboard?)
-      (update-page-tags tag-classes page-names-to-uuids page-tags-uuid)))
+      (update-page-tags tag-classes page-names-to-uuids)))
 
 (defn- build-pages-tx
   "Given all the pages and blocks parsed from a file, return a map containing
   all non-whiteboard pages to be transacted, pages' properties and additional
   data for subsequent steps"
-  [conn pages blocks {:keys [page-tags-uuid tag-classes property-classes property-parent-classes notify-user]
+  [conn pages blocks {:keys [tag-classes property-classes property-parent-classes notify-user]
                       :as options}]
   (let [all-pages (->> (extract/with-ref-pages pages blocks)
                        ;; remove unused property pages unless the page has content
@@ -673,8 +674,8 @@
                              (when (seq block-changes)
                                (cond-> (merge block-changes {:block/uuid page-uuid})
                                  (:block/tags m)
-                                 (update-page-tags tag-classes page-names-to-uuids page-tags-uuid))))
-                           (build-new-page m tag-classes page-names-to-uuids page-tags-uuid)))
+                                 (update-page-tags tag-classes page-names-to-uuids))))
+                           (build-new-page m tag-classes page-names-to-uuids)))
                        (map :block all-pages-m))]
     {:pages-tx pages-tx
      :page-properties-tx (mapcat :properties-tx all-pages-m)
@@ -810,7 +811,6 @@
 * :extract-options - Options map to pass to extract/extract
 * :user-options - User provided options maps that alter how a file is converted to db graph. Current options
    are :tag-classes (set) and :property-classes (set).
-* :page-tags-uuid - uuid of pageTags property
 * :import-state - useful import state to maintain across files e.g. property schemas or ignored properties
 * :macros - map of macros for use with macro expansion
 * :notify-user - Displays warnings to user without failing the import. Fn receives a map with :msg
@@ -1039,14 +1039,13 @@
 
 (defn build-doc-options
   "Builds options for use with export-doc-files"
-  [conn config options]
+  [config options]
   (-> {:extract-options {:date-formatter (common-config/get-date-formatter config)
                          :user-config config
                          :filename-format (or (:file/name-format config) :legacy)
                          :verbose (:verbose options)}
        :user-config config
        :user-options (select-keys options [:tag-classes :property-classes :property-parent-classes])
-       :page-tags-uuid (:block/uuid (d/entity @conn :logseq.property/page-tags))
        :import-state (new-import-state)
        :macros (or (:macros options) (:macros config))}
       (merge (select-keys options [:set-ui-state :export-file :notify-user]))))
@@ -1081,7 +1080,7 @@
                          (remove logseq-file?)
                          (filter #(contains? #{"md" "org" "markdown" "edn"} (path/file-ext (:path %)))))
           asset-files (filter #(string/starts-with? (get % rpath-key) "assets/") files)
-          doc-options (build-doc-options conn config options)]
+          doc-options (build-doc-options config options)]
       (log-fn "Importing" (count files) "files ...")
       ;; These export* fns are all the major export/import steps
       (p/do!

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

@@ -103,7 +103,7 @@
 (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))
+  (let [doc-options (gp-exporter/build-doc-options {:macros {}} (merge options default-export-options))
         files' (mapv #(hash-map :path %) files)]
     (gp-exporter/export-doc-files conn files' <read-file doc-options)))
 
@@ -116,9 +116,9 @@
                  (if (= :block/tags k)
                    (mapv #(:db/ident (d/entity db (:db/id %))) v)
                    (if (db-property-type/ref-property-types built-in-type)
-                     (db-property/ref->property-value-content db v)
+                     (db-property/ref->property-value-contents db v)
                      v))
-                 (db-property/ref->property-value-content db v))]))
+                 (db-property/ref->property-value-contents db v))]))
        (into {})))
 
 ;; Tests
@@ -145,7 +145,7 @@
       ;; Includes 2 journals as property values for :logseq.task/deadline
       (is (= 8 (count (d/q '[:find ?b :where [?b :block/type "journal"]] @conn))))
       ;; Count includes Contents and page references
-      (is (= 5
+      (is (= 6
              (count (d/q '[:find (pull ?b [*]) :where [?b :block/original-name ?name] (not [?b :block/type])] @conn))))
       (is (= 1 (count @assets))))
 
@@ -212,14 +212,18 @@
 
     (testing "tags without tag options"
       (let [block (find-block-by-content @conn #"Inception")
-            tag-page (find-page-by-name @conn "Movie")]
+            tag-page (find-page-by-name @conn "Movie")
+            tagged-page (find-page-by-name @conn "Interstellar")]
         (is (string/starts-with? (str (:block/content block)) "Inception [[")
-            "block with tag converts tag to page ref")
+            "tagged block tag converts tag to page ref")
         (is (= [(:db/id tag-page)] (map :db/id (:block/refs block)))
-            "block with tag has correct refs")
-
+            "tagged block has correct refs")
         (is (and tag-page (not (:block/type tag-page)))
-            "tag page is not a class")))))
+            "tag page is not a class")
+
+        (is (= {:logseq.property/page-tags #{"Movie"}}
+               (readable-properties @conn tagged-page))
+            "tagged page has tags imported to property by default")))))
 
 (deftest-async export-file-with-tag-classes-option
   (p/let [file-graph-dir "test/resources/exporter-test-graph"

+ 3 - 0
deps/graph-parser/test/resources/exporter-test-graph/pages/Interstellar.md

@@ -0,0 +1,3 @@
+tags:: Movie
+
+-