Skip to content

Commit

Permalink
Move block-ref fns and vars to their own ns
Browse files Browse the repository at this point in the history
Similar to page-ref to keep namespaces explicit
  • Loading branch information
logseq-cldwalker authored and tiensonqin committed Aug 4, 2022
1 parent e0b1f6b commit 4ec5827
Show file tree
Hide file tree
Showing 18 changed files with 107 additions and 98 deletions.
3 changes: 2 additions & 1 deletion .clj-kondo/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
logseq.graph-parser.property gp-property
logseq.graph-parser.config gp-config
logseq.graph-parser.date-time-util date-time-util
logseq.graph-parser.util.page-ref page-ref}}}
logseq.graph-parser.util.page-ref page-ref
logseq.graph-parser.util.block-ref block-ref}}}

:hooks {:analyze-call {rum.core/defc hooks.rum/defc
rum.core/defcs hooks.rum/defcs}}
Expand Down
8 changes: 4 additions & 4 deletions deps/graph-parser/.carve/ignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ 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
logseq.graph-parser.util.block-ref/left-and-right-parens
;; API
logseq.graph-parser.block/->block-ref
logseq.graph-parser.util.block-ref/->block-ref
;; API
logseq.graph-parser.block/block-ref?
logseq.graph-parser.util.block-ref/block-ref?
;; API
logseq.graph-parser.block/get-all-block-ref-ids
logseq.graph-parser.util.block-ref/get-all-block-ref-ids
;; API
logseq.graph-parser.util.page-ref/left-and-right-brackets
;; API
Expand Down
3 changes: 2 additions & 1 deletion deps/graph-parser/.clj-kondo/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
logseq.graph-parser.property gp-property
logseq.graph-parser.config gp-config
logseq.graph-parser.date-time-util date-time-util
logseq.graph-parser.util.page-ref page-ref}}}
logseq.graph-parser.util.page-ref page-ref
logseq.graph-parser.util.block-ref block-ref}}}
:skip-comments true
:output {:progress true}}
47 changes: 5 additions & 42 deletions deps/graph-parser/src/logseq/graph_parser/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
[logseq.graph-parser.text :as text]
[logseq.graph-parser.utf8 :as utf8]
[logseq.graph-parser.util :as gp-util]
[logseq.graph-parser.util.block-ref :as block-ref]
[logseq.graph-parser.util.page-ref :as page-ref]))

(defn heading-block?
Expand All @@ -33,44 +34,6 @@
"")))
(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))
(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 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)))

(defn ->block-ref
"Creates block ref string given id"
[block-id]
(str left-parens block-id right-parens))

(defn- get-page-reference
[block supported-formats]
(let [page (cond
Expand Down Expand Up @@ -130,7 +93,7 @@ of block-ref?"

:else
nil)]
(when page (or (get-block-ref-id page) page))))
(when page (or (block-ref/get-block-ref-id page) page))))

(defn- get-block-reference
[block]
Expand All @@ -150,8 +113,8 @@ of block-ref?"
(let [{:keys [name arguments]} (second block)]
(when (and (= name "embed")
(string? (first arguments))
(string-block-ref? (first arguments)))
(get-string-block-ref-id (first arguments))))
(block-ref/string-block-ref? (first arguments)))
(block-ref/get-string-block-ref-id (first arguments))))

(and (vector? block)
(= "Link" (first block))
Expand All @@ -161,7 +124,7 @@ of block-ref?"
(let [id (second (:url (second block)))]
;; these can be maps
(when (string? id)
(or (get-block-ref-id id) id))))
(or (block-ref/get-block-ref-id id) id))))

:else
nil)]
Expand Down
41 changes: 41 additions & 0 deletions deps/graph-parser/src/logseq/graph_parser/util/block_ref.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
(ns logseq.graph-parser.util.block-ref
"General purpose vars and util fns for block-refs"
(:require [clojure.string :as string]))

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

