Skip to content

Commit a021d5b

Browse files
Refactor! Likely to break all configs, group properties by component
## Details This is a massive breaking change that modifies nearly every configuration parameter. Give the amount of changes if you would rather punt dealing with this for now feel free to use the 'v3.3.1' git tag which will keep you at the previous commit. For example with lazy.nvim: ```lua { 'MeanderingProgrammer/markdown.nvim', tag = 'v3.3.1', ... } ``` Most configs will likely still work, in that the plugin will not break, but it will essentially ignore all user settings. There are known settings that if set previously will break the plugin: - `dash` - `checkbox.unchecked` - `checkbox.checked` - `quote` The idea of this change is rather than scattering properties for say 'headings' through the top level 'headings' parameter and a separate 'highlights.headings' parameter, we instead create a top level 'heading' parameter that stores icons and highlights. This should make the problem of what to modify easier as well as allow individual components to evolve more easily. List of all changes and how to fix them split by section below. ### Base - `start_enabled` -> `enabled` ### Latex - `latex_enabled` -> `latex.enabled` - `latex_converter` -> `latex.converter` - `highlights.latex` -> `latex.highlight` ### Headings - `headings` -> `heading.icons` - `highlights.heading.backgrounds` -> `heading.backgrounds` - `highlights.heading.foregrounds` -> `heading.foregrounds` ### Code - `code_style` -> `code.style` - `highlights.code` -> `code.highlight` ### Dash - `dash` -> `dash.icon` - `highlights.dash` -> `dash.highlight` ### Bullets - `bullets` -> `bullet.icons` - `highlights.bullet` -> `bullet.highlight` ### Checkbox - `checkbox.unchecked` -> `checkbox.unchecked.icon` - `highlights.checkbox.unchecked` -> `checkbox.unchecked.highlight` - `checkbox.checked` -> `checkbox.checked.icon` - `highlights.checkbox.checked` -> `checkbox.checked.highlight` ### Quote - `quote` -> `quote.icon` - `highlights.quote` -> `quote.highlight` ### Table - `table_style` -> `pipe_table.style` - `cell_style` -> `pipe_table.cell` - `highlight.table.head` -> `pipe_table.head` - `highlight.table.row` -> `pipe_table.row` ### Callouts - `callout.note` -> `callout.note.rendered` - `callout.tip` -> `callout.tip.rendered` - `callout.important` -> `callout.important.rendered` - `callout.warning` -> `callout.warning.rendered` - `callout.caution` -> `callout.caution.rendered` - `highlights.callout.note` -> `callout.note.highlight` - `highlights.callout.tip` -> `callout.tip.highlight` - `highlights.callout.important` -> `callout.important.highlight` - `highlights.callout.warning` -> `callout.warning.highlight` - `highlights.callout.caution` -> `callout.caution.highlight` - `callout.custom.*` -> `callout.*` (i.e. unnest from custom block) ### Others Any remaing changes are covered within that component. I.e. `code_style` is covered in Code, `highlights.table` is covered in Table, `highlights.callout.note` is covered in Callouts, etc.
1 parent 33c8571 commit a021d5b

File tree

12 files changed

+766
-743
lines changed

12 files changed

+766
-743
lines changed

README.md

Lines changed: 195 additions & 203 deletions
Large diffs are not rendered by default.

doc/render-markdown.txt

Lines changed: 198 additions & 206 deletions
Large diffs are not rendered by default.

lua/render-markdown/component.lua

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,6 @@ local state = require('render-markdown.state')
44
---@field text string
55
---@field highlight string
66

7-
---@class render.md.CalloutInfo
8-
---@field text string
9-
---@field key string
10-
11-
---@type render.md.CalloutInfo[]
12-
local callouts = {
13-
{ text = '[!NOTE]', key = 'note' },
14-
{ text = '[!TIP]', key = 'tip' },
15-
{ text = '[!IMPORTANT]', key = 'important' },
16-
{ text = '[!WARNING]', key = 'warning' },
17-
{ text = '[!CAUTION]', key = 'caution' },
18-
}
19-
207
local M = {}
218

