Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for external links with angled brackets #711

Open
protivinsky opened this issue Sep 13, 2024 · 0 comments
Open

Support for external links with angled brackets #711

protivinsky opened this issue Sep 13, 2024 · 0 comments

Comments

@protivinsky
Copy link

protivinsky commented Sep 13, 2024

🚀 The feature, motivation and pitch

Obsidian allows for escaping the URL by enclosing it in angle brackets, as described in the documentation.

It escapes not only spaces, but also parenthesis - I found it very convenient as I am using Obsidian for managing bibliography and I store PDF named based on APA citation standard, i.e. "Surname, I. (Year). Title of the Paper.pdf". With Obsidian, I can use "file:///path/on/drive" to link the PDFs from notes with markdown notes (afaik wiki links are not supported for external links), then I can just open the PDFs directly from obsidian or neovim by following the link.

In Obsidian app, this work well even with files containing parenthesis in the title, I just need to use the escaped markdown syntax with angled brackets. However this does not work in neovim, as the plugins detect end of link only with regex for closing parenthesis. Hence the file cannot be opened directly from neovim, as it truncates the link.

It is fairly easy to fix, I just created an additional regex for escaped markdown with angled brackets and everything works as expected. See the diffs for the changes below, I am happy to turn it into a merge request if it might be useful others.

Btw thanks for the plugin, it is awesome.

Alternatives

Alternatives "solutions" / workarounds:

  • Do not use parenthesis in titles. I agree it is a sensible approach, but I still found it easier to read and skim through the full human readable names.
  • Use wiki links for external files in neovim - this would work fine there even with titles with parentheses, but unfortunately does not work in the Obsidian app. So I would be able to open my files only from neovim with this approach.

Additional context

See the screenshot showing the issue with the non-escaped markdown syntax. The second link with angled brackets is also broken with this plugin, however it is correctly recognized with the diffs proposed below.

image

Suggested fix:

+++ b/lua/obsidian/search.lua
@@ -18,6 +18,7 @@ M._FIND_CMD = compat.flatten { M._BASE_CMD, "--files" }
 M.RefTypes = {
   WikiWithAlias = "WikiWithAlias",
   Wiki = "Wiki",
+  EscapedMarkdown = "EscapedMarkdown",
   Markdown = "Markdown",
   NakedUrl = "NakedUrl",
   FileUrl = "FileUrl",
@@ -40,6 +41,7 @@ M.Patterns = {
   -- References
   WikiWithAlias = "%[%[[^][%|]+%|[^%]]+%]%]", -- [[xxx|yyy]]
   Wiki = "%[%[[^][%|]+%]%]", -- [[xxx]]
+  EscapedMarkdown = "%[[^][]+%]%(<[^>]+>%)", -- [yyy](<xxx>)
   Markdown = "%[[^][]+%]%([^%)]+%)", -- [yyy](xxx)
   NakedUrl = "https?://[a-zA-Z0-9._-]+[a-zA-Z0-9._#/=&?:+%%-]+[a-zA-Z0-9/]", -- https://xyz.com
   FileUrl = "file:/[/{2}]?.*", -- file:///
@@ -156,7 +158,7 @@ end
 M.find_refs = function(s, opts)
   opts = opts and opts or {}

-  local pattern_names = { M.RefTypes.WikiWithAlias, M.RefTypes.Wiki, M.RefTypes.Markdown }
+  local pattern_names = { M.RefTypes.WikiWithAlias, M.RefTypes.Wiki, M.RefTypes.EscapedMarkdown, M.RefTypes.Markdown }
   if opts.include_naked_urls then
     pattern_names[#pattern_names + 1] = M.RefTypes.NakedUrl
   end
diff --git a/lua/obsidian/util.lua b/lua/obsidian/util.lua
index 1db3d04..da77979 100644
--- a/lua/obsidian/util.lua
+++ b/lua/obsidian/util.lua
@@ -688,7 +688,10 @@ util.parse_link = function(link, opts)
   end

   local link_location, link_name
-  if link_type == search.RefTypes.Markdown then
+  if link_type == search.RefTypes.EscapedMarkdown then
+    link_location = link:gsub("^%[(.-)%]%(<(.*)>%)$", "%2")
+    link_name = link:gsub("^%[(.-)%]%(<(.*)>%)$", "%1")
+  elseif link_type == search.RefTypes.Markdown then
     link_location = link:gsub("^%[(.-)%]%((.*)%)$", "%2")
     link_name = link:gsub("^%[(.-)%]%((.*)%)$", "%1")
   elseif link_type == search.RefTypes.NakedUrl then
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant