Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
OXY2DEV committed Aug 24, 2024
2 parents cf352bb + 03188a4 commit 1e79753
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 25 deletions.
45 changes: 44 additions & 1 deletion lua/definitions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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?
---
Expand Down
23 changes: 22 additions & 1 deletion lua/markview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1383,14 +1383,35 @@ markview.configuration = {
hyperlinks = {
icon = "󰌷 ",
hl = "MarkviewHyperlink",

custom = {
{
match = "https://(.+)$",

icon = "󰞉 ",
hl = "MarkviewHyperlink",
},
{
match = "http://(.+)$",

icon = "󰕑 ",
hl = "MarkviewHyperlink",
},
{
match = "[%.]md$",

icon = "",
hl = "MarkviewHyperlink",
}
}
},
images = {
icon = "󰥶 ",
hl = "MarkviewImageLink",
},
emails = {
icon = "",
hl = "MarkviewEmail",
hl = "MarkviewEmail"
}
},

Expand Down
2 changes: 1 addition & 1 deletion lua/markview/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
66 changes: 44 additions & 22 deletions lua/markview/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1e79753

Please sign in to comment.