Skip to content

Commit

Permalink
feat: detect and handle urls in :ObsidianFollowLink
Browse files Browse the repository at this point in the history
  • Loading branch information
pbogut committed Apr 20, 2023
1 parent a07e3fa commit 47c7227
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `open_app_foreground` option to open Obsidian.app in foreground on macOS.
- Added `:ObsidianTemplate` to insert a template, configurable using a `templates` table passed to `setup()`.
- Added support for following links in markdown format
- Added `follow_url_func` option to customize behaviour of following URLs

### Fixed

Expand All @@ -20,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `nvim-cmp` completion will search based on file names now too, not just contents.
- Fixed bug when `nvim-cmp` is not installed.
- Workaround error which prevented users from using `ObsidianOpen` when vault path was configured behind a link
- Detect URLs when following links and ignore them by default.

## [v1.8.0](https://github.com/epwalsh/obsidian.nvim/releases/tag/v1.8.0) - 2023-02-16

Expand Down
20 changes: 20 additions & 0 deletions doc/obsidian.txt
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,26 @@ The other benefit of doing this is that it will now work even if your cursor is
on the enclosing brackets (`[[` or `]]`) or the alias part of a reference (the
part after `|`).

CUSTOMIZING THE BEHAVIOR OF FOLLOWING URL LINKS

By default, when you use `:ObsidianFollowLink` on a link to an external URL it
will be ignored. However, you can customize this behavior by setting the
configuration option `follow_url_func` to a function that takes the URL as its
first argument.

For example, you might want to open it in the browser. Here's an example of
how you could implement this:

>lua
require("obsidian").setup({
follow_url_func = function(url)
-- Open the URL in the default web browser.
vim.fn.jobstart({"xdg-open", url})
end,
})

In this example, we use `vim.fn.jobstart` function to open the URL in the default
web browser.

NAVIGATE TO THE CURRENT LINE WHEN USING :OBSIDIANOPEN

Expand Down
8 changes: 8 additions & 0 deletions lua/obsidian/command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,14 @@ command.follow = function(client, _)
note_file_name = note_file_name:sub(1, note_file_name:find "|" - 1)
end

if note_file_name:match "^[%a%d]*%:%/%/" then
if client.opts.follow_url_func ~= nil then
client.opts.follow_url_func(note_file_name)
end
-- early break as we dont want to open urls as notes
return
end

if not note_file_name:match "%.md" then
note_file_name = note_file_name .. ".md"
end
Expand Down
2 changes: 2 additions & 0 deletions lua/obsidian/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ local config = {}
---@field templates.date_format string
---@field templates.time_format string
---@field note_id_func function|?
---@field follow_url_func function|?
---@field note_frontmatter_func function|?
---@field disable_frontmatter boolean|?
---@field completion obsidian.config.CompletionOpts
Expand All @@ -26,6 +27,7 @@ config.ClientOpts.default = function()
notes_subdir = nil,
templates = nil,
note_id_func = nil,
follow_url_func = nil,
note_frontmatter_func = nil,
disable_frontmatter = false,
completion = config.CompletionOpts.default(),
Expand Down

0 comments on commit 47c7227

Please sign in to comment.