Skip to content

Commit 870426e

Browse files
Add ability for custom handlers to extend builtin
# Details Currently custom handlers can only replace the logic of builtin handlers. Meaning adding one feature to the markdown handler is not possible without replacing all of the existing logic. Allow users to specify a new field in each handler called extends, which defaults to false. When set to true both the user defined handler as well as the builtin one will run. This can lead to contradictions in behavior, this is not handled in any way and I don't really think there is a good way to do this. Ideally these custom handlers offer functionality that does not overlap with any of the existing functionality. Make some other minor changes as well such as where the disable latex logic lives. Update demos and update script to handle setting the handler interface in the README.
1 parent 973a5ac commit 870426e

File tree

16 files changed

+286
-71
lines changed

16 files changed

+286
-71
lines changed

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Plugin to improve viewing Markdown files in Neovim
2424
- 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)
27-
- Support custom handlers which are ran identically to native handlers
27+
- Support custom handlers which are ran identically to builtin handlers
2828

2929
# Limitations
3030

@@ -260,10 +260,14 @@ vim.treesitter.language.register('markdown', 'vimwiki')
260260
# Custom Handlers
261261

262262
Custom handlers allow users to integrate custom rendering for either unsupported
263-
languages or to override the native implementations. This can also be used to
264-
disable a native language, as custom handlers have priority.
263+
languages or to override / extend the builtin implementations.
265264

266-
For example disabling the `LaTeX` handler can be done with:
265+
This can also be used to disable a builtin handler, by not specifying the `extends`
266+
field and leaving the implementation blank. Though this has little benefit and
267+
can be accomplished in other ways such as setting `{ latex_enabled = false }`
268+
for `LaTeX`.
269+
270+
Still as an example disabling the `LaTeX` handler can be done with:
267271

