Move all block ref parens strings to vars

Extracted a couple fns we kept reinventing across namespaces
pull/6257/head
Gabriel Horner 2022-07-25 17:14:27 -04:00 committed by Tienson Qin
parent e1e8ff8a92
commit a3d30e0b52
14 changed files with 73 additions and 54 deletions

View File

@ -4,3 +4,7 @@ logseq.graph-parser.cli/parse-graph
logseq.graph-parser.mldoc/ast-export-markdown
;; API
logseq.graph-parser.property/register-built-in-properties
;; API
logseq.graph-parser.block/left-and-right-parens
;; API
logseq.graph-parser.block/->block-ref

View File

@ -32,6 +32,26 @@
"")))
(string/join))))
(def left-parens "Opening characters for block-ref" "((")
(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))
(defn block-ref-string?
[s]
(and (string/starts-with? s left-parens)
(string/ends-with? s right-parens)))
(defn ->block-ref
"Creates block ref string given id"
[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,9 +131,8 @@
(let [{:keys [name arguments]} (second block)]
(when (and (= name "embed")
(string? (first arguments))
(string/starts-with? (first arguments) "((")
(string/ends-with? (first arguments) "))"))
(subs (first arguments) 2 (- (count (first arguments)) 2))))
(block-ref-string? (first arguments)))
(block-ref->block-id (first arguments))))
(and (vector? block)
(= "Link" (first block))

View File

@ -17,6 +17,7 @@
[frontend.util.property :as property]
[logseq.graph-parser.util :as gp-util]
[logseq.graph-parser.config :as gp-config]
[logseq.graph-parser.block :as gp-block]
[goog.dom :as gdom]
[goog.object :as gobj]
[promesa.core :as p]))
@ -218,7 +219,7 @@
[["Page reference" [[:editor/input "[[]]" {:backward-pos 2}]
[:editor/search-page]] "Create a backlink to a page"]
["Page embed" (embed-page) "Embed a page here"]
["Block reference" [[:editor/input "(())" {:backward-pos 2}]
["Block reference" [[:editor/input gp-block/left-and-right-parens {:backward-pos 2}]
[:editor/search-block :reference]] "Create a backlink to a block"]
["Block embed" (embed-block) "Embed a block here" "Embed a block here"]
["Link" (link-steps) "Create a HTTP link"]
@ -340,7 +341,7 @@
(or
(and s
(string/ends-with? s "(")
(or (string/starts-with? last-pattern "((")
(or (string/starts-with? last-pattern gp-block/left-parens)
(string/starts-with? last-pattern "[[")))
(and s (string/starts-with? s "{{embed"))
(and last-pattern

View File

@ -787,7 +787,7 @@
:delay [1000, 100]} inner)
inner)])
[:span.warning.mr-1 {:title "Block ref invalid"}
(util/format "((%s))" id)]))))
(gp-block/->block-ref id)]))))
(defn inline-text
([format v]
@ -1120,12 +1120,9 @@
(when-not (string/blank? page-name)
(page-embed (assoc config :link-depth (inc link-depth)) page-name)))
(and (string/starts-with? a "((")
(string/ends-with? a "))"))
(when-let [s (-> (string/replace a "((" "")
(string/replace "))" "")
string/trim)]
(when-let [id (some-> s string/trim parse-uuid)]
(gp-block/block-ref-string? a)
(when-let [s (-> gp-block/block-ref->block-id string/trim)]
(when-let [id (some-> s parse-uuid)]
(block-embed (assoc config :link-depth (inc link-depth)) id)))
:else ;TODO: maybe collections?
@ -2155,9 +2152,8 @@
editor-id (str "editor-" edit-input-id)
slide? (:slide? config)
trimmed-content (string/trim (:block/content block))
block-reference-only? (and (string/starts-with? trimmed-content "((")
(re-find (re-pattern util/uuid-pattern) trimmed-content)
(string/ends-with? trimmed-content "))"))]
block-reference-only? (and (gp-block/block-ref-string? trimmed-content)
(re-find (re-pattern util/uuid-pattern) trimmed-content))]
(if (and edit? editor-box)
[:div.editor-wrapper {:id editor-id}
(ui/catch-error

View File

@ -22,6 +22,7 @@
[frontend.ui :as ui]
[frontend.util :as util]
[logseq.graph-parser.util :as gp-util]
[logseq.graph-parser.block :as gp-block]
[frontend.util.url :as url-util]
[goog.dom :as gdom]
[goog.object :as gobj]
@ -207,7 +208,7 @@
(ui/menu-link
{:key "Copy block ref"
:on-click (fn [_e]
(editor-handler/copy-block-ref! block-id #(str "((" % "))")))}
(editor-handler/copy-block-ref! block-id gp-block/->block-ref))}
"Copy block ref")
(ui/menu-link

View File

@ -7,6 +7,7 @@
[frontend.ui :as ui]
[frontend.extensions.latex :as latex]
[frontend.extensions.highlight :as highlight]
[logseq.graph-parser.block :as gp-block]
[rum.core :as rum]))
(rum/defcs customize-shortcut-dialog-inner <
@ -100,7 +101,7 @@
[:td.text-right [:code "[[]]"]]]
[:tr
[:td.text-left (t :help/block-reference)]
[:td.text-right [:code "(())"]]]
[:td.text-right [:code gp-block/left-and-right-parens]]]
[:tr
[:td.text-left (t :command.editor/open-link-in-sidebar)]
[:td.text-right (ui/render-keyboard-shortcut ["shift" "click"])]]

View File

@ -11,6 +11,7 @@
[frontend.state :as state]
[frontend.util :as util]
[logseq.graph-parser.config :as gp-config]
[logseq.graph-parser.block :as gp-block]
[medley.core :as medley]
[promesa.core :as p]
[reitit.frontend.easy :as rfe]
@ -213,7 +214,7 @@
(defn copy-hl-ref!
[highlight]
(when-let [ref-block (create-ref-block! highlight)]
(util/copy-to-clipboard! (str "((" (:block/uuid ref-block) "))"))))
(util/copy-to-clipboard! (gp-block/->block-ref (:block/uuid ref-block)))))
(defn open-block-ref!
[block]

View File

@ -4,7 +4,8 @@
[frontend.date :as date]
[clojure.walk :as walk]
[clojure.string :as string]
[frontend.util :as util]
[goog.string :as gstring]
[logseq.graph-parser.block :as gp-block]
[logseq.graph-parser.util :as gp-util]
[logseq.graph-parser.text :as text]))
@ -30,7 +31,7 @@
[text]
(string/replace text uid-pattern (fn [[_ uid]]
(let [id (get @uid->uuid uid uid)]
(str "((" id "))")))))
(gp-block/->block-ref id)))))
(defn macro-transform
[text]
@ -38,7 +39,7 @@
(let [[name arg] (gp-util/split-first ":" text)]
(if name
(let [name (text/page-ref-un-brackets! name)]
(util/format "{{%s %s}}" name arg))
(gstring/format "{{%s %s}}" name arg))
original)))))
(defn- fenced-code-transform
@ -83,7 +84,7 @@
" -"))
properties (when (contains? @all-refed-uids uid)
(str
(util/format "id:: %s"
(gstring/format "id:: %s"
(str (get @uid->uuid uid)))
"\n"))]
(if string
@ -109,7 +110,7 @@
(let [journal? (date/valid-journal-title? title)
front-matter (if journal?
""
(util/format "---\ntitle: %s\n---\n\n" title))]
(gstring/format "---\ntitle: %s\n---\n\n" title))]
(str front-matter (transform text)))))]
(when (and (not (string/blank? title))
text)

View File

@ -3,8 +3,8 @@
[frontend.modules.outliner.core :as outliner-core]
[frontend.modules.outliner.tree :as tree]
[frontend.modules.outliner.transaction :as outliner-tx]
[frontend.state :as state]
[frontend.util :as util]))
[logseq.graph-parser.block :as gp-block]
[frontend.state :as state]))
(defn move-blocks
[^js event blocks target-block move-to]
@ -23,7 +23,7 @@
:id
(str (:block/uuid first-block)))
(editor-handler/api-insert-new-block!
(util/format "((%s))" (str (:block/uuid first-block)))
(gp-block/->block-ref (:block/uuid first-block))
{:block-uuid (:block/uuid target-block)
:sibling? (not nested?)
:before? top?}))

