Skip to content

Commit 1d72b63

Browse files
Update healthcheck to catch additional parameters, add excluded buftypes
# Details Rendering on LSP buffers may not be a desired behavior. Disable it by default with new `exclude.buftypes` value 'nofile'. Users can re-enable if desired. Update healthcheck logic to check that additional keys are not defined in places they are not expected. Move validate logic into state, write some unit tests as sanity check.
1 parent c0956b3 commit 1d72b63

File tree

9 files changed

+274
-189
lines changed

9 files changed

+274
-189
lines changed

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ require('render-markdown').setup({
160160
-- Vim modes that will show a rendered view of the markdown file
161161
-- All other modes will be uneffected by this plugin
162162
render_modes = { 'n', 'c' },
163+
exclude = {
164+
-- Buftypes ignored by this plugin, see :h 'buftype'
165+
buftypes = { 'nofile' },
166+
},
163167
latex = {
164168
-- Whether LaTeX should be rendered, mainly used for health check
165169
enabled = true,

Diff for: doc/render-markdown.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 July 10
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 July 11
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -197,6 +197,10 @@ Full Default Configuration ~
197197
-- Vim modes that will show a rendered view of the markdown file
198198
-- All other modes will be uneffected by this plugin
199199
render_modes = { 'n', 'c' },
200+
exclude = {
201+
-- Buftypes ignored by this plugin, see :h 'buftype'
202+
buftypes = { 'nofile' },
203+
},
200204
latex = {
201205
-- Whether LaTeX should be rendered, mainly used for health check
202206
enabled = true,

Diff for: lua/render-markdown/health.lua

+1-186
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local M = {}
44

55
function M.check()
66
vim.health.start('markdown.nvim [configuration]')
7-
local errors = M.check_config(state.config)
7+
local errors = state.validate()
88
if #errors == 0 then
99
vim.health.ok('valid')
1010
end
@@ -44,191 +44,6 @@ function M.check()
4444
end
4545
end
4646

47-
---@param config render.md.Config
48-
---@return string[]
49-
function M.check_config(config)
50-
local errors = {}
51-
52-
---@param value string
53-
---@param valid_values string[]
54-
---@return vim.validate.Spec
55-
local function one_of(value, valid_values)
56-
return {
57-
value,
58-
function(v)
59-
return vim.tbl_contains(valid_values, v)
60-
end,
61-
'one of ' .. vim.inspect(valid_values),
62-
}
63-
end
64-
65-
---@param path string?
66-
---@param opts table<string, vim.validate.Spec>
67-
local function append_errors(path, opts)
68-
local ok, err = pcall(vim.validate, opts)
69-
if not ok then
70-
if path == nil then
71-
table.insert(errors, err)
72-
else
73-
table.insert(errors, path .. '.' .. err)
74-
end
75-
end
76-
end
77-
78-
---@param path string
79-
---@param values string[]
80-
local function all_strings(path, values)
81-
for i, value in ipairs(values) do
82-
append_errors(path, {
83-
[tostring(i)] = { value, 'string' },
84-
})
85-
end
86-
end
87-
88-
append_errors(nil, {
89-
enabled = { config.enabled, 'boolean' },
90-
max_file_size = { config.max_file_size, 'number' },
91-
markdown_query = { config.markdown_query, 'string' },
92-
markdown_quote_query = { config.markdown_quote_query, 'string' },
93-
inline_query = { config.inline_query, 'string' },
94-
log_level = one_of(config.log_level, { 'debug', 'error' }),
95-
file_types = { config.file_types, 'table' },
96-
render_modes = { config.render_modes, 'table' },
97-
latex = { config.latex, 'table' },
98-
heading = { config.heading, 'table' },
99-
code = { config.code, 'table' },
100-
dash = { config.dash, 'table' },
101-
bullet = { config.bullet, 'table' },
102-
pipe_table = { config.pipe_table, 'table' },
103-
checkbox = { config.checkbox, 'table' },
104-
quote = { config.quote, 'table' },
105-
callout = { config.callout, 'table' },
106-
link = { config.link, 'table' },
107-
win_options = { config.win_options, 'table' },
108-
custom_handlers = { config.custom_handlers, 'table' },
109-
})
110-
111-
all_strings('file_types', config.file_types)
112-
all_strings('render_modes', config.render_modes)
113-
114-
local latex = config.latex
115-
append_errors('latex', {
116-
enabled = { latex.enabled, 'boolean' },
117-
converter = { latex.converter, 'string' },
118-
highlight = { latex.highlight, 'string' },
119-
})
120-
121-
local heading = config.heading
122-
append_errors('heading', {
123-
enabled = { heading.enabled, 'boolean' },
124-
icons = { heading.icons, 'table' },
125-
signs = { heading.signs, 'table' },
126-
backgrounds = { heading.backgrounds, 'table' },
127-
foregrounds = { heading.foregrounds, 'table' },
128-
})
129-
all_strings('heading.icons', heading.icons)
130-
all_strings('heading.signs', heading.signs)
131-
all_strings('heading.backgrounds', heading.backgrounds)
132-
all_strings('heading.foregrounds', heading.foregrounds)
133-
134-
local code = config.code
135-
append_errors('code', {
136-
enabled = { code.enabled, 'boolean' },
137-
style = one_of(code.style, { 'full', 'language', 'normal', 'none' }),
138-
highlight = { code.highlight, 'string' },
139-
})
140-
141-
local dash = config.dash
142-
append_errors('dash', {
143-
enabled = { dash.enabled, 'boolean' },
144-
icon = { dash.icon, 'string' },
145-
highlight = { dash.highlight, 'string' },
146-
})
147-
148-
local bullet = config.bullet
149-
append_errors('bullet', {
150-
enabled = { bullet.enabled, 'boolean' },
151-
icons = { bullet.icons, 'table' },
152-
highlight = { bullet.highlight, 'string' },
153-
})
154-
all_strings('bullet.icons', bullet.icons)
155-
156-
local checkbox = config.checkbox
157-
append_errors('checkbox', {
158-
enabled = { checkbox.enabled, 'boolean' },
159-
unchecked = { checkbox.unchecked, 'table' },
160-
checked = { checkbox.checked, 'table' },
161-
custom = { checkbox.custom, 'table' },
162-
})
163-
local unchecked = checkbox.unchecked
164-
append_errors('checkbox.unchecked', {
165-
icon = { unchecked.icon, 'string' },
166-
highlight = { unchecked.highlight, 'string' },
167-
})
168-
local checked = checkbox.checked
169-
append_errors('checkbox.checked', {
170-
icon = { checked.icon, 'string' },
171-
highlight = { checked.highlight, 'string' },
172-
})
173-
for name, component in pairs(checkbox.custom) do
174-
append_errors('checkbox.custom.' .. name, {
175-
raw = { component.raw, 'string' },
176-
rendered = { component.rendered, 'string' },
177-
highlight = { component.highlight, 'string' },
178-
})
179-
end
180-
181-
local quote = config.quote
182-
append_errors('quote', {
183-
enabled = { quote.enabled, 'boolean' },
184-
icon = { quote.icon, 'string' },
185-
highlight = { quote.highlight, 'string' },
186-
})
187-
188-
local pipe_table = config.pipe_table
189-
append_errors('pipe_table', {
190-
enabled = { pipe_table.enabled, 'boolean' },
191-
style = one_of(pipe_table.style, { 'full', 'normal', 'none' }),
192-
cell = one_of(pipe_table.cell, { 'overlay', 'raw' }),
193-
border = { pipe_table.border, 'table' },
194-
head = { pipe_table.head, 'string' },
195-
row = { pipe_table.row, 'string' },
196-
})
197-
all_strings('pipe_table.border', pipe_table.border)
198-
199-
for name, component in pairs(config.callout) do
200-
append_errors('callout.' .. name, {
201-
raw = { component.raw, 'string' },
202-
rendered = { component.rendered, 'string' },
203-
highlight = { component.highlight, 'string' },
204-
})
205-
end
206-
207-
local link = config.link
208-
append_errors('link', {
209-
enabled = { link.enabled, 'boolean' },
210-
image = { link.image, 'string' },
211-
hyperlink = { link.hyperlink, 'string' },
212-
highlight = { link.highlight, 'string' },
213-
})
214-
215-
for name, win_option in pairs(config.win_options) do
216-
append_errors('win_options.' .. name, {
217-
default = { win_option.default, { 'number', 'string' } },
218-
rendered = { win_option.rendered, { 'number', 'string' } },
219-
})
220-
end
221-
222-
for name, handler in pairs(config.custom_handlers) do
223-
append_errors('custom_handlers.' .. name, {
224-
render = { handler.render, 'function' },
225-
extends = { handler.extends, 'boolean', true },
226-
})
227-
end
228-
229-
return errors
230-
end
231-
23247
---@param name string
23348
---@param advice string?
23449
function M.check_parser(name, advice)

Diff for: lua/render-markdown/init.lua

+8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ local M = {}
6767
---@field public converter? string
6868
---@field public highlight? string
6969

70+
---@class render.md.UserExclude
71+
---@field public buftypes? string[]
72+
7073
---@class render.md.UserConfig
7174
---@field public enabled? boolean
7275
---@field public max_file_size? number
@@ -76,6 +79,7 @@ local M = {}
7679
---@field public log_level? 'debug'|'error'
7780
---@field public file_types? string[]
7881
---@field public render_modes? string[]
82+
---@field public exclude? render.md.UserExclude
7983
---@field public latex? render.md.UserLatex
8084
---@field public heading? render.md.UserHeading
8185
---@field public code? render.md.UserCode
@@ -150,6 +154,10 @@ M.default_config = {
150154
-- Vim modes that will show a rendered view of the markdown file
151155
-- All other modes will be uneffected by this plugin
152156
render_modes = { 'n', 'c' },
157+
exclude = {
158+
-- Buftypes ignored by this plugin, see :h 'buftype'
159+
buftypes = { 'nofile' },
160+
},
153161
latex = {
154162
-- Whether LaTeX should be rendered, mainly used for health check
155163
enabled = true,

Diff for: lua/render-markdown/manager.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ function M.setup()
2020
group = group,
2121
pattern = state.config.file_types,
2222
callback = function(event)
23-
M.attach(group, event.buf)
23+
local buftype = vim.api.nvim_get_option_value('buftype', { buf = event.buf })
24+
if not vim.tbl_contains(state.config.exclude.buftypes, buftype) then
25+
M.attach(group, event.buf)
26+
end
2427
end,
2528
})
2629
-- Window resizing is not buffer specific so is managed more globablly

0 commit comments

Comments
 (0)