From 03188a4658f0c0fdc0b9dc0f9926eb4c393ca97a Mon Sep 17 00:00:00 2001 From: Shawon Date: Sat, 24 Aug 2024 08:44:42 +0600 Subject: [PATCH] feat: Added support for custom hyperlinks hyperlinks now have a `custom` property that uses lua patterns to change how the links look. Closes: #121 --- lua/definitions.lua | 45 +++++++++++++++++++++++++- lua/markview.lua | 23 +++++++++++++- lua/markview/parser.lua | 2 +- lua/markview/renderer.lua | 66 ++++++++++++++++++++++++++------------- markview.nvim.wiki | 2 +- 5 files changed, 112 insertions(+), 26 deletions(-) diff --git a/lua/definitions.lua b/lua/definitions.lua index 5ef3090..29fc60f 100644 --- a/lua/definitions.lua +++ b/lua/definitions.lua @@ -334,15 +334,58 @@ --- Enable/Disable custom hyperlink ---@field enable boolean? --- ----@field hyperlinks markview.render_config.links.link +---@field hyperlinks markview.render_config.links.hyperlink --- ---@field images markview.render_config.links.link --- ---@field emails markview.render_config.links.link + +--- Configuration table for various link types +---@class markview.render_config.links.hyperlink +--- +---@field custom? markview.render_config.links.link[] +--- +--- Default highlight group for the various parts +---@field hl string? +--- +--- The icon to use for the heading +---@field icon string? +--- +--- Highlight group for the icon +---@field icon_hl string? +--- +--- Used bu the "label" style to add text before the left padding +---@field corner_left string? +--- +--- Highlight group for the left corner +---@field corner_left_hl string? +--- +--- Used bu the "label" style to add text after the right padding +---@field corner_right string? +--- +--- Highlight group for the right corner +---@field corner_right_hl string? +--- +--- Used bu the "label" style to add text before the heading text +---@field padding_left string? +--- +--- Highlight group for the left padding +---@field padding_left_hl string? +--- +--- Used bu the "label" style to add text after the heading text +---@field padding_right string? +--- +--- Highlight group for the left padding +---@field padding_right_hl string? + + --- Configuration table for various link types ---@class markview.render_config.links.link --- +--- Only for custom hyperlinks. Match string +---@field match? string +--- --- Default highlight group for the various parts ---@field hl string? --- diff --git a/lua/markview.lua b/lua/markview.lua index 89011e9..7d0570b 100644 --- a/lua/markview.lua +++ b/lua/markview.lua @@ -1383,6 +1383,27 @@ markview.configuration = { hyperlinks = { icon = "󰌷 ", hl = "MarkviewHyperlink", + + custom = { + { + match = "https://(.+)$", + + icon = "󰞉 ", + hl = "MarkviewHyperlink", + }, + { + match = "http://(.+)$", + + icon = "󰕑 ", + hl = "MarkviewHyperlink", + }, + { + match = "[%.]md$", + + icon = " ", + hl = "MarkviewHyperlink", + } + } }, images = { icon = "󰥶 ", @@ -1390,7 +1411,7 @@ markview.configuration = { }, emails = { icon = " ", - hl = "MarkviewEmail", + hl = "MarkviewEmail" } }, diff --git a/lua/markview/parser.lua b/lua/markview/parser.lua index adf4afd..1f88b49 100644 --- a/lua/markview/parser.lua +++ b/lua/markview/parser.lua @@ -597,7 +597,7 @@ parser.md_inline = function (buffer, TStree, from, to) node = capture_node, type = "email", - text = capture_text, + text = capture_text:gsub("^([<])", ""):gsub("([>])$", ""), row_start = row_start, row_end = row_end, diff --git a/lua/markview/renderer.lua b/lua/markview/renderer.lua index ae85f1b..07151e6 100644 --- a/lua/markview/renderer.lua +++ b/lua/markview/renderer.lua @@ -165,48 +165,64 @@ local display_width = function (text, config) -- Hyperlinks: normal for link, address in final_string:gmatch("%[([^%]]+)%]%(([^%)]+)%)") do + local cnf = lnk_conf; + + for _, conf in ipairs(config.links.hyperlinks.custom or {}) do + if conf.match and string.match(address or "", conf.match) then + cnf = conf + end + end + d_width = d_width - vim.fn.strdisplaywidth("[" .. "](" .. address .. ")"); - if lnk_conf ~= nil and lnk_conf.enable ~= false then + if cnf and lnk_conf and lnk_conf.enable ~= false then d_width = d_width + vim.fn.strdisplaywidth(table.concat({ - lnk_conf.corner_left or "", - lnk_conf.padding_left or "", - lnk_conf.icon or "", - lnk_conf.padding_right or "", - lnk_conf.corner_right or "" + cnf.corner_left or "", + cnf.padding_left or "", + cnf.icon or "", + cnf.padding_right or "", + cnf.corner_right or "" })); final_string = final_string:gsub("%[" .. link .. "%]%(" .. address .. "%)", table.concat({ - lnk_conf.corner_left or "", - lnk_conf.padding_left or "", - lnk_conf.icon or "", + cnf.corner_left or "", + cnf.padding_left or "", + cnf.icon or "", link, - lnk_conf.padding_right or "", - lnk_conf.corner_right or "" + cnf.padding_right or "", + cnf.corner_right or "" })); end end -- Hyperlink: full_reference_link for link, address in final_string:gmatch("[^!]%[([^%]]+)%]%[([^%]]+)%]") do + local cnf = lnk_conf; + + for _, conf in ipairs(config.links.hyperlinks.custom or {}) do + if conf.match and string.match(address or "", conf.match) then + cnf = conf + end + end + d_width = d_width - vim.fn.strdisplaywidth("[" .. "][" .. address .. "]"); - if lnk_conf ~= nil and lnk_conf.enable ~= false then + if cnf ~= nil and lnk_conf and lnk_conf.enable ~= false then d_width = d_width + vim.fn.strdisplaywidth(table.concat({ - lnk_conf.corner_left or "", - lnk_conf.padding_left or "", - lnk_conf.icon or "", - lnk_conf.padding_right or "", - lnk_conf.corner_right or "" + cnf.corner_left or "", + cnf.padding_left or "", + cnf.icon or "", + cnf.padding_right or "", + cnf.corner_right or "" })); final_string = final_string:gsub("%[" .. link .. "%]%[" .. address .. "%]", table.concat({ - lnk_conf.corner_left or "", - lnk_conf.padding_left or "", - lnk_conf.icon or "", + cnf.corner_left or "", + cnf.padding_left or "", + cnf.icon or "", link, - lnk_conf.padding_right or "", - lnk_conf.corner_right or "" + cnf.padding_right or "", + cnf.corner_right or "" })); end end @@ -1341,6 +1357,12 @@ renderer.render_links = function (buffer, content, config_table) lnk_conf = config_table.hyperlinks; + for _, conf in ipairs(config_table.hyperlinks.custom or {}) do + if conf.match and string.match(content.address or "", conf.match) then + lnk_conf = conf + end + end + -- Do not render links with no config if not lnk_conf then return; diff --git a/markview.nvim.wiki b/markview.nvim.wiki index c3c6b88..390c4bc 160000 --- a/markview.nvim.wiki +++ b/markview.nvim.wiki @@ -1 +1 @@ -Subproject commit c3c6b88d0104871a4aff28cf134f16fabf1efcc8 +Subproject commit 390c4bc8b03ec12f478155083f26fdce2edf7790