229
---@param value string
@@ -34,15 +21,7 @@ M.callout = function(value, comparison)
3421
error(string.format('Unhandled comparison: %s', comparison))
3522
end
3623
end
37-
for _, callout in ipairs(callouts) do
38-
if matches(callout.text) then
39-
return {
40-
text = state.config.callout[callout.key],
41-
highlight = state.config.highlights.callout[callout.key],
42-
}
43-
end
44-
end
45-
for _, callout in pairs(state.config.callout.custom) do
24+
for _, callout in pairs(state.config.callout) do
4625
if matches(callout.raw) then
4726
return { text = callout.rendered, highlight = callout.highlight }
4827
end

lua/render-markdown/handler/latex.lua

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ local M = {}
1515
---@param root TSNode
1616
---@param buf integer
1717
M.render = function(namespace, root, buf)
18-
if not state.config.latex_enabled then
18+
if not state.config.latex.enabled then
1919
return
2020
end
21-
local converter = state.config.latex_converter
21+
local converter = state.config.latex.converter
2222
if vim.fn.executable(converter) ~= 1 then
2323
logger.debug('Executable not found: ' .. converter)
2424
else
@@ -31,7 +31,6 @@ end
3131
---@param node TSNode
3232
---@param converter string
3333
M.render_node = function(namespace, buf, node, converter)
34-
local highlights = state.config.highlights
3534
local value = vim.treesitter.get_node_text(node, buf)
3635
local start_row, start_col, end_row, end_col = node:range()
3736
logger.debug_node('latex', node, buf)
@@ -45,7 +44,7 @@ M.render_node = function(namespace, buf, node, converter)
4544
end
4645

4746
local latex_lines = vim.tbl_map(function(expression)
48-
return { { expression, highlights.latex } }
47+
return { { expression, state.config.latex.highlight } }
4948
end, expressions)
5049
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
5150
end_row = end_row,

lua/render-markdown/handler/markdown.lua

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,23 @@ end
2424
---@param capture string
2525
---@param node TSNode
2626
M.render_node = function(namespace, buf, capture, node)
27-
local highlights = state.config.highlights
2827
local value = vim.treesitter.get_node_text(node, buf)
2928
local start_row, start_col, end_row, end_col = node:range()
3029
logger.debug_node(capture, node, buf)
3130

3231
if capture == 'heading' then
32+
local heading = state.config.heading
3333
local level = vim.fn.strdisplaywidth(value)
3434

35-
local heading = list.cycle(state.config.headings, level)
35+
local icon = list.cycle(heading.icons, level)
3636
-- Available width is level + 1, where level = number of `#` characters and one is added
3737
-- to account for the space after the last `#` but before the heading title
38-
local padding = level + 1 - vim.fn.strdisplaywidth(heading)
38+
local padding = level + 1 - vim.fn.strdisplaywidth(icon)
3939

40-
local background = list.clamp_last(highlights.heading.backgrounds, level)
41-
local foreground = list.clamp_last(highlights.heading.foregrounds, level)
40+
local background = list.clamp(heading.backgrounds, level)
41+
local foreground = list.clamp(heading.foregrounds, level)
4242

43-
local heading_text = { str.pad(heading, padding), { foreground, background } }
43+
local heading_text = { str.pad(icon, padding), { foreground, background } }
4444
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, 0, {
4545
end_row = end_row + 1,
4646
end_col = 0,
@@ -50,25 +50,29 @@ M.render_node = function(namespace, buf, capture, node)
5050
hl_eol = true,
5151
})
5252
elseif capture == 'dash' then
53+
local dash = state.config.dash
5354
local width = vim.api.nvim_win_get_width(util.buf_to_win(buf))
54-
local dash_text = { state.config.dash:rep(width), highlights.dash }
55+
56+
local dash_text = { dash.icon:rep(width), dash.highlight }
5557
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, 0, {
5658
virt_text = { dash_text },
5759
virt_text_pos = 'overlay',
5860
})
5961
elseif capture == 'code' then
60-
if state.config.code_style == 'none' then
62+
local code = state.config.code
63+
if code.style == 'none' then
6164
return
6265
end
6366

