jghauser / follow-md-links.nvim

Easily follow markdown links with this neovim plugin
GNU General Public License v3.0
143 stars 16 forks source link

Links without `.md` extensions fail to resolve #26

Closed lingling9000 closed 3 months ago

lingling9000 commented 3 months ago

Resolving markdown links without extension lead to following error:

E5108: Error executing lua ...e/nvim/lazy/follow-md-links.nvim/lua/follow-md-links.lua:99: attempt to concatenate local 'modified_link' (a nil value)    
stack traceback:                                                                                                                                         
        ...e/nvim/lazy/follow-md-links.nvim/lua/follow-md-links.lua:99: in function 'follow_local_link'                                                  
        ...e/nvim/lazy/follow-md-links.nvim/lua/follow-md-links.lua:128: in function 'follow_link'                                                       
        [string ":lua"]:1: in main chunk

The error happens in the function follow_local_link():

https://github.com/jghauser/follow-md-links.nvim/blob/a5e023f70e704197fea1c83b9e4b09333c07e178/lua/follow-md-links.lua#L93-L111

Lets say we have a file test.md and we want to link to that file, we have at least the following 4 cases:

  1. [test](test) -> fails
  2. [test](test.md) -> works
  3. [test](test:3) -> nothing happens
  4. [test](test.md:2) -> works

For case 1 both if statements in L96 and L98 are true and a string concatenation of modified_link and ".md" is tried. But as modified_link is a nil value (defined as in L94) this fails with the error described above. The fix for this is to concatenate with the link variable:

modified_link = link .. ".md"

But then the code for parsing the line number is never reached and that is why `test can't be resolved. To fix that a further refactor is necessary.

I'll file a pull request with the changes.