View File

@ -362,7 +362,7 @@
(nil? (:size first-elem-meta)))
block-with-title? (mldoc/block-with-title? first-elem-type)
content (string/triml content)
content (string/replace content (util/format "((%s))" (str uuid)) "")
content (string/replace content (gp-block/->block-ref uuid) "")
[content content'] (cond
(and first-block? properties?)
[content content]
@ -1557,7 +1557,7 @@
(commands/handle-step [:editor/search-page])
(state/set-editor-action-data! {:pos (cursor/get-caret-pos input)}))
"(("
gp-block/left-parens
(do
(commands/handle-step [:editor/search-block :reference])
(state/set-editor-action-data! {:pos (cursor/get-caret-pos input)}))
@ -1875,11 +1875,11 @@
;; block reference
(insert-command! id
(util/format "((%s))" uuid-string)
(gp-block/->block-ref uuid-string)
format
{:last-pattern (str "((" (if @*selected-text "" q))
:end-pattern "))"
:postfix-fn (fn [s] (util/replace-first "))" s ""))
{:last-pattern (str gp-block/left-parens (if @*selected-text "" q))
:end-pattern gp-block/right-parens
:postfix-fn (fn [s] (util/replace-first gp-block/right-parens s ""))
:forward-pos 3})
;; Save it so it'll be parsed correctly in the future
@ -2345,7 +2345,7 @@
(let [{:keys [raw-content start end]} embed-ref]
(delete-and-update input start end)
(if (= 5 (count raw-content))
(block-ref-fn "(())" 2)
(block-ref-fn gp-block/left-and-right-parens 2)
(insert raw-content)))
(if-let [page-ref (thingatpt/block-ref-at-point input)]
(let [{:keys [start end full-content raw-content]} page-ref]
@ -2353,7 +2353,7 @@
(if (= raw-content "")
(block-ref-fn "{{embed (())}}" 4)
(insert (util/format "{{embed %s}}" full-content))))
(block-ref-fn "(())" 2)))))))
(block-ref-fn gp-block/left-and-right-parens 2)))))))
(defn- keydown-new-block
[state]
@ -2891,9 +2891,9 @@
(contains? keycode/left-paren-keys k)
(= (:key last-key-code) k)
(> current-pos 0)
(not (wrapped-by? input "((" "))")))
(not (wrapped-by? input gp-block/left-parens gp-block/right-parens)))
(do
(commands/handle-step [:editor/input "(())" {:backward-truncate-number 2
(commands/handle-step [:editor/input gp-block/left-and-right-parens {:backward-truncate-number 2
:backward-pos 2}])
(commands/handle-step [:editor/search-block :reference])
(state/set-editor-action-data! {:pos (cursor/get-caret-pos input)}))
@ -2966,13 +2966,13 @@
(when-let [block-id (:block/uuid current-block)]
(if (= format "embed")
(copy-block-ref! block-id #(str "{{embed ((" % "))}}"))
(copy-block-ref! block-id #(str "((" % "))")))
(copy-block-ref! block-id gp-block/->block-ref))
(notification/show!
[:div
[:span.mb-1.5 (str "Block " format " copied!")]
[:div [:code.whitespace.break-all (if (= format "embed")
(str "{{embed ((" block-id "))}}")
(str "((" block-id "))"))]]]
(gp-block/->block-ref block-id))]]]
:success true
;; use uuid to make sure there is only one toast a time
(str "copied-block-ref:" block-id)))))
@ -3420,7 +3420,7 @@
(defn copy-current-ref
[block-id]
(when block-id
(util/copy-to-clipboard! (util/format "((%s))" (str block-id)))))
(util/copy-to-clipboard! (gp-block/->block-ref block-id))))
(defn delete-current-ref!
[block ref-id]
@ -3434,7 +3434,7 @@
(defn replace-ref-with-text!
[block ref-id]
(when (and block ref-id)
(let [match (util/format "((%s))" (str ref-id))
(let [match (gp-block/->block-ref ref-id)
ref-block (db/entity [:block/uuid ref-id])
block-ref-content (->> (or (:block/content ref-block)
"")
@ -3449,7 +3449,7 @@
(defn replace-ref-with-embed!
[block ref-id]
(when (and block ref-id)
(let [match (util/format "((%s))" (str ref-id))
(let [match (gp-block/->block-ref ref-id)
content (string/replace-first (:block/content block) match
(util/format "{{embed ((%s))}}"
(str ref-id)))]

View File

@ -23,6 +23,7 @@
[lambdaisland.glogi :as log]
[logseq.graph-parser.mldoc :as gp-mldoc]
[logseq.graph-parser.util :as gp-util]
[logseq.graph-parser.block :as gp-block]
[promesa.core :as p]
[frontend.handler.notification :as notification])
(:import
@ -188,15 +189,9 @@
(string/lower-case)))
(some-> (:arguments (second i))
(first)
(string/starts-with? "(("))
(some-> (:arguments (second i))
(first)
(string/ends-with? "))")))
gp-block/block-ref-string?))
(let [arguments (:arguments (second i))
block-ref (first arguments)
block-uuid (-> block-ref
(subs 2)
(#(subs % 0 (- (count %) 2))))]
block-uuid (gp-block/block-ref->block-id (first arguments))]
(conj! result block-uuid)
i)
:else

View File

@ -82,7 +82,7 @@
(editor-handler/html-link-format! text)
(and (text/block-ref? text)
(editor-handler/wrapped-by? input "((" "))"))
(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)
:else

View File

@ -11,6 +11,7 @@
[goog.dom :as gdom]
[goog.object :as gobj]
[rum.core :as rum]
[logseq.graph-parser.block :as gp-block]
[frontend.mobile.util :as mobile-util]))
(defn- action-command
@ -63,7 +64,7 @@
(action-command "cut" "Cut" #(editor-handler/cut-selection-blocks true))
(action-command "trash" "Delete" #(editor-handler/delete-block-aux! block true))
(action-command "registered" "Copy ref"
(fn [_event] (editor-handler/copy-block-ref! uuid #(str "((" % "))"))))
(fn [_event] (editor-handler/copy-block-ref! uuid gp-block/->block-ref)))
(action-command "link" "Copy url"
(fn [_event] (let [current-repo (state/get-current-repo)
tap-f (fn [block-id]
@ -74,5 +75,3 @@
(fn [_event]
(let [current-repo (state/get-current-repo)]
(state/sidebar-add-block! current-repo uuid :block-ref)))))]])))

View File

@ -5,6 +5,7 @@
[frontend.config :as config]
[logseq.graph-parser.text :as text]
[logseq.graph-parser.property :as gp-property]
[logseq.graph-parser.block :as gp-block]
[cljs.reader :as reader]
[goog.object :as gobj]))
@ -46,7 +47,7 @@
:end line-end-pos}))))
(defn block-ref-at-point [& [input]]
(when-let [block-ref (thing-at-point ["((" "))"] input " ")]
(when-let [block-ref (thing-at-point [gp-block/left-parens gp-block/right-parens] input " ")]
(when-let [uuid (uuid (:raw-content block-ref))]
(assoc block-ref
:type "block-ref"