Skip to content

Commit 47f0074

Browse files
committed
Add opts.follow_img_func option
This adds the ability to preview images on Mac OS using `qlmanage`. See the example in the README. (#411) See also #660.
1 parent ee298fd commit 47f0074

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
### Added
11+
12+
- Added `opts.follow_img_func` option for customizing how to handle image paths.
13+
1014
## [v3.9.0](https://github.com/epwalsh/obsidian.nvim/releases/tag/v3.9.0) - 2024-07-11
1115

1216
### Added

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,15 @@ This is a complete list of all of the options that can be passed to `require("ob
400400
-- vim.cmd(':silent exec "!start ' .. url .. '"') -- Windows
401401
end,
402402

403+
-- Optional, by default when you use `:ObsidianFollowLink` on a link to an image
404+
-- file it will be ignored but you can customize this behavior here.
405+
---@param img string
406+
follow_url_func = function(img)
407+
vim.fn.jobstart { "qlmanage", "-p", img } -- Mac OS quick look preview
408+
-- vim.fn.jobstart({"xdg-open", url}) -- linux
409+
-- vim.cmd(':silent exec "!start ' .. url .. '"') -- Windows
410+
end,
411+
403412
-- Optional, set to true if you use the Obsidian Advanced URI plugin.
404413
-- https://github.com/Vinzent03/obsidian-advanced-uri
405414
use_advanced_uri = false,

lua/obsidian/client.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,15 @@ Client.follow_link_async = function(self, link, opts)
875875
return self:open_note(res.note, { line = res.line, col = res.col, open_strategy = opts.open_strategy })
876876
end
877877

878+
if util.is_img(res.location) then
879+
if self.opts.follow_img_func ~= nil then
880+
self.opts.follow_img_func(res.location)
881+
else
882+
log.warn "This looks like an image path. You can customize the behavior of images with the 'follow_img_func' option."
883+
end
884+
return
885+
end
886+
878887
if res.link_type == search.RefTypes.Wiki or res.link_type == search.RefTypes.WikiWithAlias then
879888
-- Prompt to create a new note.
880889
if util.confirm("Create new note '" .. res.location .. "'?") then

lua/obsidian/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ local config = {}
1616
---@field markdown_link_func (fun(opts: {path: string, label: string, id: string|?}): string)
1717
---@field preferred_link_style obsidian.config.LinkStyle
1818
---@field follow_url_func fun(url: string)|?
19+
---@field follow_img_func fun(img: string)|?
1920
---@field image_name_func (fun(): string)|?
2021
---@field note_frontmatter_func (fun(note: obsidian.Note): table)|?
2122
---@field disable_frontmatter (fun(fname: string?): boolean)|boolean|?

lua/obsidian/util.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,15 @@ util.is_url = function(s)
193193
end
194194
end
195195

196+
util.is_img = function(s)
197+
for _, suffix in ipairs { ".png", ".jpg", ".jpeg", ".heic", ".gif", ".svg", ".ico" } do
198+
if vim.endswith(s, suffix) then
199+
return true
200+
end
201+
end
202+
return false
203+
end
204+
196205
-- This function removes a single backslash within double square brackets
197206
util.unescape_single_backslash = function(text)
198207
return text:gsub("(%[%[[^\\]+)\\(%|[^\\]+]])", "%1%2")

0 commit comments

Comments
 (0)