Skip to content

Commit

Permalink
Merge branch 'master' into feat/custom-children-list-style
Browse files Browse the repository at this point in the history
  • Loading branch information
xyhp915 committed Apr 28, 2023
2 parents 04c0e50 + 880ea8b commit 9367590
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 79 deletions.
8 changes: 8 additions & 0 deletions deps/common/src/logseq/common/path.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,11 @@
(string/starts-with? p "/")
;; is windows dir
(re-find #"^[a-zA-Z]:[/\\]" p)))))

(defn protocol-url?
"Whether path `p` is a protocol URL.
This is a loose check, it only checks if there is a valid protocol prefix."
[p]
(boolean (and (re-find #"^[a-zA-Z0-9_+\-\.]{2,}:" p) ;; HACK: avoid matching windows drive
(not (string/includes? p " ")))))
12 changes: 12 additions & 0 deletions deps/common/test/logseq/common/path_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,15 @@
(is (false? (path/absolute? "test.md")))
(is (false? (path/absolute? "test")))
(is (false? (path/absolute? "D:test.md")))))

(deftest protocol-url
(testing "protocol url"
(is (true? (path/protocol-url? "mailto:[email protected]")))
(is (true? (path/protocol-url? "https://logseq.com")))
(is (true? (path/protocol-url? "ftp://logseq.com")))
(is (true? (path/protocol-url? "file:///home/xxx/logseq/test.md")))
(is (true? (path/protocol-url? "assets:///home/xxx/logseq/test.md")))
(is (false? (path/protocol-url? "logseq/test.md")))
(is (false? (path/protocol-url? "test.md")))
(is (false? (path/protocol-url? "test")))
(is (false? (path/protocol-url? "D:test.md")))))
3 changes: 3 additions & 0 deletions deps/graph-parser/src/logseq/graph_parser/util.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
(and (string? v) (>= (count v) 2) (= "\"" (first v) (last v))))

(defn url?
"Test if it is a `protocol://`-style URL.
NOTE: Can not handle mailto: links, use this with caution."
[s]
(and (string? s)
(try
Expand Down
109 changes: 55 additions & 54 deletions src/electron/electron/search.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
[clojure.string :as string]
["electron" :refer [app]]
[electron.logger :as logger]
[medley.core :as medley]))
[medley.core :as medley]
[electron.utils :as utils]))

(defonce databases (atom nil))

Expand All @@ -28,14 +29,23 @@
[repo]
(get @databases (sanitize-db-name repo)))

(declare delete-db!)

(defn prepare
[^object db sql]
[^object db sql db-name]
(when db
(.prepare db sql)))
(try
(.prepare db sql)
(catch :default e
(logger/error (str "SQLite prepare failed: " e ": " db-name))
;; case 1: vtable constructor failed: blocks_fts https://github.com/logseq/logseq/issues/7467
(delete-db! db-name)
(utils/send-to-renderer "rebuildSearchIndice" {})
(throw e)))))

