Skip to content

Commit

Permalink
feat: support unespacing backslash when following wikilinks (#211)
Browse files Browse the repository at this point in the history
* feat: support unespacing backslash when following wikilinks

Allow user to escape wikilink in certain case, e.g. within a table.

| Column1 | Column2            | Column3 || ------- | ------------------ | ------- || Item1   | [[NOTE-ID\|TITLE]] | Item1   |

* chore: add test case for removing single backslash

* chore: use assert.equals instead of assert.equal
  • Loading branch information
y3owk1n authored Oct 26, 2023
1 parent 3d08b8d commit 1149c5b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lua/obsidian/command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 5 additions & 0 deletions lua/obsidian/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions test/obsidian/util_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 1149c5b

Please sign in to comment.