Skip to content

Commit d3a565e

Browse files
chore(refactor): finish moving main markdown logic into individual component renderers
1 parent 91ce0b5 commit d3a565e

File tree

7 files changed

+103
-49
lines changed

7 files changed

+103
-49
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,10 @@ require('render-markdown').setup({
163163
(list_marker_star)
164164
] @list_marker
165165
166-
(task_list_marker_unchecked) @checkbox_unchecked
167-
(task_list_marker_checked) @checkbox_checked
166+
[
167+
(task_list_marker_unchecked)
168+
(task_list_marker_checked)
169+
] @checkbox
168170
169171
(block_quote) @quote
170172

doc/render-markdown.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,10 @@ Default Configuration ~
211211
(list_marker_star)
212212
] @list_marker
213213

214-
(task_list_marker_unchecked) @checkbox_unchecked
215-
(task_list_marker_checked) @checkbox_checked
214+
[
215+
(task_list_marker_unchecked)
216+
(task_list_marker_checked)
217+
] @checkbox
216218

217219
(block_quote) @quote
218220

lua/render-markdown/handler/markdown.lua

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ local Context = require('render-markdown.core.context')
22
local list = require('render-markdown.core.list')
33
local log = require('render-markdown.core.log')
44
local state = require('render-markdown.state')
5-
local str = require('render-markdown.core.str')
65

76
---@class render.md.handler.buf.Markdown
87
---@field private marks render.md.Marks
@@ -20,7 +19,9 @@ function Handler.new(buf)
2019
self.config = state.get(buf)
2120
self.context = Context.get(buf)
2221
self.renderers = {
22+
checkbox = require('render-markdown.render.checkbox'),
2323
code = require('render-markdown.render.code'),
24+
dash = require('render-markdown.render.dash'),
2425
heading = require('render-markdown.render.heading'),
2526
list_marker = require('render-markdown.render.list_marker'),
2627
quote = require('render-markdown.render.quote'),
@@ -40,54 +41,13 @@ function Handler:parse(root)
4041
if render:setup() then
4142
render:render()
4243
end
43-
elseif capture == 'dash' then
44-
self:dash(info)
45-
elseif capture == 'checkbox_unchecked' then
46-
self:checkbox(info, self.config.checkbox.unchecked)
47-
elseif capture == 'checkbox_checked' then
48-
self:checkbox(info, self.config.checkbox.checked)
4944
else
5045
log.unhandled_capture('markdown', capture)
5146
end
5247
end)
5348
return self.marks:get()
5449
end
5550

56-
---@private
57-
---@param info render.md.NodeInfo
58-
function Handler:dash(info)
59-
local dash = self.config.dash
60-
if not dash.enabled then
61-
return
62-
end
63-
64-
local width = dash.width
65-
width = type(width) == 'number' and width or self.context:get_width()
66-
67-
self.marks:add(true, info.start_row, 0, {
68-
virt_text = { { dash.icon:rep(width), dash.highlight } },
69-
virt_text_pos = 'overlay',
70-
})
71-
end
72-
73-
---@private
74-
---@param info render.md.NodeInfo
75-
---@param checkbox render.md.CheckboxComponent
76-
function Handler:checkbox(info, checkbox)
77-
if not self.config.checkbox.enabled then
78-
return
79-
end
80-
local inline = self.config.checkbox.position == 'inline'
81-
local icon, highlight = checkbox.icon, checkbox.highlight
82-
self.marks:add(true, info.start_row, info.start_col, {
83-
end_row = info.end_row,
84-
end_col = info.end_col,
85-
virt_text = { { inline and icon or str.pad_to(info.text, icon) .. icon, highlight } },
86-
virt_text_pos = inline and 'inline' or 'overlay',
87-
conceal = inline and '' or nil,
88-
})
89-
end
90-
9151
---@class render.md.handler.Markdown: render.md.Handler
9252
local M = {}
9353

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local state = require('render-markdown.state')
44
local M = {}
55

66
---@private
7-
M.version = '7.2.2'
7+
M.version = '7.2.3'
88

99
function M.check()
1010
M.start('version')

lua/render-markdown/init.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,10 @@ M.default_config = {
237237
(list_marker_star)
238238
] @list_marker
239239
240-
(task_list_marker_unchecked) @checkbox_unchecked
241-
(task_list_marker_checked) @checkbox_checked
240+
[
241+
(task_list_marker_unchecked)
242+
(task_list_marker_checked)
243+
] @checkbox
242244
243245
(block_quote) @quote
244246
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
local Base = require('render-markdown.render.base')
2+
local str = require('render-markdown.core.str')
3+
4+
---@class render.md.render.Checkbox: render.md.Renderer
5+
---@field private checkbox render.md.CheckboxComponent
6+
---@field private inline boolean
7+
local Render = setmetatable({}, Base)
8+
Render.__index = Render
9+
10+
---@param marks render.md.Marks
11+
---@param config render.md.buffer.Config
12+
---@param context render.md.Context
13+
---@param info render.md.NodeInfo
14+
---@return render.md.Renderer
15+
function Render:new(marks, config, context, info)
16+
return Base.new(self, marks, config, context, info)
17+
end
18+
19+
---@return boolean
20+
function Render:setup()
21+
local checkbox = self.config.checkbox
22+
if not checkbox.enabled then
23+
return false
24+
end
25+
26+
local type_mapping = {
27+
task_list_marker_unchecked = checkbox.unchecked,
28+
task_list_marker_checked = checkbox.checked,
29+
}
30+
self.checkbox = type_mapping[self.info.type]
31+
if self.checkbox == nil then
32+
return false
33+
end
34+
35+
self.inline = checkbox.position == 'inline'
36+
37+
return true
38+
end
39+
40+
function Render:render()
41+
local icon = self.checkbox.icon
42+
local text = self.inline and icon or str.pad_to(self.info.text, icon) .. icon
43+
self.marks:add(true, self.info.start_row, self.info.start_col, {
44+
end_row = self.info.end_row,
45+
end_col = self.info.end_col,
46+
virt_text = { { text, self.checkbox.highlight } },
47+
virt_text_pos = self.inline and 'inline' or 'overlay',
48+
conceal = self.inline and '' or nil,
49+
})
50+
end
51+
52+
return Render

lua/render-markdown/render/dash.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
local Base = require('render-markdown.render.base')
2+
3+
---@class render.md.render.Dash: render.md.Renderer
4+
---@field private dash render.md.Dash
5+
local Render = setmetatable({}, Base)
6+
Render.__index = Render
7+
8+
---@param marks render.md.Marks
9+
---@param config render.md.buffer.Config
10+
---@param context render.md.Context
11+
---@param info render.md.NodeInfo
12+
---@return render.md.Renderer
13+
function Render:new(marks, config, context, info)
14+
return Base.new(self, marks, config, context, info)
15+
end
16+
17+
---@return boolean
18+
function Render:setup()
19+
self.dash = self.config.dash
20+
if not self.dash.enabled then
21+
return false
22+
end
23+
return true
24+
end
25+
26+
function Render:render()
27+
local width = self.dash.width
28+
width = type(width) == 'number' and width or self.context:get_width()
29+
30+
self.marks:add(true, self.info.start_row, 0, {
31+
virt_text = { { self.dash.icon:rep(width), self.dash.highlight } },
32+
virt_text_pos = 'overlay',
33+
})
34+
end
35+
36+
return Render

0 commit comments

Comments
 (0)