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

refactor: internal ref uses [[uuid]] instead of ~^[[uuid]]

Tienson Qin 10 сар өмнө
parent
commit
faf7f48860

+ 3 - 4
deps/db/src/logseq/db.cljs

@@ -15,9 +15,9 @@
             [logseq.db.frontend.rules :as rules]
             [logseq.db.sqlite.common-db :as sqlite-common-db]
             [logseq.db.sqlite.util :as sqlite-util]
-            [logseq.db.frontend.content :as db-content]
             [logseq.db.frontend.property :as db-property]
-            [logseq.common.util.namespace :as ns-util])
+            [logseq.common.util.namespace :as ns-util]
+            [logseq.common.util.page-ref :as page-ref])
   (:refer-clojure :exclude [object?]))
 
 (defonce *transact-fn (atom nil))
@@ -588,8 +588,7 @@
 (defn inline-tag?
   [block-raw-title tag]
   (assert (string? block-raw-title) "block-raw-title should be a string")
-  (or (string/includes? block-raw-title (str "#" (db-content/block-id->special-id-ref (:block/uuid tag))))
-      (string/includes? block-raw-title (str "#" db-content/page-ref-special-chars (:block/uuid tag)))))
+  (string/includes? block-raw-title (str "#" (page-ref/->page-ref (:block/uuid tag)))))
 
 (defonce node-display-type-classes
   #{:logseq.class/Code-block :logseq.class/Math-block :logseq.class/Quote-block})

+ 25 - 58
deps/db/src/logseq/db/frontend/content.cljs

@@ -1,87 +1,55 @@
 (ns logseq.db.frontend.content
-  "Fns to handle block content e.g. special ids"
+  "Fns to handle block content e.g. internal ids"
   (:require [clojure.string :as string]
             [logseq.common.util.page-ref :as page-ref]
             [datascript.core :as d]
             [logseq.common.util :as common-util]
             [logseq.db.frontend.entity-util :as entity-util]))
 
-(defonce page-ref-special-chars "~^")
+#_(defonce page-ref-special-chars "~^")
 
-(defonce special-id-ref-pattern
-  (re-pattern
-   (str
-    "(?i)"
-    "~\\^"
-    "("
-    common-util/uuid-pattern
-    ")")))
+(defonce id-ref-pattern
+  #"\[\[([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\]\]")
 
-(defn block-id->special-id-ref
-  [id]
-  (str page-ref/left-brackets
-       page-ref-special-chars
-       id
-       page-ref/right-brackets))
-
-(defn special-id-ref->page
-  "Convert special id ref backs to page name using refs."
+(defn content-id-ref->page
+  "Convert id ref backs to page name using refs."
   [content refs]
   (reduce
    (fn [content ref]
      (if (:block/title ref)
-       (-> content
-           (string/replace (block-id->special-id-ref (:block/uuid ref))
-                           (:block/title ref))
-           (string/replace
-                (str "#" page-ref-special-chars
-                     (:block/uuid ref))
-                (str "#" (:block/title ref))))
+       (string/replace content (page-ref/->page-ref (:block/uuid ref)) (:block/title ref))
        content))
    content
    refs))
 
-(defn special-id-ref->page-ref
-  "Convert special id ref backs to page name refs using refs."
+(defn id-ref->title-ref
+  "Convert id ref backs to page name refs using refs."
   [content* refs]
   (let [content (str content*)]
-    (if (or (string/includes? content (str page-ref/left-brackets page-ref-special-chars))
-            (string/includes? content (str "#" page-ref-special-chars)))
+    (if (re-find id-ref-pattern content)
       (reduce
        (fn [content ref]
          (if (:block/title ref)
-           (-> content
-               ;; Replace page refs
-               (string/replace
-                (str page-ref/left-brackets
-                     page-ref-special-chars
-                     (:block/uuid ref)
-                     page-ref/right-brackets)
-                (page-ref/->page-ref (:block/title ref)))
-               ;; Replace tags
-               (string/replace
-                (str "#" page-ref-special-chars
-                     (:block/uuid ref))
-                (str "#" (:block/title ref))))
-
+           (string/replace content
+                           (page-ref/->page-ref (:block/uuid ref))
+                           (page-ref/->page-ref (:block/title ref)))
            content))
        content
        refs)
       content)))
 
-(defn get-matched-special-ids
+(defn get-matched-ids
   [content]
-  (->> (re-seq special-id-ref-pattern content)
+  (->> (re-seq id-ref-pattern content)
        (distinct)
        (map second)
        (map uuid)))
 
 (defn- replace-tag-ref
   [content page-name id]
-  (let [id' (str page-ref-special-chars id)
-        [page wrapped-id] (if (string/includes? page-name " ")
-                            (map page-ref/->page-ref [page-name id'])
-                            [page-name id'])
+  (let [[page wrapped-id] (if (string/includes? page-name " ")
+                            (map page-ref/->page-ref [page-name id])
+                            [page-name id])
         page-name (common-util/format "#%s" page)
         r (common-util/format "#%s" wrapped-id)]
     ;; hash tag parsing rules https://github.com/logseq/mldoc/blob/701243eaf9b4157348f235670718f6ad19ebe7f8/test/test_markdown.ml#L631
@@ -95,8 +63,7 @@
 
 (defn- replace-page-ref
   [content page-name id]
-  (let [id' (str page-ref-special-chars id)
-        [page wrapped-id] (map page-ref/->page-ref [page-name id'])]
+  (let [[page wrapped-id] (map page-ref/->page-ref [page-name id])]
     (common-util/replace-ignore-case content page wrapped-id)))
 
 (defn- replace-page-ref-with-id
@@ -109,8 +76,8 @@
       (replace-tag-ref content' page-name id)
       content')))
 
-(defn refs->special-id-ref
-  "Convert ref to special id refs e.g. `[[page name]] -> [[~^...]]."
+(defn title-ref->id-ref
+  "Convert ref to id refs e.g. `[[page name]] -> [[uuid]]."
   [title refs & {:keys [replace-tag?]
                  :or {replace-tag? true}}]
   (assert (string? title))
@@ -134,7 +101,7 @@
   (if (entity-util/db-based-graph? db)
     (if-let [content (:block/title item)]
       (let [refs (:block/refs (d/entity db eid))]
-        (assoc item :block/title (special-id-ref->page-ref content refs)))
+        (assoc item :block/title (id-ref->title-ref content refs)))
       item)
     item))
 
@@ -145,13 +112,13 @@
   (->>
    (reduce
     (fn [content tag]
-      (let [id-ref (block-id->special-id-ref (:block/uuid tag))]
+      (let [id-ref (page-ref/->page-ref (:block/uuid tag))]
         (-> content
-           ;; #[[favorite book]]
+            ;; #[[favorite book]]
             (common-util/replace-ignore-case
              (str "#" page-ref/left-brackets (:block/title tag) page-ref/right-brackets)
              id-ref)
-          ;; #book
+            ;; #book
             (common-util/replace-ignore-case (str "#" (:block/title tag)) id-ref))))
     content
     (sort-by :block/title > tags))

+ 1 - 7
deps/db/src/logseq/db/frontend/delete_blocks.cljs

@@ -5,8 +5,7 @@
             [logseq.common.util.page-ref :as page-ref]
             [datascript.core :as d]
             [clojure.string :as string]
-            [logseq.db.frontend.entity-util :as entity-util]
-            [logseq.db.frontend.content :as db-content]))
+            [logseq.db.frontend.entity-util :as entity-util]))
 
 (defn- replace-ref-with-deleted-block-title
   [block ref-raw-title]
@@ -19,11 +18,6 @@
 
             (string/replace (block-ref/->block-ref (str (:block/uuid block)))
                             block-content)
-
-                                               ;; Replace object
-            (string/replace (db-content/block-id->special-id-ref (:block/uuid block))
-                            block-content)
-                                               ;; Replace non-object
             (string/replace (page-ref/->page-ref (str (:block/uuid block)))
                             block-content))))
 

+ 2 - 2
deps/db/src/logseq/db/frontend/entity_plus.cljc

@@ -34,7 +34,7 @@
        (let [result (lookup-entity e k default-value)
              refs (:block/refs e)
              result' (if (and (string? result) refs)
-                       (db-content/special-id-ref->page-ref result refs)
+                       (db-content/id-ref->title-ref result refs)
                        result)]
          (or result' default-value))))))
 
@@ -104,7 +104,7 @@
   [^js this]
   (let [v @(.-cache this)
         v' (if (:block/title v)
-             (assoc v :block/title (db-content/special-id-ref->page-ref (:block/title v) (:block/refs this)))
+             (assoc v :block/title (db-content/id-ref->title-ref (:block/title v) (:block/refs this)))
              v)]
     (concat (seq v')
             (seq (.-kv this)))))

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

@@ -138,7 +138,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 (:block/title m) block-refs {:replace-tag? false})
+                       {:block/title (db-content/title-ref->id-ref (:block/title m) block-refs {:replace-tag? false})
                         :block/refs block-refs})))))))
 
 (defn- build-properties-tx [properties page-uuids all-idents]

+ 1 - 1
deps/db/test/logseq/db/frontend/content_test.cljs

@@ -4,7 +4,7 @@
 
 (deftest replace-tags-with-page-refs
   (testing "tags with overlapping names get replaced correctly"
-    (is (= "string [[~^foo]] string2 [[~^foo-bar]]"
+    (is (= "string [[foo]] string2 [[foo-bar]]"
            (db-content/replace-tags-with-page-refs
             "string #foo string2 #foo-bar"
             [{:block/title "foo" :block/uuid "foo"}

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

@@ -825,7 +825,7 @@
                                   ;; 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 (:block/title block) refs {:replace-tag? false}))))
+                 (db-content/title-ref->id-ref (:block/title block) refs {:replace-tag? false}))))
       block)))
 
 (defn- fix-pre-block-references

+ 1 - 1
deps/outliner/src/logseq/outliner/pipeline.cljs

@@ -136,7 +136,7 @@
   (let [content (or (:block/raw-title block)
                     (:block/title block))]
     (when (string? content)
-      (->> (db-content/get-matched-special-ids content)
+      (->> (db-content/get-matched-ids content)
            (map (fn [id]
                   (when-let [e (d/entity db [:block/uuid id])]
                     (:db/id e))))))))

+ 2 - 2
deps/outliner/test/logseq/outliner/pipeline_test.cljs

@@ -8,7 +8,7 @@
             [logseq.outliner.pipeline :as outliner-pipeline]
             [clojure.string :as string]
             [logseq.db.test.helper :as db-test]
-            [logseq.db.frontend.content :as db-content]))
+            [logseq.common.util.page-ref :as page-ref]))
 
 (defn- get-blocks [db]
   (->> (d/q '[:find (pull ?b [* {:block/path-refs [:block/name :db/id]}])
@@ -64,4 +64,4 @@
     (assert block)
     (is (= [(:db/id block)]
            (outliner-pipeline/block-content-refs @conn
-                                                 {:block/title (str "ref to " (db-content/block-id->special-id-ref (:block/uuid block)))})))))
+                                                 {:block/title (str "ref to " (page-ref/->page-ref (:block/uuid block)))})))))

+ 1 - 1
src/main/frontend/common_keywords.cljs

@@ -25,7 +25,7 @@
 
 (sr/defkeyword :block/title
   "Title or content string of the blocks.
-in db-version, page-references(e.g. [[page-name]]) are stored as [[~^uuid]]."
+in db-version, page-references(e.g. [[page-name]]) are stored as [[uuid]]."
   :string)
 
 (sr/defkeyword :block/raw-title

+ 4 - 11
src/main/frontend/components/block.cljs

@@ -751,8 +751,8 @@
                                                            :block/title "FIX unknown page"
                                                            :block/name "fix unknown page"}])
                                            "Unknown title")
