Sfoglia il codice sorgente

fix: graph-parser tests

Tienson Qin 1 anno fa
parent
commit
f3c59a9db3

+ 17 - 16
deps/db/src/logseq/db/frontend/content.cljs

@@ -97,33 +97,34 @@
   [content page-name id]
   (let [id' (str page-ref-special-chars id)
         [page wrapped-id] (map page-ref/->page-ref [page-name id'])]
-        (common-util/replace-ignore-case content page wrapped-id)))
+    (common-util/replace-ignore-case content page wrapped-id)))
 
 (defn- replace-page-ref-with-id
-  [content page-name id]
+  [content page-name id replace-tag?]
   (let [page-name (str page-name)
-        id (str id)]
-    (-> content
-       (replace-page-ref page-name id)
-       (replace-tag-ref page-name id))))
+        id (str id)
+        content' (replace-page-ref content page-name id)]
+    (if replace-tag?
+      (replace-tag-ref content' page-name id)
+      content')))
 
 (defn refs->special-id-ref
   "Convert ref to special id refs e.g. `[[page name]] -> [[~^...]]."
-  [db title refs]
+  [title refs & {:keys [replace-tag?]
+                 :or {replace-tag? true}}]
+  (assert (string? title))
   (let [refs' (map
                (fn [ref]
                  (if (and (vector? ref) (= :block/uuid (first ref)))
-                   (d/entity db ref)
+                   {:block/uuid (second ref)
+                    :block/title (str (first ref))}
                    ref))
                refs)]
-    (if (some :block/title refs')
-      (reduce
-       (fn [content {:block/keys [title uuid]}]
-         (-> (replace-page-ref-with-id content title uuid)
-             (replace-page-ref-with-id uuid uuid)))
-       title
-       (filter :block/title refs'))
-      title)))
+    (reduce
+     (fn [content {:block/keys [title uuid]}]
+       (replace-page-ref-with-id content title uuid replace-tag?))
+     title
+     (filter :block/title refs'))))
 
 (defn update-block-content
   "Replace `[[internal-id]]` with `[[page name]]`"

+ 11 - 11
deps/db/src/logseq/db/sqlite/build.cljs

@@ -107,7 +107,7 @@
     []
     (map second (re-seq page-ref/page-ref-re s))))
 
-(defn- ->block-tx [db {:keys [build/properties] :as m} properties-config page-uuids all-idents page-id]
+(defn- ->block-tx [{:keys [build/properties] :as m} properties-config page-uuids all-idents page-id]
   (let [new-block {:db/id (new-db-id)
                    :block/format :markdown
                    :block/page {:db/id page-id}
@@ -134,7 +134,7 @@
                                                            (throw (ex-info (str "No uuid for page ref name" (pr-str %)) {})))
                                                        :block/title %)
                                             ref-names)]
-                       {:block/title (db-content/refs->special-id-ref db (:block/title m) block-refs)
+                       {:block/title (db-content/refs->special-id-ref (:block/title m) block-refs {:replace-tag? false})
                         :block/refs block-refs})))))))
 
 (defn- build-properties-tx [properties page-uuids all-idents]
@@ -332,8 +332,8 @@
     all-idents))
 
 (defn- build-pages-and-blocks-tx
-  [db pages-and-blocks all-idents page-uuids {:keys [page-id-fn properties]
-                                              :or {page-id-fn :db/id}}]
+  [pages-and-blocks all-idents page-uuids {:keys [page-id-fn properties]
+                                           :or {page-id-fn :db/id}}]
   (vec
    (mapcat
     (fn [{:keys [page blocks]}]
@@ -366,7 +366,7 @@
          ;; blocks tx
          (reduce (fn [acc m]
                    (into acc
-                         (->block-tx db m properties page-uuids all-idents (page-id-fn new-page))))
+                         (->block-tx m properties page-uuids all-idents (page-id-fn new-page))))
                  []
                  blocks))))
     pages-and-blocks)))
@@ -515,8 +515,8 @@
     {:classes classes' :properties properties'}))
 
 (defn- build-blocks-tx*
-  [db {:keys [pages-and-blocks properties graph-namespace auto-create-ontology?]
-       :as options}]
+  [{:keys [pages-and-blocks properties graph-namespace auto-create-ontology?]
+    :as options}]
   (let [pages-and-blocks' (pre-build-pages-and-blocks pages-and-blocks properties)
         page-uuids (create-page-uuids pages-and-blocks')
         {:keys [classes properties]} (if auto-create-ontology? (auto-create-ontology options) options)
@@ -534,7 +534,7 @@
                                                  cs)))
                                  m))
                              properties-tx)
