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

feat: add support to follow links in markdown format #122

Merged
merged 1 commit into from
Apr 17, 2023

Conversation

pbogut
Copy link
Contributor

@pbogut pbogut commented Apr 8, 2023

Currently this plugin supports links in wiki format like this:

[[some/file.md|My file]]

but markdown links looks bit different

[My file](some/file.md)

This PR will add support for markdown links to ObsidianFollowLink command as well as utils.cursor_on_markdown_link function.

local note_name = current_line:sub(open, close)
if note_name:match('^%[.-%]%(.*%)$') then
-- transform markdown link to wiki link
note_name = note_name:gsub("^%[(.-)%]%((.*)%)$", "%2|%1")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I'm changing markdown link to look like wiki link. I think that will be better for compatibility. If someone has custom note_id_func it will still work nicely with markdown links.

@pbogut pbogut force-pushed the follow-markdown-links branch 3 times, most recently from 2909a8f to f7b4f0b Compare April 8, 2023 20:12
Copy link
Owner

@epwalsh epwalsh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @pbogut, this looks good overall, but I just noticed one issue. If you have a markdown link that's actually a URL, :ObsidianFollowLink will try to create a note based on that URL. That's not ideal. One way we could handle this is to not create notes when the link is markdown style as opposed to Wiki style. Open to other suggestions as well. I just think that URL detection can be tricky.

lua/obsidian/util.lua Outdated Show resolved Hide resolved
lua/obsidian/util.lua Outdated Show resolved Hide resolved
lua/obsidian/util.lua Outdated Show resolved Hide resolved
@pbogut
Copy link
Contributor Author

pbogut commented Apr 15, 2023

If you have a markdown link that's actually a URL, :ObsidianFollowLink will try to create a note based on that URL.

Isn't that an issue already? I mean, isn't [[https://example.com|My Example]] valid wikilink?
Anyway, maybe we could look for ^[a-z0-9]*%:%/%/ ^[%a%d]*%:%/%/ and if that matches we consider it a link (let it be http://, file://, ftp:// or otherwise) and ignore? I guess we can either ignore it or pass it through to the system to open it.
I don't want to mess with that, especially because I'm only using one OS, but maybe I could add an option where user could set up their own system command that should be run for links and by default they would be ignored?

So my questions:

  1. Is matching against ^[a-z0-9]*%:%/%/ ^[%a%d]*%:%/%/ acceptable for you to detect URI's?
  2. Can I add an option to run some command when link is detected?
    It would be something like:
require("obsidian").setup({
  url_handle_cmd = "gio open {}",
})

-- Edit:

Or even better, add hook instead:

require("obsidian").setup({
  follow_url_func = function(url) --[[ do what you fancy ]] end,
})

@epwalsh
Copy link
Owner

epwalsh commented Apr 17, 2023

Isn't that an issue already? I mean, isn't [[https://example.com|My Example]] valid wikilink?

I suppose you're right. We should probably handle URLs better at some point, but that seems out-of-scope for this PR.

Copy link
Owner

@epwalsh epwalsh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks @pbogut :)

@epwalsh epwalsh merged commit 9dd1930 into epwalsh:main Apr 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants