Skip to content

Commit fea6f3d

Browse files
Add user command and function to toggle this plugin
## Details Feature suggested through: #4 Sometimes user would like to see raw view in normal mode. Created a `toggle` which essentially enables / disables this entire plugin. Wrapped this in user command `RenderMarkdownToggle`.
1 parent df98da8 commit fea6f3d

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

Diff for: README.md

+6
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ require('render-markdown').setup({
101101
})
102102
```
103103

104+
# Commands
105+
106+
`:RenderMarkdownToggle` - Switch between enabling & disabling this plugin
107+
108+
- Function can also be accessed directly through `require('render-markdown').toggle()`
109+
104110
# Purpose
105111

106112
There are many existing markdown rendering plugins in the Neovim ecosystem. However,

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

+22-1
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ function M.setup(opts)
7575
},
7676
}
7777
state.config = vim.tbl_deep_extend('force', default_config, opts or {})
78+
state.enabled = true
7879

7980
-- Call immediately to re-render on LazyReload
80-
M.refresh()
81+
vim.schedule(M.refresh)
8182

8283
vim.api.nvim_create_autocmd({
8384
'FileChangedShellPost',
@@ -91,16 +92,36 @@ function M.setup(opts)
9192
vim.schedule(M.refresh)
9293
end,
9394
})
95+
96+
vim.api.nvim_create_user_command(
97+
'RenderMarkdownToggle',
98+
M.toggle,
99+
{ desc = 'Switch between enabling & disabling render markdown plugin' }
100+
)
94101
end
95102

96103
M.namespace = vim.api.nvim_create_namespace('render-markdown.nvim')
97104

105+
M.toggle = function()
106+
if state.enabled then
107+
state.enabled = false
108+
vim.schedule(M.clear)
109+
else
110+
state.enabled = true
111+
-- Call to refresh must happen after state change
112+
vim.schedule(M.refresh)
113+
end
114+
end
115+
98116
M.clear = function()
99117
-- Remove existing highlights / virtual text
100118
vim.api.nvim_buf_clear_namespace(0, M.namespace, 0, -1)
101119
end
102120

103121
M.refresh = function()
122+
if not state.enabled then
123+
return
124+
end
104125
if not vim.tbl_contains(state.config.file_types, vim.bo.filetype) then
105126
return
106127
end

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

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
---@field public highlights Highlights
2222

2323
---@class State
24+
---@field enabled boolean
2425
---@field config Config
2526
local state = {}
2627
return state

0 commit comments

Comments
 (0)