Skip to content

Commit d482fb5

Browse files
committed
feat(asciidoc): Added Image macro support
1 parent 40ac165 commit d482fb5

8 files changed

Lines changed: 202 additions & 3 deletions

File tree

lua/markview/config/asciidoc.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,21 @@ return {
7575
enable = true,
7676
},
7777

78+
images = {
79+
enable = true,
80+
81+
default = {
82+
icon = "󰥶 ",
83+
hl = "MarkviewImage",
84+
},
85+
86+
["%.svg$"] = { icon = "󰜡 " },
87+
["%.png$"] = { icon = "󰸭 " },
88+
["%.jpg$"] = { icon = "󰈥 " },
89+
["%.gif$"] = { icon = "󰵸 " },
90+
["%.pdf$"] = { icon = "" }
91+
},
92+
7893
tocs = {
7994
enable = true,
8095

lua/markview/parsers/asciidoc.lua

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,38 @@ asciidoc.section_title = function (buffer, TSNode, text, range)
115115
});
116116
end
117117

118+
---@param buffer integer
119+
---@param TSNode TSNode
120+
---@param text string[]
121+
---@param range markview.parsed.asciidoc.images.range
122+
asciidoc.image = function (buffer, TSNode, text, range)
123+
local _destination = TSNode:named_child(1);
124+
125+
if not _destination then
126+
return;
127+
end
128+
129+
local destination = vim.treesitter.get_node_text(_destination, buffer, {});
130+
range.destination = { _destination:range() };
131+
132+
--[[
133+
refactor: Range correction
134+
135+
Block macros end at the start of the next line.
136+
So, we correct the end column & end row.
137+
]]
138+
range.row_end = range.row_end - 1;
139+
range.col_end = range.col_start + #(text[#text] or "");
140+
141+
asciidoc.insert({
142+
class = "asciidoc_image",
143+
destination = destination,
144+
145+
text = text,
146+
range = range
147+
});
148+
end
149+
118150
---@param text string[]
119151
---@param range markview.parsed.range
120152
asciidoc.toc_pos = function (_, _, text, range)
@@ -315,6 +347,12 @@ asciidoc.parse = function (buffer, TSTree, from, to)
315347
(ordered_list_item) @asciidoc.list_item
316348
317349
(checked_list_item) @asciidoc.list_item
350+
351+
(block_macro
352+
(
353+
(block_macro_name) @image_keyword
354+
(#eq? @image_keyword "image")
355+
)) @asciidoc.image
318356
]]);
319357

320358
if not can_scan then

lua/markview/parsers/asciidoc_inline.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ end
156156
---@param text string[]
157157
---@param range markview.parsed.range
158158
asciidoc_inline.uri = function (buffer, TSNode, text, range)
159+
local before = vim.api.nvim_buf_get_text(buffer, range.row_start, 0, range.row_start, range.col_start, {})[1];
160+
161+
-- NOTE: Do not parse a URI if it's part of an image macro.
162+
if string.match(before, "image::$") then
163+
return;
164+
end
165+
159166
local delimiters = {};
160167
local destination;
161168

lua/markview/renderers/asciidoc.lua

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,77 @@ asciidoc.document_attribute = function (buffer, item)
2323
});
2424
end
2525

26+
---@param buffer integer
27+
---@param item markview.parsed.asciidoc.images
28+
asciidoc.image = function (buffer, item)
29+
---@type markview.config.asciidoc.images?
30+
local main_config = spec.get({ "asciidoc", "images" }, { fallback = nil });
31+
local range = item.range;
32+
33+
if not main_config then
34+
return;
35+
end
36+
37+
---@type markview.config.asciidoc.images.opts?
38+
local config = utils.match(
39+
main_config,
40+
item.destination,
41+
{
42+
eval_args = { buffer, item }
43+
}
44+
);
45+
46+
if config == nil then
47+
return;
48+
end
49+
50+
utils.set_extmark(buffer, asciidoc.ns, range.row_start, range.col_start, {
51+
end_col = range.destination[2],
52+
conceal = "",
53+
54+
virt_text_pos = "inline",
55+
virt_text = {
56+
{ config.corner_left or "", utils.set_hl(config.corner_left_hl or config.hl) },
57+
{ config.padding_left or "", utils.set_hl(config.padding_left_hl or config.hl) },
58+
{ config.icon or "", utils.set_hl(config.icon_hl or config.hl) }
59+
},
60+
61+
hl_mode = "combine"
62+
});
63+
64+
if config.text then
65+
utils.set_extmark(buffer, asciidoc.ns, range.destination[1], range.destination[2], {
66+
end_col = range.destination[4], end_row = range.destination[3],
67+
68+
virt_text = {
69+
{ config.text or "", utils.set_hl(config.text_hl or config.hl) }
70+
},
71+
72+
hl_mode = "combine"
73+
});
74+
else
75+
utils.set_extmark(buffer, asciidoc.ns, range.destination[1], range.destination[2], {
76+
end_col = range.destination[4], end_row = range.destination[3],
77+
78+
hl_group = utils.set_hl(config.hl),
79+
hl_mode = "combine"
80+
});
81+
end
82+
83+
utils.set_extmark(buffer, asciidoc.ns, range.row_end, range.destination[4], {
84+
end_col = range.col_end,
85+
conceal = "",
86+
87+
virt_text_pos = "inline",
88+
virt_text = {
89+
{ config.corner_right or "", utils.set_hl(config.corner_right_hl or config.hl) },
90+
{ config.padding_right or "", utils.set_hl(config.padding_right_hl or config.hl) }
91+
},
92+
93+
hl_mode = "combine"
94+
});
95+
end
96+
2697
---@param buffer integer
2798
---@param item markview.parsed.asciidoc.document_titles
2899
asciidoc.document_title = function (buffer, item)

