Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- New obsidian.config.UrlPatternSpec to configure additional URL patterns that function follow_url_func can handle
- Allow custom directory and ID logic for templates
- When filling out a template with user-provided substitution functions, pass a "context" object to each invocation so that users can respond accordingly.
- Added `obsidian.InsertTemplateContext` and `obsidian.CloneTemplateContext` as these new "context" objects.
Expand Down
19 changes: 18 additions & 1 deletion doc/obsidian.txt
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,24 @@ Click to see configuration options ~
--- See: https://github.com/obsidian-nvim/obsidian.nvim/wiki/Template#customizations
customizations = {},
},

-- Optional, URL patterns that can be handled by function `follow_img_func`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

follow_url_func

url_patterns = {
-- an arbitrary number of patterns can be provided
{
-- example for an UUID like patttern, allows to open links like
-- [UUID markdown](uuid://abcdef-1234-5678-90af-abcdef)
-- [[uuid://abcdef-1234-5678-90af-abcdef|UUID wikki]]
name = "URL-UUID",
pattern = "uuid://[0-9a-fA-F]+%-[0-9a-fA-F]+%-[0-9a-fA-F]+%-[0-9a-fA-F]+%-[0-9a-fA-F]+",
},
{
-- example for an app specific patttern, allows to open links like
-- [App markdown](app:///data/testData)
-- [[app:///data/testData|App wikki]]
name = "App",
pattern = "app:///data/[a-zA-Z]+",
},
},
-- Sets how you follow URLs
---@param url string
follow_url_func = function(url)
Expand Down
8 changes: 8 additions & 0 deletions lua/obsidian/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local config = {}
---@field wiki_link_func? fun(opts: {path: string, label: string, id: string|?}): string
---@field markdown_link_func? fun(opts: {path: string, label: string, id: string|?}): string
---@field preferred_link_style? obsidian.config.LinkStyle
---@field url_patterns? obsidian.config.UrlPatternSpec[]
---@field follow_url_func? fun(url: string)
---@field follow_img_func? fun(img: string)
---@field note_frontmatter_func? (fun(note: obsidian.Note): table)
Expand Down Expand Up @@ -47,6 +48,7 @@ local config = {}
---@field wiki_link_func (fun(opts: {path: string, label: string, id: string|?}): string)
---@field markdown_link_func (fun(opts: {path: string, label: string, id: string|?}): string)
---@field preferred_link_style obsidian.config.LinkStyle
---@field url_patterns? obsidian.config.UrlPatternSpec[]
---@field follow_url_func fun(url: string)|?
---@field follow_img_func fun(img: string)|?
---@field note_frontmatter_func (fun(note: obsidian.Note): table)|?
Expand All @@ -69,6 +71,11 @@ local config = {}
---@field checkbox obsidian.config.CheckboxOpts
---@field comment obsidian.config.CommentOpts

---@class obsidian.config.UrlPatternSpec
---
---@field name string
---@field pattern string

---@enum obsidian.config.OpenStrategy
config.OpenStrategy = {
current = "current",
Expand Down Expand Up @@ -119,6 +126,7 @@ config.default = {
wiki_link_func = require("obsidian.builtin").wiki_link_id_prefix,
markdown_link_func = require("obsidian.builtin").markdown_link,
preferred_link_style = config.LinkStyle.wiki,
url_patterns = {},
follow_url_func = vim.ui.open,
follow_img_func = vim.ui.open,
note_frontmatter_func = nil,
Expand Down
9 changes: 8 additions & 1 deletion lua/obsidian/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,14 @@ util.is_url = function(s)
then
return true
else
return false
local found = false
for _, url in ipairs(Obsidian.opts.url_patterns) do
-- @type url obsidian.config.UrlPatternSpec
if string.match(vim.trim(s), "^" .. url.pattern .. "$") then
found = true
end
end
return found
end
end

Expand Down
55 changes: 55 additions & 0 deletions tests/test_url_patterns.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
local M = require "obsidian.util"
local Path = require "obsidian.path"
local new_set, eq = MiniTest.new_set, MiniTest.expect.equality

local T = new_set {
hooks = {
pre_case = function()
local dir = Path.temp { suffix = "-obsidian" }
dir:mkdir { parents = true }
require("obsidian").setup {
url_patterns = {
{
name = "URL-UUID",
pattern = "uuid://[0-9a-fA-F]+%-[0-9a-fA-F]+%-[0-9a-fA-F]+%-[0-9a-fA-F]+%-[0-9a-fA-F]+",
},
{
name = "App",
pattern = "app:///data/[a-zA-Z]+",
},
},
workspaces = { {
path = tostring(dir),
} },
}
end,
},
}

T["url_patterns"] = new_set()

T["url_patterns"]["should identify basic URLs"] = function()
eq(true, M.is_url "https://example.com")
end

T["url_patterns"]["should identify semantic scholar API URLS"] = function()
eq(true, M.is_url "https://api.semanticscholar.org/CorpusID:235829052")
end

T["url_patterns"]["should identify 'mailto' URLS"] = function()
eq(true, M.is_url "mailto:[email protected]")
end

T["url_patterns"]["should identify URL-UUID"] = function()
eq(true, M.is_url "uuid://abcdef-1234-5678-90af-abcdef")
end

T["url_patterns"]["should identify App-URL"] = function()
eq(true, M.is_url "app:///data/testData")
end

T["url_patterns"]["should not identify unknown URL pattern"] = function()
eq(false, M.is_url "unknown://some.unknown.url")
end

return T
4 changes: 4 additions & 0 deletions tests/test_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ T["is_url"]["should identify 'mailto' URLS"] = function()
eq(true, M.is_url "mailto:[email protected]")
end

T["is_url"]["uncofigured URLs should not be identified"] = function()
eq(false, M.is_url "some_protocoll://data")
end

T["is_checkbox"] = new_set()

T["is_checkbox"]["should return true for valid checkbox list items"] = function()
Expand Down
Loading