Skip to content

Commit

Permalink
fix: add pdf callback
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyeol-lee committed Jan 2, 2025
1 parent 14e0427 commit 2935c80
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Added `opts.follow_img_func` option for customizing how to handle image paths.
- Added better handling for undefined template fields, which will now be prompted for.
- Added `opts.follow_pdf_func` option for customizing how to handle pdf paths.

### Changed

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,15 @@ This is a complete list of all of the options that can be passed to `require("ob
-- vim.cmd(':silent exec "!start ' .. url .. '"') -- Windows
end,

-- Optional, by default when you use `:ObsidianFollowLink` on a link to an pdf
-- file it will be ignored but you can customize this behavior here.
---@param pdf string
follow_pdf_func = function(pdf)
os.execute('open "' .. pdf .. '"') -- For macOS
-- os.execute('xdg-open "' .. pdf .. '"') -- For Linux
-- os.execute('start "" "' .. pdf .. '"') -- For Windows
end,

-- Optional, set to true if you use the Obsidian Advanced URI plugin.
-- https://github.com/Vinzent03/obsidian-advanced-uri
use_advanced_uri = false,
Expand Down
9 changes: 9 additions & 0 deletions doc/obsidian.txt
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,15 @@ carefully and customize it to your needs:
-- vim.fn.jobstart({"xdg-open", url}) -- linux
-- vim.cmd(':silent exec "!start ' .. url .. '"') -- Windows
end,

-- Optional, by default when you use `:ObsidianFollowLink` on a link to an pdf
-- file it will be ignored but you can customize this behavior here.
---@param pdf string
follow_pdf_func = function(pdf)
os.execute('open "' .. pdf .. '"') -- For macOS
-- os.execute('xdg-open "' .. pdf .. '"') -- For Linux
-- os.execute('start "" "' .. pdf .. '"') -- For Windows
end,

-- Optional, set to true if you use the Obsidian Advanced URI plugin.
-- https://github.com/Vinzent03/obsidian-advanced-uri
Expand Down
9 changes: 9 additions & 0 deletions lua/obsidian/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,15 @@ Client.follow_link_async = function(self, link, opts)
return
end

if util.is_pdf(res.location) then
if self.opts.follow_pdf_func ~= nil then
self.opts.follow_pdf_func(res.location)
else
log.warn "This looks like an image path. You can customize the behavior of images with the 'follow_pdf_func' option."
end
return
end

if res.link_type == search.RefTypes.Wiki or res.link_type == search.RefTypes.WikiWithAlias then
-- Prompt to create a new note.
if util.confirm("Create new note '" .. res.location .. "'?") then
Expand Down
1 change: 1 addition & 0 deletions lua/obsidian/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ local config = {}
---@field preferred_link_style obsidian.config.LinkStyle
---@field follow_url_func fun(url: string)|?
---@field follow_img_func fun(img: string)|?
---@field follow_pdf_func fun(pdf: string)|?
---@field note_frontmatter_func (fun(note: obsidian.Note): table)|?
---@field disable_frontmatter (fun(fname: string?): boolean)|boolean|?
---@field completion obsidian.config.CompletionOpts
Expand Down
9 changes: 9 additions & 0 deletions lua/obsidian/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ util.is_img = function(s)
return false
end

util.is_pdf = function(s)
for _, suffix in ipairs { ".pdf" } do
if vim.endswith(s, suffix) then
return true
end
end
return false
end

-- This function removes a single backslash within double square brackets
util.unescape_single_backslash = function(text)
return text:gsub("(%[%[[^\\]+)\\(%|[^\\]+]])", "%1%2")
Expand Down

0 comments on commit 2935c80

Please sign in to comment.