lua/markview/renderers/asciidoc_inline.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,9 @@ asciidoc_inline.uri = function (buffer, item)
258258
utils.set_extmark(buffer, asciidoc_inline.ns, range.row_start, range.col_start + #(item.delimiters[1] or ""), {
259259
end_col = range.col_end - #(item.delimiters[2] or ""), end_row = range.row_end,
260260

261-
virt_text = {
262-
{ config.text or "", utils.set_hl(config.text_hl or config.hl) }
263-
},
261+
virt_text = {
262+
{ config.text or "", utils.set_hl(config.text_hl or config.hl) }
263+
},
264264

265265
hl_mode = "combine"
266266
});

lua/markview/types/parsers/asciidoc.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@
2020

2121
------------------------------------------------------------------------------
2222

23+
---@class markview.parsed.asciidoc.images
24+
---
25+
---@field class "asciidoc_image"
26+
---@field destination string Source of the image.
27+
---
28+
---@field text string[]
29+
---@field range markview.parsed.asciidoc.images.range
30+
31+
32+
---@class markview.parsed.asciidoc.images.range
33+
---
34+
---@field row_start integer
35+
---@field col_start integer
36+
---
37+
---@field row_end integer
38+
---@field col_end integer
39+
---
40+
---@field destination integer[] Range of the image destination(output of `{ TSNode:range() }`.
41+
42+
------------------------------------------------------------------------------
43+
2344
---@class markview.parsed.asciidoc.list_items
2445
---
2546
---@field class "asciidoc_list_item"

lua/markview/types/renderers/asciidoc.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,43 @@
4242

4343
------------------------------------------------------------------------------
4444

45+
--- Configuration for image links.
46+
---@class markview.config.asciidoc.images
47+
---
48+
---@field enable boolean Enable rendering of image links
49+
---
50+
---@field default markview.config.asciidoc.images.opts Default configuration for image links
51+
---@field [string] markview.config.asciidoc.images.opts Configuration image links whose description matches `string`.
52+
53+
54+
--[[ Options for a specific image link type. ]]
55+
---@class markview.config.asciidoc.images.opts
56+
---
57+
---@field corner_left? string Left corner.
58+
---@field corner_left_hl? string Highlight group for the left corner.
59+
---
60+
---@field padding_left? string Left padding(added after `corner_left`).
61+
---@field padding_left_hl? string Highlight group for the left padding.
62+
---
63+
---@field icon? string Icon(added after `padding_left`).
64+
---@field icon_hl? string Highlight group for the icon.
65+
---
66+
--[[ Text to show instead of the `URL`.]]
67+
---@field text?
68+
---| string
69+
---| function(buffer: integer, item: markview.parsed.asciidoc_inline.uris): string
70+
---@field text_hl? string Highlight group for the text.
71+
---
72+
---@field hl? string Default highlight group(used by `*_hl` options when they are not set).
73+
---
74+
---@field padding_right? string Right padding.
75+
---@field padding_right_hl? string Highlight group for the right padding.
76+
---
77+
---@field corner_right? string Right corner(added after `padding_right`).
78+
---@field corner_right_hl? string Highlight group for the right corner.
79+
80+
------------------------------------------------------------------------------
81+
4582
--- Configuration for list items.
4683
---@class markview.config.asciidoc.list_items
4784
---

test/asciidoc.adoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
image::sunset.jpg[]
2+
3+
image::sunset.jpg[Sunset]
4+
5+
.A mountain sunset
6+
[#img-sunset,caption="Figure 1: ",link=https://www.flickr.com/photos/javh/5448336655]
7+
image::macros:sunset.jpg[Sunset,200,100]
8+
9+
image::https://asciidoctor.org/images/octocat.jpg[GitHub mascot]
10+
111
= Hello world
212

313
:toc:

0 commit comments

Comments
 (0)