Browse Source

Move common uses of page-ref to its own ns

By having a specific ns for page-ref utils, our code is more readable
and intention revealing. Also found that
text/{get-page-name,page-ref-un-brackets!} was getting called in
contexts that didn't make sense e.g. query layer was checking for
markdown and org page-refs
Gabriel Horner 3 years ago
parent
commit
e0b1f6b2de

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

@@ -32,7 +32,8 @@
              logseq.graph-parser.util gp-util
              logseq.graph-parser.property gp-property
              logseq.graph-parser.config gp-config
-             logseq.graph-parser.date-time-util date-time-util}}}
+             logseq.graph-parser.date-time-util date-time-util
+             logseq.graph-parser.util.page-ref page-ref}}}
 
  :hooks {:analyze-call {rum.core/defc hooks.rum/defc
                         rum.core/defcs hooks.rum/defcs}}

+ 6 - 0
deps/graph-parser/.carve/ignore

@@ -12,3 +12,9 @@ logseq.graph-parser.block/->block-ref
 logseq.graph-parser.block/block-ref?
 ;; API
 logseq.graph-parser.block/get-all-block-ref-ids
+;; API
+logseq.graph-parser.util.page-ref/left-and-right-brackets
+;; API
+logseq.graph-parser.util.page-ref/->page-ref
+;; API
+logseq.graph-parser.util.page-ref/get-page-name!

+ 2 - 1
deps/graph-parser/.clj-kondo/config.edn

@@ -9,6 +9,7 @@
              logseq.graph-parser.util gp-util
              logseq.graph-parser.property gp-property
              logseq.graph-parser.config gp-config
-             logseq.graph-parser.date-time-util date-time-util}}}
+             logseq.graph-parser.date-time-util date-time-util
+             logseq.graph-parser.util.page-ref page-ref}}}
  :skip-comments true
  :output {:progress true}}

+ 3 - 2
deps/graph-parser/src/logseq/graph_parser/block.cljs

@@ -10,7 +10,8 @@
             [logseq.graph-parser.property :as gp-property]
             [logseq.graph-parser.text :as text]
             [logseq.graph-parser.utf8 :as utf8]
-            [logseq.graph-parser.util :as gp-util]))
+            [logseq.graph-parser.util :as gp-util]
+            [logseq.graph-parser.util.page-ref :as page-ref]))
 
 (defn heading-block?
   [block]
@@ -87,7 +88,7 @@ of block-ref?"
 
                   (and
                    (= typ "Search")
-                   (text/page-ref? value)
+                   (page-ref/page-ref? value)
                    (text/page-ref-un-brackets! value))
 
                   (and

+ 24 - 32
deps/graph-parser/src/logseq/graph_parser/text.cljs

@@ -4,11 +4,8 @@
             [clojure.string :as string]
             [clojure.set :as set]
             [logseq.graph-parser.mldoc :as gp-mldoc]
-            [logseq.graph-parser.util :as gp-util]))
-
-(def page-ref-re-0 #"\[\[(.*)\]\]")
-(def org-page-ref-re #"\[\[(file:.*)\]\[.+?\]\]")
-(def markdown-page-ref-re #"\[(.*)\]\(file:.*\)")
+            [logseq.graph-parser.util :as gp-util]
+            [logseq.graph-parser.util.page-ref :as page-ref :refer [right-brackets]]))
 
 (defn get-file-basename
   [path]
@@ -16,7 +13,14 @@
     ;; Same as util/node-path.name
     (.-name (path/parse (string/replace path "+" "/")))))
 
