diff --git a/lua/obsidian/command.lua b/lua/obsidian/command.lua index 5f3446a5a..ea1c6e573 100644 --- a/lua/obsidian/command.lua +++ b/lua/obsidian/command.lua @@ -546,6 +546,9 @@ command.follow = function(client, _) end local note_name = current_line:sub(open, close) + + note_name = util.unescape_single_backslash(note_name) + if note_name:match "^%[.-%]%(.*%)$" then -- transform markdown link to wiki link note_name = note_name:gsub("^%[(.-)%]%((.*)%)$", "%2|%1") diff --git a/lua/obsidian/util.lua b/lua/obsidian/util.lua index b13ffb308..e926b542a 100644 --- a/lua/obsidian/util.lua +++ b/lua/obsidian/util.lua @@ -660,4 +660,9 @@ util.gf_passthrough = function() end end +-- This function removes a single backslash within double square brackets +util.unescape_single_backslash = function(text) + return text:gsub("(%[%[[^\\]+)\\(%|[^\\]+]])", "%1%2") +end + return util diff --git a/test/obsidian/util_spec.lua b/test/obsidian/util_spec.lua index 1dd459eb8..99d829b62 100644 --- a/test/obsidian/util_spec.lua +++ b/test/obsidian/util_spec.lua @@ -89,4 +89,9 @@ describe("obsidian.util", function() assert.equal(util.escape_magic_characters "foo?bar", "foo%?bar") assert.equal(util.escape_magic_characters "foo%bar", "foo%%bar") end) + it("should correctly remove single backslash", function() + -- [[123\|NOTE1]] should get [[123|NOTE1]] in markdown file + -- in lua, it needs to be with double backslash '\\' + assert.equals(util.unescape_single_backslash "[[foo\\|bar]]", "[[foo|bar]]") + end) end)