6467
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, 0, {
6568
end_row = end_row,
6669
end_col = 0,
67-
hl_group = highlights.code,
70+
hl_group = code.highlight,
6871
hl_eol = true,
6972
})
7073
elseif capture == 'language' then
71-
if state.config.code_style ~= 'full' then
74+
local code = state.config.code
75+
if code.style ~= 'full' then
7276
return
7377
end
7478
-- Requires inline extmarks
@@ -81,7 +85,7 @@ M.render_node = function(namespace, buf, capture, node)
8185
return
8286
end
8387

84-
local icon_text = { icon .. ' ' .. value, { icon_highlight, highlights.code } }
88+
local icon_text = { icon .. ' ' .. value, { icon_highlight, code.highlight } }
8589
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
8690
virt_text = { icon_text },
8791
virt_text_pos = 'inline',
@@ -95,14 +99,15 @@ M.render_node = function(namespace, buf, capture, node)
9599
conceal = '',
96100
})
97101
else
102+
local bullet = state.config.bullet
98103
-- List markers from tree-sitter should have leading spaces removed, however there are known
99104
-- edge cases in the parser: https://github.com/tree-sitter-grammars/tree-sitter-markdown/issues/127
100105
-- As a result we handle leading spaces here, can remove if this gets fixed upstream
101106
local _, leading_spaces = value:find('^%s*')
102107
local level = ts.level_in_section(node, 'list')
103-
local bullet = list.cycle(state.config.bullets, level)
108+
local icon = list.cycle(bullet.icons, level)
104109

105-
local list_marker_text = { str.pad(bullet, leading_spaces), highlights.bullet }
110+
local list_marker_text = { str.pad(icon, leading_spaces), bullet.highlight }
106111
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
107112
end_row = end_row,
108113
end_col = end_col,
@@ -116,16 +121,17 @@ M.render_node = function(namespace, buf, capture, node)
116121
M.render_node(namespace, buf, query.captures[id], nested_node)
117122
end
118123
elseif capture == 'quote_marker' then
119-
local highlight = highlights.quote
120-
local quote = ts.parent_in_section(node, 'block_quote')
121-
if quote ~= nil then
122-
local callout = component.callout(vim.treesitter.get_node_text(quote, buf), 'contains')
124+
local quote = state.config.quote
125+
local highlight = quote.highlight
126+
local quote_node = ts.parent_in_section(node, 'block_quote')
127+
if quote_node ~= nil then
128+
local callout = component.callout(vim.treesitter.get_node_text(quote_node, buf), 'contains')
123129
if callout ~= nil then
124130
highlight = callout.highlight
125131
end
126132
end
127133

128-
local quote_marker_text = { value:gsub('>', state.config.quote), highlight }
134+
local quote_marker_text = { value:gsub('>', quote.icon), highlight }
129135
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
130136
end_row = end_row,
131137
end_col = end_col,
@@ -134,21 +140,20 @@ M.render_node = function(namespace, buf, capture, node)
134140
})
135141
elseif vim.tbl_contains({ 'checkbox_unchecked', 'checkbox_checked' }, capture) then
136142
local checkbox = state.config.checkbox.unchecked
137-
local highlight = highlights.checkbox.unchecked
138143
if capture == 'checkbox_checked' then
139144
checkbox = state.config.checkbox.checked
140-
highlight = highlights.checkbox.checked
141145
end
142146