-                                         (re-find db-content/special-id-ref-pattern s)
-                                         (db-content/special-id-ref->page s (:block/refs page-entity))
+                                         (re-find db-content/id-ref-pattern s)
+                                         (db-content/content-id-ref->page s (:block/refs page-entity))
                                          :else
                                          s)
                                      s (if tag? (str "#" s) s)]
@@ -904,11 +904,7 @@
                  *result (atom nil)
                  page-name (or (:block/uuid page)
                                (when-let [s (:block/name page)]
-                                 (let [s (string/trim s)
-                                       s (if (string/starts-with? s db-content/page-ref-special-chars)
-                                           (common-util/safe-subs s 2)
-                                           s)]
-                                   s)))
+                                 (string/trim s)))
                  page-entity (if (e/entity? page) page (db/get-page page-name))]
              (if page-entity
                (reset! *result page-entity)
@@ -1060,10 +1056,7 @@
   [html-export? uuid-or-title* {:keys [nested-link? show-brackets? id] :as config} label]
   (when uuid-or-title*
     (let [uuid-or-title (if (string? uuid-or-title*)
-                          (as-> (string/trim uuid-or-title*) s
-                            (if (string/starts-with? s db-content/page-ref-special-chars)
-                              (common-util/safe-subs s 2)
-                              s))
+                          (string/trim uuid-or-title*)
                           uuid-or-title*)
           show-brackets? (if (some? show-brackets?) show-brackets? (state/show-brackets?))
           contents-page? (= "contents" (string/lower-case (str id)))

+ 2 - 2
src/main/frontend/db/model.cljs

@@ -171,8 +171,8 @@ independent of format as format specific heading characters are stripped"
                   (let [block (db-utils/entity repo block-id)
                         ref-tags (distinct (concat (:block/tags block) (:block/refs block)))]
                     (= (-> block-content
-                           (db-content/special-id-ref->page-ref ref-tags)
-                           (db-content/special-id-ref->page ref-tags)
+                           (db-content/id-ref->title-ref ref-tags)
+                           (db-content/content-id-ref->page ref-tags)
                            heading-content->route-name)
                        (string/lower-case external-content))))
                 (rules/extract-rules rules/db-query-dsl-rules [:has-property]))

+ 1 - 6
src/main/frontend/handler/common/page.cljs

@@ -54,8 +54,7 @@
              has-tags? (and db-based? (seq (:block/tags parsed-result)))
              title' (if has-tags?
                       (some-> (first
-                               (or (common-util/split-first (str "#" db-content/page-ref-special-chars) (:block/title parsed-result))
-                                   (common-util/split-first (str "#" page-ref/left-brackets db-content/page-ref-special-chars) (:block/title parsed-result))))
+                               (common-util/split-first (str "#" page-ref/left-brackets) (:block/title parsed-result)))
                               string/trim)
                       title)]
        (if (and has-tags? (nil? title'))
@@ -78,7 +77,6 @@
                  (block-handler/edit-block! first-block :max {:container-id :unknown-container}))
                page))))))))
 