268272
```lua
269273
require('render-markdown').setup({
@@ -278,6 +282,7 @@ Each handler must conform to the following interface:
278282
```lua
279283
---@class render.md.Handler
280284
---@field public render fun(namespace: integer, root: TSNode, buf: integer)
285+
---@field public extends? boolean
281286
```
282287

283288
The `render` function parameters are:
@@ -286,15 +291,18 @@ The `render` function parameters are:
286291
- `root`: The root treesitter node for the specified language
287292
- `buf`: The buffer containing the root node
288293

289-
Custom handlers are ran identically to native ones, so by writing custom `extmark`s
294+
The `extends` parameter defines whether the builtin handler should still be run in
295+
conjunction with this one. Defaults to `false`.
296+
297+
Custom handlers are ran identically to builtin ones, so by writing custom `extmark`s
290298
(see :h nvim_buf_set_extmark()) to the provided `namespace` this plugin will handle
291299
clearing the `extmark`s on mode changes as well as re-calling the `render` function
292300
when needed.
293301

294302
This is a high level interface, as such creating, parsing, and iterating through
295303
a treesitter query is entirely up to the user if the functionality they want needs
296304
this. We do not provide any convenience functions, but you are more than welcome
297-
to use patterns from the native handlers.
305+
to use patterns from the builtin handlers.
298306

299307
## More Complex Example
300308

demo/box_dash_quote.gif

3.47 KB
Loading

demo/callout.gif

-6.81 KB
Loading

demo/heading_code.gif

-5.47 KB
Loading

demo/latex.gif

-7.22 KB
Loading

demo/list_table.gif

3.67 KB
Loading

doc/render-markdown.txt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 June 17
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 June 19
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -51,7 +51,7 @@ Plugin to improve viewing Markdown files in Neovim
5151
- Basic support for `LaTeX` if `latex` parser and `pylatexenc` are installed
5252
- Disable rendering when file is larger than provided value
5353
- Support for callouts <https://github.com/orgs/community/discussions/16925>
54-
- Support custom handlers which are ran identically to native handlers
54+
- Support custom handlers which are ran identically to builtin handlers
5555

5656

5757
==============================================================================
@@ -303,10 +303,14 @@ the `filetype` of `markdown` files there are additional setup steps.
303303
9. Custom Handlers *render-markdown-custom-handlers*
304304

305305
Custom handlers allow users to integrate custom rendering for either
306-
unsupported languages or to override the native implementations. This can also
307-
be used to disable a native language, as custom handlers have priority.
306+
unsupported languages or to override / extend the builtin implementations.
308307

309-
For example disabling the `LaTeX` handler can be done with:
308+
This can also be used to disable a builtin handler, by not specifying the
309+
`extends` field and leaving the implementation blank. Though this has little
310+
benefit and can be accomplished in other ways such as setting `{ latex_enabled
311+
= false }` for `LaTeX`.
312+
313+
Still as an example disabling the `LaTeX` handler can be done with:
310314

311315
>lua
312316
require('render-markdown').setup({
@@ -321,6 +325,7 @@ Each handler must conform to the following interface:
321325
>lua
322326
---@class render.md.Handler
323327
---@field public render fun(namespace: integer, root: TSNode, buf: integer)
328+
---@field public extends? boolean
324329
<
325330

326331
The `render` function parameters are:
@@ -329,15 +334,18 @@ The `render` function parameters are:
329334
- `root`: The root treesitter node for the specified language
330335
- `buf`: The buffer containing the root node
331336

332-
Custom handlers are ran identically to native ones, so by writing custom
337+
The `extends` parameter defines whether the builtin handler should still be run
338+
in conjunction with this one. Defaults to `false`.
339+
340+
Custom handlers are ran identically to builtin ones, so by writing custom
333341
`extmark`s (see :h nvim_buf_set_extmark()) to the provided `namespace` this
334342
plugin will handle clearing the `extmark`s on mode changes as well as
335343
re-calling the `render` function when needed.
336344

337345
This is a high level interface, as such creating, parsing, and iterating
338346
through a treesitter query is entirely up to the user if the functionality they
339347
want needs this. We do not provide any convenience functions, but you are more
340-
than welcome to use patterns from the native handlers.
348+
than welcome to use patterns from the builtin handlers.
341349

342350

343351
MORE COMPLEX EXAMPLE *render-markdown-custom-handlers-more-complex-example*

justfile

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,21 @@ demo file rows content:
3737
rm {{file}}.cast
3838

3939
update:
40+
# Updates types.lua & README.md
4041
python -Wignore scripts/update.py
41-
42-
gen-doc:
43-
# https://github.com/kdheepak/panvimdoc
4442
# https://pandoc.org/
43+
# https://github.com/kdheepak/panvimdoc
4544
../../open-source/panvimdoc/panvimdoc.sh \
4645
--project-name render-markdown \
4746
--input-file README.md \
4847
--vim-version 0.10.0
4948

5049
[private]
51-
gen-file-text:
50+
gen-large-file-text:
5251
#!/usr/bin/env python
5352
for i in range(100_000):
5453
level = "#" * ((i % 6) + 1)
5554
print(f"{level} Title {i}\n")
5655

5756
gen-large-file:
58-
just gen-file-text > large.md
57+
just gen-large-file-text > large.md

lua/render-markdown/handler/latex.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ local M = {}
1515
---@param root TSNode
1616
---@param buf integer
1717
M.render = function(namespace, root, buf)
18+
if not state.config.latex_enabled then
19+
return
20+
end
1821
local converter = state.config.latex_converter
1922
if vim.fn.executable(converter) ~= 1 then
2023
logger.debug('Executable not found: ' .. converter)

lua/render-markdown/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ local M = {}
3636

3737
---@class render.md.Handler
3838
---@field public render fun(namespace: integer, root: TSNode, buf: integer)
39+
---@field public extends? boolean
3940

4041
---@class render.md.WindowOption
4142
---@field public default any

0 commit comments

Comments
 (0)