Skip to content

Commit 2f32e71

Browse files
minor edits
1 parent 79d0de4 commit 2f32e71

File tree

6 files changed

+36
-53
lines changed

6 files changed

+36
-53
lines changed

lua/render-markdown/component.lua

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
local state = require('render-markdown.state')
2+
13
---@class render.md.ComponentHelper
24
local M = {}
35

4-
---@param config render.md.BufferConfig
6+
---@param buf integer
57
---@param text string
68
---@param comparison 'exact'|'contains'
79
---@return render.md.CustomComponent?
8-
function M.callout(config, text, comparison)
10+
function M.callout(buf, text, comparison)
911
---@param callout render.md.CustomComponent
1012
---@return boolean
1113
local function matches(callout)
@@ -17,19 +19,19 @@ function M.callout(config, text, comparison)
1719
error(string.format('Unhandled comparison: %s', comparison))
1820
end
1921
end
20-
for _, callout in pairs(config.callout) do
22+
for _, callout in pairs(state.get_config(buf).callout) do
2123
if matches(callout) then
2224
return callout
2325
end
2426
end
2527
return nil
2628
end
2729

28-
---@param config render.md.BufferConfig
30+
---@param buf integer
2931
---@param text string
3032
---@param comparison 'exact'|'starts'
3133
---@return render.md.CustomComponent?
32-
function M.checkbox(config, text, comparison)
34+
function M.checkbox(buf, text, comparison)
3335
---@param checkbox render.md.CustomComponent
3436
---@return boolean
3537
local function matches(checkbox)
@@ -41,7 +43,7 @@ function M.checkbox(config, text, comparison)
4143
error(string.format('Unhandled comparison: %s', comparison))
4244
end
4345
end
44-
for _, checkbox in pairs(config.checkbox.custom) do
46+
for _, checkbox in pairs(state.get_config(buf).checkbox.custom) do
4547
if matches(checkbox) then
4648
return checkbox
4749
end