-
 ;; favorite fns
 ;; ============
 (defn file-favorited?
@@ -145,10 +143,8 @@
      {:outliner-op :delete-blocks}
      (outliner-op/delete-blocks! [block] {}))))
 
-
 ;; favorites fns end ================
 
-
 (defn <delete!
   "Deletes a page. If delete is successful calls ok-handler. Otherwise calls error-handler
    if given. Note that error-handler is being called in addition to error messages that worker
@@ -181,7 +177,6 @@
 ;; other fns
 ;; =========
 
-
 (defn after-page-deleted!
   [repo page-name file-path tx-meta]
   (let [repo-dir (config/get-repo-dir repo)]

+ 1 - 1
src/main/frontend/handler/db_based/editor.cljs

@@ -66,7 +66,7 @@
         result (-> block
                    (merge (if level {:block/level level} {}))
                    (assoc :block/title
-                          (db-content/refs->special-id-ref (:block/title block) (:block/refs block))))]
+                          (db-content/title-ref->id-ref (:block/title block) (:block/refs block))))]
     result))
 
 (defn save-file!

+ 2 - 8
src/main/frontend/handler/page.cljs

@@ -41,13 +41,11 @@
             [logseq.common.config :as common-config]
             [logseq.common.path :as path]
             [logseq.common.util :as common-util]
-            [logseq.common.util.block-ref :as block-ref]
             [logseq.common.util.page-ref :as page-ref]
             [logseq.db :as ldb]
             [logseq.graph-parser.db :as gp-db]
             [logseq.graph-parser.text :as text]
-            [promesa.core :as p]
-            [logseq.db.frontend.content :as db-content]))
+            [promesa.core :as p]))
 
 (def <create! page-common-handler/<create!)
 (def <delete! page-common-handler/<delete!)
