Skip to content

Commit

Permalink
feat: added custom handlers for markdown links and help tags
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Oct 26, 2022
1 parent 6ea06c9 commit 37e7203
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lua/noice/text/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,44 @@ function M.format(message, text)
end
end

function M.keys(buf)
if vim.b[buf].markdown_keys then
return
end

local function map(lhs)
vim.keymap.set("n", lhs, function()
local line = vim.api.nvim_get_current_line()
local pos = vim.api.nvim_win_get_cursor(0)
local col = pos[2] + 1

for pattern, handler in pairs(require("noice.config").options.markdown.hover) do
local from = 1
local to, url
while from do
from, to, url = line:find(pattern, from)
if from and col >= from and col <= to then
return handler(url)
end
end
end
vim.api.nvim_feedkeys(lhs, "n", false)
end, { buffer = buf, silent = true })
end

map("gx")
map("K")

vim.b[buf].markdown_keys = true
end

---@param message NoiceMessage
function M.horizontal_line(message)
message:append(NoiceText("", {
virt_text_win_col = 0,
virt_text = { { string.rep("", vim.go.columns), "@punctuation.special.markdown" } },
priority = 100,
}))
end

return M
21 changes: 21 additions & 0 deletions lua/noice/util/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ function M.once(fn)
end
end

function M.open(uri)
local cmd
if vim.fn.has("win32") == 1 then
cmd = { "cmd.exe", "/c", "start", '""', vim.fn.shellescape(uri) }
elseif vim.fn.has("macunix") == 1 then
cmd = { "open", uri }
else
cmd = { "xdg-open", uri }
end

local ret = vim.fn.system(cmd)
if vim.v.shell_error ~= 0 then
local msg = {
"Failed to open uri",
ret,
vim.inspect(cmd),
}
vim.notify(table.concat(msg, "\n"), vim.log.levels.ERROR)
end
end

function M.tag(buf, tag)
local ft = vim.api.nvim_buf_get_option(buf, "filetype")

Expand Down
5 changes: 5 additions & 0 deletions lua/noice/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local ConfigViews = require("noice.config.views")
local Util = require("noice.util")
local Object = require("nui.object")
local Format = require("noice.text.format")
local Markdown = require("noice.text.markdown")

---@class NoiceViewBaseOptions
---@field buf_options? table<string,any>
Expand Down Expand Up @@ -208,6 +209,10 @@ function View:render(buf, opts)
require("nui.utils")._.set_buf_options(buf, self._opts.buf_options)
end

if self._opts.lang == "markdown" then
Markdown.keys(buf)
end

if self._opts.lang and not vim.b[buf].ts_highlight then
if not pcall(vim.treesitter.start, buf, self._opts.lang) then
vim.bo[buf].syntax = self._opts.lang
Expand Down

0 comments on commit 37e7203

Please sign in to comment.