-        pages-and-blocks-tx (build-pages-and-blocks-tx db pages-and-blocks' all-idents page-uuids
+        pages-and-blocks-tx (build-pages-and-blocks-tx pages-and-blocks' all-idents page-uuids
                                                        (assoc options :properties properties))]
     ;; Properties first b/c they have schema and are referenced by all. Then classes b/c they can be referenced by pages. Then pages
     (split-blocks-tx (concat properties-tx'
@@ -588,9 +588,9 @@
    supported: :default, :url, :checkbox, :number, :node and :date. :checkbox and
    :number values are written as booleans and integers/floats. :node references
    are written as vectors e.g. `[:page \"PAGE NAME\"]`"
-  [db options]
+  [options]
   (validate-options options)
-  (build-blocks-tx* db options))
+  (build-blocks-tx* options))
 
 (defn create-blocks
   "Builds txs with build-blocks-tx and transacts them. Also provides a shorthand
@@ -598,7 +598,7 @@
   [conn options]
   (let [options' (merge {:auto-create-ontology? true}
                         (if (vector? options) {:pages-and-blocks options} options))
-        {:keys [init-tx block-props-tx]} (build-blocks-tx @conn options')]
+        {:keys [init-tx block-props-tx]} (build-blocks-tx options')]
     (d/transact! conn init-tx)
     (when (seq block-props-tx)
       (d/transact! conn block-props-tx))))

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

@@ -580,7 +580,7 @@
 (defn- update-block-refs
   "Updates the attributes of a block ref as this is where a new page is defined. Also
    updates block content effected by refs"
-  [db block page-names-to-uuids {:keys [whiteboard?]}]
+  [block page-names-to-uuids {:keys [whiteboard?]}]
   (let [ref-to-ignore? (if whiteboard?
                          #(and (map? %) (:block/uuid %))
                          #(and (vector? %) (= :block/uuid (first %))))]
@@ -600,15 +600,14 @@
                      ref))
                  refs)))
         (:block/title block)
-        (update :block/title
-                (fn [title]
-                  ;; TODO: Handle refs for whiteboard block which has none
-                  (let [refs (->> (:block/refs block)
-                                  (remove #(or (ref-to-ignore? %)
+        (assoc :block/title
+               ;; TODO: Handle refs for whiteboard block which has none
+               (let [refs (->> (:block/refs block)
+                               (remove #(or (ref-to-ignore? %)
                                   ;; ignore deadline related refs that don't affect content
-                                               (and (keyword? %) (db-malli-schema/internal-ident? %))))
-                                  (map #(add-uuid-to-page-map % page-names-to-uuids)))]
-                    (db-content/refs->special-id-ref db title refs)))))
+                                            (and (keyword? %) (db-malli-schema/internal-ident? %))))
+                               (map #(add-uuid-to-page-map % page-names-to-uuids)))]
+                 (db-content/refs->special-id-ref (:block/title block) refs {:replace-tag? false}))))
       block)))
 
 (defn- fix-pre-block-references
@@ -638,7 +637,7 @@
         block' (-> block-after-built-in-props
                    (fix-pre-block-references pre-blocks page-names-to-uuids)
                    (fix-block-name-lookup-ref page-names-to-uuids)
-                   (update-block-refs db page-names-to-uuids options)
+                   (update-block-refs page-names-to-uuids options)
                    (update-block-tags db tag-classes page-names-to-uuids (:all-idents import-state))
                    (update-block-marker options)
                    (update-block-priority options)

+ 2 - 3
src/main/frontend/handler/db_based/editor.cljs

@@ -61,9 +61,8 @@
                                 (use-cached-refs! block))))))
         result (-> block
                    (merge (if level {:block/level level} {}))
-                   (update :block/title
-                           (fn [title]
-                             (db-content/refs->special-id-ref (db/get-db) title (:block/refs block)))))]
+                   (assoc :block/title
+                          (db-content/refs->special-id-ref (:block/title block) (:block/refs block))))]
     result))
 
 (defn save-file!

+ 1 - 2
src/main/frontend/handler/paste.cljs

@@ -38,8 +38,7 @@
                                  (dissoc :block/tags)
                                  (update :block/title (fn [title]
                                                         (let [title' (db-content/replace-tags-with-page-refs title refs)]
-                                                          (db-content/refs->special-id-ref (db/get-db) title' refs))
-                                                        )))))))]
+                                                          (db-content/refs->special-id-ref title' refs)))))))))]
       (editor-handler/paste-blocks blocks' {:keep-uuid? true}))))
 
 (defn- paste-segmented-text