Skip to content

Commit 6fbd149

Browse files
chore: add base component class that all others extend
## Details Adds a `BaseComponent` class that is inherited by all major components. Currently only adds the `enabled` flag information, may be extended in the future to add additional common attributes. Aligns configuration validation logic with this new type hierarchy to make any additions easy to validate across all components.
1 parent 032c640 commit 6fbd149

File tree

4 files changed

+63
-79
lines changed

4 files changed

+63
-79
lines changed

lua/render-markdown/health.lua

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

77
---@private
8-
M.version = '7.7.6'
8+
M.version = '7.7.7'
99

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

lua/render-markdown/init.lua

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ local M = {}
1515
---@field public attach? fun(buf: integer)
1616
---@field public render? fun(buf: integer)
1717

18-
---@class (exact) render.md.UserLatex
18+
---@class (exact) render.md.UserBaseComponent
1919
---@field public enabled? boolean
20+
21+
---@class (exact) render.md.UserLatex: render.md.UserBaseComponent
2022
---@field public converter? string
2123
---@field public highlight? string
2224
---@field public top_pad? integer
@@ -37,22 +39,18 @@ local M = {}
3739
---@field public text? string
3840
---@field public highlight? string
3941

40-
---@class (exact) render.md.UserHtml
41-
---@field public enabled? boolean
42+
---@class (exact) render.md.UserHtml: render.md.UserBaseComponent
4243
---@field public comment? render.md.UserHtmlComment
4344

44-
---@class (exact) render.md.UserIndent
45-
---@field public enabled? boolean
45+
---@class (exact) render.md.UserIndent: render.md.UserBaseComponent
4646
---@field public per_level? integer
4747
---@field public skip_level? integer
4848
---@field public skip_heading? boolean
4949

50-
---@class (exact) render.md.UserInlineHighlight
51-
---@field public enabled? boolean
50+
---@class (exact) render.md.UserInlineHighlight: render.md.UserBaseComponent
5251
---@field public highlight? string
5352

54-
---@class (exact) render.md.UserSign
55-
---@field public enabled? boolean
53+
---@class (exact) render.md.UserSign: render.md.UserBaseComponent
5654
---@field public highlight? string
5755

5856
---@class (exact) render.md.UserLinkComponent
@@ -69,8 +67,7 @@ local M = {}
6967
---@field public prefix? string
7068
---@field public suffix? string
7169

72-
---@class (exact) render.md.UserLink
73-
---@field public enabled? boolean
70+
---@class (exact) render.md.UserLink: render.md.UserBaseComponent
7471
---@field public footnote? render.md.UserFootnote
7572
---@field public image? string
7673
---@field public email? string
@@ -89,8 +86,7 @@ local M = {}
8986
---@alias render.md.table.Style 'full'|'normal'|'none'
9087
---@alias render.md.table.Cell 'trimmed'|'padded'|'raw'|'overlay'
9188

92-
---@class (exact) render.md.UserPipeTable
93-
---@field public enabled? boolean
89+
---@class (exact) render.md.UserPipeTable: render.md.UserBaseComponent
9490
---@field public preset? render.md.table.Preset
9591
---@field public style? render.md.table.Style
9692
---@field public cell? render.md.table.Cell
@@ -102,8 +98,7 @@ local M = {}
10298
---@field public row? string
10399
---@field public filler? string
104100

105-
---@class (exact) render.md.UserQuote
106-
---@field public enabled? boolean
101+
---@class (exact) render.md.UserQuote: render.md.UserBaseComponent
107102
---@field public icon? string
108103
---@field public repeat_linebreak? boolean
109104
---@field public highlight? string
@@ -121,8 +116,7 @@ local M = {}
121116

122117
---@alias render.md.checkbox.Position 'overlay'|'inline'
123118

124-
---@class (exact) render.md.UserCheckbox
125-
---@field public enabled? boolean
119+
---@class (exact) render.md.UserCheckbox: render.md.UserBaseComponent
126120
---@field public position? render.md.checkbox.Position
127121
---@field public unchecked? render.md.UserCheckboxComponent
128122
---@field public checked? render.md.UserCheckboxComponent
@@ -133,16 +127,14 @@ local M = {}
133127
---| string[][]
134128
---| fun(level: integer, index: integer, value: string): string?
135129

