Skip to content

Commit df59836

Browse files
Add support for rendering inline markdown code spans
# Details - Adds support for markdown_inline code_span elements to be highlighted with the same group as code blocks. - Update health check to check for inline parser. - Update demo to show this new behavior. - Minor update to how demo is created.
1 parent da85a5e commit df59836

File tree

8 files changed

+48
-13
lines changed

8 files changed

+48
-13
lines changed

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ Plugin to improve viewing Markdown files in Neovim
1010
- Changes between `rendered` view in normal mode and raw view in all other modes
1111
- Supports rendering `markdown` injected into other file types
1212
- Highlights headings with different groups depending on level and replaces `#`
13-
- Highlights code blocks to better stand out
13+
- Highlights code blocks and inline code to better stand out
1414
- Replaces whichever style bullet point is being used with provided character
1515
- Replaces block quote leading `>` with provided character
1616
- Updates table borders with better border characters, does NOT automatically align
1717
- Basic support for `LaTeX` if `pylatexenc` is installed on system
1818

1919
# Dependencies
2020

21-
- [markdown](https://github.com/tree-sitter-grammars/tree-sitter-markdown) parser for
22-
[treesitter](https://github.com/nvim-treesitter/nvim-treesitter): Used to parse
21+
- [markdown & markdown_inline](https://github.com/tree-sitter-grammars/tree-sitter-markdown)
22+
parsers for [treesitter](https://github.com/nvim-treesitter/nvim-treesitter): Used to parse
2323
`markdown` files
2424
- [pylatexenc](https://pypi.org/project/pylatexenc/): Used to transform `LaTeX` strings
2525
to appropriate unicode using `latex2text`, not a mandatory dependency
@@ -46,8 +46,7 @@ by the user.
4646

4747
```lua
4848
require('render-markdown').setup({
49-
-- Capture groups that get pulled from markdown, these are later used to
50-
-- modify how the file gets rendered
49+
-- Capture groups that get pulled from markdown
5150
markdown_query = [[
5251
(atx_heading [
5352
(atx_h1_marker)
@@ -73,10 +72,14 @@ require('render-markdown').setup({
7372
(pipe_table_delimiter_row) @table_delim
7473
(pipe_table_row) @table_row
7574
]],
75+
-- Capture groups that get pulled from inline markdown
76+
inline_query = [[
77+
(code_span) @code
78+
]],
7679
-- Filetypes this plugin will run on
7780
file_types = { 'markdown' },
78-
-- vim modes that will show a rendered view of the markdown file, all other
79-
-- modes will be uneffected by this plugin
81+
-- Vim modes that will show a rendered view of the markdown file
82+
-- All other modes will be uneffected by this plugin
8083
render_modes = { 'n', 'c' },
8184
-- Characters that will replace the # at the start of headings
8285
headings = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },

demo/demo.gif

68.2 KB
Loading

demo/record.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def main(zoom: int, file: str, cast: str) -> None:
1717
# https://docs.asciinema.org/manual/cli/usage/
1818
pyautogui.write(f"asciinema rec -c 'nvim {file}' {cast}")
1919
pyautogui.press("enter")
20-
time.sleep(1.0)
20+
time.sleep(1.5)
2121

2222
# Start typing in new heading
2323
pyautogui.press("o")
@@ -28,11 +28,12 @@ def main(zoom: int, file: str, cast: str) -> None:
2828
pyautogui.press("esc")
2929
time.sleep(2.0)
3030

31-
for _ in range(2):
31+
# Swith between insert and normal mode a few times
32+
for i in range(2):
3233
pyautogui.press("i")
33-
time.sleep(1.0)
34+
time.sleep(i + 1)
3435
pyautogui.press("esc")
35-
time.sleep(2.0)
36+
time.sleep((i + 1) * 2)
3637

3738
# Close demo file
3839
pyautogui.write(":q!")

demo/sample.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ if __name__ == "__main__":
2121
```
2222

2323
- List Item 1
24-
* List Item 2
24+
* List Item 2: with `inline` code
2525
* Nested List Item 1
2626
* Nested List Item 2
2727
+ List Item 3

lua/render-markdown/health.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ local M = {}
1313
function M.check()
1414
vim.health.start('Checking required treesitter parsers')
1515
parser_installed('markdown')
16+
parser_installed('markdown_inline')
1617
end
1718

1819
return M

lua/render-markdown/init.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ local M = {}
2121

2222
---@class UserConfig
2323
---@field public markdown_query? string
24+
---@field public inline_query? string
2425
---@field public file_types? string[]
2526
---@field public render_modes? string[]
2627
---@field public headings? string[]
@@ -57,6 +58,9 @@ function M.setup(opts)
5758
(pipe_table_delimiter_row) @table_delim
5859
(pipe_table_row) @table_row
5960
]],
61+
inline_query = [[
62+
(code_span) @code
63+
]],
6064
file_types = { 'markdown' },
6165
render_modes = { 'n', 'c' },
6266
headings = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
@@ -87,6 +91,7 @@ function M.setup(opts)
8791
state.enabled = true
8892
state.config = vim.tbl_deep_extend('force', default_config, opts or {})
8993
state.markdown_query = vim.treesitter.query.parse('markdown', state.config.markdown_query)
94+
state.inline_query = vim.treesitter.query.parse('markdown_inline', state.config.inline_query)
9095

9196
-- Call immediately to re-render on LazyReload
9297
vim.schedule(ui.refresh)

lua/render-markdown/state.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
---@class Config
1818
---@field public markdown_query string
19+
---@field public inline_query string
1920
---@field public file_types string[]
2021
---@field public render_modes string[]
2122
---@field public headings string[]
@@ -27,5 +28,6 @@
2728
---@field enabled boolean
2829
---@field config Config
2930
---@field markdown_query Query
31+
---@field inline_query Query
3032
local state = {}
3133
return state

lua/render-markdown/ui.lua

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ M.refresh = function()
2727
local language = language_tree:lang()
2828
if language == 'markdown' then
2929
M.markdown(tree:root())
30+
elseif language == 'markdown_inline' then
31+
M.markdown_inline(tree:root())
3032
elseif language == 'latex' then
3133
M.latex(tree:root())
3234
end
@@ -109,7 +111,28 @@ M.markdown = function(root)
109111
})
110112
else
111113
-- Should only get here if user provides custom capture, currently unhandled
112-
vim.print('Unhandled capture: ' .. capture)
114+
vim.print('Unhandled markdown capture: ' .. capture)
115+
end
116+
end
117+
end
118+
119+
---@param root TSNode
120+
M.markdown_inline = function(root)
121+
local highlights = state.config.highlights
122+
---@diagnostic disable-next-line: missing-parameter
123+
for id, node in state.inline_query:iter_captures(root, 0) do
124+
local capture = state.inline_query.captures[id]
125+
local start_row, start_col, end_row, end_col = node:range()
126+
127+
if capture == 'code' then
128+
vim.api.nvim_buf_set_extmark(0, M.namespace, start_row, start_col, {
129+
end_row = end_row,
130+
end_col = end_col,
131+
hl_group = highlights.code,
132+
})
133+
else
134+
-- Should only get here if user provides custom capture, currently unhandled
135+
vim.print('Unhandled inline capture: ' .. capture)
113136
end
114137
end
115138
end

0 commit comments

Comments
 (0)