(defn ->block-ref
"Creates block ref string given id"
[block-id]
(str left-parens block-id right-parens))
6 changes: 3 additions & 3 deletions src/main/frontend/commands.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
[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]
[logseq.graph-parser.property :as gp-property]
[logseq.graph-parser.util.page-ref :as page-ref]
[logseq.graph-parser.util.block-ref :as block-ref]
[goog.dom :as gdom]
[goog.object :as gobj]
[promesa.core :as p]))
Expand Down Expand Up @@ -221,7 +221,7 @@
[["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}]
["Block reference" [[:editor/input block-ref/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"]
Expand Down Expand Up @@ -343,7 +343,7 @@
(or
(and s
(string/ends-with? s "(")
(or (string/starts-with? last-pattern gp-block/left-parens)
(or (string/starts-with? last-pattern block-ref/left-parens)
(string/starts-with? last-pattern page-ref/left-brackets)))
(and s (string/starts-with? s "{{embed"))
(and last-pattern
Expand Down
13 changes: 7 additions & 6 deletions src/main/frontend/components/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
[logseq.graph-parser.text :as text]
[logseq.graph-parser.util :as gp-util]
[logseq.graph-parser.util.page-ref :as page-ref]
[logseq.graph-parser.util.block-ref :as block-ref]
[medley.core :as medley]
[promesa.core :as p]
[reitit.frontend.easy :as rfe]
Expand Down Expand Up @@ -788,7 +789,7 @@
:delay [1000, 100]} inner)
inner)])
[:span.warning.mr-1 {:title "Block ref invalid"}
(gp-block/->block-ref id)]))))
(block-ref/->block-ref id)]))))

(defn inline-text
([format v]
Expand Down Expand Up @@ -931,8 +932,8 @@
(not= \* (last s)))
(->elem :a {:on-click #(route-handler/jump-to-anchor! (mldoc/anchorLink (subs s 1)))} (subs s 1))

(gp-block/block-ref? s)
(let [id (gp-block/get-block-ref-id s)]
(block-ref/block-ref? s)
(let [id (block-ref/get-block-ref-id s)]
(block-reference config id label))

(not (string/includes? s "."))
Expand Down Expand Up @@ -1120,8 +1121,8 @@
(when-not (string/blank? page-name)
(page-embed (assoc config :link-depth (inc link-depth)) page-name)))

(gp-block/string-block-ref? a)
(when-let [s (-> gp-block/get-string-block-ref-id string/trim)]
(block-ref/string-block-ref? a)
(when-let [s (-> block-ref/get-string-block-ref-id string/trim)]
(when-let [id (some-> s parse-uuid)]
(block-embed (assoc config :link-depth (inc link-depth)) id)))

Expand Down Expand Up @@ -2152,7 +2153,7 @@
editor-id (str "editor-" edit-input-id)
slide? (:slide? config)
trimmed-content (string/trim (:block/content block))
block-reference-only? (gp-block/block-ref? trimmed-content)]
block-reference-only? (block-ref/block-ref? trimmed-content)]
(if (and edit? editor-box)
[:div.editor-wrapper {:id editor-id}
(ui/catch-error
Expand Down
4 changes: 2 additions & 2 deletions src/main/frontend/components/content.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
[frontend.ui :as ui]
[frontend.util :as util]
[logseq.graph-parser.util :as gp-util]
[logseq.graph-parser.block :as gp-block]
[logseq.graph-parser.util.block-ref :as block-ref]
[frontend.util.url :as url-util]
[goog.dom :as gdom]
[goog.object :as gobj]
Expand Down Expand Up @@ -208,7 +208,7 @@
(ui/menu-link
{:key "Copy block ref"
:on-click (fn [_e]
(editor-handler/copy-block-ref! block-id gp-block/->block-ref))}
(editor-handler/copy-block-ref! block-id block-ref/->block-ref))}
"Copy block ref")

(ui/menu-link
Expand Down
4 changes: 2 additions & 2 deletions src/main/frontend/components/shortcut.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[frontend.ui :as ui]
[frontend.extensions.latex :as latex]
[frontend.extensions.highlight :as highlight]
[logseq.graph-parser.block :as gp-block]
[logseq.graph-parser.util.block-ref :as block-ref]
[logseq.graph-parser.util.page-ref :as page-ref]
[rum.core :as rum]))

Expand Down Expand Up @@ -102,7 +102,7 @@
[: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]]]
[:td.text-right [:code block-ref/left-and-right-parens]]]
[:tr
[:td.text-left (t :command.editor/open-link-in-sidebar)]
[:td.text-right (ui/render-keyboard-shortcut ["shift" "click"])]]
Expand Down
4 changes: 2 additions & 2 deletions src/main/frontend/extensions/pdf/assets.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[frontend.state :as state]
[frontend.util :as util]
[logseq.graph-parser.config :as gp-config]
[logseq.graph-parser.block :as gp-block]
[logseq.graph-parser.util.block-ref :as block-ref]
[medley.core :as medley]
[promesa.core :as p]
[reitit.frontend.easy :as rfe]
Expand Down Expand Up @@ -214,7 +214,7 @@
(defn copy-hl-ref!
[highlight]
(when-let [ref-block (create-ref-block! highlight)]
(util/copy-to-clipboard! (gp-block/->block-ref (:block/uuid ref-block)))))
(util/copy-to-clipboard! (block-ref/->block-ref (:block/uuid ref-block)))))

