1- *render-markdown.txt* For 0.10.0 Last change: 2024 May 22
1+ *render-markdown.txt* For 0.10.0 Last change: 2024 May 31
22
33==============================================================================
44Table of Contents *render-markdown-table-of-contents*
@@ -11,12 +11,14 @@ Table of Contents *render-markdown-table-of-contents*
1111 - packer.nvim | render-markdown-install-packer.nvim |
12125. Setup | render-markdown-setup |
13136. Commands | render-markdown-commands |
14- 7. Purpose | render-markdown-purpose |
15- 8. Markdown Ecosystem | render-markdown-markdown-ecosystem |
14+ 7. Custom Handlers | render-markdown-custom-handlers |
15+ - More Complex Example| render-markdown-custom-handlers-more-complex-example |
16+ 8. Purpose | render-markdown-purpose |
17+ 9. Markdown Ecosystem | render-markdown-markdown-ecosystem |
1618 - Render in Neovim | render-markdown-markdown-ecosystem-render-in-neovim |
1719 - Render in Browser | render-markdown-markdown-ecosystem-render-in-browser |
1820 - Orthogonal | render-markdown-markdown-ecosystem-orthogonal |
19- 9 . Links | render-markdown-links |
21+ 10 . Links | render-markdown-links |
2022
2123==============================================================================
22241. markdown.nvim *render-markdown-markdown.nvim*
@@ -41,6 +43,7 @@ Plugin to improve viewing Markdown files in Neovim
4143- Basic support for `LaTeX` if `pylatexenc` is installed on system
4244- Disable rendering when file is larger than provided value
4345- Support for callouts <https://github.com/orgs/community/discussions/16925 >
46+ - Support custom handlers which are ran identically to native handlers
4447
4548
4649==============================================================================
@@ -178,6 +181,9 @@ modified by the user.
178181 -- normal: renders the rows of tables
179182 -- none: disables rendering, use this if you prefer having cell highlights
180183 table_style = 'full' ,
184+ -- Mapping from treesitter language to user defined handlers
185+ -- See 'Custom Handlers' section for more info
186+ custom_handlers = {},
181187 -- Define the highlight groups to use when rendering various components
182188 highlights = {
183189 heading = {
@@ -237,7 +243,77 @@ modified by the user.
237243
238244
239245==============================================================================
240- 7. Purpose *render-markdown-purpose*
246+ 7. Custom Handlers *render-markdown-custom-handlers*
247+
248+ Custom handlers allow users to integrate custom rendering for either
249+ unsupported languages or to override the native implementations. This can also
250+ be used to disable a native language, as custom handlers have priority.
251+
252+ For example disabling the `LaTeX` handler can be done with:
253+
254+ >lua
255+ require('render-markdown').setup({
256+ custom_handlers = {
257+ latex = { render = function() end },
258+ },
259+ }
260+ <
261+
262+ Each handler must conform to the following interface:
263+
264+ >lua
265+ ---@class render.md.Handler
266+ ---@field public render fun(namespace: integer, root: TSNode, buf: integer)
267+ <
268+
269+ The `render` function parameters are:
270+
271+ - `namespace` : The id that this plugin interacts with when setting and clearing `extmark` s
272+ - `root` : The root treesitter node for the specified language
273+ - `buf ` : The buffer containing the root node
274+
275+ Custom handlers are ran identically to native ones, so by writing custom
276+ `extmark` s (see :h nvim_buf_set_extmark()) to the provided `namespace` this
277+ plugin will handle clearing the `extmark` s on mode changes as well as
278+ re-calling the `render` function when needed.
279+
280+ This is a high level interface, as such creating, parsing, and iterating
281+ through a treesitter query is entirely up to the user if the functionality they
282+ want needs this. We do not provide any convenience functions, but you are more
283+ than welcome to use patterns from the native handlers.
284+
285+
286+ MORE COMPLEX EXAMPLE *render-markdown-custom-handlers-more-complex-example*
287+
288+ Lets say for `python ` we want to highlight lines with function definitions.
289+
290+ >lua
291+ -- Parse query outside of the render function to avoid doing it for each call
292+ local query = vim.treesitter.query.parse('python' , '(function_definition) @def')
293+ local function render_python(namespace, root, buf)
294+ for id, node in query:iter_captures(root, buf) do
295+ local capture = query.captures[id]
296+ local start_row, _, _, _ = node:range()
297+ if capture == 'def' then
298+ vim.api.nvim_buf_set_extmark(buf, namespace, start_row, 0, {
299+ end_row = start_row + 1,
300+ end_col = 0,
301+ hl_group = 'DiffDelete',
302+ hl_eol = true,
303+ })
304+ end
305+ end
306+ end
307+ require('render-markdown').setup({
308+ custom_handlers = {
309+ python = { render = render_python },
310+ },
311+ }
312+ <
313+
314+
315+ ==============================================================================
316+ 8. Purpose *render-markdown-purpose*
241317
242318There are many existing markdown rendering plugins in the Neovim ecosystem.
243319However, most of these rely on syncing a separate browser window with the
@@ -254,7 +330,7 @@ this plugin.
254330
255331
256332==============================================================================
257- 8 . Markdown Ecosystem *render-markdown-markdown-ecosystem*
333+ 9 . Markdown Ecosystem *render-markdown-markdown-ecosystem*
258334
259335There are many `markdown` plugins that specialize in different aspects of
260336interacting with `markdown` files. This plugin specializes in rendering the
@@ -297,7 +373,7 @@ also have no issues running alongside this plugin.
297373 specific keybindings for interacting with `markdown` files
298374
299375==============================================================================
300- 9 . Links *render-markdown-links*
376+ 10 . Links *render-markdown-links*
301377
3023781. *Demo*: demo/demo.gif
303379
0 commit comments