Skip to content

Commit a2788a8

Browse files
Add latex parser as optional and update health check
## Details The requirements to get latex support working for this plugin are not clear as shown by: #32. Update the README to add the missing need for the latex parser. More importantly the current health check logic only checked for the minimum needs of this plugin as there was no way to indicate whether the user wanted latex support or not. Add a `latex_enabled` flag which defaults to true. Functionally it does allow enabling / disabling the latex handler but it is mainly used in the updated health check logic to check for the additional requirements needed for latex, i.e. the parser and the latex2text executable. Setting the flag to false allows users to remove these warnings if they do not want latex support by this plugin.
1 parent 3c6a0e1 commit a2788a8

File tree

7 files changed

+95
-53
lines changed

7 files changed

+95
-53
lines changed

Diff for: README.md

+12-7
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,21 @@ Plugin to improve viewing Markdown files in Neovim
2121
- Replaces checkboxes with provided characters based on whether they are checked
2222
- Replaces block quote leading `>` with provided character
2323
- Updates table borders with better border characters, does NOT automatically align
24-
- Basic support for `LaTeX` if `pylatexenc` is installed on system
24+
- Basic support for `LaTeX` if `latex` parser and `pylatexenc` are installed
2525
- Disable rendering when file is larger than provided value
2626
- Support for [callouts](https://github.com/orgs/community/discussions/16925)
2727
- Support custom handlers which are ran identically to native handlers
2828

2929
# Dependencies
3030

31-
- [markdown & markdown_inline](https://github.com/tree-sitter-grammars/tree-sitter-markdown)
32-
parsers for [treesitter](https://github.com/nvim-treesitter/nvim-treesitter):
33-
Used to parse `markdown` files
34-
- [pylatexenc](https://pypi.org/project/pylatexenc/) (Optional): Used to
35-
transform `LaTeX` strings to appropriate unicode using `latex2text`
31+
- [treesitter](https://github.com/nvim-treesitter/nvim-treesitter) parsers:
32+
- [markdown & markdown_inline](https://github.com/tree-sitter-grammars/tree-sitter-markdown):
33+
Used to parse `markdown` files
34+
- [latex](https://github.com/latex-lsp/tree-sitter-latex) (Optional):
35+
Used to get `LaTeX` blocks from `markdown` files
36+
- System dependencies:
37+
- [pylatexenc](https://pypi.org/project/pylatexenc/) (Optional):
38+
Used to transform `LaTeX` strings to appropriate unicode using `latex2text`
3639

3740
# Install
3841

@@ -69,8 +72,10 @@ by the user.
6972

7073
```lua
7174
require('render-markdown').setup({
72-
-- Configure whether Markdown should be rendered by default or not
75+
-- Whether Markdown should be rendered by default or not
7376
start_enabled = true,
77+
-- Whether LaTeX should be rendered, mainly used for health check
78+
latex_enabled = true,
7479
-- Maximum file size (in MB) that this plugin will attempt to render
7580
-- Any file larger than this will effectively be ignored
7681
max_file_size = 1.5,

Diff for: doc/render-markdown.txt

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 June 03
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 June 05
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -46,7 +46,7 @@ Plugin to improve viewing Markdown files in Neovim
4646
- Replaces checkboxes with provided characters based on whether they are checked
4747
- Replaces block quote leading `>` with provided character
4848
- Updates table borders with better border characters, does NOT automatically align
49-
- Basic support for `LaTeX` if `pylatexenc` is installed on system
49+
- Basic support for `LaTeX` if `latex` parser and `pylatexenc` are installed
5050
- Disable rendering when file is larger than provided value
5151
- Support for callouts <https://github.com/orgs/community/discussions/16925>
5252
- Support custom handlers which are ran identically to native handlers
@@ -55,11 +55,14 @@ Plugin to improve viewing Markdown files in Neovim
5555
==============================================================================
5656
3. Dependencies *render-markdown-dependencies*
5757

58-
- markdown & markdown_inline <https://github.com/tree-sitter-grammars/tree-sitter-markdown>
59-
parsers for treesitter <https://github.com/nvim-treesitter/nvim-treesitter>:
60-
Used to parse `markdown` files
61-
- pylatexenc <https://pypi.org/project/pylatexenc/> (Optional): Used to
62-
transform `LaTeX` strings to appropriate unicode using `latex2text`
58+
- treesitter <https://github.com/nvim-treesitter/nvim-treesitter> parsers:
59+
- markdown & markdown_inline <https://github.com/tree-sitter-grammars/tree-sitter-markdown>:
60+
Used to parse `markdown` files
61+
- latex <https://github.com/latex-lsp/tree-sitter-latex> (Optional):
62+
Used to get `LaTeX` blocks from `markdown` files
63+
- System dependencies:
64+
- pylatexenc <https://pypi.org/project/pylatexenc/> (Optional):
65+
Used to transform `LaTeX` strings to appropriate unicode using `latex2text`
6366

6467

6568
==============================================================================
@@ -102,8 +105,10 @@ modified by the user.
102105

103106
>lua
104107
require('render-markdown').setup({
105-
-- Configure whether Markdown should be rendered by default or not
108+
-- Whether Markdown should be rendered by default or not
106109
start_enabled = true,
110+
-- Whether LaTeX should be rendered, mainly used for health check
111+
latex_enabled = true,
107112
-- Maximum file size (in MB) that this plugin will attempt to render
108113
-- Any file larger than this will effectively be ignored
109114
max_file_size = 1.5,

Diff for: lua/render-markdown/handler/latex.lua

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ M.render = function(namespace, root, buf)
2020
logger.debug('Executable not found: ' .. converter)
2121
return
2222
end
23-
logger.debug('Executable found: ' .. converter)
2423

2524
local value = vim.treesitter.get_node_text(root, buf)
2625
local start_row, start_col, end_row, end_col = root:range()

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

+64-35
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,96 @@
11
local md = require('render-markdown')
22
local state = require('render-markdown.state')
33

4-
local function validate_treesitter()
5-
local ok, ts = pcall(require, 'nvim-treesitter.parsers')
6-
if not ok then
7-
vim.health.error('treesitter is not installed')
8-
return
9-
end
10-
vim.health.ok('treesitter is installed')
4+
local M = {}
5+
6+
function M.check()
7+
local latex_advice = 'If you do not want LaTeX support avoid this warning by setting { latex_enabled = false }'
118

12-
for _, name in ipairs({ 'markdown', 'markdown_inline' }) do
13-
if ts.has_parser(name) then
14-
vim.health.ok(name .. ' parser installed')
9+
vim.health.start('markdown.nvim [nvim-treesitter]')
10+
local ok = pcall(require, 'nvim-treesitter')
11+
if ok then
12+
vim.health.ok('installed')
13+
14+
M.check_parser('markdown')
15+
M.check_parser('markdown_inline')
16+
if state.config.latex_enabled then
17+
M.check_parser('latex', latex_advice)
18+
end
19+
20+
local highlight = require('nvim-treesitter.configs').get_module('highlight')
21+
if highlight ~= nil and highlight.enable then
22+
vim.health.ok('highlights enabled')
1523
else
16-
vim.health.error(name .. ' parser not installed')
24+
vim.health.error('highlights not enabled')
1725
end
26+
else
27+
vim.health.error('not installed')
28+
end
29+
30+
vim.health.start('markdown.nvim [executables]')
31+
if state.config.latex_enabled then
32+
M.check_executable(state.config.latex_converter, latex_advice)
33+
else
34+
vim.health.ok('none to check')
35+
end
36+
37+
vim.health.start('markdown.nvim [configuration]')
38+
local errors = M.check_keys(md.default_config, state.config, {})
39+
if #errors == 0 then
40+
vim.health.ok('valid')
41+
end
42+
for _, message in ipairs(errors) do
43+
vim.health.error(message)
44+
end
45+
end
46+
47+
---@param name string
48+
---@param advice string?
49+
function M.check_parser(name, advice)
50+
local parsers = require('nvim-treesitter.parsers')
51+
if parsers.has_parser(name) then
52+
vim.health.ok(name .. ': parser installed')
53+
elseif advice == nil then
54+
vim.health.error(name .. ': parser not installed')
55+
else
56+
vim.health.warn(name .. ': parser not installed', advice)
1857
end
58+
end
1959

20-
local highlight = require('nvim-treesitter.configs').get_module('highlight')
21-
if highlight ~= nil and highlight.enable then
22-
vim.health.ok('treesitter highlights enabled')
60+
---@param name string
61+
---@param advice string?
62+
function M.check_executable(name, advice)
63+
if vim.fn.executable(name) == 1 then
64+
vim.health.ok(name .. ': installed')
65+
elseif advice == nil then
66+
vim.health.error(name .. ': not installed')
2367
else
24-
vim.health.error('treesitter highlights not enabled')
68+
vim.health.warn(name .. ': not installed', advice)
2569
end
2670
end
2771

2872
---@param t1 table<any, any>
2973
---@param t2 table<any, any>
3074
---@param path string[]
3175
---@return string[]
32-
local function check_keys(t1, t2, path)
76+
function M.check_keys(t1, t2, path)
3377
local errors = {}
3478
for k, v2 in pairs(t2) do
3579
local v1 = t1[k]
3680
local key_path = vim.list_extend(vim.list_extend({}, path), { k })
37-
local key = vim.fn.join(key_path, ' -> ')
81+
local key = vim.fn.join(key_path, '.')
3882
if v1 == nil then
39-
table.insert(errors, string.format('Invalid parameter: %s', key))
83+
table.insert(errors, string.format('Invalid key: %s', key))
4084
elseif type(v1) ~= type(v2) then
41-
table.insert(errors, string.format('Invalid type: %s, expected %s but found %s', key, type(v1), type(v2)))
85+
table.insert(errors, string.format('Invalid type: %s, expected %s, found %s', key, type(v1), type(v2)))
4286
elseif type(v1) == 'table' and type(v2) == 'table' then
4387
-- Some tables are meant to have unrestricted keys
4488
if not vim.list_contains({ 'win_options', 'custom_handlers' }, k) then
45-
vim.list_extend(errors, check_keys(v1, v2, key_path))
89+
vim.list_extend(errors, M.check_keys(v1, v2, key_path))
4690
end
4791
end
4892
end
4993
return errors
5094
end
5195

52-
local M = {}
53-
54-
function M.check()
55-
vim.health.start('Validating treesitter parsers & settings')
56-
validate_treesitter()
57-
vim.health.start('Validating configuration')
58-
local errors = check_keys(md.default_config, state.config, {})
59-
if #errors == 0 then
60-
vim.health.ok('Configuration is valid')
61-
end
62-
for _, message in ipairs(errors) do
63-
vim.health.error(message)
64-
end
65-
end
66-
6796
return M

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ local M = {}
4747

4848
---@class render.md.UserConfig
4949
---@field public start_enabled? boolean
50+
---@field public latex_enabled? boolean
5051
---@field public max_file_size? number
5152
---@field public markdown_query? string
5253
---@field public inline_query? string
@@ -67,8 +68,10 @@ local M = {}
6768

6869
---@type render.md.Config
6970
M.default_config = {
70-
-- Configure whether Markdown should be rendered by default or not
71+
-- Whether Markdown should be rendered by default or not
7172
start_enabled = true,
73+
-- Whether LaTeX should be rendered, mainly used for health check
74+
latex_enabled = true,
7275
-- Maximum file size (in MB) that this plugin will attempt to render
7376
-- Any file larger than this will effectively be ignored
7477
max_file_size = 1.5,

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

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
---@class render.md.Config
3636
---@field public start_enabled boolean
37+
---@field public latex_enabled boolean
3738
---@field public max_file_size number
3839
---@field public markdown_query string
3940
---@field public inline_query string

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ M.refresh = function(buf)
4040
markdown.render(M.namespace, tree:root(), buf)
4141
elseif language == 'markdown_inline' then
4242
markdown_inline.render(M.namespace, tree:root(), buf)
43-
elseif language == 'latex' then
43+
elseif language == 'latex' and state.config.latex_enabled then
4444
latex.render(M.namespace, tree:root(), buf)
4545
else
4646
logger.debug('No handler found')

0 commit comments

Comments
 (0)