@@ -363,11 +361,7 @@
                                               [page (db/get-page page)])))
                                         [chosen' chosen-result])
             ref-text (if (and (de/entity? chosen-result) (not (ldb/page? chosen-result)))
-                       (cond
-                         db-based?
-                         (db-content/block-id->special-id-ref (:block/uuid chosen-result))
-                         :else
-                         (block-ref/->block-ref (:block/uuid chosen-result)))
+                       (page-ref/->page-ref (:block/uuid chosen-result))
                        (get-page-ref-text chosen'))
             result (when db-based?
                      (when-not (de/entity? chosen-result)

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

@@ -38,7 +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 title' refs)))))))))]
+                                                          (db-content/title-ref->id-ref title' refs)))))))))]
       (editor-handler/paste-blocks blocks' {:keep-uuid? true}))))
 
 (defn- paste-segmented-text

+ 1 - 1
src/main/frontend/worker/handler/page.cljs

@@ -46,7 +46,7 @@
   [repo page-entity]
   (when (sqlite-util/db-based-graph? repo)
     (let [refs (:block/_refs page-entity)
-          id-ref->page #(db-content/special-id-ref->page % [page-entity])]
+          id-ref->page #(db-content/content-id-ref->page % [page-entity])]
       (when (seq refs)
         (let [tx-data (mapcat (fn [{:block/keys [raw-title] :as ref}]
                                 ;; block content