Skip to content

Commit e96f40d

Browse files
Disable rendering for large files
# Details Adds a config parameter `max_file_size` that gets compared to the file size in megabytes of the current buffer. If the file is larger than this maximum size then this plugin essentially becomes a NOOP. Set the default value relatively high at 1.5 MB, at this value performance gets pretty bad so no one should be using this plugin with such files. Allows users to tune the value based on their setup. Add some `just` recipes to generate a large markdown file for testing.
1 parent 0581a9a commit e96f40d

File tree

8 files changed

+58
-22
lines changed

8 files changed

+58
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
large.md

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Plugin to improve viewing Markdown files in Neovim
1818
- Replaces block quote leading `>` with provided character
1919
- Updates table borders with better border characters, does NOT automatically align
2020
- Basic support for `LaTeX` if `pylatexenc` is installed on system
21+
- Disable rendering when file is larger than provided value
2122

2223
# Dependencies
2324

@@ -64,6 +65,9 @@ by the user.
6465
require('render-markdown').setup({
6566
-- Configure whether Markdown should be rendered by default or not
6667
start_enabled = true,
68+
-- Maximum file size (in MB) that this plugin will attempt to render
69+
-- Any file larger than this will effectively be ignored
70+
max_file_size = 1.5,
6771
-- Capture groups that get pulled from markdown
6872
markdown_query = [[
6973
(atx_heading [

doc/render-markdown.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 May 16
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 May 17
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -39,6 +39,7 @@ Plugin to improve viewing Markdown files in Neovim
3939
- Replaces block quote leading `>` with provided character
4040
- Updates table borders with better border characters, does NOT automatically align
4141
- Basic support for `LaTeX` if `pylatexenc` is installed on system
42+
- Disable rendering when file is larger than provided value
4243

4344

4445
==============================================================================
@@ -93,6 +94,9 @@ modified by the user.
9394
require('render-markdown').setup({
9495
-- Configure whether Markdown should be rendered by default or not
9596
start_enabled = true,
97+
-- Maximum file size (in MB) that this plugin will attempt to render
98+
-- Any file larger than this will effectively be ignored
99+
max_file_size = 1.5,
96100
-- Capture groups that get pulled from markdown
97101
markdown_query = [[
98102
(atx_heading [

justfile

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,35 @@ init := "tests/minimal.lua"
22
default_zoom := '2'
33

44
test:
5-
nvim --headless --noplugin -u {{init}} \
6-
-c "PlenaryBustedDirectory tests { minimal_init = '{{init}}', sequential=true }"
5+
nvim --headless --noplugin -u {{init}} \
6+
-c "PlenaryBustedDirectory tests { minimal_init = '{{init}}', sequential=true }"
77

88
demo zoom=default_zoom:
9-
rm -f demo/demo.gif
10-
python demo/record.py \
11-
--zoom {{zoom}} \
12-
--file demo/sample.md \
13-
--cast demo.cast
14-
# https://docs.asciinema.org/manual/agg/usage/
15-
agg demo.cast demo/demo.gif \
16-
--font-family "Monaspace Neon,Hack Nerd Font" \
17-
--last-frame-duration 1
18-
rm demo.cast
9+
rm -f demo/demo.gif
10+
python demo/record.py \
11+
--zoom {{zoom}} \
12+
--file demo/sample.md \
13+
--cast demo.cast
14+
# https://docs.asciinema.org/manual/agg/usage/
15+
agg demo.cast demo/demo.gif \
16+
--font-family "Monaspace Neon,Hack Nerd Font" \
17+
--last-frame-duration 1
18+
rm demo.cast
1919

20-
docgen:
21-
# https://github.com/kdheepak/panvimdoc
22-
# https://pandoc.org/
23-
../../open-source/panvimdoc/panvimdoc.sh \
24-
--project-name render-markdown \
25-
--input-file README.md \
26-
--vim-version 0.10.0
20+
gen-doc:
21+
# https://github.com/kdheepak/panvimdoc
22+
# https://pandoc.org/
23+
../../open-source/panvimdoc/panvimdoc.sh \
24+
--project-name render-markdown \
25+
--input-file README.md \
26+
--vim-version 0.10.0
27+
28+
[private]
29+
gen-file-text:
30+
#!/usr/bin/env python
31+
for i in range(100_000):
32+
level = "#" * ((i % 6) + 1)
33+
print(f"{level} Title {i}\n")
34+
35+
gen-large-file:
36+
just gen-file-text > large.md

lua/render-markdown/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ local M = {}
3535

3636
---@class UserConfig
3737
---@field public start_enabled? boolean
38+
---@field public max_file_size? number
3839
---@field public markdown_query? string
3940
---@field public inline_query? string
4041
---@field public log_level? 'debug'|'error'
@@ -54,6 +55,7 @@ function M.setup(opts)
5455
---@type Config
5556
local default_config = {
5657
start_enabled = true,
58+
max_file_size = 1.5,
5759
markdown_query = [[
5860
(atx_heading [
5961
(atx_h1_marker)

lua/render-markdown/state.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
---@class Config
3232
---@field public start_enabled boolean
33+
---@field public max_file_size number
3334
---@field public markdown_query string
3435
---@field public inline_query string
3536
---@field public log_level 'debug'|'error'

lua/render-markdown/ui.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ M.refresh = function()
2626
if not vim.tbl_contains(state.config.render_modes, vim.fn.mode()) then
2727
return
2828
end
29+
if M.file_size_mb() > state.config.max_file_size then
30+
return
31+
end
2932

3033
logger.start()
3134
vim.opt_local.conceallevel = state.config.conceal.rendered
@@ -49,4 +52,15 @@ M.refresh = function()
4952
logger.flush()
5053
end
5154

55+
---@return number
56+
M.file_size_mb = function()
57+
local ok, stats = pcall(function()
58+
return vim.loop.fs_stat(vim.api.nvim_buf_get_name(0))
59+
end)
60+
if not (ok and stats) then
61+
return 0
62+
end
63+
return stats.size / (1024 * 1024)
64+
end
65+
5266
return M

tests/init_spec.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,12 @@ async_tests.describe('init', function()
185185
},
186186
})
187187

188-
-- Line break, TODO: fragile need to determine width
188+
-- Line break
189189
vim.list_extend(expected, {
190190
{
191191
row = { 38 },
192192
col = { 0 },
193-
virt_text = { { string.rep('', 80), 'LineNr' } },
193+
virt_text = { { string.rep('', vim.opt.columns:get()), 'LineNr' } },
194194
virt_text_pos = 'overlay',
195195
},
196196
})

0 commit comments

Comments
 (0)