136-
---@class (exact) render.md.UserBullet
137-
---@field public enabled? boolean
130+
---@class (exact) render.md.UserBullet: render.md.UserBaseComponent
138131
---@field public icons? render.md.bullet.Icons
139132
---@field public ordered_icons? render.md.bullet.Icons
140133
---@field public left_pad? integer
141134
---@field public right_pad? integer
142135
---@field public highlight? string
143136

144-
---@class (exact) render.md.UserDash
145-
---@field public enabled? boolean
137+
---@class (exact) render.md.UserDash: render.md.UserBaseComponent
146138
---@field public icon? string
147139
---@field public width? 'full'|number
148140
---@field public left_margin? number
@@ -153,8 +145,7 @@ local M = {}
153145
---@alias render.md.code.Width 'full'|'block'
154146
---@alias render.md.code.Border 'thin'|'thick'|'none'
155147

156-
---@class (exact) render.md.UserCode
157-
---@field public enabled? boolean
148+
---@class (exact) render.md.UserCode: render.md.UserBaseComponent
158149
---@field public sign? boolean
159150
---@field public style? render.md.code.Style
160151
---@field public position? render.md.code.Position
@@ -173,16 +164,14 @@ local M = {}
173164
---@field public highlight_inline? string
174165
---@field public highlight_language? string
175166

176-
---@class (exact) render.md.UserParagraph
177-
---@field public enabled? boolean
167+
---@class (exact) render.md.UserParagraph: render.md.UserBaseComponent
178168
---@field public left_margin? number
179169
---@field public min_width? integer
180170

181171
---@alias render.md.heading.Position 'overlay'|'inline'|'right'
182172
---@alias render.md.heading.Width 'full'|'block'
183173

184-
---@class (exact) render.md.UserHeading
185-
---@field public enabled? boolean
174+
---@class (exact) render.md.UserHeading: render.md.UserBaseComponent
186175
---@field public sign? boolean
187176
---@field public position? render.md.heading.Position
188177
---@field public icons? string[]

lua/render-markdown/state.lua

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,15 @@ end
127127

