Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(zotero): better multi-profile experience #10430

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/main/frontend/extensions/pdf/assets.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
[frontend.util :as util]
[frontend.extensions.pdf.utils :as pdf-utils]
[frontend.extensions.pdf.windows :as pdf-windows]
[frontend.extensions.zotero.setting :as setting]
[logseq.common.path :as path]
[logseq.graph-parser.config :as gp-config]
[logseq.graph-parser.util.block-ref :as block-ref]
Expand Down Expand Up @@ -152,7 +153,7 @@
(when-let [page-name (util/trim-safe (:key pdf-current))]
(let [page-name (str "hls__" page-name)
page (db-model/get-page page-name)
file-path (:original-path pdf-current)
file-path (clojure.string/replace (:original-path pdf-current) #"^file://" "")
format (state/get-preferred-format)
repo-dir (config/get-repo-dir (state/get-current-repo))
asset-dir (util/node-path.join repo-dir gp-config/local-assets-dir)
Expand Down Expand Up @@ -237,6 +238,37 @@
(state/set-current-pdf! (inflate-asset file-path)))
(js/console.debug "[Unmatched highlight ref]" block)))))))

(defn zotero-update-pdf-path [pdf-path]
;; Check if pdf-path is a sub-path of any of the base-dirs in profiles
;; FEAT Suggest users to check profiles if pdf-path is not a sub-path of any of the base-dirs in the config
(when-let [base-dirs (->> (setting/sub-zotero-config) vals
(map :zotero-linked-attachment-base-directory)
vec rest)]
(let [pollution-source
(reduce (fn [longest-path path]
(if (and (clojure.string/includes? pdf-path path)
(> (count path) (count longest-path)))
path longest-path))
nil base-dirs)]
(if pollution-source
(string/replace pdf-path pollution-source
(setting/setting :zotero-linked-attachment-base-directory))
nil))))

(defn handle-missing-pdf-error!
;; Handle "MissingPDFException" error.
;; Return true if missing pdf is found and then handled,
;; otherwise return false
;; For now it only deals with :file-path pollution caused by zotero multi-profiles workflow
[pdf-current]
(let [file-path (-> (:original-path pdf-current)
(zotero-update-pdf-path))]
(if file-path
(do
(state/set-state! :pdf/current (inflate-asset file-path))
true)
false)))

(defn goto-block-ref!
[{:keys [id] :as hl}]
(when id
Expand Down
13 changes: 8 additions & 5 deletions src/main/frontend/extensions/pdf/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -945,11 +945,14 @@
(case (.-name error)
"MissingPDFException"
(do
(notification/show!
(str "Error: " (.-message error) "\n Is this the correct path?")
:error
false)
(state/set-state! :pdf/current nil))
(if (pdf-assets/handle-missing-pdf-error! pdf-current)
(set-loader-state! {:error nil})
(do
(notification/show!
(str "Error: " (.-message error) "\n Is this the correct path?")
:error
false)
(state/set-state! :pdf/current nil))))

"InvalidPDFException"
(do
Expand Down