From 95714dc7e746aa2cd7979fbdfb548c8b81a9a85e Mon Sep 17 00:00:00 2001 From: BeltranLeo Date: Sat, 28 Sep 2024 01:01:52 -0300 Subject: [PATCH 1/7] Added `use_name_only` --- lua/obsidian/config.lua | 2 ++ lua/obsidian/util.lua | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) 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..9bee0022 100644 --- a/lua/obsidian/util.lua +++ b/lua/obsidian/util.lua @@ -1022,6 +1022,26 @@ 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 anchor = "" + local header = "" + if opts.anchor then + anchor = opts.anchor.anchor + header = util.format_anchor_label(opts.anchor) + elseif opts.block then + anchor = "#" .. opts.block.id + header = "#" .. opts.block.id + end + local name = opts.path:gsub("%.md", "") + if opts.label ~= name then + return string.format("[[%s%s|%s%s]]", name, anchor, opts.label, header) + else + return string.format("[[%s%s]]", name, anchor) + 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) From 9a08ace47975c4fa39235489d55c150c60433d03 Mon Sep 17 00:00:00 2001 From: BeltranLeo Date: Sat, 28 Sep 2024 01:02:18 -0300 Subject: [PATCH 2/7] Added tests for `use_name_only` --- test/obsidian/util_spec.lua | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/obsidian/util_spec.lua b/test/obsidian/util_spec.lua index c4cb7e0e..37c73775 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]]", 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]]", + 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" }) From bb69db31183d48a5a2d1a795abf0edec00e3bdac Mon Sep 17 00:00:00 2001 From: BeltranLeo Date: Sat, 28 Sep 2024 01:04:00 -0300 Subject: [PATCH 3/7] Added documentation for `use_name_only` --- README.md | 1 + doc/obsidian.txt | 1 + 2 files changed, 2 insertions(+) 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/doc/obsidian.txt b/doc/obsidian.txt index fc053994..2f60d2bc 100644 --- a/doc/obsidian.txt +++ b/doc/obsidian.txt @@ -393,6 +393,7 @@ carefully and customize it to your needs: -- * "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) From a6f7309af0e5f5e909756e9ff729fc4f2ec3615c Mon Sep 17 00:00:00 2001 From: BeltranLeo Date: Sat, 28 Sep 2024 01:25:07 -0300 Subject: [PATCH 4/7] Removed doc/obsidian.txt reference and edited CHANGELOG.md --- CHANGELOG.md | 1 + doc/obsidian.txt | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) 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/doc/obsidian.txt b/doc/obsidian.txt index 2f60d2bc..fc053994 100644 --- a/doc/obsidian.txt +++ b/doc/obsidian.txt @@ -393,7 +393,6 @@ carefully and customize it to your needs: -- * "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) From 6956ae4b06919a9e8dbdbd28f939aeb7289d2060 Mon Sep 17 00:00:00 2001 From: BeltranLeo Date: Sat, 28 Sep 2024 01:54:54 -0300 Subject: [PATCH 5/7] Fixed some issues to get expected behavior --- lua/obsidian/util.lua | 13 +++++-------- test/obsidian/util_spec.lua | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/lua/obsidian/util.lua b/lua/obsidian/util.lua index 9bee0022..3fe5cbd0 100644 --- a/lua/obsidian/util.lua +++ b/lua/obsidian/util.lua @@ -1025,20 +1025,17 @@ 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 anchor = "" - local header = "" + local header_or_block = "" if opts.anchor then - anchor = opts.anchor.anchor - header = util.format_anchor_label(opts.anchor) + header_or_block = opts.anchor.anchor elseif opts.block then - anchor = "#" .. opts.block.id - header = "#" .. opts.block.id + 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%s]]", name, anchor, opts.label, header) + return string.format("[[%s%s|%s]]", name, header_or_block, opts.label) else - return string.format("[[%s%s]]", name, anchor) + return string.format("[[%s%s]]", name, header_or_block) end end diff --git a/test/obsidian/util_spec.lua b/test/obsidian/util_spec.lua index 37c73775..9e8d9b7a 100644 --- a/test/obsidian/util_spec.lua +++ b/test/obsidian/util_spec.lua @@ -367,7 +367,7 @@ end) describe("util.wiki_link_name_only()", function() it("should work without an anchor link", function() - assert.equals("[[123-foo]]", util.wiki_link_name_only { path = "123-foo.md", id = "123-foo", label = "Foo" }) + 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() From e66ba317c0c20e584711b10c696e1a04b41e9fc3 Mon Sep 17 00:00:00 2001 From: BeltranLeo Date: Sat, 28 Sep 2024 02:20:30 -0300 Subject: [PATCH 6/7] Fixed anchor appearing instead of header --- lua/obsidian/util.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/obsidian/util.lua b/lua/obsidian/util.lua index 3fe5cbd0..b46f2a0a 100644 --- a/lua/obsidian/util.lua +++ b/lua/obsidian/util.lua @@ -1027,7 +1027,7 @@ end util.wiki_link_name_only = function(opts) local header_or_block = "" if opts.anchor then - header_or_block = opts.anchor.anchor + header_or_block = string.format("#%s", opts.anchor.header) elseif opts.block then header_or_block = string.format("#%s", opts.block.id) end From 630ddb01d5e89d25a48bf6c948e5bb82cd95979a Mon Sep 17 00:00:00 2001 From: BeltranLeo Date: Sat, 28 Sep 2024 02:30:14 -0300 Subject: [PATCH 7/7] Fix tests to ensure correct results for wiki_link_name_only function --- test/obsidian/util_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/obsidian/util_spec.lua b/test/obsidian/util_spec.lua index 9e8d9b7a..27304973 100644 --- a/test/obsidian/util_spec.lua +++ b/test/obsidian/util_spec.lua @@ -372,7 +372,7 @@ describe("util.wiki_link_name_only()", function() it("should work with an anchor link", function() assert.equals( - "[[123-foo#heading]]", + "[[123-foo#Heading|Foo]]", util.wiki_link_name_only { path = "123-foo.md", id = "123-foo",