Finish block-ref helpers

There were a couple scattered in 2 text namespaces and there were
a couple uses of block-ref that involved escaping.
Removed block-ref-un-brackets! once it was seen as the equivalent
of an or statement
pull/6257/head
Gabriel Horner 2022-07-26 15:21:47 -04:00 committed by Tienson Qin
parent a3d30e0b52
commit 001e0d302d
9 changed files with 51 additions and 54 deletions

View File

@ -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?

View File

@ -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)]

View File

@ -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]

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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)))