Skip to content

Commit

Permalink
zotero: support attachment with relative link
Browse files Browse the repository at this point in the history
  • Loading branch information
Weihua Lu committed Aug 16, 2021
1 parent 86c2c0a commit 66a330f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 54 deletions.
15 changes: 9 additions & 6 deletions src/main/frontend/components/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -953,12 +953,6 @@
(state/set-state! :pdf/current current)))}
(get-label-text label)]

(and
(util/electron?)
(= protocol "zotero")
(= (-> label get-label-text util/get-file-ext) "pdf"))
(zotero/zotero-pdf-link (get-label-text label) href)

:else
(->elem
:a.external-link
Expand Down Expand Up @@ -1091,6 +1085,15 @@
(= name "tutorial-video")
(tutorial-video)

(= name "zotero-imported-file")
(let [[item-key filename] arguments]
(when (and item-key filename)
[:span.ml-1 (zotero/zotero-imported-file item-key filename)]))

(= name "zotero-linked-file")
(when-let [path (first arguments)]
[:span.ml-1 (zotero/zotero-linked-file path)])

(= name "vimeo")
(when-let [url (first arguments)]
(let [Vimeo-regex #"^((?:https?:)?//)?((?:www).)?((?:player.vimeo.com|vimeo.com)?)((?:/video/)?)([\w-]+)(\S+)?$"]
Expand Down
69 changes: 42 additions & 27 deletions src/main/frontend/extensions/zotero.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns frontend.extensions.zotero
(:require [cljs.core.async :refer [<! >! go chan go-loop] :as a]
[clojure.edn :refer [read-string]]
[clojure.string :as str]
[frontend.components.svg :as svg]
[frontend.extensions.pdf.assets :as pdf-assets]
Expand Down Expand Up @@ -318,30 +319,44 @@
[:div.bg-greenred-200.py-3.rounded-lg.col-span-full
[:progress.w-full {:max (+ @(::total state) 30) :value @(::progress state)}] "Importing items from Zotero....Please wait..."]])])


(rum/defc zotero-pdf-link
[label-text href]
[:div.zotero-pdf-link
[:a.external-link
{:href href
:target "_blank"}
label-text]
(ui/button
"annotate"
:small? true
:class "ml-1"
:on-click
(fn []
(if (str/blank? (setting/setting :zotero-data-directory))
(do
(route-handler/redirect! {:to :zotero-setting})
(notification/show! "Please setup Zotero data directory" :warn false))
(let [item-key (->> (str/split href #"/") last)
full-path
(util/node-path.join
(setting/setting :zotero-data-directory)
"storage"
item-key
label-text)
current (pdf-assets/inflate-asset full-path)]
(state/set-state! :pdf/current current)))))])
(defn open-button [full-path]
(if (str/ends-with? (str/lower-case full-path) "pdf")
(ui/button
"open"
:small? true
:on-click
(fn [e]
(when-let [current (pdf-assets/inflate-asset full-path)]
(util/stop e)
(state/set-state! :pdf/current current))))
(ui/button
"open"
:small? true
:target "_blank"
:href full-path)))

(rum/defc zotero-imported-file
[item-key filename]
(if (str/blank? (setting/setting :zotero-data-directory))
[:p.warning "This is a zotero imported file, setting Zotero data directory would allow you to open the file in Logseq"]
(let [filename (read-string filename)
full-path
(str "file://"
(util/node-path.join
(setting/setting :zotero-data-directory)
"storage"
item-key
filename))]
(open-button full-path))))

(rum/defc zotero-linked-file
[path]
(if (str/blank? (setting/setting :zotero-linked-attachment-base-directory))
[:p.warning "This is a zotero linked file, setting Zotero linked attachment base directory would allow you to open the file in Logseq"]
(let [path (read-string path)
full-path
(str "file://"
(util/node-path.join
(setting/setting :zotero-linked-attachment-base-directory)
(str/replace-first path "attachments:" "")))]
(open-button full-path))))
41 changes: 20 additions & 21 deletions src/main/frontend/extensions/zotero/extractor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -144,34 +144,33 @@
(let [note-html (-> item :data :note)]
(html-parser/parse :markdown note-html)))

(defn linked-attachment-check [path]
(if (str/starts-with? path "attachments:")
(let [base-directory (setting/setting :zotero-linked-attachment-base-directory)]
(if (str/blank? base-directory)
(notification/show!
"Linked attachment with relative path detected! Did you forget to setup Zotero linked attachment base directory in setting?"
:warning
false)
(util/node-path.join
base-directory
(str/replace-first path "attachments:" ""))))
path))
(defn zotero-imported-file-macro [item-key filename]
(util/format "{{zotero-imported-file %s, %s}}" item-key (pr-str filename)))

(defn zotero-linked-file-macro [path]
(util/format "{{zotero-linked-file %s}}" (pr-str path)))

(defmethod extract "attachment"
[item]
(let [{:keys [title url link-mode path content-type]} (-> item :data)]
(let [{:keys [title url link-mode path content-type filename]} (-> item :data)]
(case link-mode
"imported_file"
(markdown-link title (local-link item))
(str
(markdown-link title (local-link item))
" "
(zotero-imported-file-macro (item-key item) filename))
"linked_file"
(if (str/starts-with? path "attachments:")
(str
(markdown-link title (local-link item))
" "
(zotero-linked-file-macro path))
(let [path (str/replace path " " "%20")]
(if (= content-type "application/pdf")
(markdown-link title (str "file://" path) true)
(markdown-link title (str "file://" path)))))
"imported_url"
(markdown-link title url)
"linked_file"
(let [path (-> path
linked-attachment-check
(str/replace " " "%20"))]
(if (= content-type "application/pdf")
(markdown-link title (str "file://" path) true)
(markdown-link title (str "file://" path))))
"linked_url"
(markdown-link title url))))

Expand Down

0 comments on commit 66a330f

Please sign in to comment.