lua/render-markdown/handler/markdown.lua

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,7 @@ function Handler:list_marker(info)
124124
return true
125125
end
126126
local paragraph = info:sibling('paragraph')
127-
if paragraph == nil then
128-
return false
129-
end
130-
return component.checkbox(self.config, paragraph.text, 'starts') ~= nil
127+
return paragraph ~= nil and component.checkbox(self.buf, paragraph.text, 'starts') ~= nil
131128
end
132129
if sibling_checkbox() then
133130
-- Hide the list marker for checkboxes rather than replacing with a bullet point
@@ -198,7 +195,7 @@ function Handler:quote_marker(info, block_quote)
198195
if not quote.enabled then
199196
return
200197
end
201-
local callout = component.callout(self.config, block_quote.text, 'contains')
198+
local callout = component.callout(self.buf, block_quote.text, 'contains')
202199
local highlight = callout ~= nil and callout.highlight or quote.highlight
203200
self.marks:add(true, info.start_row, info.start_col, {
204201
end_row = info.end_row,

lua/render-markdown/handler/markdown_inline.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ end
6161
---@private
6262
---@param info render.md.NodeInfo
6363
function Handler:shortcut(info)
64-
local callout = component.callout(self.config, info.text, 'exact')
64+
local callout = component.callout(self.buf, info.text, 'exact')
6565
if callout ~= nil then
6666
self:callout(info, callout)
6767
return
6868
end
6969

70-
local checkbox = component.checkbox(self.config, info.text, 'exact')
70+
local checkbox = component.checkbox(self.buf, info.text, 'exact')
7171
if checkbox ~= nil then
7272
self:checkbox(info, checkbox)
7373
return

lua/render-markdown/render/code.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ end
7676
---@param widths integer[]
7777
---@return integer, integer
7878
function Parser.get_width(config, context, widths)
79-
local code_width = vim.fn.max(widths)
80-
local longest_line = config.left_pad + code_width + config.right_pad
79+
local longest_line = config.left_pad + vim.fn.max(widths) + config.right_pad
8180
local width = math.max(longest_line, config.min_width)
8281
if config.width == 'block' then
8382
return width, width
@@ -113,13 +112,14 @@ function Render:render(info)
113112
if not self.config.enabled or self.config.style == 'none' then
114113
return
115114
end
115+
116116
local code = Parser.parse(self.config, self.context, info)
117117
if code == nil then
118118
return
119119
end
120120

121-
local add_background = vim.tbl_contains({ 'normal', 'full' }, self.config.style)
122-
add_background = add_background and not vim.tbl_contains(self.config.disable_background, code.language)
121+
local disabled_language = vim.tbl_contains(self.config.disable_background, code.language)
122+
local add_background = vim.tbl_contains({ 'normal', 'full' }, self.config.style) and not disabled_language
123123

124124
local icon_added = self:language(code, add_background)
125125
if add_background then

lua/render-markdown/render/table.lua

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ local logger = require('render-markdown.logger')
33
local str = require('render-markdown.str')
44
local util = require('render-markdown.render.util')
55

6+
---@class render.md.table.Column
7+
---@field info render.md.NodeInfo
8+
---@field width integer
9+
10+
---@class render.md.table.Row
11+
---@field info render.md.NodeInfo
12+
---@field pipes render.md.NodeInfo[]
13+
---@field columns render.md.table.Column[]
14+
615
---@alias render.md.table.Alignment 'left'|'right'|'center'|'default'
716

817
---@class render.md.table.DelimColumn
@@ -13,15 +22,6 @@ local util = require('render-markdown.render.util')
1322
---@field info render.md.NodeInfo
1423
---@field columns render.md.table.DelimColumn[]
1524

16-
---@class render.md.table.Column
17-
---@field info render.md.NodeInfo
18-
---@field width integer
19-
20-
---@class render.md.table.Row
21-
---@field info render.md.NodeInfo
22-
---@field pipes render.md.NodeInfo[]
23-
---@field columns render.md.table.Column[]
24-
2525
---@class render.md.table.Table
2626
---@field info render.md.NodeInfo
2727
---@field delim render.md.table.DelimRow
@@ -47,7 +47,7 @@ function Parser.parse(context, info)
4747
if row.type == 'pipe_table_delimiter_row' then
4848
delim = Parser.delim(row)
4949
elseif context:contains_info(row) then
50-
if row.type == 'pipe_table_header' or row.type == 'pipe_table_row' then
50+
if vim.tbl_contains({ 'pipe_table_header', 'pipe_table_row' }, row.type) then
5151
table.insert(table_rows, row)
5252
else
5353
logger.unhandled_type('markdown', 'row', row.type)
@@ -73,8 +73,7 @@ function Parser.parse(context, info)
7373
-- Store the max width information in the delimiter
7474
for _, row in ipairs(rows) do
7575
for i, column in ipairs(row.columns) do
76-
local delim_column = delim.columns[i]
77-
delim_column.width = math.max(delim_column.width, column.width)
76+
delim.columns[i].width = math.max(delim.columns[i].width, column.width)
7877
end
7978
end
8079

@@ -90,17 +89,12 @@ function Parser.delim(info)
9089
if row_data == nil then
9190
return nil
9291
end
93-
94-
local pipes = row_data.pipes
95-
local cells = row_data.cells
96-
97-
---@type render.md.table.DelimColumn[]
92+
local pipes, cells = row_data.pipes, row_data.cells
9893
local columns = {}
9994
for i = 1, #cells do
100-
local cell = cells[i]
101-
local width = pipes[i + 1].start_col - pipes[i].end_col
95+
local cell, width = cells[i], pipes[i + 1].start_col - pipes[i].end_col
10296
if width < 0 then
103-
return {}
97+
return nil
10498
end
10599
---@type render.md.table.DelimColumn
106100
local column = { width = width, alignment = Parser.alignment(cell) }
@@ -137,18 +131,13 @@ function Parser.row(context, info, num_columns)
137131
if row_data == nil then
138132
return nil
139133
end
140-
141-
local pipes = row_data.pipes
142-
local cells = row_data.cells
134+
local pipes, cells = row_data.pipes, row_data.cells
143135
if #cells ~= num_columns then
144136
return nil
145137
end
146-
147-
---@type render.md.table.Column[]
148138
local columns = {}
149139
for i = 1, #cells do
150-
local cell = cells[i]
151-
local width = pipes[i + 1].start_col - pipes[i].end_col
140+
local cell, width = cells[i], pipes[i + 1].start_col - pipes[i].end_col
152141
-- Account for double width glyphs by replacing cell spacing with text width
153142
width = width - (cell.end_col - cell.start_col) + str.width(cell.text)
154143
-- Remove concealed and add inlined text
@@ -160,7 +149,6 @@ function Parser.row(context, info, num_columns)
160149
local column = { info = cell, width = width }
161150
table.insert(columns, column)
162151
end
163-
164152
---@type render.md.table.Row
165153
return { info = info, pipes = pipes, columns = columns }
166154
end
@@ -170,10 +158,7 @@ end
170158
---@param cell_type string
171159
---@return { pipes: render.md.NodeInfo[], cells: render.md.NodeInfo[] }?
172160
function Parser.row_data(info, cell_type)
173-
---@type render.md.NodeInfo[]
174-
local pipes = {}
175-
---@type render.md.NodeInfo[]
176-
local cells = {}
161+
local pipes, cells = {}, {}
177162
info:for_each_child(function(cell)
178163
if cell.type == '|' then
179164
table.insert(pipes, cell)
@@ -218,6 +203,7 @@ function Render:render(info)
218203
if not self.config.enabled or self.config.style == 'none' then
219204
return
220205
end
206+
221207
local tbl = Parser.parse(self.context, info)
222208
if tbl == nil then
223209
return
@@ -350,9 +336,7 @@ function Render:full(tbl)
350336
return math.max(str.leading_spaces(row.info.text), row.info.start_col)
351337
end
352338

353-
local delim = tbl.delim
354-
local first = tbl.rows[1]
355-
local last = tbl.rows[#tbl.rows]
339+
local delim, first, last = tbl.delim, tbl.rows[1], tbl.rows[#tbl.rows]
356340
if not width_equal(first, delim) or not width_equal(last, delim) then
357341
return
358342
end

lua/render-markdown/state.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ end
6565

6666
---@param buf integer
6767
---@return render.md.BufferConfig
68-
M.get_config = function(buf)
68+
function M.get_config(buf)
6969
local config = configs[buf]
7070
if config == nil then
7171
config = M.default_buffer_config()

0 commit comments

Comments
 (0)