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

fix: support pdf #803

Open
wants to merge 2 commits into
base: main
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The log.warn should read:
"This looks like a pdf path. You can customize the behavior of pdfs with the 'follow_pdf_func' option."

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, updated

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 a pdf path. You can customize the behavior of pdfs 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
Loading