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

enhance: add import option to remove-inline-tags

Part of LOG-3235. Now with the ability to import all tags, Some users
will want to keep inline tags
Gabriel Horner 1 год назад
Родитель
Сommit
a3fde2a9f4

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

@@ -139,7 +139,7 @@
    (string/trim)))
 
 (defn- update-block-tags
-  [block db user-options page-names-to-uuids all-idents]
+  [block db {:keys [remove-inline-tags?] :as user-options} page-names-to-uuids all-idents]
   (let [block'
         (if (seq (:block/tags block))
           (let [original-tags (remove #(or (:block.temp/new-class %)
@@ -147,23 +147,26 @@
                                            (logseq-class-ident? %))
                                       (:block/tags block))
                 convert-tag?' #(convert-tag? (:block/name %) user-options)]
-            (-> block
-                (update :block/title
-                        content-without-tags-ignore-case
-                        (->> original-tags
-                             (filter convert-tag?')
-                             (map :block/title)))
-                (update :block/title
-                        db-content/replace-tags-with-page-refs
-                        (->> original-tags
-                             (remove convert-tag?')
-                             (map #(add-uuid-to-page-map % page-names-to-uuids))))
-                (update :block/tags
-                        (fn [tags]
-                          (vec (keep #(if (logseq-class-ident? %)
-                                        %
-                                        (convert-tag-to-class db % page-names-to-uuids user-options all-idents))
-                                     tags))))))
+            (cond-> block
+              remove-inline-tags?
+              (update :block/title
+                      content-without-tags-ignore-case
+                      (->> original-tags
+                           (filter convert-tag?')
+                           (map :block/title)))
+              true
+              (update :block/title
+                      db-content/replace-tags-with-page-refs
+                      (->> original-tags
+                           (remove convert-tag?')
+                           (map #(add-uuid-to-page-map % page-names-to-uuids))))
+              true
+              (update :block/tags
+                      (fn [tags]
+                        (vec (keep #(if (logseq-class-ident? %)
+                                      %
+                                      (convert-tag-to-class db % page-names-to-uuids user-options all-idents))
+                                   tags))))))
           block)]
     block'))
 
@@ -784,7 +787,7 @@
                    (fix-pre-block-references pre-blocks page-names-to-uuids)
                    (fix-block-name-lookup-ref page-names-to-uuids)
                    (update-block-refs page-names-to-uuids options)
-                   (update-block-tags db (select-keys options [:convert-all-tags? :tag-classes]) page-names-to-uuids (:all-idents import-state))
+                   (update-block-tags db (select-keys options [:convert-all-tags? :tag-classes :remove-inline-tags?]) page-names-to-uuids (:all-idents import-state))
                    (update-block-marker options)
                    (update-block-priority options)
                    add-missing-timestamps
@@ -1016,6 +1019,7 @@
     ;; Track per file changes to make to existing properties
     ;; Map of property names (keyword) and their changes (map)
     :upstream-properties (atom {})
+    :remove-inline-tags? (:remove-inline-tags? user-options)
     :convert-all-tags? (:convert-all-tags? user-options)
     :tag-classes (set (map string/lower-case (:tag-classes user-options)))
     :property-classes (set/difference
@@ -1381,7 +1385,9 @@
                          :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 :convert-all-tags?])
+       :user-options (merge
+                      {:remove-inline-tags? true}
+                      (select-keys options [:tag-classes :property-classes :property-parent-classes :convert-all-tags? :remove-inline-tags?]))
        :import-state (new-import-state)
        :macros (or (:macros options) (:macros config))}
       (merge (select-keys options [:set-ui-state :export-file :notify-user]))))

+ 12 - 0
deps/graph-parser/test/logseq/graph_parser/exporter_test.cljs

@@ -551,6 +551,18 @@
              (:block/tags (readable-properties @conn (find-page-by-name @conn "url"))))
           "tagged page has configured tag imported as a class"))))
 
+(deftest-async export-files-with-remove-inline-tags
+  (p/let [file-graph-dir "test/resources/exporter-test-graph"
+          files (mapv #(node-path/join file-graph-dir %) ["journals/2024_02_07.md"])
+          conn (db-test/create-conn)
+          _ (import-files-to-db files conn {:remove-inline-tags? false :convert-all-tags? true})]
+
+    (is (empty? (map :entity (:errors (db-validate/validate-db! @conn))))
+        "Created graph has no validation errors")
+    (is (string/starts-with? (:block/title (find-block-by-content @conn #"Inception"))
+                             "Inception #Movie")
+        "block with tag preserves inline tag")))
+
 (deftest-async export-files-with-ignored-properties
   (p/let [file-graph-dir "test/resources/exporter-test-graph"
           files (mapv #(node-path/join file-graph-dir %) ["ignored/icon-page.md"])

+ 19 - 6
src/main/frontend/components/imports.cljs

@@ -170,8 +170,9 @@
   [:div.border.p-6.rounded.bg-gray-01.mt-4
    (let [form-ctx (form-core/use-form
                    {:defaultValues {:graph-name initial-name
-                                    :convert-all-tags false
+                                    :convert-all-tags? false
                                     :tag-classes ""
+                                    :remove-inline-tags? true
                                     :property-classes ""
                                     :property-parent-classes ""}
                     :yupSchema (-> (.object form-core/yup)
@@ -199,7 +200,7 @@
                               (shui/form-description
                                [:b.text-red-800 (:message error)])))))
 
-        (shui/form-field {:name "convert-all-tags"}
+        (shui/form-field {:name "convert-all-tags?"}
                          (fn [field]
                            (shui/form-item
                             {:class "pt-3 flex justify-start items-center space-x-3 space-y-0 my-3 pr-3"}
@@ -218,8 +219,17 @@
                             (shui/form-control
                              (shui/input (merge field
                                                 {:placeholder "tag 1, tag 2" :disabled convert-all-tags-input})))
-                            (shui/form-description
-                             "Tags are case insensitive"))))
+                            (shui/form-description "Tags are case insensitive"))))
+
+        (shui/form-field {:name "remove-inline-tags?"}
+                         (fn [field]
+                           (shui/form-item
+                            {:class "pt-3 flex justify-start items-center space-x-3 space-y-0 my-3 pr-3"}
+                            (shui/form-label "Remove inline tags")
+                            (shui/form-description "Default behavior for DB graphs")
+                            (shui/form-control
+                             (shui/checkbox {:checked (:value field)
+                                             :on-checked-change (:onChange field)})))))
 
         (shui/form-field {:name "property-classes"}
                          (fn [field _error]
@@ -310,7 +320,9 @@
                    (fs/write-file! repo repo-dir (:path file) content {:skip-transact? true})))))))
 
 (defn- import-file-graph
-  [*files {:keys [graph-name tag-classes convert-all-tags property-classes property-parent-classes]} config-file]
+  [*files
+   {:keys [graph-name tag-classes convert-all-tags? property-classes property-parent-classes remove-inline-tags?]}
+   config-file]
   (state/set-state! :graph/importing :file-graph)
   (state/set-state! [:graph/importing-state :current-page] "Config files")
   (p/let [start-time (t/now)
@@ -321,7 +333,8 @@
                    :tag-classes (set (string/split tag-classes #",\s*"))
                    :property-classes (set (string/split property-classes #",\s*"))
                    :property-parent-classes (set (string/split property-parent-classes #",\s*"))
-                   :convert-all-tags? convert-all-tags
+                   :convert-all-tags? convert-all-tags?
+                   :remove-inline-tags? remove-inline-tags?
                    ;; common options
                    :notify-user show-notification
                    :set-ui-state state/set-state!