From 8c238300c541b0dc6fa294af038c8ac2a569d574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bogut?= Date: Thu, 20 Apr 2023 22:15:43 +0200 Subject: [PATCH] feat: detect and handle urls in :ObsidianFollowLink (#123) --- CHANGELOG.md | 2 ++ README.md | 17 +++++++++++++++++ lua/obsidian/command.lua | 9 +++++++++ lua/obsidian/config.lua | 2 ++ 4 files changed, 30 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0becf2e85..c8e7d36c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/README.md b/README.md index 0f500fa0f..ebedc8349 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/lua/obsidian/command.lua b/lua/obsidian/command.lua index c6d888df3..8b4b23f91 100644 --- a/lua/obsidian/command.lua +++ b/lua/obsidian/command.lua @@ -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 diff --git a/lua/obsidian/config.lua b/lua/obsidian/config.lua index a75030af8..1cf6ca24e 100644 --- a/lua/obsidian/config.lua +++ b/lua/obsidian/config.lua @@ -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 @@ -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(),