Skip to content

Commit

Permalink
chore(refactor): finish moving main markdown logic into individual co…
Browse files Browse the repository at this point in the history
…mponent renderers
  • Loading branch information
MeanderingProgrammer committed Sep 28, 2024
1 parent 91ce0b5 commit d3a565e
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 49 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,10 @@ require('render-markdown').setup({
(list_marker_star)
] @list_marker
(task_list_marker_unchecked) @checkbox_unchecked
(task_list_marker_checked) @checkbox_checked
[
(task_list_marker_unchecked)
(task_list_marker_checked)
] @checkbox
(block_quote) @quote
Expand Down
6 changes: 4 additions & 2 deletions doc/render-markdown.txt
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,10 @@ Default Configuration ~
(list_marker_star)
] @list_marker

(task_list_marker_unchecked) @checkbox_unchecked
(task_list_marker_checked) @checkbox_checked
[
(task_list_marker_unchecked)
(task_list_marker_checked)
] @checkbox

(block_quote) @quote

Expand Down
44 changes: 2 additions & 42 deletions lua/render-markdown/handler/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ local Context = require('render-markdown.core.context')
local list = require('render-markdown.core.list')
local log = require('render-markdown.core.log')
local state = require('render-markdown.state')
local str = require('render-markdown.core.str')

---@class render.md.handler.buf.Markdown
---@field private marks render.md.Marks
Expand All @@ -20,7 +19,9 @@ function Handler.new(buf)
self.config = state.get(buf)
self.context = Context.get(buf)
self.renderers = {
checkbox = require('render-markdown.render.checkbox'),
code = require('render-markdown.render.code'),
dash = require('render-markdown.render.dash'),
heading = require('render-markdown.render.heading'),
list_marker = require('render-markdown.render.list_marker'),
quote = require('render-markdown.render.quote'),
Expand All @@ -40,54 +41,13 @@ function Handler:parse(root)
if render:setup() then
render:render()
end
elseif capture == 'dash' then
self:dash(info)
elseif capture == 'checkbox_unchecked' then
self:checkbox(info, self.config.checkbox.unchecked)
elseif capture == 'checkbox_checked' then
self:checkbox(info, self.config.checkbox.checked)
else
log.unhandled_capture('markdown', capture)
end
end)
return self.marks:get()
end

---@private
---@param info render.md.NodeInfo
function Handler:dash(info)
local dash = self.config.dash
if not dash.enabled then
return
end

local width = dash.width
width = type(width) == 'number' and width or self.context:get_width()

self.marks:add(true, info.start_row, 0, {
virt_text = { { dash.icon:rep(width), dash.highlight } },
virt_text_pos = 'overlay',
})
end

---@private
---@param info render.md.NodeInfo
---@param checkbox render.md.CheckboxComponent
function Handler:checkbox(info, checkbox)
if not self.config.checkbox.enabled then
return
end
local inline = self.config.checkbox.position == 'inline'
local icon, highlight = checkbox.icon, checkbox.highlight
self.marks:add(true, info.start_row, info.start_col, {
end_row = info.end_row,
end_col = info.end_col,
virt_text = { { inline and icon or str.pad_to(info.text, icon) .. icon, highlight } },
virt_text_pos = inline and 'inline' or 'overlay',
conceal = inline and '' or nil,
})
end

---@class render.md.handler.Markdown: render.md.Handler
local M = {}

Expand Down
2 changes: 1 addition & 1 deletion lua/render-markdown/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local state = require('render-markdown.state')
local M = {}

---@private
M.version = '7.2.2'
M.version = '7.2.3'

function M.check()
M.start('version')
Expand Down
6 changes: 4 additions & 2 deletions lua/render-markdown/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,10 @@ M.default_config = {
(list_marker_star)
] @list_marker
(task_list_marker_unchecked) @checkbox_unchecked
(task_list_marker_checked) @checkbox_checked
[
(task_list_marker_unchecked)
(task_list_marker_checked)
] @checkbox
(block_quote) @quote
Expand Down
52 changes: 52 additions & 0 deletions lua/render-markdown/render/checkbox.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
local Base = require('render-markdown.render.base')
local str = require('render-markdown.core.str')

---@class render.md.render.Checkbox: render.md.Renderer
---@field private checkbox render.md.CheckboxComponent
---@field private inline boolean
local Render = setmetatable({}, Base)
Render.__index = Render

---@param marks render.md.Marks
---@param config render.md.buffer.Config
---@param context render.md.Context
---@param info render.md.NodeInfo
---@return render.md.Renderer
function Render:new(marks, config, context, info)
return Base.new(self, marks, config, context, info)
end

---@return boolean
function Render:setup()
local checkbox = self.config.checkbox
if not checkbox.enabled then
return false
end

local type_mapping = {
task_list_marker_unchecked = checkbox.unchecked,
task_list_marker_checked = checkbox.checked,
}
self.checkbox = type_mapping[self.info.type]
if self.checkbox == nil then
return false
end

self.inline = checkbox.position == 'inline'

return true
end

function Render:render()
local icon = self.checkbox.icon
local text = self.inline and icon or str.pad_to(self.info.text, icon) .. icon
self.marks:add(true, self.info.start_row, self.info.start_col, {
end_row = self.info.end_row,
end_col = self.info.end_col,
virt_text = { { text, self.checkbox.highlight } },
virt_text_pos = self.inline and 'inline' or 'overlay',
conceal = self.inline and '' or nil,
})
end

return Render
36 changes: 36 additions & 0 deletions lua/render-markdown/render/dash.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
local Base = require('render-markdown.render.base')

---@class render.md.render.Dash: render.md.Renderer
---@field private dash render.md.Dash
local Render = setmetatable({}, Base)
Render.__index = Render

---@param marks render.md.Marks
---@param config render.md.buffer.Config
---@param context render.md.Context
---@param info render.md.NodeInfo
---@return render.md.Renderer
function Render:new(marks, config, context, info)
return Base.new(self, marks, config, context, info)
end

---@return boolean
function Render:setup()
self.dash = self.config.dash
if not self.dash.enabled then
return false
end
return true
end

function Render:render()
local width = self.dash.width
width = type(width) == 'number' and width or self.context:get_width()

self.marks:add(true, self.info.start_row, 0, {
virt_text = { { self.dash.icon:rep(width), self.dash.highlight } },
virt_text_pos = 'overlay',
})
end

return Render

0 comments on commit d3a565e

Please sign in to comment.