diff --git a/CHANGELOG.md b/CHANGELOG.md index b585ed0e..e78592f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added `opts.follow_img_func` option for customizing how to handle image paths. - Added better handling for undefined template fields, which will now be prompted for. +- You can now set `wiki_link_func` to `use_name_only` to generate wiki links in the format `[[Foo]]`, ensuring compatibility with the Obsidian desktop app. ### Changed diff --git a/README.md b/README.md index d5d382b6..c744186c 100644 --- a/README.md +++ b/README.md @@ -337,6 +337,7 @@ This is a complete list of all of the options that can be passed to `require("ob -- * "prepend_note_id", e.g. '[[foo-bar|Foo Bar]]' -- * "prepend_note_path", e.g. '[[foo-bar.md|Foo Bar]]' -- * "use_path_only", e.g. '[[foo-bar.md]]' + -- * "use_name_only", e.g, '[[foo-bar]]' -- Or you can set it to a function that takes a table of options and returns a string, like this: wiki_link_func = function(opts) return require("obsidian.util").wiki_link_id_prefix(opts) diff --git a/lua/obsidian/config.lua b/lua/obsidian/config.lua index 4f3ae84d..588486e2 100644 --- a/lua/obsidian/config.lua +++ b/lua/obsidian/config.lua @@ -148,6 +148,8 @@ config.ClientOpts.normalize = function(opts, defaults) opts.wiki_link_func = util.wiki_link_path_only elseif opts.wiki_link_func == "use_alias_only" then opts.wiki_link_func = util.wiki_link_alias_only + elseif opts.wiki_link_func == "use_name_only" then + opts.wiki_link_func = util.wiki_link_name_only elseif type(opts.wiki_link_func) == "string" then error(string.format("invalid option '%s' for 'wiki_link_func'", opts.wiki_link_func)) end diff --git a/lua/obsidian/util.lua b/lua/obsidian/util.lua index 1db3d044..b46f2a0a 100644 --- a/lua/obsidian/util.lua +++ b/lua/obsidian/util.lua @@ -1022,6 +1022,23 @@ util.wiki_link_path_prefix = function(opts) end end +---@param opts { path: string, label: string, id: string|integer|?, anchor: obsidian.note.HeaderAnchor|?, block: obsidian.note.Block|? } +---@return string +util.wiki_link_name_only = function(opts) + local header_or_block = "" + if opts.anchor then + header_or_block = string.format("#%s", opts.anchor.header) + elseif opts.block then + header_or_block = string.format("#%s", opts.block.id) + end + local name = opts.path:gsub("%.md", "") + if opts.label ~= name then + return string.format("[[%s%s|%s]]", name, header_or_block, opts.label) + else + return string.format("[[%s%s]]", name, header_or_block) + end +end + ---@param opts { path: string, label: string, id: string|integer|?, anchor: obsidian.note.HeaderAnchor|?, block: obsidian.note.Block|? } ---@return string util.wiki_link_id_prefix = function(opts) diff --git a/test/obsidian/util_spec.lua b/test/obsidian/util_spec.lua index c4cb7e0e..27304973 100644 --- a/test/obsidian/util_spec.lua +++ b/test/obsidian/util_spec.lua @@ -365,6 +365,24 @@ describe("util.wiki_link_path_only()", function() end) end) +describe("util.wiki_link_name_only()", function() + it("should work without an anchor link", function() + assert.equals("[[123-foo|Foo]]", util.wiki_link_name_only { path = "123-foo.md", id = "123-foo", label = "Foo" }) + end) + + it("should work with an anchor link", function() + assert.equals( + "[[123-foo#Heading|Foo]]", + util.wiki_link_name_only { + path = "123-foo.md", + id = "123-foo", + label = "Foo", + anchor = { anchor = "#heading", header = "Heading", level = 1, line = 1 }, + } + ) + end) +end) + describe("util.markdown_link()", function() it("should work without an anchor link", function() assert.equals("[Foo](123-foo.md)", util.markdown_link { path = "123-foo.md", id = "123-foo", label = "Foo" })