143-
local checkbox_text = { str.pad_to(value, checkbox), highlight }
147+
local checkbox_text = { str.pad_to(value, checkbox.icon), checkbox.highlight }
144148
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
145149
end_row = end_row,
146150
end_col = end_col,
147151
virt_text = { checkbox_text },
148152
virt_text_pos = 'overlay',
149153
})
150154
elseif capture == 'table' then
151-
if state.config.table_style ~= 'full' then
155+
local pipe_table = state.config.pipe_table
156+
if pipe_table.style ~= 'full' then
152157
return
153158
end
154159

@@ -157,7 +162,7 @@ M.render_node = function(namespace, buf, capture, node)
157162
---@return integer
158163
local function get_table_row_width(row, s)
159164
local result = vim.fn.strdisplaywidth(s)
160-
if state.config.cell_style == 'raw' then
165+
if pipe_table.cell == 'raw' then
161166
result = result - ts.concealed(buf, row, s)
162167
end
163168
return result
@@ -181,26 +186,27 @@ M.render_node = function(namespace, buf, capture, node)
181186
return string.rep('', vim.fn.strdisplaywidth(part))
182187
end, headings)
183188

184-
local line_above = { { '' .. table.concat(lengths, '') .. '', highlights.table.head } }
189+
local line_above = { { '' .. table.concat(lengths, '') .. '', pipe_table.head } }
185190
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
186191
virt_lines_above = true,
187192
virt_lines = { line_above },
188193
})
189194

190-
local line_below = { { '' .. table.concat(lengths, '') .. '', highlights.table.row } }
195+
local line_below = { { '' .. table.concat(lengths, '') .. '', pipe_table.row } }
191196
vim.api.nvim_buf_set_extmark(buf, namespace, end_row, start_col, {
192197
virt_lines_above = true,
193198
virt_lines = { line_below },
194199
})
195200
end
196201
elseif vim.tbl_contains({ 'table_head', 'table_delim', 'table_row' }, capture) then
197-
if state.config.table_style == 'none' then
202+
local pipe_table = state.config.pipe_table
203+
if pipe_table.style == 'none' then
198204
return
199205
end
200206

201-
local highlight = highlights.table.head
207+
local highlight = pipe_table.head
202208
if capture == 'table_row' then
203-
highlight = highlights.table.row
209+
highlight = pipe_table.row
204210
end
205211

206212
if capture == 'table_delim' then
@@ -220,15 +226,15 @@ M.render_node = function(namespace, buf, capture, node)
220226
virt_text = { table_delim_text },
221227
virt_text_pos = 'overlay',
222228
})
223-
elseif state.config.cell_style == 'overlay' then
229+
elseif pipe_table.cell == 'overlay' then
224230
local table_row_text = { value:gsub('|', ''), highlight }
225231
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
226232
end_row = end_row,
227233
end_col = end_col,
228234
virt_text = { table_row_text },
229235
virt_text_pos = 'overlay',
230236
})
231-
elseif state.config.cell_style == 'raw' then
237+
elseif pipe_table.cell == 'raw' then
232238
for i = 1, #value do
233239
local ch = value:sub(i, i)
234240
if ch == '|' then

lua/render-markdown/handler/markdown_inline.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ end
2121
---@param capture string
2222
---@param node TSNode
2323
M.render_node = function(namespace, buf, capture, node)
24-
local highlights = state.config.highlights
2524
local value = vim.treesitter.get_node_text(node, buf)
2625
local start_row, start_col, end_row, end_col = node:range()
2726
logger.debug_node(capture, node, buf)
2827

2928
if capture == 'code' then
29+
local code = state.config.code
3030
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
3131
end_row = end_row,
3232
end_col = end_col,
33-
hl_group = highlights.code,
33+
hl_group = code.highlight,
3434
})
3535
elseif capture == 'callout' then
3636
local callout = component.callout(value, 'exact')

0 commit comments

Comments
 (0)