diff --git a/deps/graph-parser/.carve/ignore b/deps/graph-parser/.carve/ignore index f4f59687a..3c67be574 100644 --- a/deps/graph-parser/.carve/ignore +++ b/deps/graph-parser/.carve/ignore @@ -8,3 +8,5 @@ logseq.graph-parser.property/register-built-in-properties logseq.graph-parser.block/left-and-right-parens ;; API logseq.graph-parser.block/->block-ref +;; API +logseq.graph-parser.block/block-ref? diff --git a/deps/graph-parser/src/logseq/graph_parser/block.cljs b/deps/graph-parser/src/logseq/graph_parser/block.cljs index c3dc38dda..87143ed71 100644 --- a/deps/graph-parser/src/logseq/graph_parser/block.cljs +++ b/deps/graph-parser/src/logseq/graph_parser/block.cljs @@ -36,8 +36,31 @@ (def right-parens "Closing characters for block-ref" "))") (def left-and-right-parens "Opening and closing characters for block-ref" (str left-parens right-parens)) +(def block-ref-re #"\(\(([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})\)\)") -(defn block-ref-string? +(defn get-all-block-ref-ids + [content] + (map second (re-seq block-ref-re content))) + +(defn get-block-ref-id + "Extracts block id from block-ref using regex" + [s] + (second (re-matches block-ref-re s))) + +(defn get-string-block-ref-id + "Extracts block id from block-ref by stripping parens e.g. ((123)) -> 123. + This is a less strict version of get-block-ref-id" + [s] + (subs s 2 (- (count s) 2))) + +(defn block-ref? + "Determines if string is block ref using regex" + [s] + (boolean (get-block-ref-id s))) + +(defn string-block-ref? + "Determines if string is block ref by checking parens. This is less strict version +of block-ref?" [s] (and (string/starts-with? s left-parens) (string/ends-with? s right-parens))) @@ -47,11 +70,6 @@ [block-id] (str left-parens block-id right-parens)) -(defn block-ref->block-id - "Extracts block id from block-ref string e.g. ((123)) -> 123." - [s] - (subs s 2 (- (count s) 2))) - (defn- get-page-reference [block supported-formats] (let [page (cond @@ -111,7 +129,7 @@ :else nil)] - (text/block-ref-un-brackets! page))) + (when page (or (get-block-ref-id page) page)))) (defn- get-block-reference [block] @@ -131,8 +149,8 @@ (let [{:keys [name arguments]} (second block)] (when (and (= name "embed") (string? (first arguments)) - (block-ref-string? (first arguments))) - (block-ref->block-id (first arguments)))) + (string-block-ref? (first arguments))) + (get-string-block-ref-id (first arguments)))) (and (vector? block) (= "Link" (first block)) @@ -140,7 +158,9 @@ (if (= "id" (:protocol (second (:url (second block))))) (:link (second (:url (second block)))) (let [id (second (:url (second block)))] - (text/block-ref-un-brackets! id))) + ;; these can be maps + (when (string? id) + (or (get-block-ref-id id) id)))) :else nil)] diff --git a/deps/graph-parser/src/logseq/graph_parser/text.cljs b/deps/graph-parser/src/logseq/graph_parser/text.cljs index 20cc78794..0ac53be6a 100644 --- a/deps/graph-parser/src/logseq/graph_parser/text.cljs +++ b/deps/graph-parser/src/logseq/graph_parser/text.cljs @@ -34,17 +34,6 @@ (string/starts-with? s "[[") (string/ends-with? s "]]"))) -(def block-ref-re #"\(\(([a-zA-z0-9]{8}-[a-zA-z0-9]{4}-[a-zA-z0-9]{4}-[a-zA-z0-9]{4}-[a-zA-z0-9]{12})\)\)") - -(defn get-block-ref - [s] - (and (string? s) - (second (re-matches block-ref-re s)))) - -(defn block-ref? - [s] - (boolean (get-block-ref s))) - (defonce page-ref-re #"\[\[(.*?)\]\]") (defonce page-ref-re-2 #"(\[\[.*?\]\])") @@ -55,13 +44,6 @@ [s] (or (get-page-name s) s)) -(defn block-ref-un-brackets! - [s] - (when (string? s) - (if (block-ref? s) - (subs s 2 (- (count s) 2)) - s))) - ;; E.g "Foo Bar" (defn sep-by-comma [s] diff --git a/src/main/frontend/components/block.cljs b/src/main/frontend/components/block.cljs index 4925e5894..a6cec58a2 100644 --- a/src/main/frontend/components/block.cljs +++ b/src/main/frontend/components/block.cljs @@ -930,8 +930,8 @@ (not= \* (last s))) (->elem :a {:on-click #(route-handler/jump-to-anchor! (mldoc/anchorLink (subs s 1)))} (subs s 1)) - (text/block-ref? s) - (let [id (text/get-block-ref s)] + (gp-block/block-ref? s) + (let [id (gp-block/get-block-ref-id s)] (block-reference config id label)) (not (string/includes? s ".")) @@ -1120,8 +1120,8 @@ (when-not (string/blank? page-name) (page-embed (assoc config :link-depth (inc link-depth)) page-name))) - (gp-block/block-ref-string? a) - (when-let [s (-> gp-block/block-ref->block-id string/trim)] + (gp-block/string-block-ref? a) + (when-let [s (-> gp-block/get-string-block-ref-id string/trim)] (when-let [id (some-> s parse-uuid)] (block-embed (assoc config :link-depth (inc link-depth)) id))) @@ -2152,8 +2152,7 @@ editor-id (str "editor-" edit-input-id) slide? (:slide? config) trimmed-content (string/trim (:block/content block)) - block-reference-only? (and (gp-block/block-ref-string? trimmed-content) - (re-find (re-pattern util/uuid-pattern) trimmed-content))] + block-reference-only? (gp-block/block-ref? trimmed-content)] (if (and edit? editor-box) [:div.editor-wrapper {:id editor-id} (ui/catch-error diff --git a/src/main/frontend/fs/watcher_handler.cljs b/src/main/frontend/fs/watcher_handler.cljs index b27f2b4a5..9ff2b6807 100644 --- a/src/main/frontend/fs/watcher_handler.cljs +++ b/src/main/frontend/fs/watcher_handler.cljs @@ -9,7 +9,7 @@ [frontend.handler.repo :as repo-handler] [frontend.handler.ui :as ui-handler] [logseq.graph-parser.util :as gp-util] - [frontend.util.text :as text-util] + [logseq.graph-parser.block :as gp-block] [lambdaisland.glogi :as log] [electron.ipc :as ipc] [promesa.core :as p] @@ -22,7 +22,7 @@ (defn- set-missing-block-ids! [content] (when (string? content) - (doseq [block-id (text-util/extract-all-block-refs content)] + (doseq [block-id (gp-block/get-all-block-ref-ids content)] (when-let [block (try (model/get-block-by-uuid block-id) (catch js/Error _e diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index e88bc9add..620db8c6d 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -1030,9 +1030,9 @@ (map (fn [{:keys [id level]}] (condp = (:block/format block) :org - (util/format (str (string/join (repeat level "*")) " ((%s))") id) + (str (string/join (repeat level "*")) " " (gp-block/->block-ref id)) :markdown - (util/format (str (string/join (repeat (dec level) "\t")) "- ((%s))") id)))) + (str (string/join (repeat (dec level) "\t")) "- " (gp-block/->block-ref id))))) (string/join "\n\n"))] (set-blocks-id! (map :id blocks)) (util/copy-to-clipboard! copy-str)))) @@ -1551,18 +1551,16 @@ (when (>= prefix-pos 0) [(subs new-value prefix-pos (+ prefix-pos 2)) (+ prefix-pos 2)]))})] - (case prefix - "[[" + (cond + (= prefix "[[") (do (commands/handle-step [:editor/search-page]) (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)})) - gp-block/left-parens + (= prefix gp-block/left-parens) (do (commands/handle-step [:editor/search-block :reference]) - (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)})) - - nil))))) + (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)}))))))) (defn surround-by? [input before end] @@ -3425,7 +3423,8 @@ (defn delete-current-ref! [block ref-id] (when (and block ref-id) - (let [match (re-pattern (str "\\s?" (util/format "\\(\\(%s\\)\\)" (str ref-id)))) + (let [match (re-pattern (str "\\s?" + (string/replace (gp-block/->block-ref ref-id) #"([\(\)])" "\\$1"))) content (string/replace-first (:block/content block) match "")] (save-block! (state/get-current-repo) (:block/uuid block) diff --git a/src/main/frontend/handler/export.cljs b/src/main/frontend/handler/export.cljs index db04e9c87..efde8609f 100644 --- a/src/main/frontend/handler/export.cljs +++ b/src/main/frontend/handler/export.cljs @@ -189,9 +189,9 @@ (string/lower-case))) (some-> (:arguments (second i)) (first) - gp-block/block-ref-string?)) + gp-block/string-block-ref?)) (let [arguments (:arguments (second i)) - block-uuid (gp-block/block-ref->block-id (first arguments))] + block-uuid (gp-block/get-string-block-ref-id (first arguments))] (conj! result block-uuid) i) :else diff --git a/src/main/frontend/handler/paste.cljs b/src/main/frontend/handler/paste.cljs index 130e30d81..9e640030e 100644 --- a/src/main/frontend/handler/paste.cljs +++ b/src/main/frontend/handler/paste.cljs @@ -15,7 +15,6 @@ ["/frontend/utils" :as utils] [frontend.commands :as commands] [cljs.core.match :refer [match]] - [logseq.graph-parser.text :as text] [frontend.handler.notification :as notification] [frontend.util.text :as text-util] [frontend.format.mldoc :as mldoc] @@ -81,9 +80,9 @@ (not (string/blank? (util/get-selected-text)))) (editor-handler/html-link-format! text) - (and (text/block-ref? text) + (and (gp-block/block-ref? text) (editor-handler/wrapped-by? input gp-block/left-parens gp-block/right-parens)) - (commands/simple-insert! (state/get-edit-input-id) (text/get-block-ref text) nil) + (commands/simple-insert! (state/get-edit-input-id) (gp-block/get-block-ref-id text) nil) :else ;; from external diff --git a/src/main/frontend/util/text.cljs b/src/main/frontend/util/text.cljs index 95a2500e9..0e0d31ad9 100644 --- a/src/main/frontend/util/text.cljs +++ b/src/main/frontend/util/text.cljs @@ -122,7 +122,3 @@ (string/join "/" parts) (last parts)) js/decodeURI)))) - -(defn extract-all-block-refs - [content] - (map second (re-seq #"\(\(([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})\)\)" content)))