Browse Source

refactor: DRY up spread out definition of db's

namespace/parent feature. Following up to #11517, provide vars
so it's clear how a feature is used and coupled throughout the codebase
Gabriel Horner 1 year ago
parent
commit
afe4280c6a

+ 1 - 0
.clj-kondo/config.edn

@@ -155,6 +155,7 @@
              logseq.common.util.page-ref page-ref
              logseq.common.util.block-ref block-ref
              logseq.common.util.macro macro-util
+             logseq.common.util.namespace ns-util
              logseq.db ldb
              logseq.db.frontend.content db-content
              logseq.db.frontend.class db-class

+ 1 - 0
deps/common/.carve/config.edn

@@ -1,6 +1,7 @@
 {:paths ["src"]
  :api-namespaces [logseq.common.path
                   logseq.common.uuid
+                  logseq.common.util.namespace
                   logseq.common.util.page-ref
                   logseq.common.util.block-ref
                   logseq.common.util

+ 20 - 0
deps/common/src/logseq/common/util/namespace.cljs

@@ -0,0 +1,20 @@
+(ns logseq.common.util.namespace
+  "Util fns for namespace and parent features"
+  (:require [clojure.string :as string]
+            [logseq.common.util :as common-util]))
+
+;; Only used by DB graphs
+(defonce parent-char "/")
+(defonce parent-re #"/")
+;; Used by DB and file graphs
+(defonce namespace-char "/")
+
+(defn namespace-page?
+  "Used by DB and file graphs"
+  [page-name]
+  (and (string? page-name)
+       (string/includes? page-name namespace-char)
+       (not= (string/trim page-name) namespace-char)
+       (not (string/starts-with? page-name "../"))
+       (not (string/starts-with? page-name "./"))
+       (not (common-util/url? page-name))))

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

@@ -12,6 +12,7 @@
             [logseq.db.frontend.property :as db-property]
             [logseq.db.frontend.entity-util :as entity-util]
             [logseq.common.util.date-time :as date-time-util]
+            [logseq.common.util.namespace :as ns-util]
             [datascript.core :as d]))
 
 (def db-based-graph? entity-util/db-based-graph?)
@@ -41,7 +42,7 @@
                 parent (when (= (:block/type e) "page")
                          (:logseq.property/parent e))]
             (if (and db-based? parent parent-title?)
-              (str (:block/title parent) "/" result')
+              (str (:block/title parent) ns-util/parent-char result')
               result'))
           default-value))))))
 

+ 3 - 9
deps/graph-parser/src/logseq/graph_parser/text.cljs

@@ -6,7 +6,8 @@
             [logseq.graph-parser.property :as gp-property]
             [logseq.graph-parser.mldoc :as gp-mldoc]
             [logseq.common.util :as common-util]
-            [logseq.common.util.page-ref :as page-ref]))
+            [logseq.common.util.page-ref :as page-ref]
+            [logseq.common.util.namespace :as ns-util]))
 
 (def get-file-basename page-ref/get-file-basename)
 
@@ -149,11 +150,4 @@
             new-val
             v'))))))
 
-(defn namespace-page?
-  [page-name]
-  (and (string? page-name)
-       (string/includes? page-name "/")
-       (not= (string/trim page-name) "/")
-       (not (string/starts-with? page-name "../"))
-       (not (string/starts-with? page-name "./"))
-       (not (common-util/url? page-name))))
+(def namespace-page? ns-util/namespace-page?)

+ 4 - 4
deps/outliner/src/logseq/outliner/core.cljs

@@ -21,9 +21,9 @@
             [logseq.outliner.batch-tx :include-macros true :as batch-tx]
             [logseq.db.frontend.order :as db-order]
             [logseq.outliner.pipeline :as outliner-pipeline]
-            [logseq.graph-parser.text :as text]
             [logseq.common.util.macro :as macro-util]
