mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
feat: allow gx to function for markdown links
In other words, `gx` works regardless of where it was used in `[...](https://...)`. This only works on markdown buffers. Co-authored-by: ribru17 <ribru17@gmail.com>
This commit is contained in:
parent
6dc62c2e2b
commit
f864b68c5b
@ -107,22 +107,25 @@ do
|
|||||||
vim.inspect(cmd.cmd)
|
vim.inspect(cmd.cmd)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
return err
|
||||||
if err then
|
|
||||||
vim.notify(err, vim.log.levels.ERROR)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local gx_desc =
|
local gx_desc =
|
||||||
'Opens filepath or URI under cursor with the system handler (file explorer, web browser, …)'
|
'Opens filepath or URI under cursor with the system handler (file explorer, web browser, …)'
|
||||||
vim.keymap.set({ 'n' }, 'gx', function()
|
vim.keymap.set({ 'n' }, 'gx', function()
|
||||||
do_open(vim.fn.expand('<cfile>'))
|
local err = do_open(require('vim.ui')._get_url())
|
||||||
|
if err then
|
||||||
|
vim.notify(err, vim.log.levels.ERROR)
|
||||||
|
end
|
||||||
end, { desc = gx_desc })
|
end, { desc = gx_desc })
|
||||||
vim.keymap.set({ 'x' }, 'gx', function()
|
vim.keymap.set({ 'x' }, 'gx', function()
|
||||||
local lines =
|
local lines =
|
||||||
vim.fn.getregion(vim.fn.getpos('.'), vim.fn.getpos('v'), { type = vim.fn.mode() })
|
vim.fn.getregion(vim.fn.getpos('.'), vim.fn.getpos('v'), { type = vim.fn.mode() })
|
||||||
-- Trim whitespace on each line and concatenate.
|
-- Trim whitespace on each line and concatenate.
|
||||||
do_open(table.concat(vim.iter(lines):map(vim.trim):totable()))
|
local err = do_open(table.concat(vim.iter(lines):map(vim.trim):totable()))
|
||||||
|
if err then
|
||||||
|
vim.notify(err, vim.log.levels.ERROR)
|
||||||
|
end
|
||||||
end, { desc = gx_desc })
|
end, { desc = gx_desc })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -162,4 +162,24 @@ function M.open(path)
|
|||||||
return vim.system(cmd, { text = true, detach = true }), nil
|
return vim.system(cmd, { text = true, detach = true }), nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Gets the URL at cursor, if any.
|
||||||
|
function M._get_url()
|
||||||
|
if vim.bo.filetype == 'markdown' then
|
||||||
|
local range = vim.api.nvim_win_get_cursor(0)
|
||||||
|
vim.treesitter.get_parser():parse(range)
|
||||||
|
-- marking the node as `markdown_inline` is required. Setting it to `markdown` does not
|
||||||
|
-- work.
|
||||||
|
local current_node = vim.treesitter.get_node { lang = 'markdown_inline' }
|
||||||
|
while current_node do
|
||||||
|
local type = current_node:type()
|
||||||
|
if type == 'inline_link' or type == 'image' then
|
||||||
|
local child = assert(current_node:named_child(1))
|
||||||
|
return vim.treesitter.get_node_text(child, 0)
|
||||||
|
end
|
||||||
|
current_node = current_node:parent()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return vim.fn.expand('<cfile>')
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
Loading…
Reference in New Issue
Block a user