Skip to content

Commit 90072fd

Browse files
Fix language selector to pull language from info_string
resolves: #44 # Details Previously when getting the langauge for a fenced code block we simply pulled the info_string in full. However `markdown` supports additional information in this component so would not resolve correctly. Instead we need to pull the language node from the info_string node to handle these cases. To do this add a separate query / capture to handle getting the language of a fenced code block. Since the language can now be in different places, including nested in curly braces generating the right virtual text becomes harder. To avoid dealing with these cases use an inline extmark to shift things for us. This does mean that the icon feature is now restricted to neovim versions >= 0.10.0. Add a version check to avoid failing for early versions.
1 parent 0d6ebe0 commit 90072fd

File tree

7 files changed

+45
-41
lines changed

7 files changed

+45
-41
lines changed

Diff for: README.md

+15-10
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@ Plugin to improve viewing Markdown files in Neovim
1212

1313
- Functions entirely inside of Neovim with no external windows
1414
- Changes between `rendered` view in normal mode and `raw` view in all other modes
15-
- Changes `conceallevel` between `rendered` and `raw` view based on configuration
15+
- Changes window options between `rendered` and `raw` view based on configuration
16+
- Effects `conceallevel` & `concealcursor` by default
1617
- Supports rendering `markdown` injected into other file types
17-
- Highlights headings with different groups depending on level and replaces `#`
18-
- Updates horizontal breaks with full-width lines
19-
- Highlights code blocks and inline code to better stand out
20-
- Replaces bullet points with provided character based on level
21-
- Replaces checkboxes with provided characters based on whether they are checked
22-
- Replaces block quote leading `>` with provided character
23-
- Updates table borders with better border characters, does NOT automatically align
24-
- Basic support for `LaTeX` if `latex` parser and `pylatexenc` are installed
18+
- Renders the following `markdown` components:
19+
- Headings: highlight depending on level and replaces `#` with icon
20+
- Horizontal breaks: replace with full-width lines
21+
- Code blocks: highlight to better stand out
22+
- Adds language icon, requires `nvim-web-devicons` and neovim >= `0.10.0`
23+
- Inline code: highlight to better stand out
24+
- List bullet points: replace with provided icon based on level
25+
- Checkboxes: replace with provided icon based on whether they are checked
26+
- Block quotes: replace leading `>` with provided icon
27+
- Tables: replace border characters, does NOT automatically align
28+
- [Callouts](https://github.com/orgs/community/discussions/16925)
29+
- `LaTeX` blocks: renders formulas if `latex` parser and `pylatexenc` are installed
2530
- Disable rendering when file is larger than provided value
26-
- Support for [callouts](https://github.com/orgs/community/discussions/16925)
2731
- Support custom handlers which are ran identically to builtin handlers
2832

2933
# Dependencies
@@ -99,6 +103,7 @@ require('render-markdown').setup({
99103
(thematic_break) @dash
100104
101105
(fenced_code_block) @code
106+
(fenced_code_block (info_string (language) @language))
102107
103108
[
104109
(list_marker_plus)

Diff for: demo/heading_code.gif

12.8 KB
Loading

Diff for: demo/heading_code.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
###### Heading 6
1010

11-
```python
11+
```python {filename="demo.py"}
1212
def main() -> None:
1313
sum = 0
1414
for i in range(10):

Diff for: doc/render-markdown.txt

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 June 28
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 June 30
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -32,18 +32,22 @@ Plugin to improve viewing Markdown files in Neovim
3232

3333
- Functions entirely inside of Neovim with no external windows
3434
- Changes between `rendered` view in normal mode and `raw` view in all other modes
35-
- Changes `conceallevel` between `rendered` and `raw` view based on configuration
35+
- Changes window options between `rendered` and `raw` view based on configuration
36+
- Effects `conceallevel` & `concealcursor` by default
3637
- Supports rendering `markdown` injected into other file types
37-
- Highlights headings with different groups depending on level and replaces `#`
38-
- Updates horizontal breaks with full-width lines
39-
- Highlights code blocks and inline code to better stand out
40-
- Replaces bullet points with provided character based on level
41-
- Replaces checkboxes with provided characters based on whether they are checked
42-
- Replaces block quote leading `>` with provided character
43-
- Updates table borders with better border characters, does NOT automatically align
44-
- Basic support for `LaTeX` if `latex` parser and `pylatexenc` are installed
38+
- Renders the following `markdown` components:
39+
- Headings: highlight depending on level and replaces `#` with icon
40+
- Horizontal breaks: replace with full-width lines
41+
- Code blocks: highlight to better stand out
42+
- Adds language icon, requires `nvim-web-devicons` and neovim >= `0.10.0`
43+
- Inline code: highlight to better stand out
44+
- List bullet points: replace with provided icon based on level
45+
- Checkboxes: replace with provided icon based on whether they are checked
46+
- Block quotes: replace leading `>` with provided icon
47+
- Tables: replace border characters, does NOT automatically align
48+
- Callouts <https://github.com/orgs/community/discussions/16925>
49+
- `LaTeX` blocks: renders formulas if `latex` parser and `pylatexenc` are installed
4550
- Disable rendering when file is larger than provided value
46-
- Support for callouts <https://github.com/orgs/community/discussions/16925>
4751
- Support custom handlers which are ran identically to builtin handlers
4852

4953

@@ -127,6 +131,7 @@ modified by the user.
127131
(thematic_break) @dash
128132

129133
(fenced_code_block) @code
134+
(fenced_code_block (info_string (language) @language))
130135

131136
[
132137
(list_marker_plus)

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

+9-13
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,24 @@ M.render_node = function(namespace, buf, capture, node)
6666
hl_group = highlights.code,
6767
hl_eol = true,
6868
})
69-
69+
elseif capture == 'language' then
7070
if state.config.code_style ~= 'full' then
7171
return
7272
end
73-
74-
local info = ts.child(node, 'info_string')
75-
if info == nil then
73+
-- Requires inline extmarks
74+
if vim.fn.has('nvim-0.10') == 0 then
7675
return
7776
end
78-
local language = vim.treesitter.get_node_text(info, buf)
79-
local icon, icon_highlight = icons.get(language)
77+
78+
local icon, icon_highlight = icons.get(value)
8079
if icon == nil or icon_highlight == nil then
8180
return
8281
end
83-
local icon_text = { icon .. ' ', { icon_highlight, highlights.code } }
84-
-- language takes care of info_string so padding needs to take care of code block start, i.e. ```
85-
-- subtract space taken up by icon and pad the remainder
86-
local padding = string.rep(' ', 3 - vim.fn.strdisplaywidth(icon_text[1]))
87-
local language_text = { language .. padding, { 'Normal', highlights.code } }
82+
83+
local icon_text = { icon .. ' ' .. value, { icon_highlight, highlights.code } }
8884
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
89-
virt_text = { icon_text, language_text },
90-
virt_text_pos = 'overlay',
85+
virt_text = { icon_text },
86+
virt_text_pos = 'inline',
9187
})
9288
elseif capture == 'list_marker' then
9389
if ts.sibling(node, { 'task_list_marker_unchecked', 'task_list_marker_checked' }) ~= nil then

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

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ M.default_config = {
9292
(thematic_break) @dash
9393
9494
(fenced_code_block) @code
95+
(fenced_code_block (info_string (language) @language))
9596
9697
[
9798
(list_marker_plus)

Diff for: tests/heading_code_spec.lua

+3-6
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,9 @@ async_tests.describe('heading_code.md', function()
6161
},
6262
{
6363
row = { 10 },
64-
col = { 0 },
65-
virt_text = {
66-
{ '', { 'DevIconPy', 'ColorColumn' } },
67-
{ 'python ', { 'Normal', 'ColorColumn' } },
68-
},
69-
virt_text_pos = 'overlay',
64+
col = { 3 },
65+
virt_text = { { ' python', { 'DevIconPy', 'ColorColumn' } } },
66+
virt_text_pos = 'inline',
7067
},
7168
})
7269

0 commit comments

Comments
 (0)