Просмотр исходного кода

enhance: import block tags that are

specified via tags property. Fixes
https://github.com/logseq/db-test/issues/434
Gabriel Horner 1 неделя назад
Родитель
Сommit
045cd5f537

+ 19 - 8
deps/graph-parser/src/logseq/graph_parser/block.cljs

@@ -455,13 +455,14 @@
                result))))) col)))
 
 (defn- with-page-refs-and-tags
-  [{:keys [title body tags refs marker priority] :as block} db date-formatter]
+  [{:keys [title body tags refs marker priority] :as block} db date-formatter {:keys [structured-tags]
+                                                                               :or {structured-tags #{}}}]
   (let [db-based? (and (ldb/db-based-graph? db) (not @*export-to-db-graph?))
         refs (->> (concat tags refs (when-not db-based? [marker priority]))
                   (remove string/blank?)
                   (distinct))
         *refs (atom refs)
-        *structured-tags (atom #{})]
+        *structured-tags (atom (set structured-tags))]
     (walk/prewalk
      (fn [form]
        ;; skip custom queries
@@ -556,9 +557,9 @@
     (map (fn [page] (page-name->map page db true date-formatter)) page-refs)))
 
 (defn- with-page-block-refs
-  [block db date-formatter]
+  [block db date-formatter opts]
   (some-> block
-          (with-page-refs-and-tags db date-formatter)
+          (with-page-refs-and-tags db date-formatter opts)
           with-block-refs
           (update :refs (fn [col] (remove nil? col)))))
 
@@ -612,7 +613,7 @@
                                 :block/macros (extract-macros-from-ast body)
                                 :block.temp/ast-body body}
                          {:keys [tags refs]}
-                         (with-page-block-refs {:body body :refs property-refs} db date-formatter)]
+                         (with-page-block-refs {:body body :refs property-refs} db date-formatter {})]
                      (cond-> block
                        tags
                        (assoc :block/tags tags)
@@ -630,8 +631,17 @@
     properties))
 
 (defn- construct-block
-  [block properties timestamps body encoded-content format pos-meta {:keys [block-pattern db date-formatter remove-properties? db-graph-mode? export-to-db-graph?]}]
-  (let [id (get-custom-id-or-new-id properties)
+  [block properties* timestamps body encoded-content format pos-meta {:keys [block-pattern db date-formatter remove-properties? db-graph-mode? export-to-db-graph?]}]
+  (let [id (get-custom-id-or-new-id properties*)
+        block-tags (and export-to-db-graph? (get-in properties* [:properties :tags]))
+        ;; For export, remove tags from properties as they are being converted to classes
+        properties (if (seq block-tags)
+                     (-> properties*
+                         (update :properties #(dissoc % :tags))
+                         (update :properties-text-values #(dissoc % :tags))
+                         (update :properties-order (fn [v] (remove #(= :tags %) v)))
+                         (update :page-refs (fn [v] (remove #(= "tags" %) v))))
+                     properties*)
         ref-pages-in-properties (->> (:page-refs properties)
                                      (remove string/blank?))
         block (second block)
@@ -671,7 +681,8 @@
         db-based? (or db-graph-mode? export-to-db-graph?)
         block (-> block
                   (assoc :body body)
-                  (with-page-block-refs db date-formatter))
+                  (with-page-block-refs db date-formatter
+                    (cond-> {} (seq block-tags) (assoc :structured-tags block-tags))))
         block (if db-based? block
                   (-> block
                       (update :tags (fn [tags] (map #(assoc % :block/format format) tags)))

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

@@ -210,7 +210,7 @@
 
       ;; Counts
       ;; Includes journals as property values e.g. :logseq.property/deadline
-      (is (= 30 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Journal]] @conn))))
+      (is (= 31 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Journal]] @conn))))
 
       (is (= 5 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Asset]] @conn))))
       (is (= 4 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Task]] @conn))))
@@ -231,7 +231,7 @@
                   #_(map #(select-keys % [:block/title :block/tags]))
                   count))
           "Correct number of pages with block content")
-      (is (= 12 (->> @conn
+      (is (= 14 (->> @conn
                      (d/q '[:find [?ident ...]
                             :where [?b :block/tags :logseq.class/Tag] [?b :db/ident ?ident] (not [?b :logseq.property/built-in?])])
                      count))
@@ -493,6 +493,10 @@
              (mapv :db/ident (:block/tags (db-test/find-block-by-content @conn #"with namespace tag"))))
           "Block tagged with namespace tag is only associated with leaf child tag")
 
+      (is (= #{:user.class/ai :user.class/block-tag :user.class/p1}
+             (set (map :db/ident (:block/tags (db-test/find-block-by-content @conn #"Block tags")))))
+          "Block with tags through tags property")
+
       (is (= []
              (->> (d/q '[:find (pull ?b [:block/title {:block/tags [:db/ident]}])
                          :where [?b :block/tags :logseq.class/Tag]]

+ 2 - 0
deps/graph-parser/test/resources/exporter-test-graph/journals/2025_12_17.md

@@ -0,0 +1,2 @@
+- Block tags #p1
+  tags:: ai, #block-tag