(defn open-block-ref!
[block]
Expand Down
4 changes: 2 additions & 2 deletions src/main/frontend/external/roam.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[clojure.walk :as walk]
[clojure.string :as string]
[goog.string :as gstring]
[logseq.graph-parser.block :as gp-block]
[logseq.graph-parser.util.block-ref :as block-ref]
[logseq.graph-parser.util :as gp-util]
[logseq.graph-parser.text :as text]))

Expand All @@ -31,7 +31,7 @@
[text]
(string/replace text uid-pattern (fn [[_ uid]]
(let [id (get @uid->uuid uid uid)]
(gp-block/->block-ref id)))))
(block-ref/->block-ref id)))))

(defn macro-transform
[text]
Expand Down
4 changes: 2 additions & 2 deletions src/main/frontend/fs/watcher_handler.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[frontend.handler.repo :as repo-handler]
[frontend.handler.ui :as ui-handler]
[logseq.graph-parser.util :as gp-util]
[logseq.graph-parser.block :as gp-block]
[logseq.graph-parser.util.block-ref :as block-ref]
[lambdaisland.glogi :as log]
[electron.ipc :as ipc]
[promesa.core :as p]
Expand All @@ -22,7 +22,7 @@
(defn- set-missing-block-ids!
[content]
(when (string? content)
(doseq [block-id (gp-block/get-all-block-ref-ids content)]
(doseq [block-id (block-ref/get-all-block-ref-ids content)]
(when-let [block (try
(model/get-block-by-uuid block-id)
(catch js/Error _e
Expand Down
4 changes: 2 additions & 2 deletions src/main/frontend/handler/dnd.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[frontend.modules.outliner.core :as outliner-core]
[frontend.modules.outliner.tree :as tree]
[frontend.modules.outliner.transaction :as outliner-tx]
[logseq.graph-parser.block :as gp-block]
[logseq.graph-parser.util.block-ref :as block-ref]
[frontend.state :as state]))

(defn move-blocks
Expand All @@ -23,7 +23,7 @@
:id
(str (:block/uuid first-block)))
(editor-handler/api-insert-new-block!
(gp-block/->block-ref (:block/uuid first-block))
(block-ref/->block-ref (:block/uuid first-block))
{:block-uuid (:block/uuid target-block)
:sibling? (not nested?)
:before? top?}))
Expand Down
Loading

0 comments on commit 4ec5827

Please sign in to comment.