128128
---@return string[]
129129
function M.validate()
130+
---@param component render.md.debug.ValidatorSpec
131+
---@return render.md.debug.ValidatorSpec
132+
local function component_rules(component)
133+
return component:type('enabled', 'boolean')
134+
end
135+
130136
---@param buffer render.md.debug.ValidatorSpec
131137
---@return render.md.debug.ValidatorSpec
132-
local function add_buffer_rules(buffer)
138+
local function buffer_rules(buffer)
133139
return buffer
134140
:type('enabled', 'boolean')
135141
:type({ 'max_file_size', 'debounce' }, 'number')
@@ -153,8 +159,8 @@ function M.validate()
153159
padding:type('highlight', 'string'):check()
154160
end)
155161
:nested('heading', function(heading)
156-
heading
157-
:type({ 'enabled', 'sign', 'border_virtual', 'border_prefix' }, 'boolean')
162+
component_rules(heading)
163+
:type({ 'sign', 'border_virtual', 'border_prefix' }, 'boolean')
158164
:type({ 'above', 'below' }, 'string')
159165
:list('border', 'boolean', 'boolean')
160166
:list({ 'left_margin', 'left_pad', 'right_pad', 'min_width' }, 'number', 'number')
@@ -164,10 +170,11 @@ function M.validate()
164170
:check()
165171
end)
166172
:nested('paragraph', function(paragraph)
167-
paragraph:type('enabled', 'boolean'):type({ 'left_margin', 'min_width' }, 'number'):check()
173+
component_rules(paragraph):type({ 'left_margin', 'min_width' }, 'number'):check()
168174
end)
169175
:nested('code', function(code)
170-
code:type({ 'enabled', 'sign', 'language_name' }, 'boolean')
176+
component_rules(code)
177+
:type({ 'sign', 'language_name' }, 'boolean')
171178
:type({ 'language_pad', 'left_margin', 'left_pad', 'right_pad', 'min_width' }, 'number')
172179
:type({ 'above', 'below', 'highlight', 'highlight_inline' }, 'string')
173180
:type('highlight_language', { 'string', 'nil' })
@@ -179,23 +186,21 @@ function M.validate()
179186
:check()
180187
end)
181188
:nested('dash', function(dash)
182-
dash:type('enabled', 'boolean')
189+
component_rules(dash)
183190
:type('left_margin', 'number')
184191
:type({ 'icon', 'highlight' }, 'string')
185192
:one_of('width', { 'full' }, 'number')
186193
:check()
187194
end)
188195
:nested('bullet', function(bullet)
189-
bullet
190-
:type('enabled', 'boolean')
196+
component_rules(bullet)
191197
:type({ 'left_pad', 'right_pad' }, 'number')
192198
:type('highlight', 'string')
193199
:list_or_list_of_list({ 'icons', 'ordered_icons' }, 'string', 'function')
194200
:check()
195201
end)
196202
:nested('checkbox', function(checkbox)
197-
checkbox
198-
:type('enabled', 'boolean')
203+
component_rules(checkbox)
199204
:one_of('position', { 'overlay', 'inline' })
200205
:nested({ 'unchecked', 'checked' }, function(box)
201206
box:type({ 'icon', 'highlight' }, 'string'):type('scope_highlight', { 'string', 'nil' }):check()
@@ -210,11 +215,13 @@ function M.validate()
210215
:check()
211216
end)
212217
:nested('quote', function(quote)
213-
quote:type({ 'enabled', 'repeat_linebreak' }, 'boolean'):type({ 'icon', 'highlight' }, 'string'):check()
218+
component_rules(quote)
219+
:type('repeat_linebreak', 'boolean')
220+
:type({ 'icon', 'highlight' }, 'string')
221+
:check()
214222
end)
215223
:nested('pipe_table', function(pipe_table)
216-
pipe_table
217-
:type('enabled', 'boolean')
224+
component_rules(pipe_table)
218225
:type({ 'padding', 'min_width' }, 'number')
219226
:type({ 'alignment_indicator', 'head', 'row', 'filler' }, 'string')
220227
:list('border', 'string')
@@ -234,7 +241,7 @@ function M.validate()
234241
:check()
235242
end)
236243
:nested('link', function(link)
237-
link:type('enabled', 'boolean')
244+
component_rules(link)
238245
:type({ 'image', 'email', 'hyperlink', 'highlight' }, 'string')
239246
:nested('footnote', function(footnote)
240247
footnote:type('superscript', 'boolean'):type({ 'prefix', 'suffix' }, 'string'):check()
@@ -255,19 +262,19 @@ function M.validate()
255262
:check()
256263
end)
257264
:nested('sign', function(sign)
258-
sign:type('enabled', 'boolean'):type('highlight', 'string'):check()
265+
component_rules(sign):type('highlight', 'string'):check()
259266
end)
260-
:nested('inline_highlight', function(sign)
261-
sign:type('enabled', 'boolean'):type('highlight', 'string'):check()
267+
:nested('inline_highlight', function(inline_highlight)
268+
component_rules(inline_highlight):type('highlight', 'string'):check()
262269
end)
263270
:nested('indent', function(indent)
264-
indent
265-
:type({ 'enabled', 'skip_heading' }, 'boolean')
271+
component_rules(indent)
272+
:type('skip_heading', 'boolean')
266273
:type({ 'per_level', 'skip_level' }, 'number')
267274
:check()
268275
end)
269276
:nested('html', function(html)
270-
html:type('enabled', 'boolean')
277+
component_rules(html)
271278
:nested('comment', function(comment)
272279
comment
273280
:type('conceal', 'boolean')
@@ -288,7 +295,7 @@ function M.validate()
288295

289296
local validator = require('render-markdown.debug.validator').new()
290297

291-
add_buffer_rules(validator:spec(M.config, false))
298+
buffer_rules(validator:spec(M.config, false))
292299
:type('log_runtime', 'boolean')
293300
:list('file_types', 'string')
294301
:one_of('preset', { 'none', 'lazy', 'obsidian' })
@@ -301,8 +308,7 @@ function M.validate()
301308
:check()
302309
end)
303310
:nested('latex', function(latex)
304-
latex
305-
:type('enabled', 'boolean')
311+
component_rules(latex)
306312
:type({ 'top_pad', 'bottom_pad' }, 'number')
307313
:type({ 'converter', 'highlight' }, 'string')
308314
:check()
@@ -315,7 +321,7 @@ function M.validate()
315321
:nested({ 'buftype', 'filetype' }, function(override)
316322
override
317323
:nested('ALL', function(buffer)
318-
add_buffer_rules(buffer):check()
324+
buffer_rules(buffer):check()
319325
end, true)
320326
:check()
321327
end)

0 commit comments

Comments
 (0)