From a3d30e0b52068adb1494460523a03660ae514f3e Mon Sep 17 00:00:00 2001 From: Gabriel Horner Date: Mon, 25 Jul 2022 17:14:27 -0400 Subject: [PATCH] Move all block ref parens strings to vars Extracted a couple fns we kept reinventing across namespaces --- deps/graph-parser/.carve/ignore | 4 +++ .../src/logseq/graph_parser/block.cljs | 25 ++++++++++++++-- src/main/frontend/commands.cljs | 5 ++-- src/main/frontend/components/block.cljs | 16 ++++------ src/main/frontend/components/content.cljs | 3 +- src/main/frontend/components/shortcut.cljs | 3 +- src/main/frontend/extensions/pdf/assets.cljs | 3 +- src/main/frontend/external/roam.cljs | 11 +++---- src/main/frontend/handler/dnd.cljs | 6 ++-- src/main/frontend/handler/editor.cljs | 30 +++++++++---------- src/main/frontend/handler/export.cljs | 11 ++----- src/main/frontend/handler/paste.cljs | 2 +- src/main/frontend/mobile/action_bar.cljs | 5 ++-- src/main/frontend/util/thingatpt.cljs | 3 +- 14 files changed, 73 insertions(+), 54 deletions(-) diff --git a/deps/graph-parser/.carve/ignore b/deps/graph-parser/.carve/ignore index 21c1be558..f4f59687a 100644 --- a/deps/graph-parser/.carve/ignore +++ b/deps/graph-parser/.carve/ignore @@ -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 diff --git a/deps/graph-parser/src/logseq/graph_parser/block.cljs b/deps/graph-parser/src/logseq/graph_parser/block.cljs index 0f042feae..c3dc38dda 100644 --- a/deps/graph-parser/src/logseq/graph_parser/block.cljs +++ b/deps/graph-parser/src/logseq/graph_parser/block.cljs @@ -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)) diff --git a/src/main/frontend/commands.cljs b/src/main/frontend/commands.cljs index ef30fa815..767c21dc5 100644 --- a/src/main/frontend/commands.cljs +++ b/src/main/frontend/commands.cljs @@ -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 diff --git a/src/main/frontend/components/block.cljs b/src/main/frontend/components/block.cljs index 8f1fea57c..4925e5894 100644 --- a/src/main/frontend/components/block.cljs +++ b/src/main/frontend/components/block.cljs @@ -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 diff --git a/src/main/frontend/components/content.cljs b/src/main/frontend/components/content.cljs index c1637b43f..19f529e6b 100644 --- a/src/main/frontend/components/content.cljs +++ b/src/main/frontend/components/content.cljs @@ -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 diff --git a/src/main/frontend/components/shortcut.cljs b/src/main/frontend/components/shortcut.cljs index 72471f69b..6d382f64e 100644 --- a/src/main/frontend/components/shortcut.cljs +++ b/src/main/frontend/components/shortcut.cljs @@ -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"])]] diff --git a/src/main/frontend/extensions/pdf/assets.cljs b/src/main/frontend/extensions/pdf/assets.cljs index f25573f75..bb31fd85d 100644 --- a/src/main/frontend/extensions/pdf/assets.cljs +++ b/src/main/frontend/extensions/pdf/assets.cljs @@ -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] diff --git a/src/main/frontend/external/roam.cljs b/src/main/frontend/external/roam.cljs index 74cbdc87a..bd52ca899 100644 --- a/src/main/frontend/external/roam.cljs +++ b/src/main/frontend/external/roam.cljs @@ -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) diff --git a/src/main/frontend/handler/dnd.cljs b/src/main/frontend/handler/dnd.cljs index b3597ee27..b6f08eef2 100644 --- a/src/main/frontend/handler/dnd.cljs +++ b/src/main/frontend/handler/dnd.cljs @@ -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?})) diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index 9192babaf..e88bc9add 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -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)))] diff --git a/src/main/frontend/handler/export.cljs b/src/main/frontend/handler/export.cljs index f4c6e2b80..db04e9c87 100644 --- a/src/main/frontend/handler/export.cljs +++ b/src/main/frontend/handler/export.cljs @@ -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 diff --git a/src/main/frontend/handler/paste.cljs b/src/main/frontend/handler/paste.cljs index 745069a15..130e30d81 100644 --- a/src/main/frontend/handler/paste.cljs +++ b/src/main/frontend/handler/paste.cljs @@ -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 diff --git a/src/main/frontend/mobile/action_bar.cljs b/src/main/frontend/mobile/action_bar.cljs index 623c0fe07..a006188c5 100644 --- a/src/main/frontend/mobile/action_bar.cljs +++ b/src/main/frontend/mobile/action_bar.cljs @@ -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)))))]]))) - - diff --git a/src/main/frontend/util/thingatpt.cljs b/src/main/frontend/util/thingatpt.cljs index f13d0cbd2..ae58b2e38 100644 --- a/src/main/frontend/util/thingatpt.cljs +++ b/src/main/frontend/util/thingatpt.cljs @@ -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"