Skip to content

Commit

Permalink
feat: detect and handle urls in :ObsidianFollowLink (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
pbogut authored Apr 20, 2023
1 parent a07e3fa commit 8c23830
Show file tree
Hide file tree
Showing 4 changed files with 30 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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,23 @@ vim.keymap.set(

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`

If you have the [Obsidian Advanced URI](https://github.com/Vinzent03/obsidian-advanced-uri) plugin enabled, the Obsidian editor can automatically navigate to the same line in the current NeoVim buffer. For files that are already open, it will update the cursor position within Obsidian's editor. To enable this feature, add `use_advanced_uri = true` to the setup options. For example:
Expand Down
9 changes: 9 additions & 0 deletions lua/obsidian/command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,15 @@ 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)
else
echo.warn "This looks like a URL. You can customize the behavior of URLs with the 'follow_url_func' option."
end
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 8c23830

Please sign in to comment.