+(def page-ref-re-0 #"\[\[(.*)\]\]")
+(def org-page-ref-re #"\[\[(file:.*)\]\[.+?\]\]")
+(def markdown-page-ref-re #"\[(.*)\]\(file:.*\)")
+
 (defn get-page-name
+  "Extracts page names from format-specific page-refs e.g. org/md specific and
+  logseq page-refs. Only call in contexts where format-specific page-refs are
+  used. For logseq page-refs use page-ref/get-page-name"
   [s]
   (and (string? s)
        (or (when-let [[_ label _path] (re-matches markdown-page-ref-re s)]
@@ -27,19 +31,6 @@
            (-> (re-matches page-ref-re-0 s)
                second))))
 
-(defn page-ref?
-  [s]
-  (and
-   (string? s)
-   (string/starts-with? s "[[")
-   (string/ends-with? s "]]")))
-
-(defonce page-ref-re #"\[\[(.*?)\]\]")
-
-(defonce page-ref-re-2 #"(\[\[.*?\]\])")
-
-(def page-ref-re-without-nested #"\[\[([^\[\]]+)\]\]")
-
 (defn page-ref-un-brackets!
   [s]
   (or (get-page-name s) s))
@@ -64,18 +55,18 @@
 (defn- not-matched-nested-pages
   [s]
   (and (string? s)
-       (> (count (re-seq #"\[\[" s))
-          (count (re-seq #"\]\]" s)))))
+       (> (count (re-seq page-ref/left-brackets-re s))
+          (count (re-seq page-ref/right-brackets-re s)))))
 
 (defn- ref-matched?
   [s]
-  (let [x (re-seq #"\[\[" s)
-        y (re-seq #"\]\]" s)]
+  (let [x (re-seq page-ref/left-brackets-re s)
+        y (re-seq page-ref/right-brackets-re s)]
     (and (> (count x) 0) (= (count x) (count y)))))
 
 (defn get-nested-page-name
   [page-name]
-  (when-let [first-match (re-find page-ref-re-without-nested page-name)]
+  (when-let [first-match (re-find page-ref/page-ref-without-nested-re page-name)]
     (second first-match)))
 
 (defn- concat-nested-pages
@@ -83,7 +74,7 @@
   (first
    (reduce (fn [[acc not-matched-s] s]
              (cond
-               (and not-matched-s (= s "]]"))
+               (and not-matched-s (= s right-brackets))
                (let [s' (str not-matched-s s)]
                  (if (ref-matched? s')
                    [(conj acc s') nil]
@@ -118,30 +109,31 @@
 
      (and (string? s)
             ;; Either a page ref, a tag or a comma separated collection
-            (or (gp-util/safe-re-find page-ref-re s)
+            (or (gp-util/safe-re-find page-ref/page-ref-re s)
                 (gp-util/safe-re-find #"[\,|,|#|\"]+" s)))
      (let [result (->> (sep-by-quotes s)
                        (mapcat
                         (fn [s]
                           (when-not (gp-util/wrapped-by-quotes? (string/trim s))
-                            (string/split s page-ref-re-2))))
+                            (string/split s page-ref/page-ref-outer-capture-re))))
                        (mapcat (fn [s]
                                  (cond
                                    (gp-util/wrapped-by-quotes? s)
                                    nil
 
-                                   (string/includes? (string/trimr s) "]],")
-                                   (let [idx (string/index-of s "]],")]
+                                   (string/includes? (string/trimr s)
+                                                     (str right-brackets ","))
+                                   (let [idx (string/index-of s (str right-brackets ","))]
                                      [(subs s 0 idx)
-                                      "]]"
+                                      right-brackets
                                       (subs s (+ idx 3))])
 
                                    :else
                                    [s])))
                        (remove #(= % ""))
-                       (mapcat (fn [s] (if (string/ends-with? s "]]")
+                       (mapcat (fn [s] (if (string/ends-with? s right-brackets)
                                          [(subs s 0 (- (count s) 2))
-                                          "]]"]
+                                          right-brackets]
                                          [s])))
                        concat-nested-pages
                        (remove string/blank?)
@@ -150,7 +142,7 @@
                                    (gp-util/wrapped-by-quotes? s)
                                    nil
 
-                                   (page-ref? s)
+                                   (page-ref/page-ref? s)
                                    [(if un-brackets? (page-ref-un-brackets! s) s)]
 
                                    :else

+ 39 - 0
deps/graph-parser/src/logseq/graph_parser/util/page_ref.cljs

@@ -0,0 +1,39 @@
+(ns logseq.graph-parser.util.page-ref
+  "General purpose vars and util fns for page-ref. Currently this only handles
+a logseq page-ref e.g. [[page name]]"
+  (:require [clojure.string :as string]))
+
+(def left-brackets "Opening characters for page-ref" "[[")
+(def right-brackets "Closing characters for page-ref" "]]")
+(def left-and-right-brackets "Opening and closing characters for page-ref"
+  (str left-brackets right-brackets))
+
+;; common regular expressions
+(def left-brackets-re #"\[\[")
+(def right-brackets-re #"\]\]")
+(def page-ref-re "Inner capture and doesn't match nested brackets" #"\[\[(.*?)\]\]")
+(def page-ref-outer-capture-re #"(\[\[.*?\]\])")
+(def page-ref-without-nested-re "Matches most inner nested brackets" #"\[\[([^\[\]]+)\]\]")
+(def page-ref-any-re "Inner capture that matches anything between brackets" #"\[\[(.*)\]\]")
+
+(defn page-ref?
+  "Determines if string is page-ref. Avoid using with format-specific page-refs e.g. org"
+  [s]
+  (and (string/starts-with? s left-brackets)
+       (string/ends-with? s right-brackets)))
+
+(defn ->page-ref
+  "Create a page ref given a page name"
+  [page-name]
+  (str left-brackets page-name right-brackets))
+
+(defn get-page-name
+  "Extracts page-name from page-ref string"
+  [s]
+  (second (re-matches page-ref-any-re s)))
+
+(defn get-page-name!
+  "Extracts page-name from page-ref and fall back to arg. Useful for when user
+  input may (not) be a page-ref"
+  [s]
+  (or (get-page-name s) s))

+ 2 - 0
deps/graph-parser/test/logseq/graph_parser/nbb_test_runner.cljs

@@ -7,6 +7,7 @@
             [logseq.graph-parser.property-test]
             [logseq.graph-parser.extract-test]
             [logseq.graph-parser.cli-test]
+            [logseq.graph-parser.util.page-ref-test]
             [logseq.graph-parser-test]))
 
 (defmethod cljs.test/report [:cljs.test/default :end-run-tests] [m]
@@ -21,4 +22,5 @@
                'logseq.graph-parser.block-test
                'logseq.graph-parser.extract-test
                'logseq.graph-parser.cli-test
+               'logseq.graph-parser.util.page-ref-test
                'logseq.graph-parser-test))

+ 0 - 8
deps/graph-parser/test/logseq/graph_parser/text_test.cljs

@@ -26,14 +26,6 @@
          "[logseq/page](file:./logseq.page.md)" "logseq/page"
          "[logseq/page](file:./pages/logseq.page.md)" "logseq/page"))
 
-(deftest page-ref?
-  []
-  (are [x y] (= (text/page-ref? x) y)
-    "[[page]]" true
-    "[[another page]]" true
-    "[single bracket]" false
-    "no brackets" false))
-
 (deftest page-ref-un-brackets!
   []
   (are [x y] (= (text/page-ref-un-brackets! x) y)

+ 11 - 0
deps/graph-parser/test/logseq/graph_parser/util/page_ref_test.cljs

@@ -0,0 +1,11 @@
+(ns logseq.graph-parser.util.page-ref-test
+  (:require [logseq.graph-parser.util.page-ref :as page-ref]
+            [cljs.test :refer [are deftest]]))
+
+(deftest page-ref?
+  []
+  (are [x y] (= (page-ref/page-ref? x) y)
+       "[[page]]" true
+       "[[another page]]" true
+       "[single bracket]" false
+       "no brackets" false))

+ 5 - 4
src/main/frontend/commands.cljs

@@ -19,6 +19,7 @@
             [logseq.graph-parser.config :as gp-config]
             [logseq.graph-parser.block :as gp-block]
             [logseq.graph-parser.property :as gp-property]
+            [logseq.graph-parser.util.page-ref :as page-ref]
             [goog.dom :as gdom]
             [goog.object :as gobj]
             [promesa.core :as p]))
@@ -217,7 +218,7 @@
   (->>
    (concat
     ;; basic
-    [["Page reference" [[:editor/input "[[]]" {:backward-pos 2}]
+    [["Page reference" [[:editor/input page-ref/left-and-right-brackets {:backward-pos 2}]
                         [:editor/search-page]] "Create a backlink to a page"]
      ["Page embed" (embed-page) "Embed a page here"]
      ["Block reference" [[:editor/input gp-block/left-and-right-parens {:backward-pos 2}]
@@ -276,7 +277,7 @@
      ["Draw" (fn []
                (let [file (draw/file-name)
                      path (str gp-config/default-draw-directory "/" file)
-                     text (util/format "[[%s]]" path)]
+                     text (page-ref/->page-ref path)]
                  (p/let [_ (draw/create-draw-with-default-content path)]
                    (println "draw file created, " path))
                  text)) "Draw a graph with Excalidraw"]
@@ -343,7 +344,7 @@
                                     (and s
                                          (string/ends-with? s "(")
                                          (or (string/starts-with? last-pattern gp-block/left-parens)
-                                             (string/starts-with? last-pattern "[[")))
+                                             (string/starts-with? last-pattern page-ref/left-brackets)))
                                     (and s (string/starts-with? s "{{embed"))
                                     (and last-pattern
                                          (or (string/ends-with? last-pattern gp-property/colons)
@@ -381,7 +382,7 @@
         (state/set-block-content-and-last-pos! id new-value new-pos)
         (cursor/move-cursor-to input
                                (if (and (or backward-pos forward-pos)
-                                        (not= end-pattern "]]"))
+                                        (not= end-pattern page-ref/right-brackets))
                                  new-pos
                                  (inc new-pos)))))))
 

+ 12 - 12
src/main/frontend/components/block.cljs

@@ -64,6 +64,7 @@
             [logseq.graph-parser.mldoc :as gp-mldoc]
             [logseq.graph-parser.text :as text]
             [logseq.graph-parser.util :as gp-util]
+            [logseq.graph-parser.util.page-ref :as page-ref]
             [medley.core :as medley]
             [promesa.core :as p]
             [reitit.frontend.easy :as rfe]
@@ -632,7 +633,7 @@
        (when (and (or show-brackets? nested-link?)
                   (not html-export?)
                   (not contents-page?))
-         [:span.text-gray-500.bracket "[["])
+         [:span.text-gray-500.bracket page-ref/left-brackets])
        (let [s (string/trim s)]
          (page-cp (assoc config
                          :label (mldoc/plain->text label)
@@ -641,7 +642,7 @@
        (when (and (or show-brackets? nested-link?)
                   (not html-export?)
                   (not contents-page?))
-         [:span.text-gray-500.bracket "]]"])])))
+         [:span.text-gray-500.bracket page-ref/right-brackets])])))
 
 (defn- latex-environment-content
   [name option content]
@@ -820,7 +821,7 @@
      (when (and show-brackets?
                 (not html-export?)
                 (not (= (:id config) "contents")))
-       [:span.text-gray-500 "[["])
+       [:span.text-gray-500 page-ref/left-brackets])
      (let [page-name (subs content 2 (- (count content) 2))]
        (page-cp (assoc config
                        :children children
@@ -828,7 +829,7 @@
      (when (and show-brackets?
                 (not html-export?)
                 (not (= (:id config) "contents")))
-       [:span.text-gray-500 "]]"])]))
+       [:span.text-gray-500 page-ref/right-brackets])]))
 
 (declare custom-query)
 
@@ -992,7 +993,7 @@
           (image-link config url page nil metadata full_text)
           (let [label* (if (seq (mldoc/plain->text label)) label nil)]
             (if (and (string? page) (string/blank? page))
-              [:span (util/format "[[%s]]" page)]
+              [:span (page-ref/->page-ref page)]
               (page-reference (:html-export? config) page config label*)))))
 
       ["Embed_data" src]
@@ -1032,9 +1033,9 @@
                        (when-let [ext (util/get-file-ext href)]
                          (gp-config/mldoc-support? ext)))
                 [:span.page-reference
-                 (when show-brackets? [:span.text-gray-500 "[["])
+                 (when show-brackets? [:span.text-gray-500 page-ref/left-brackets])
                  (page-cp config page)
-                 (when show-brackets? [:span.text-gray-500 "]]"])]
+                 (when show-brackets? [:span.text-gray-500 page-ref/right-brackets])]
 
                 (let [href* (if (util/electron?)
                               (relative-assets-path->absolute-path href)
@@ -1114,8 +1115,7 @@
       (> link-depth max-depth-of-links)
       [:p.warning.text-sm "Embed depth is too deep"]
 
-      (and (string/starts-with? a "[[")
-           (string/ends-with? a "]]"))
+      (page-ref/page-ref? a)
       (let [page-name (text/get-page-name a)]
         (when-not (string/blank? page-name)
           (page-embed (assoc config :link-depth (inc link-depth)) page-name)))
@@ -1299,8 +1299,8 @@
   (let [{:keys [name arguments]} options
         arguments (if (and
                        (>= (count arguments) 2)
-                       (and (string/starts-with? (first arguments) "[[")
-                            (string/ends-with? (last arguments) "]]"))) ; page reference
+                       (and (string/starts-with? (first arguments) page-ref/left-brackets)
+                            (string/ends-with? (last arguments) page-ref/right-brackets))) ; page reference
                     (let [title (string/join ", " arguments)]
                       [title])
                     arguments)]
@@ -1314,7 +1314,7 @@
       (= name "namespace")
       (let [namespace (first arguments)]
         (when-not (string/blank? namespace)
-          (let [namespace (string/lower-case (text/page-ref-un-brackets! namespace))
+          (let [namespace (string/lower-case (page-ref/get-page-name! namespace))
                 children (model/get-namespace-hierarchy (state/get-current-repo) namespace)]
             (namespace-hierarchy config namespace children))))
 

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

@@ -10,7 +10,8 @@
             [frontend.ui :as ui]
             [frontend.util :as util]
             [frontend.mixins :as mixins]
-            [rum.core :as rum]))
+            [rum.core :as rum]
+            [logseq.graph-parser.util.page-ref :as page-ref]))
 
 (defonce default-timestamp-value {:time ""
                                   :repeater {}})
@@ -160,7 +161,7 @@
              (when-not deadline-or-schedule?
                ;; similar to page reference
                (editor-handler/insert-command! id
-                                               (util/format "[[%s]]" journal)
+                                               (page-ref/->page-ref journal)
                                                format
                                                nil)
                (state/clear-editor-action!)

+ 2 - 1
src/main/frontend/components/shortcut.cljs

@@ -8,6 +8,7 @@
             [frontend.extensions.latex :as latex]
             [frontend.extensions.highlight :as highlight]
             [logseq.graph-parser.block :as gp-block]
+            [logseq.graph-parser.util.page-ref :as page-ref]
             [rum.core :as rum]))
 
 (rum/defcs customize-shortcut-dialog-inner <
@@ -98,7 +99,7 @@
      [:td.text-right [:code "<"]]]
     [:tr
      [:td.text-left (t :help/reference-autocomplete)]
-     [:td.text-right [:code "[[]]"]]]
+     [:td.text-right [:code page-ref/left-and-right-brackets]]]
     [:tr
      [:td.text-left (t :help/block-reference)]
      [:td.text-right [:code gp-block/left-and-right-parens]]]

+ 27 - 25
src/main/frontend/db/query_dsl.cljs

@@ -14,6 +14,7 @@
             [logseq.db.rules :as rules]
             [frontend.template :as template]
             [logseq.graph-parser.text :as text]
+            [logseq.graph-parser.util.page-ref :as page-ref]
             [frontend.util.text :as text-util]
             [frontend.util :as util]))
 
@@ -60,8 +61,8 @@
       (= "tomorrow" input)
       (db-utils/date->int (t/plus (t/today) (t/days 1)))
 
-      (text/page-ref? input)
-      (let [input (-> (text/page-ref-un-brackets! input)
+      (page-ref/page-ref? input)
+      (let [input (-> (page-ref/get-page-name input)
                       (string/replace ":" "")
                       (string/capitalize))]
         (when (date/valid-journal-title? input)
@@ -92,8 +93,8 @@
       (= "tomorrow" input)
       (tc/to-long (t/plus (t/today) (t/days 1)))
 
-      (text/page-ref? input)
-      (let [input (-> (text/page-ref-un-brackets! input)
+      (page-ref/page-ref? input)
+      (let [input (-> (page-ref/get-page-name input)
                       (string/replace ":" "")
                       (string/capitalize))]
         (when (date/valid-journal-title? input)
@@ -298,7 +299,7 @@
                (rest e))
         tags (map (comp string/lower-case name) tags)]
     (when (seq tags)
-      (let [tags (set (map (comp text/page-ref-un-brackets! string/lower-case name) tags))]
+      (let [tags (set (map (comp page-ref/get-page-name! string/lower-case name) tags))]
         {:query (list 'page-tags '?p tags)
          :rules [:page-tags]}))))
 
@@ -342,14 +343,14 @@
 
 (defn- build-page
   [e]
-  (let [page-name (text/page-ref-un-brackets! (str (first (rest e))))
+  (let [page-name (page-ref/get-page-name! (str (first (rest e))))
         page-name (util/page-name-sanity-lc page-name)]
     {:query (list 'page '?b page-name)
      :rules [:page]}))
 
 (defn- build-namespace
   [e]
-  (let [page-name (text/page-ref-un-brackets! (str (first (rest e))))
+  (let [page-name (page-ref/get-page-name! (str (first (rest e))))
         page (util/page-name-sanity-lc page-name)]
     (when-not (string/blank? page)
       {:query (list 'namespace '?p page)
@@ -357,7 +358,7 @@
 
 (defn- build-page-ref
   [e]
-  (let [page-name (-> (text/page-ref-un-brackets! e)
+  (let [page-name (-> (page-ref/get-page-name! e)
                       (util/page-name-sanity-lc))]
     {:query (list 'page-ref '?b page-name)
      :rules [:page-ref]}))
@@ -381,7 +382,7 @@ Some bindings in this fn:
    ; {:post [(or (nil? %) (map? %))]}
    (let [fe (first e)
          fe (when fe (symbol (string/lower-case (name fe))))
-         page-ref? (text/page-ref? e)]
+         page-ref? (page-ref/page-ref? e)]
      (when (or (and page-ref?
                     (not (contains? #{'page-property 'page-tags} (:current-filter env))))
                (contains? #{'between 'property 'todo 'task 'priority 'sort-by 'page} fe)
@@ -442,20 +443,21 @@ Some bindings in this fn:
 
 (defn- pre-transform
   [s]
-  (some-> s
-          (string/replace text/page-ref-re "\"[[$1]]\"")
-          (string/replace text-util/between-re
-                          (fn [[_ x]]
-                            (->> (string/split x #" ")
-                                 (remove string/blank?)
-                                 (map (fn [x]
-                                        (if (or (contains? #{"+" "-"} (first x))
-                                                (and (util/safe-re-find #"\d" (first x))
-                                                     (some #(string/ends-with? x %) ["y" "m" "d" "h" "min"])))
-                                          (keyword (name x))
-                                          x)))
-                                 (string/join " ")
-                                 (util/format "(between %s)"))))))
+  (let [quoted-page-ref (str "\"" page-ref/left-brackets "$1" page-ref/right-brackets "\"")]
+    (some-> s
+            (string/replace page-ref/page-ref-re quoted-page-ref)
+            (string/replace text-util/between-re
+                            (fn [[_ x]]
+                              (->> (string/split x #" ")
+                                   (remove string/blank?)
+                                   (map (fn [x]
+                                          (if (or (contains? #{"+" "-"} (first x))
+                                                  (and (util/safe-re-find #"\d" (first x))
+                                                       (some #(string/ends-with? x %) ["y" "m" "d" "h" "min"])))
+                                            (keyword (name x))
+                                            x)))
+                                   (string/join " ")
+                                   (util/format "(between %s)")))))))
 
 (defn- add-bindings!
   [form q]
@@ -482,7 +484,7 @@ Some bindings in this fn:
       or?
       (cond
         (->> (flatten form)
-             (remove text/page-ref?)
+             (remove (every-pred string? page-ref/page-ref?))
              (some string?))            ; block full-text search
         (concat [['?b :block/content '?content]] [q])
 
@@ -499,7 +501,7 @@ Some bindings in this fn:
   [s]
   (when (and (string? s)
              (not (string/blank? s)))
-    (let [s (if (= \# (first s)) (util/format "[[%s]]" (subs s 1)) s)
+    (let [s (if (= \# (first s)) (page-ref/->page-ref (subs s 1)) s)
           form (some-> s
                        (pre-transform)
                        (reader/read-string))

+ 5 - 5
src/main/frontend/db/query_react.cljs

@@ -10,7 +10,7 @@
             [frontend.debug :as debug]
             [frontend.extensions.sci :as sci]
             [frontend.state :as state]
-            [logseq.graph-parser.text :as text]
+            [logseq.graph-parser.util.page-ref :as page-ref]
             [frontend.util :as util]
             [frontend.date :as date]
             [lambdaisland.glogi :as log]))
@@ -44,8 +44,8 @@
           days (parse-long (re-find #"^\d+" input))]
       (date->int (t/plus (t/today) (t/days days))))
 
-    (and (string? input) (text/page-ref? input))
-    (-> (text/page-ref-un-brackets! input)
+    (and (string? input) (page-ref/page-ref? input))
+    (-> (page-ref/get-page-name input)
         (string/lower-case))
 
     :else
@@ -81,7 +81,7 @@
 
 (defn- resolve-query
   [query]
-  (let [page-ref? #(and (string? %) (text/page-ref? %))]
+  (let [page-ref? #(and (string? %) (page-ref/page-ref? %))]
     (walk/postwalk
      (fn [f]
        (cond
@@ -100,7 +100,7 @@
          (let [[x y] (rest f)
                [page-ref sym] (if (page-ref? x) [x y] [y x])
                page-ref (string/lower-case page-ref)]
-           (list 'contains? sym (text/page-ref-un-brackets! page-ref)))
+           (list 'contains? sym (page-ref/get-page-name page-ref)))
 
          (and (vector? f)
               (= (first f) 'page-property)

+ 2 - 1
src/main/frontend/extensions/srs.cljs

@@ -4,6 +4,7 @@
             [frontend.db.query-react :as query-react]
             [frontend.util :as util]
             [logseq.graph-parser.property :as gp-property]
+            [logseq.graph-parser.util.page-ref :as page-ref]
             [frontend.util.property :as property]
             [frontend.util.drawer :as drawer]
             [frontend.util.persist-var :as persist-var]
@@ -264,7 +265,7 @@
                           query-string (if-not (or (string/blank? query-string)
                                                    (string/starts-with? query-string "(")
                                                    (string/starts-with? query-string "["))
-                                         (util/format "[[%s]]" (string/trim query-string))
+                                         (page-ref/->page-ref (string/trim query-string))
                                          query-string)
                           {:keys [query sort-by rules]} (query-dsl/parse query-string)
                           query* (util/concat-without-nil

+ 5 - 4
src/main/frontend/extensions/zotero/extractor.cljs

@@ -5,7 +5,8 @@
             [frontend.extensions.html-parser :as html-parser]
             [frontend.extensions.zotero.schema :as schema]
             [frontend.extensions.zotero.setting :as setting]
-            [frontend.util :as util]))
+            [frontend.util :as util]
+            [logseq.graph-parser.util.page-ref :as page-ref]))
 
 (defn item-type [item] (-> item :data :item-type))
 
@@ -64,8 +65,8 @@
 
 (defn date->journal [item]
   (if-let [date (-> item :meta :parsed-date
-                      (date/journal-name-s))]
-    (util/format "[[%s]]" date)
+                    (date/journal-name-s))]
+    (page-ref/->page-ref date)
     (-> item :data :date)))
 
 (defn wrap-in-doublequotes [m]
@@ -129,7 +130,7 @@
                                 :authors authors
                                 :tags tags
                                 :date date
-                                :item-type (util/format "[[%s]]" type))
+                                :item-type (page-ref/->page-ref type))
                          (dissoc :creators :abstract-note)
                          (rename-keys {:title :original-title})
                          (assoc :title (page-name item)))]

+ 3 - 2
src/main/frontend/extensions/zotero/handler.cljs

@@ -8,7 +8,8 @@
             [frontend.state :as state]
             [frontend.handler.editor :as editor-handler]
             [frontend.handler.page :as page-handler]
-            [frontend.db :as db]))
+            [frontend.db :as db]
+            [logseq.graph-parser.util.page-ref :as page-ref]))
 
 (defn add [page-name type item]
   (go
@@ -42,7 +43,7 @@
 (defn handle-command-zotero
   [id page-name]
   (state/clear-editor-action!)
-  (editor-handler/insert-command! id (str "[[" page-name "]]") nil {}))
+  (editor-handler/insert-command! id (page-ref/->page-ref page-name) nil {}))
 
 (defn- create-abstract-note!
   [page-name abstract-note]

+ 13 - 12
src/main/frontend/handler/editor.cljs

@@ -51,6 +51,7 @@
             [lambdaisland.glogi :as log]
             [promesa.core :as p]
             [logseq.graph-parser.util :as gp-util]
+            [logseq.graph-parser.util.page-ref :as page-ref]
             [logseq.graph-parser.mldoc :as gp-mldoc]
             [logseq.graph-parser.block :as gp-block]))
 
@@ -1089,11 +1090,11 @@
 
 (defn extract-nearest-link-from-text
   [text pos & additional-patterns]
-  (let [page-pattern #"\[\[([^\]]+)]]"
-        block-pattern #"\(\(([^\)]+)\)\)"
+  (let [;; didn't use page-ref regexs b/c it handles page-ref and org link cases
+        page-pattern #"\[\[([^\]]+)]]"
         tag-pattern #"#\S+"
         page-matches (util/re-pos page-pattern text)
-        block-matches (util/re-pos block-pattern text)
+        block-matches (util/re-pos gp-block/block-ref-re text)
         tag-matches (util/re-pos tag-pattern text)
         additional-matches (mapcat #(util/re-pos % text) additional-patterns)
         matches (->> (concat page-matches block-matches tag-matches additional-matches)
@@ -1553,7 +1554,7 @@
                                                                    [(subs new-value prefix-pos (+ prefix-pos 2))
                                                                     (+ prefix-pos 2)]))})]
         (cond
-          (= prefix "[[")
+          (= prefix page-ref/left-brackets)
           (do
             (commands/handle-step [:editor/search-page])
             (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)}))
@@ -1772,7 +1773,7 @@
   [input]
   (when (and input
              (state/get-editor-action)
-             (not (wrapped-by? input "[[" "]]")))
+             (not (wrapped-by? input page-ref/left-brackets page-ref/right-brackets)))
     (when (get-search-q)
       (let [value (gobj/get input "value")
             pos (state/get-editor-last-pos)
@@ -2312,12 +2313,12 @@
               {:keys [selection-start selection-end selection]} selection]
           (if selection
             (do (delete-and-update input selection-start selection-end)
-                (insert (util/format "[[%s]]" selection)))
+                (insert (page-ref/->page-ref selection)))
             (if-let [embed-ref (thingatpt/embed-macro-at-point input)]
               (let [{:keys [raw-content start end]} embed-ref]
                 (delete-and-update input start end)
                 (if (= 5 (count raw-content))
-                  (page-ref-fn "[[]]" 2)
+                  (page-ref-fn page-ref/left-and-right-brackets 2)
                   (insert raw-content)))
               (if-let [page-ref (thingatpt/page-ref-at-point input)]
                 (let [{:keys [start end full-content raw-content]} page-ref]
@@ -2325,7 +2326,7 @@
                   (if (= raw-content "")
                     (page-ref-fn "{{embed [[]]}}" 4)
                     (insert (util/format "{{embed %s}}" full-content))))
-                (page-ref-fn "[[]]" 2)))))))))
+                (page-ref-fn page-ref/left-and-right-brackets 2)))))))))
 
 (defn toggle-block-reference-embed
   [parent-id]
@@ -2863,10 +2864,10 @@
           (when (and (not editor-action) (not non-enter-processed?))
             (cond
               (and (not (contains? #{"ArrowDown" "ArrowLeft" "ArrowRight" "ArrowUp"} k))
-                   (wrapped-by? input "[[" "]]"))
+                   (wrapped-by? input page-ref/left-brackets page-ref/right-brackets))
               (let [orig-pos (cursor/get-caret-pos input)
                     value (gobj/get input "value")
-                    square-pos (string/last-index-of (subs value 0 (:pos orig-pos)) "[[")
+                    square-pos (string/last-index-of (subs value 0 (:pos orig-pos)) page-ref/left-brackets)
                     pos (+ square-pos 2)
                     _ (state/set-editor-last-pos! pos)
                     pos (assoc orig-pos :pos pos)
@@ -2880,9 +2881,9 @@
                    (contains? keycode/left-square-brackets-keys k)
                    (= (:key last-key-code) k)
                    (> current-pos 0)
-                   (not (wrapped-by? input "[[" "]]")))
+                   (not (wrapped-by? input page-ref/left-brackets page-ref/right-brackets)))
               (do
-                (commands/handle-step [:editor/input "[[]]" {:backward-truncate-number 2
+                (commands/handle-step [:editor/input page-ref/left-and-right-brackets {:backward-truncate-number 2
                                                              :backward-pos 2}])
                 (commands/handle-step [:editor/search-page])
                 (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)}))

+ 3 - 5
src/main/frontend/handler/export.cljs

@@ -24,6 +24,7 @@
             [logseq.graph-parser.mldoc :as gp-mldoc]
             [logseq.graph-parser.util :as gp-util]
             [logseq.graph-parser.block :as gp-block]
+            [logseq.graph-parser.util.page-ref :as page-ref]
             [promesa.core :as p]
             [frontend.handler.notification :as notification])
   (:import
@@ -160,11 +161,8 @@
                              (= "embed" (some-> (:name (second i))
                                                 (string/lower-case)))
                              (some-> (:arguments (second i))
-                                     (first)
-                                     (string/starts-with? "[["))
-                             (some-> (:arguments (second i))
-                                     (first)
-                                     (string/ends-with? "]]")))
+                                     first
+                                     page-ref/page-ref?))
                         (let [arguments (:arguments (second i))
                               page-ref (first arguments)
                               page-name (-> page-ref

+ 19 - 17
src/main/frontend/handler/page.cljs

@@ -37,6 +37,7 @@
             [logseq.graph-parser.config :as gp-config]
             [logseq.graph-parser.block :as gp-block]
             [logseq.graph-parser.property :as gp-property]
+            [logseq.graph-parser.util.page-ref :as page-ref]
             [frontend.format.block :as block]
             [goog.functions :refer [debounce]]))
 
@@ -211,7 +212,7 @@
   "Unsanitized names"
   [content old-name new-name]
   (let [[original-old-name original-new-name] (map string/trim [old-name new-name])
-        [old-ref new-ref] (map #(util/format "[[%s]]" %) [old-name new-name])
+        [old-ref new-ref] (map page-ref/->page-ref [old-name new-name])
         [old-name new-name] (map #(if (string/includes? % "/")
                                     (string/replace % "/" ".")
                                     %)
@@ -455,8 +456,9 @@
   "Unsanitized names only"
   [old-ns-name new-ns-name]
   (let [repo            (state/get-current-repo)
-        nested-page-str (util/format "[[%s]]" (util/page-name-sanity-lc old-ns-name))
-        ns-prefix       (util/format "[[%s/" (util/page-name-sanity-lc old-ns-name))
+        nested-page-str (page-ref/->page-ref (util/page-name-sanity-lc old-ns-name))
+        ns-prefix-format-str (str page-ref/left-brackets "%s/")
+        ns-prefix       (util/format ns-prefix-format-str (util/page-name-sanity-lc old-ns-name))
         nested-pages    (db/get-pages-by-name-partition repo nested-page-str)
         nested-pages-ns (db/get-pages-by-name-partition repo ns-prefix)]
     (when nested-pages
@@ -465,8 +467,8 @@
         (let [old-page-title (or original-name name)
               new-page-title (string/replace
                               old-page-title
-                              (util/format "[[%s]]" old-ns-name)
-                              (util/format "[[%s]]" new-ns-name))]
+                              (page-ref/->page-ref old-ns-name)
+                              (page-ref/->page-ref new-ns-name))]
           (when (and old-page-title new-page-title)
             (p/do!
              (rename-page-aux old-page-title new-page-title false)
@@ -477,8 +479,8 @@
         (let [old-page-title (or original-name name)
               new-page-title (string/replace
                               old-page-title
-                              (util/format "[[%s/" old-ns-name)
-                              (util/format "[[%s/" new-ns-name))]
+                              (util/format ns-prefix-format-str old-ns-name)
+                              (util/format ns-prefix-format-str new-ns-name))]
           (when (and old-page-title new-page-title)
             (p/do!
              (rename-page-aux old-page-title new-page-title false)
@@ -637,7 +639,7 @@
           (util/format "[[file:%s][%s]]"
                        (util/get-relative-path edit-block-file-path ref-file-path)
                        page)))
-      (util/format "[[%s]]" page))))
+      (page-ref/->page-ref page))))
 
 (defn init-commands!
   []
@@ -690,7 +692,7 @@
   (if (state/org-mode-file-link? (state/get-current-repo))
     (let [page-ref-text (get-page-ref-text q)
           value (gobj/get input "value")
-          old-page-ref (util/format "[[%s]]" q)
+          old-page-ref (page-ref/->page-ref q)
           new-value (string/replace value
                                     old-page-ref
                                     page-ref-text)]
@@ -718,13 +720,13 @@
     (if hashtag?
       (fn [chosen _click?]
         (state/clear-editor-action!)
-        (let [wrapped? (= "[[" (gp-util/safe-subs edit-content (- pos 2) pos))
+        (let [wrapped? (= page-ref/left-brackets (gp-util/safe-subs edit-content (- pos 2) pos))
               prefix (str (t :new-page) ": ")
               chosen (if (string/starts-with? chosen prefix) ;; FIXME: What if a page named "New page: XXX"?
                        (string/replace-first chosen prefix "")
                        chosen)
               chosen (if (and (util/safe-re-find #"\s+" chosen) (not wrapped?))
-                       (util/format "[[%s]]" chosen)
+                       (page-ref/->page-ref chosen)
                        chosen)
               q (if @editor-handler/*selected-text "" q)
               [last-pattern forward-pos] (if wrapped?
@@ -732,12 +734,12 @@
                                            (if (= \# (first q))
                                              [(subs q 1) 1]
                                              [q 2]))
-              last-pattern (str "#" (when wrapped? "[[") last-pattern)]
+              last-pattern (str "#" (when wrapped? page-ref/left-brackets) last-pattern)]
           (editor-handler/insert-command! id
-                                          (str "#" (when wrapped? "[[") chosen)
+                                          (str "#" (when wrapped? page-ref/left-brackets) chosen)
                                           format
                                           {:last-pattern last-pattern
-                                           :end-pattern (when wrapped? "]]")
+                                           :end-pattern (when wrapped? page-ref/right-brackets)
                                            :forward-pos forward-pos})))
       (fn [chosen _click?]
         (state/clear-editor-action!)
@@ -749,9 +751,9 @@
           (editor-handler/insert-command! id
                                           page-ref-text
                                           format
-                                          {:last-pattern (str "[[" (if @editor-handler/*selected-text "" q))
-                                           :end-pattern "]]"
-                                           :postfix-fn   (fn [s] (util/replace-first "]]" s ""))
+                                          {:last-pattern (str page-ref/left-brackets (if @editor-handler/*selected-text "" q))
+                                           :end-pattern page-ref/right-brackets
+                                           :postfix-fn   (fn [s] (util/replace-first page-ref/right-brackets s ""))
                                            :forward-pos 3}))))))
 
 (defn create-today-journal!

+ 2 - 1
src/main/frontend/mobile/intent.cljs

@@ -17,6 +17,7 @@
             [lambdaisland.glogi :as log]
             [logseq.graph-parser.config :as gp-config]
             [logseq.graph-parser.mldoc :as gp-mldoc]
+            [logseq.graph-parser.util.page-ref :as page-ref]
             [promesa.core :as p]))
 
 (defn- handle-received-text [result]
@@ -89,7 +90,7 @@
                 (.copy Filesystem (clj->js {:from url :to path}))
                 (fn [error]
                   (log/error :copy-file-error {:error error})))
-          url (util/format "[[%s]]" title)
+          url (page-ref/->page-ref title)
           template (get-in (state/get-config)
                            [:quick-capture-templates :text]
                            "**{time}** [[quick capture]]: {url}")]

+ 7 - 8
src/main/frontend/template.cljs

@@ -2,17 +2,16 @@
   (:require [clojure.string :as string]
             [frontend.date :as date]
             [frontend.state :as state]
-            [frontend.util :as util]))
+            [logseq.graph-parser.util.page-ref :as page-ref]))
 
 (defn- variable-rules
   []
-  {"today" (util/format "[[%s]]" (date/today))
-   "yesterday" (util/format "[[%s]]" (date/yesterday))
-   "tomorrow" (util/format "[[%s]]" (date/tomorrow))
+  {"today" (page-ref/->page-ref (date/today))
+   "yesterday" (page-ref/->page-ref (date/yesterday))
+   "tomorrow" (page-ref/->page-ref (date/tomorrow))
    "time" (date/get-current-time)
-   "current page" (util/format "[[%s]]"
-                               (or (state/get-current-page)
-                                   (date/today)))})
+   "current page" (page-ref/->page-ref (or (state/get-current-page)
+                                           (date/today)))})
 
 ;; TODO: programmable
 ;; context information, date, current page
@@ -31,5 +30,5 @@
                          (let [;; NOTE: This following cannot handle timezones
                                ;; date (tc/to-local-date-time nld)
                                date (doto (goog.date.DateTime.) (.setTime (.getTime nld)))]
-                           (util/format "[[%s]]" (date/journal-name date)))
+                           (page-ref/->page-ref (date/journal-name date)))
                          match))))))

+ 2 - 1
src/main/frontend/util/property.cljs

@@ -7,6 +7,7 @@
             [logseq.graph-parser.util :as gp-util]
             [logseq.graph-parser.mldoc :as gp-mldoc]
             [logseq.graph-parser.property :as gp-property :refer [properties-start properties-end]]
+            [logseq.graph-parser.util.page-ref :as page-ref]
             [frontend.format.mldoc :as mldoc]
             [logseq.graph-parser.text :as text]
             [frontend.util.cursor :as cursor]))
@@ -325,7 +326,7 @@
                (some->>
                 (seq v)
                 (distinct)
-                (map (fn [item] (util/format "[[%s]]" (text/page-ref-un-brackets! item))))
+                (map (fn [item] (page-ref/->page-ref (text/page-ref-un-brackets! item))))
                 (string/join ", "))
                v)]
        (insert-property format content k v)))

+ 2 - 1
src/main/frontend/util/thingatpt.cljs

@@ -6,6 +6,7 @@
             [logseq.graph-parser.text :as text]
             [logseq.graph-parser.property :as gp-property]
             [logseq.graph-parser.block :as gp-block]
+            [logseq.graph-parser.util.page-ref :as page-ref]
             [cljs.reader :as reader]
             [goog.object :as gobj]))
 
@@ -54,7 +55,7 @@
              :link uuid))))
 
 (defn page-ref-at-point [& [input]]
-  (when-let [page-ref (thing-at-point ["[[" "]]"] input)]
+  (when-let [page-ref (thing-at-point [page-ref/left-brackets page-ref/right-brackets] input)]
     (assoc page-ref
            :type "page-ref"
            :link (text/get-page-name