Skip to content

Commit

Permalink
fix: open pdfs in zotero storage via #19 by @adam-coates
Browse files Browse the repository at this point in the history
Fixes opening the pdf
  • Loading branch information
jmbuhr authored Feb 7, 2025
2 parents feb453f + 721483d commit 0eaff32
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lua/zotero/database.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ local query_items = [[
itemTypes.typeName,
itemAttachments.path AS attachment_path,
itemAttachments.contentType AS attachment_content_type,
itemAttachments.linkMode AS attachment_link_mode
itemAttachments.linkMode AS attachment_link_mode,
-- Fetch the folder name from the itemAttachments table
SUBSTR(itemAttachments.path, INSTR(itemAttachments.path, ':') + 1) AS folder_name
FROM
items
INNER JOIN itemData ON itemData.itemID = items.itemID
Expand All @@ -48,7 +50,6 @@ local query_items = [[
INNER JOIN itemTypes ON itemTypes.itemTypeID = items.itemTypeID
LEFT JOIN itemAttachments ON items.itemID = itemAttachments.parentItemID AND itemAttachments.contentType = 'application/pdf'
]]

local query_creators = [[
SELECT
DISTINCT items.key,
Expand Down
25 changes: 21 additions & 4 deletions lua/zotero/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ end
local function get_attachment_options(item)
local options = {}
if item.attachment and item.attachment.path then
table.insert(options, { type = 'pdf', path = item.attachment.path, link_mode = item.attachment.link_mode })
table.insert(options, {
type = 'pdf',
path = item.attachment.path,
link_mode = item.attachment.link_mode,
})
end
if item.DOI then
table.insert(options, { type = 'doi', url = 'https://doi.org/' .. item.DOI })
Expand All @@ -67,6 +71,7 @@ local function open_url(url, file_type)
local open_cmd
if file_type == 'pdf' and M.config.pdf_opener then
-- Use the custom PDF opener if specified
vim.notify('Opening PDF with: ' .. M.config.pdf_opener .. ' ' .. vim.fn.shellescape(url), vim.log.levels.INFO)
vim.fn.system(M.config.pdf_opener .. ' ' .. vim.fn.shellescape(url))
elseif vim.fn.has 'win32' == 1 then
open_cmd = 'start'
Expand All @@ -75,9 +80,9 @@ local function open_url(url, file_type)
else -- Assume Unix
open_cmd = 'xdg-open'
end
vim.notify('Opening URL with: ' .. open_cmd .. ' ' .. vim.fn.shellescape(url), vim.log.levels.INFO)
vim.fn.system(open_cmd .. ' ' .. vim.fn.shellescape(url))
end

local function open_in_zotero(item_key)
local zotero_url = 'zotero://select/library/items/' .. item_key
open_url(zotero_url)
Expand All @@ -90,10 +95,22 @@ local function open_attachment(item)
local file_path = choice.path
if choice.link_mode == 1 then -- 1 typically means stored file
local zotero_storage = vim.fn.expand(M.config.zotero_storage_path)
file_path = zotero_storage .. '/' .. file_path
-- Remove the ':storage' prefix from the path
file_path = file_path:gsub('^storage:', '')
-- Use a wildcard to search for the PDF file in subdirectories
local search_path = zotero_storage .. '/*/' .. file_path
local matches = vim.fn.glob(search_path, true, true) -- Returns a list of matching files
if #matches > 0 then
file_path = matches[1] -- Use the first match
else
vim.notify('File not found: ' .. search_path, vim.log.levels.ERROR)
return
end
end
-- Debug: Print the full path
vim.notify('Attempting to open PDF: ' .. file_path, vim.log.levels.INFO)
if file_path ~= 0 then
open_url(file_path)
open_url(file_path, 'pdf')
else
vim.notify('File not found: ' .. file_path, vim.log.levels.ERROR)
end
Expand Down

0 comments on commit 0eaff32

Please sign in to comment.