(defn add-blocks-fts-triggers!
"Table bindings of blocks tables and the blocks FTS virtual tables"
[db]
[db db-name]
(let [triggers [;; add
"CREATE TRIGGER IF NOT EXISTS blocks_ad AFTER DELETE ON blocks
BEGIN
Expand All @@ -55,12 +65,12 @@
VALUES (new.id, new.uuid, new.content, new.page);
END;"]]
(doseq [trigger triggers]
(let [stmt (prepare db trigger)]
(let [stmt (prepare db trigger db-name)]
(.run ^object stmt)))))

(defn add-pages-fts-triggers!
"Table bindings of pages tables and the pages FTS virtual tables"
[db]
[db db-name]
(let [triggers [;; add
"CREATE TRIGGER IF NOT EXISTS pages_ad AFTER DELETE ON pages
BEGIN
Expand All @@ -80,34 +90,36 @@
VALUES (new.id, new.uuid, new.content);
END;"]]
(doseq [trigger triggers]
(let [stmt (prepare db trigger)]
(let [stmt (prepare db trigger db-name)]
(.run ^object stmt)))))

(defn create-blocks-table!
[db]
[db db-name]
(let [stmt (prepare db "CREATE TABLE IF NOT EXISTS blocks (
id INTEGER PRIMARY KEY,
uuid TEXT NOT NULL,
content TEXT NOT NULL,
page INTEGER)")]
page INTEGER)"
db-name)]
(.run ^object stmt)))

(defn create-blocks-fts-table!
[db]
(let [stmt (prepare db "CREATE VIRTUAL TABLE IF NOT EXISTS blocks_fts USING fts5(uuid, content, page)")]
[db db-name]
(let [stmt (prepare db "CREATE VIRTUAL TABLE IF NOT EXISTS blocks_fts USING fts5(uuid, content, page)" db-name)]
(.run ^object stmt)))

(defn create-pages-table!
[db]
[db db-name]
(let [stmt (prepare db "CREATE TABLE IF NOT EXISTS pages (
id INTEGER PRIMARY KEY,
uuid TEXT NOT NULL,
content TEXT NOT NULL)")]
content TEXT NOT NULL)"
db-name)]
(.run ^object stmt)))

(defn create-pages-fts-table!
[db]
(let [stmt (prepare db "CREATE VIRTUAL TABLE IF NOT EXISTS pages_fts USING fts5(uuid, content)")]
[db db-name]
(let [stmt (prepare db "CREATE VIRTUAL TABLE IF NOT EXISTS pages_fts USING fts5(uuid, content)" db-name)]
(.run ^object stmt)))

(defn get-search-dir
Expand Down Expand Up @@ -136,12 +148,12 @@
[db-name]
(let [[db-sanitized-name db-full-path] (get-db-full-path db-name)]
(try (let [db (sqlite3 db-full-path nil)]
(create-blocks-table! db)
(create-blocks-fts-table! db)
(create-pages-table! db)
(create-pages-fts-table! db)
(add-blocks-fts-triggers! db)
(add-pages-fts-triggers! db)
(create-blocks-table! db db-name)
(create-blocks-fts-table! db db-name)
(create-pages-table! db db-name)
(create-pages-fts-table! db db-name)
(add-blocks-fts-triggers! db db-name)
(add-pages-fts-triggers! db db-name)
(swap! databases assoc db-sanitized-name db))
(catch :default e
(logger/error (str e ": " db-name))
Expand Down Expand Up @@ -170,7 +182,7 @@
(if-let [db (get-db repo)]
;; TODO: what if a CONFLICT on uuid
;; Should update all values on id conflict
(let [insert (prepare db "INSERT INTO pages (id, uuid, content) VALUES (@id, @uuid, @content) ON CONFLICT (id) DO UPDATE SET (uuid, content) = (@uuid, @content)")
(let [insert (prepare db "INSERT INTO pages (id, uuid, content) VALUES (@id, @uuid, @content) ON CONFLICT (id) DO UPDATE SET (uuid, content) = (@uuid, @content)" repo)
insert-many (.transaction ^object db
(fn [pages]
(doseq [page pages]
Expand All @@ -184,15 +196,15 @@
[repo ids]
(when-let [db (get-db repo)]
(let [sql (str "DELETE from pages WHERE id IN " (clj-list->sql ids))
stmt (prepare db sql)]
stmt (prepare db sql repo)]
(.run ^object stmt))))

(defn upsert-blocks!
[repo blocks]
(if-let [db (get-db repo)]
;; TODO: what if a CONFLICT on uuid
;; Should update all values on id conflict
(let [insert (prepare db "INSERT INTO blocks (id, uuid, content, page) VALUES (@id, @uuid, @content, @page) ON CONFLICT (id) DO UPDATE SET (uuid, content, page) = (@uuid, @content, @page)")
(let [insert (prepare db "INSERT INTO blocks (id, uuid, content, page) VALUES (@id, @uuid, @content, @page) ON CONFLICT (id) DO UPDATE SET (uuid, content, page) = (@uuid, @content, @page)" repo)
insert-many (.transaction ^object db
(fn [blocks]
(doseq [block blocks]
Expand All @@ -206,20 +218,13 @@
[repo ids]
(when-let [db (get-db repo)]
(let [sql (str "DELETE from blocks WHERE id IN " (clj-list->sql ids))
stmt (prepare db sql)]
stmt (prepare db sql repo)]
(.run ^object stmt))))

;; (defn search-blocks-fts
;; [q]
;; (when-not (string/blank? q)
;; (let [stmt (prepare @database
;; "select id, uuid, content from blocks_fts where content match ? ORDER BY rank")]
;; (js->clj (.all ^object stmt q) :keywordize-keys true))))

(defn- search-blocks-aux
[database sql input page limit]
[repo database sql input page limit]
(try
(let [stmt (prepare database sql)]
(let [stmt (prepare database sql repo)]
(js->clj
(if page
(.all ^object stmt (int page) input limit)
Expand Down Expand Up @@ -264,12 +269,12 @@
matched-result (->>
(map
(fn [match-input]
(search-blocks-aux database match-sql match-input page limit))
(search-blocks-aux repo database match-sql match-input page limit))
match-inputs)
(apply concat))]
(->>
(concat matched-result
(search-blocks-aux database non-match-sql non-match-input page limit))
(search-blocks-aux repo database non-match-sql non-match-input page limit))
(distinct-by :rowid)
(take limit)
(vec))))))
Expand Down Expand Up @@ -299,8 +304,8 @@
snippet)}))

(defn- search-pages-aux
[database sql input limit]
(let [stmt (prepare database sql)]
[repo database sql input limit]
(let [stmt (prepare database sql repo)]
(try
(doall
(map search-pages-res-unpack (-> (.raw ^object stmt)
Expand Down Expand Up @@ -329,36 +334,38 @@
matched-result (->>
(map
(fn [match-input]
(search-pages-aux database match-sql match-input limit))
(search-pages-aux repo database match-sql match-input limit))
match-inputs)
(apply concat))]
(->>
(concat matched-result
(search-pages-aux database non-match-sql non-match-input limit))
(search-pages-aux repo database non-match-sql non-match-input limit))
(distinct-by :id)
(take limit)
(vec))))))

(defn truncate-blocks-table!
[repo]
(when-let [database (get-db repo)]
(let [stmt (prepare database
"delete from blocks;")
(let [stmt (prepare database "delete from blocks;" repo)
_ (.run ^object stmt)
stmt (prepare database
"delete from blocks_fts;")]
stmt (prepare database "delete from blocks_fts;" repo)]
(.run ^object stmt))))

(defn truncate-pages-table!
[repo]
(when-let [database (get-db repo)]
(let [stmt (prepare database
"delete from pages;")
(let [stmt (prepare database "delete from pages;" repo)
_ (.run ^object stmt)
stmt (prepare database
"delete from pages_fts;")]
stmt (prepare database "delete from pages_fts;" repo)]
(.run ^object stmt))))

(defn query
[repo sql]
(when-let [database (get-db repo)]
(let [stmt (prepare database sql repo)]
(.all ^object stmt))))

(defn delete-db!
[repo]
(when-let [database (get-db repo)]
Expand All @@ -367,9 +374,3 @@
(logger/info "Delete search indice: " db-full-path)
(fs/unlinkSync db-full-path)
(swap! databases dissoc db-name))))

(defn query
[repo sql]
(when-let [database (get-db repo)]
(let [stmt (prepare database sql)]
(.all ^object stmt))))
6 changes: 6 additions & 0 deletions src/main/electron/listener.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
[frontend.handler.route :as route-handler]
[frontend.handler.ui :as ui-handler]
[frontend.handler.user :as user]
[frontend.handler.search :as search-handler]
[frontend.state :as state]
[frontend.ui :as ui]
[logseq.common.path :as path]
Expand Down Expand Up @@ -76,6 +77,11 @@
(let [repo (bean/->clj data)]
(repo-handler/remove-repo! repo))))

(safe-api-call "rebuildSearchIndice"
(fn [_data]
(prn "Rebuild search indices")
(search-handler/rebuild-indices!)))

(safe-api-call "setGitUsernameAndEmail"
(fn []
(state/pub-event! [:modal/set-git-username-and-email])))
Expand Down
10 changes: 6 additions & 4 deletions src/main/frontend/components/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,9 @@

(defn- relative-assets-path->absolute-path
[path]
(if (path/absolute? path)
(when (path/protocol-url? path)
(js/console.error "BUG: relative-assets-path->absolute-path called with protocol url" path))
(if (or (path/absolute? path) (path/protocol-url? path))
path
(.. util/node-path
(join (config/get-repo-dir (state/get-current-repo))
Expand Down Expand Up @@ -1080,7 +1082,7 @@
(not (string/includes? s "."))
(page-reference (:html-export? config) s config label)

(gp-util/url? s)
(path/protocol-url? s)
(->elem :a {:href s
:data-href s
:target "_blank"}
Expand All @@ -1102,7 +1104,7 @@
(->elem
:a
(cond->
{:href (str "file://" path)
{:href (path/path-join "file://" path)
:data-href path
:target "_blank"}
title
Expand Down Expand Up @@ -1184,7 +1186,7 @@
href)]
(->elem
:a
(cond-> {:href (str "file://" href*)
(cond-> {:href (path/path-join "file://" href*)
:data-href href*
:target "_blank"}
title (assoc :title title))
Expand Down
Loading

0 comments on commit 9367590

Please sign in to comment.