diff --git a/CHANGELOG.md b/CHANGELOG.md index b585ed0e2..3644b711d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index d5d382b62..d13e519f9 100644 --- a/README.md +++ b/README.md @@ -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, diff --git a/doc/obsidian.txt b/doc/obsidian.txt index fc0539948..08ae61287 100644 --- a/doc/obsidian.txt +++ b/doc/obsidian.txt @@ -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 diff --git a/lua/obsidian/client.lua b/lua/obsidian/client.lua index 00c09c77f..361a3b656 100644 --- a/lua/obsidian/client.lua +++ b/lua/obsidian/client.lua @@ -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 diff --git a/lua/obsidian/config.lua b/lua/obsidian/config.lua index 4f3ae84de..691607216 100644 --- a/lua/obsidian/config.lua +++ b/lua/obsidian/config.lua @@ -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 diff --git a/lua/obsidian/util.lua b/lua/obsidian/util.lua index 1db3d044e..02e1f8dc8 100644 --- a/lua/obsidian/util.lua +++ b/lua/obsidian/util.lua @@ -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")