Skip to content

Commit a615bae

Browse files
chore: move configs into individual modules
## Details Rather than storing all type definition in `init.lua` & having all the validation in `state.lua` create individual modules for each of the top level configuration in a config folder and put the validation for these in these modules. Change the configuration naming pattern: - from: `render.md.<module>` / `render.md.User<module>` - to: `render.md.<module>.Config` / `render.md.<module>.UserConfig` Makes writing code to automate the creation of user types easier and more consistent. Users shouldn't really be relying on these but if they are then they'll need to make changes.
1 parent 0ed141a commit a615bae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1408
-1193
lines changed

benches/util.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
---@module 'luassert'
22

3+
local eq = assert.are.same
4+
local truthy = assert.truthy
5+
36
---@class render.md.bench.Util
47
local M = {}
58

@@ -49,14 +52,14 @@ end
4952
---@param actual number
5053
---@param max number
5154
function M.less_than(actual, max)
52-
assert.truthy(actual < max, string.format('expected %f < %f', actual, max))
55+
truthy(actual < max, string.format('expected %f < %f', actual, max))
5356
end
5457

5558
---@param expected integer
5659
function M.num_marks(expected)
5760
local ui = require('render-markdown.core.ui')
5861
local marks = vim.api.nvim_buf_get_extmarks(0, ui.ns, 0, -1, {})
59-
assert.are.same(expected, #marks)
62+
eq(expected, #marks)
6063
end
6164

6265
return M

doc/custom-handlers.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,26 @@ Each handler must conform to the following interface:
1414
```lua
1515
---@class (exact) render.md.Handler
1616
---@field extends? boolean
17-
---@field parse fun(ctx: render.md.HandlerContext): render.md.Mark[]
17+
---@field parse fun(ctx: render.md.handler.Context): render.md.Mark[]
1818

19-
---@class (exact) render.md.HandlerContext
19+
---@class (exact) render.md.handler.Context
2020
---@field buf integer
2121
---@field root TSNode
2222

2323
---@class (exact) render.md.Mark
2424
---@field conceal boolean
2525
---@field start_row integer
2626
---@field start_col integer
27-
---@field opts render.md.MarkOpts
27+
---@field opts render.md.mark.Opts
2828

29-
---@class render.md.MarkOpts: vim.api.keyset.set_extmark
30-
---@field virt_text? render.md.MarkLine
29+
---@class render.md.mark.Opts: vim.api.keyset.set_extmark
30+
---@field virt_text? render.md.mark.Line
3131
---@field virt_text_pos? 'eol'|'inline'|'overlay'
32-
---@field virt_lines? render.md.MarkLine[]
32+
---@field virt_lines? render.md.mark.Line[]
3333

34-
---@alias render.md.MarkLine render.md.MarkText[]
34+
---@alias render.md.mark.Line render.md.mark.Text[]
3535

36-
---@class (exact) render.md.MarkText
36+
---@class (exact) render.md.mark.Text
3737
---@field [1] string text
3838
---@field [2] string|string[] highlights
3939
```
@@ -90,7 +90,7 @@ local function parse_python(ctx)
9090
local marks = {}
9191
for id, node in query:iter_captures(ctx.root, ctx.buf) do
9292
local capture = query.captures[id]
93-
local start_row, _, _, _ = node:range()
93+
local start_row = node:range()
9494
if capture == 'def' then
9595
marks[#marks + 1] = {
9696
conceal = true,

doc/render-markdown.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.11.0 Last change: 2025 April 03
1+
*render-markdown.txt* For 0.11.0 Last change: 2025 April 06
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*

lua/render-markdown/api.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function M.debug()
5050
end
5151

5252
function M.config()
53-
local difference = state.difference(require('render-markdown').default_config)
53+
local difference = state.difference(require('render-markdown').default)
5454
if vim.tbl_count(difference) == 0 then
5555
vim.print('Default Configuration')
5656
else

lua/render-markdown/config.lua

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
local Env = require('render-markdown.lib.env')
22
local Range = require('render-markdown.core.range')
33

4-
---@class render.md.component.Config
5-
---@field callout table<string, render.md.CustomCallout>
6-
---@field checkbox table<string, render.md.CustomCheckbox>
4+
---@class render.md.Components
5+
---@field callout table<string, render.md.callout.Config>
6+
---@field checkbox table<string, render.md.checkbox.custom.Config>
77

8-
---@class render.md.buffer.Config: render.md.BufferConfig
8+
---@class render.md.BufferConfig: render.md.buffer.Config
99
---@field private modes render.md.Modes
10-
---@field private component render.md.component.Config
10+
---@field private components render.md.Components
1111
local Config = {}
1212
Config.__index = Config
1313

14-
---@param config render.md.BufferConfig
15-
---@return render.md.buffer.Config
14+
---@param config render.md.buffer.Config
15+
---@return render.md.BufferConfig
1616
function Config.new(config)
1717
-- Super set of render modes across top level and individual components
1818
local modes = config.render_modes
@@ -22,16 +22,42 @@ function Config.new(config)
2222
end
2323
end
2424

25-
---@type render.md.component.Config
26-
local component = {
25+
---@type render.md.Components
26+
local components = {
2727
callout = Config.normalize(config.callout),
2828
checkbox = Config.normalize(config.checkbox.custom),
2929
}
3030

31-
local instance = vim.tbl_deep_extend('force', { modes = modes, component = component }, config)
31+
local instance = vim.tbl_deep_extend('force', { modes = modes, components = components }, config)
3232
return setmetatable(instance, Config)
3333
end
3434

35+
---@param spec render.md.debug.ValidatorSpec
36+
---@return render.md.debug.ValidatorSpec
37+
function Config.validate(spec)
38+
require('render-markdown.config.base').validate(spec)
39+
return spec:type('max_file_size', 'number')
40+
:type('debounce', 'number')
41+
:nested('anti_conceal', require('render-markdown.config.anti_conceal').validate)
42+
:nested('bullet', require('render-markdown.config.bullet').validate)
43+
:nested('callout', require('render-markdown.config.callout').validate)
44+
:nested('checkbox', require('render-markdown.config.checkbox').validate)
45+
:nested('code', require('render-markdown.config.code').validate)
46+
:nested('dash', require('render-markdown.config.dash').validate)
47+
:nested('heading', require('render-markdown.config.heading').validate)
48+
:nested('html', require('render-markdown.config.html').validate)
49+
:nested('indent', require('render-markdown.config.indent').validate)
50+
:nested('inline_highlight', require('render-markdown.config.inline_highlight').validate)
51+
:nested('latex', require('render-markdown.config.latex').validate)
52+
:nested('link', require('render-markdown.config.link').validate)
53+
:nested('padding', require('render-markdown.config.padding').validate)
54+
:nested('paragraph', require('render-markdown.config.paragraph').validate)
55+
:nested('pipe_table', require('render-markdown.config.pipe_table').validate)
56+
:nested('quote', require('render-markdown.config.quote').validate)
57+
:nested('sign', require('render-markdown.config.sign').validate)
58+
:nested('win_options', require('render-markdown.config.win_options').validate)
59+
end
60+
3561
---@private
3662
---@param current render.md.Modes
3763
---@param new? render.md.Modes
@@ -56,7 +82,7 @@ function Config.fold_modes(current, new)
5682
end
5783

5884
---@private
59-
---@generic T: render.md.CustomCallout|render.md.CustomCheckbox
85+
---@generic T: render.md.callout.Config|render.md.checkbox.custom.Config
6086
---@param components table<string, T>
6187
---@return table<string, T>
6288
function Config.normalize(components)
@@ -74,15 +100,15 @@ function Config:render(mode)
74100
end
75101

76102
---@param node render.md.Node
77-
---@return render.md.CustomCallout?
103+
---@return render.md.callout.Config?
78104
function Config:get_callout(node)
79-
return self.component.callout[node.text:lower()]
105+
return self.components.callout[node.text:lower()]
80106
end
81107

82108
---@param node render.md.Node
83-
---@return render.md.CustomCheckbox?
109+
---@return render.md.checkbox.custom.Config?
84110
function Config:get_checkbox(node)
85-
return self.component.checkbox[node.text:lower()]
111+
return self.components.checkbox[node.text:lower()]
86112
end
87113

88114
---@param mode string
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---@class (exact) render.md.anti.conceal.Config
2+
---@field enabled boolean
3+
---@field ignore render.md.conceal.Ignore
4+
---@field above integer
5+
---@field below integer
6+
7+
---@alias render.md.conceal.Ignore table<render.md.Element, render.md.Modes>
8+
9+
---@enum (key) render.md.Element
10+
local element = {
11+
head_icon = 'head_icon',
12+
head_background = 'head_background',
13+
head_border = 'head_border',
14+
code_language = 'code_language',
15+
code_background = 'code_background',
16+
code_border = 'code_border',
17+
dash = 'dash',
18+
bullet = 'bullet',
19+
check_icon = 'check_icon',
20+
check_scope = 'check_scope',
21+
quote = 'quote',
22+
table_border = 'table_border',
23+
callout = 'callout',
24+
link = 'link',
25+
sign = 'sign',
26+
}
27+
28+
local M = {}
29+
30+
---@param spec render.md.debug.ValidatorSpec
31+
function M.validate(spec)
32+
spec:type('enabled', 'boolean')
33+
:nested('ignore', function(ignore)
34+
ignore:list(vim.tbl_keys(element), 'string', { 'boolean', 'nil' }):check()
35+
end)
36+
:type('above', 'number')
37+
:type('below', 'number')
38+
:check()
39+
end
40+
41+
return M

lua/render-markdown/config/base.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---@class (exact) render.md.base.Config
2+
---@field enabled boolean
3+
---@field render_modes render.md.Modes
4+
5+
---@alias render.md.Modes boolean|string[]
6+
7+
local M = {}
8+
9+
---@param spec render.md.debug.ValidatorSpec
10+
function M.validate(spec)
11+
spec:type('enabled', 'boolean'):list('render_modes', 'string', 'boolean')
12+
end
13+
14+
return M

lua/render-markdown/config/bullet.lua

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---@class (exact) render.md.bullet.Config: render.md.base.Config
2+
---@field icons render.md.bullet.Text
3+
---@field ordered_icons render.md.bullet.Text
4+
---@field left_pad render.md.bullet.Int
5+
---@field right_pad render.md.bullet.Int
6+
---@field highlight render.md.bullet.Text
7+
---@field scope_highlight render.md.bullet.Text
8+
9+
---@class (exact) render.md.bullet.Context
10+
---@field level integer
11+
---@field index integer
12+
---@field value string
13+
14+
---@alias render.md.bullet.Text
15+
---| string
16+
---| string[]
17+
---| string[][]
18+
---| fun(ctx: render.md.bullet.Context): string?
19+
20+
---@alias render.md.bullet.Int
21+
---| integer
22+
---| fun(ctx: render.md.bullet.Context): integer
23+
24+
local M = {}
25+
26+
---@param spec render.md.debug.ValidatorSpec
27+
function M.validate(spec)
28+
require('render-markdown.config.base').validate(spec)
29+
spec:nested_list('icons', 'string', 'function')
30+
:nested_list('ordered_icons', 'string', 'function')
31+
:type('left_pad', { 'number', 'function' })
32+
:type('right_pad', { 'number', 'function' })
33+
:nested_list('highlight', 'string', 'function')
34+
:nested_list('scope_highlight', 'string', 'function')
35+
:check()
36+
end
37+
38+
return M
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---@class (exact) render.md.callout.Config
2+
---@field raw string
3+
---@field rendered string
4+
---@field highlight string
5+
---@field quote_icon? string
6+
---@field category? string
7+
8+
local M = {}
9+
10+
---@param spec render.md.debug.ValidatorSpec
11+
function M.validate(spec)
12+
spec:each(function(callout)
13+
callout
14+
:type('raw', 'string')
15+
:type('rendered', 'string')
16+
:type('highlight', 'string')
17+
:type('quote_icon', { 'string', 'nil' })
18+
:type('category', { 'string', 'nil' })
19+
:check()
20+
end, false):check()
21+
end
22+
23+
return M
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---@class (exact) render.md.checkbox.Config: render.md.base.Config
2+
---@field right_pad integer
3+
---@field unchecked render.md.checkbox.component.Config
4+
---@field checked render.md.checkbox.component.Config
5+
---@field custom table<string, render.md.checkbox.custom.Config>
6+
7+
---@class (exact) render.md.checkbox.component.Config
8+
---@field icon string
9+
---@field highlight string
10+
---@field scope_highlight? string
11+
12+
---@class (exact) render.md.checkbox.custom.Config
13+
---@field raw string
14+
---@field rendered string
15+
---@field highlight string
16+
---@field scope_highlight? string
17+
18+
local M = {}
19+
20+
---@param spec render.md.debug.ValidatorSpec
21+
function M.validate(spec)
22+
require('render-markdown.config.base').validate(spec)
23+
spec:type('right_pad', 'number')
24+
:nested({ 'unchecked', 'checked' }, function(box)
25+
box:type('icon', 'string'):type('highlight', 'string'):type('scope_highlight', { 'string', 'nil' }):check()
26+
end)
27+
:nested('custom', function(boxes)
28+
boxes:each(function(box)
29+
box:type('raw', 'string')
30+
:type('rendered', 'string')
31+
:type('highlight', 'string')
32+
:type('scope_highlight', { 'string', 'nil' })
33+
:check()
34+
end)
35+
end)
36+
:check()
37+
end
38+
39+
return M

0 commit comments

Comments
 (0)