Browse Source

enhance: all page refs in imported content are special ids

close LOG-3055. Also fix graph-parser errors being ignored
Gabriel Horner 2 years ago
parent
commit
48b974d64e

+ 18 - 4
deps/db/src/logseq/db/frontend/content.cljs

@@ -34,6 +34,22 @@
    content
    refs))
 
+(defn page-ref->special-id-ref
+  "Convert page ref to special id refs e.g. `[[page name]] -> [[~^...]]"
+  [content refs]
+  (reduce
+   (fn [content ref]
+     (string/replace content
+                     (str page-ref/left-brackets
+                          (:block/original-name ref)
+                          page-ref/right-brackets)
+                     (str page-ref/left-brackets
+                          page-ref-special-chars
+                          (:block/uuid ref)
+                          page-ref/right-brackets)))
+   content
+   refs))
+
 (defn update-block-content
   "Replace `[[internal-id]]` with `[[page name]]`"
   [repo db item eid]
@@ -65,10 +81,8 @@
      (string/replace content
                      (str "#" (:block/original-name tag))
                      (str page-ref/left-brackets
-                                ;; TODO: Use uuid when it becomes available
-                                ;; page-ref-special-chars
-                                ;; (:block/uuid tag)
-                          (:block/original-name tag)
+                          page-ref-special-chars
+                          (:block/uuid tag)
                           page-ref/right-brackets)))
    content
    tags))

+ 39 - 19
deps/graph-parser/src/logseq/graph_parser/exporter.cljs

@@ -50,8 +50,16 @@
         (assoc :block/properties {page-tags-uuid page-tags})))
     block))
 
+(defn- add-uuid-to-page-map [m page-names-to-uuids]
+  (assoc m
+         :block/uuid
+         (or (get page-names-to-uuids (:block/name m))
+             (throw (ex-info (str "No uuid found for page " (pr-str (:block/name m)))
+                             {:page m})))))
+
+
 (defn- update-block-tags
-  [block tag-classes]
+  [block tag-classes page-names-to-uuids]
   (if (seq (:block/tags block))
     (-> block
         (update :block/content
@@ -61,8 +69,9 @@
                      (map :block/original-name)))
         (update :block/content
                 db-content/replace-tags-with-page-refs
-                (remove #(tag-classes (:block/name %))
-                        (:block/tags block)))
+                (->> (:block/tags block)
+                     (remove #(tag-classes (:block/name %)))
+                     (map #(add-uuid-to-page-map % page-names-to-uuids))))
         (update :block/tags
                 (fn [tags]
                   (keep #(when (contains? tag-classes (:block/name %))
@@ -76,8 +85,8 @@
                         tags))))
     block))
 
-(defn- update-imported-block
-  [conn block tag-classes]
+(defn- convert-to-db-block
+  [conn block tag-classes page-names-to-uuids]
   (prn ::block block)
   (let [remove-keys (fn [m pred] (into {} (remove (comp pred key) m)))]
     (-> block
@@ -106,16 +115,25 @@
                                                new-key
                                                k)))
                               (remove-keys keyword?)))))))
-        (update-block-tags tag-classes)
+        (update-block-tags tag-classes page-names-to-uuids)
         ((fn [block']
            (if (seq (:block/refs block'))
-             (update block' :block/refs
-                     (fn [refs]
-                       (mapv (fn [ref]
-                               (if (and (vector? ref) (= :block/uuid (first ref)))
-                                 ref
-                                 (assoc ref :block/format :markdown)))
-                             refs)))
+             (cond-> block'
+               true
+               (update :block/refs
+                       (fn [refs]
+                         (mapv (fn [ref]
+                                 (if (and (vector? ref) (= :block/uuid (first ref)))
+                                   ref
+                                   (assoc ref :block/format :markdown)))
+                               refs)))
+               ;; check for now until :block/pre-block? is removed
+               (:block/content block')
+               (update :block/content
+                       db-content/page-ref->special-id-ref
+                       (->> (:block/refs block)
+                            (remove #(and (vector? %) (= :block/uuid (first %))))
+                            (map #(add-uuid-to-page-map % page-names-to-uuids)))))
              block')))
         add-missing-timestamps
         ;; FIXME: Remove when properties are supported
@@ -135,8 +153,8 @@
         existing-pages (keep #(d/entity @conn [:block/name (:block/name %)]) all-pages)
         existing-page-names (set (map :block/name existing-pages))
         new-pages (remove #(contains? existing-page-names (:block/name %)) all-pages)
-        names-uuids (into {} (map (juxt :block/name :block/uuid)
-                                  (concat new-pages existing-pages)))
+        page-names-to-uuids (into {} (map (juxt :block/name :block/uuid)
+                                          (concat new-pages existing-pages)))
         pages (map #(-> (merge {:block/journal? false} %)
                               ;; Fix pages missing :block/original-name. Shouldn't happen
                         ((fn [m]
@@ -150,9 +168,10 @@
                                 :block/whiteboard?)
                         ;; FIXME: Remove when properties are supported
                         (assoc :block/properties {})
-                        (update-page-tags tag-classes names-uuids page-tags-uuid))
+                        (update-page-tags tag-classes page-names-to-uuids page-tags-uuid))
                    new-pages)]
-                   pages))
+    {:pages pages
+     :page-names-to-uuids page-names-to-uuids}))
 
 (defn add-file-to-db-graph
   "Parse file and save parsed data to the given db graph. Options available:
@@ -180,7 +199,8 @@
               :else
               (println "Skipped file since its format is not supported:" file))
         ;; Build page and block txs
-        pages (build-pages-tx conn (:pages extracted) (:blocks extracted) tag-classes page-tags-uuid)
+        {:keys [pages page-names-to-uuids]}
+        (build-pages-tx conn (:pages extracted) (:blocks extracted) tag-classes page-tags-uuid)
         whiteboard-pages (->> pages
                               (filter #(= "whiteboard" (:block/type %)))
                               (map (fn [page-block]
@@ -189,7 +209,7 @@
                                                 :block/format :markdown
                                                       ;; fixme: missing properties
                                                 :block/properties {(get-pid @conn :ls-type) :whiteboard-page})))))
-        blocks (map #(update-imported-block conn % tag-classes) (:blocks extracted))
+        blocks (map #(convert-to-db-block conn % tag-classes page-names-to-uuids) (:blocks extracted))
         ;; Build indices
         pages-index (map #(select-keys % [:block/name]) pages)
         block-ids (map (fn [block] {:block/uuid (:block/uuid block)}) blocks)

+ 5 - 1
src/main/frontend/components/imports.cljs

@@ -198,7 +198,11 @@
                                                     :user-options user-options
                                                     :page-tags-uuid page-tags-uuid})]
                                               (db-browser/transact! @db-browser/*worker repo (:tx-data tx-report) (:tx-meta tx-report)))
-                                            m)))))
+                                            m))
+                                  (p/catch (fn [error]
+                                             (notification/show! (str "Import failed on " (pr-str rpath) " with error:\n" error)
+                                                                 :error)
+                                             (log/error :import-error {:path rpath :error error}))))))
               (recur))
             (async/offer! imported-chan true))))
       (catch :default e