-            [logseq.db.frontend.class :as db-class]))
+            [logseq.db.frontend.class :as db-class]
+            [logseq.common.util.namespace :as ns-util]))
 
 (def ^:private block-map
   (mu/optional-keys
@@ -275,9 +275,9 @@
               block-type (if (contains? tags-set (:block/uuid page))
                            "class"
                            (:block/type page))]
-          (if (and (contains? #{"page" "class"} block-type) (text/namespace-page? title))
+          (if (and (contains? #{"page" "class"} block-type) (ns-util/namespace-page? title))
             (let [class? (= block-type "class")
-                  parts (->> (string/split title #"/")
+                  parts (->> (string/split title ns-util/parent-re)
                              (map string/trim)
                              (remove string/blank?))
                   pages (doall

+ 3 - 2
src/main/frontend/components/block.cljs

@@ -92,7 +92,8 @@
             [logseq.db.frontend.content :as db-content]
             [logseq.db :as ldb]
             [frontend.components.title :as title]
-            [frontend.modules.shortcut.utils :as shortcut-utils]))
+            [frontend.modules.shortcut.utils :as shortcut-utils]
+            [logseq.common.util.namespace :as ns-util]))
 
 ;; local state
 (defonce *dragging?
@@ -664,7 +665,7 @@
           (let [parent (:logseq.property/parent page-entity)]
             (if (and display-parent? parent (not (ldb/class? page-entity)))
               [:span
-               (str (:block/title parent) "/")
+               (str (:block/title parent) ns-util/parent-char)
                page-component]
               page-component))))]]))
 

+ 3 - 2
src/main/frontend/components/cmdk/core.cljs

@@ -37,7 +37,8 @@
    [frontend.util.text :as text-util]
    [goog.userAgent]
    [frontend.db.async :as db-async]
-   [logseq.db :as ldb]))
+   [logseq.db :as ldb]
+   [logseq.common.util.namespace :as ns-util]))
 
 (defn translate [t {:keys [id desc]}]
   (when id
@@ -106,7 +107,7 @@
                             (take 5 items))))
         node-exists? (let [blocks-result (keep :source-block (get-in results [:nodes :items]))]
                        (when-not (string/blank? input)
-                         (or (some-> (last (string/split input "/"))
+                         (or (some-> (last (string/split input ns-util/parent-char))
                                      string/trim
                                      db/get-page)
                              (some (fn [block]

+ 3 - 2
src/main/frontend/components/container.cljs

@@ -57,7 +57,8 @@
             [react-draggable]
             [reitit.frontend.easy :as rfe]
             [rum.core :as rum]
-            [logseq.db :as ldb]))
+            [logseq.db :as ldb]
+            [logseq.common.util.namespace :as ns-util]))
 
 (rum/defc nav-content-item < rum/reactive
   [name {:keys [class count]} child]
@@ -144,7 +145,7 @@
         :else (let [title' (pdf-utils/fix-local-asset-pagename title)
                     parent (:logseq.property/parent page)]
                 (if (and parent (not (ldb/class? page)))
-                  (str (:block/title parent) "/" title')
+                  (str (:block/title parent) ns-util/parent-char title')
                   title')))]
 
      ;; dots trigger

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

@@ -45,7 +45,8 @@
             [frontend.modules.outliner.op :as outliner-op]
             [frontend.handler.property.util :as pu]
             [datascript.impl.entity :as de]
-            [frontend.handler.db-based.page :as db-page-handler]))
+            [frontend.handler.db-based.page :as db-page-handler]
+            [logseq.common.util.namespace :as ns-util]))
 
 (def <create! page-common-handler/<create!)
 (def <delete! page-common-handler/<delete!)
@@ -375,7 +376,7 @@
                                   :split-namespace? true})))
             ref-text' (if result
                         (let [title (if-let [parent (:logseq.property/parent result)]
-                                      (str (:block/title parent) "/" (:block/title result))
+                                      (str (:block/title parent) ns-util/parent-char (:block/title result))
                                       (:block/title result))]
                           (page-ref/->page-ref title))
                         ref-text)]

+ 5 - 5
src/main/frontend/worker/search.cljs

@@ -11,7 +11,7 @@
             [logseq.common.util :as common-util]
             [logseq.db :as ldb]
             [clojure.set :as set]
-            [logseq.graph-parser.text :as text]))
+            [logseq.common.util.namespace :as ns-util]))
 
 ;; TODO: use sqlite for fuzzy search
 ;; maybe https://github.com/nalgeon/sqlean/blob/main/docs/fuzzy.md?
@@ -142,9 +142,9 @@ DROP TRIGGER IF EXISTS blocks_au;
 (defn- search-blocks-aux
   [db sql q input page limit enable-snippet?]
   (try
-    (p/let [namespace? (text/namespace-page? q)
+    (p/let [namespace? (ns-util/namespace-page? q)
             last-part (when namespace?
-                        (some-> (last (string/split q "/"))
+                        (some-> (last (string/split q ns-util/parent-char))
                                 get-match-input))
             bind (cond
                    (and namespace? page)
@@ -222,7 +222,7 @@ DROP TRIGGER IF EXISTS blocks_au;
       ;;                 content)])
     (let [parent (:logseq.property/parent block)
           title (if (and parent (= "page" (:block/type block)))
-                  (str (:block/title parent) "/" title)
+                  (str (:block/title parent) ns-util/parent-char title)
                   title)]
       (when uuid
         {:id (str uuid)
@@ -286,7 +286,7 @@ DROP TRIGGER IF EXISTS blocks_au;
                      (str "select id, page, title, " snippet-aux " from blocks_fts where ")
                      "select id, page, title from blocks_fts where ")
             pg-sql (if page "page = ? and" "")
-            match-sql (if (text/namespace-page? q)
+            match-sql (if (ns-util/namespace-page? q)
                         (str select pg-sql " title match ? or title match ? order by rank limit ?")
                         (str select pg-sql " title match ? order by rank limit ?"))
             matched-result (search-blocks-aux search-db match-sql q match-input page limit enable-snippet?)