Skip to content

Commit

Permalink
Add sign text for headings & code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
MeanderingProgrammer committed Jul 9, 2024
1 parent be3f6e3 commit 7acc1bf
Show file tree
Hide file tree
Showing 20 changed files with 204 additions and 19 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Plugin to improve viewing Markdown files in Neovim
- Block quotes: replace leading `>` with provided icon
- Tables: replace border characters, does NOT automatically align
- [Callouts](https://github.com/orgs/community/discussions/16925)
- Base set as well as custom ones
- Github & Obsidian out of the box, supports user defined as well
- Custom checkbox states, function similar to `callouts`
- `LaTeX` blocks: renders formulas if `latex` parser and `pylatexenc` are installed
- Disable rendering when file is larger than provided value
Expand Down Expand Up @@ -172,18 +172,21 @@ require('render-markdown').setup({
-- The 'level' is used to index into the array using a cycle
-- The result is left padded with spaces to hide any additional '#'
icons = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
-- Added to the sign column
-- The 'level' is used to index into the array using a cycle
signs = { '󰫎 ' },
-- The 'level' is used to index into the array using a clamp
-- Highlight for the heading icon and extends through the entire line
backgrounds = { 'DiffAdd', 'DiffChange', 'DiffDelete' },
-- The 'level' is used to index into the array using a clamp
-- Highlight for the heading icon only
-- Highlight for the heading and sign icons
foregrounds = { 'markdownH1', 'markdownH2', 'markdownH3', 'markdownH4', 'markdownH5', 'markdownH6' },
},
code = {
-- Determines how code blocks & inline code are rendered:
-- none: disables all rendering
-- normal: adds highlight group to code blocks & inline code
-- language: adds language icon & name above code blocks
-- language: adds language icon to sign column and icon + name above code blocks
-- full: normal + language
style = 'full',
-- Highlight for code blocks & inline code
Expand Down Expand Up @@ -326,11 +329,14 @@ require('render-markdown').setup({
-- The 'level' is used to index into the array using a cycle
-- The result is left padded with spaces to hide any additional '#'
icons = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
-- Added to the sign column
-- The 'level' is used to index into the array using a cycle
signs = { '󰫎 ' },
-- The 'level' is used to index into the array using a clamp
-- Highlight for the heading icon and extends through the entire line
backgrounds = { 'DiffAdd', 'DiffChange', 'DiffDelete' },
-- The 'level' is used to index into the array using a clamp
-- Highlight for the heading icon only
-- Highlight for the heading and sign icons
foregrounds = { 'markdownH1', 'markdownH2', 'markdownH3', 'markdownH4', 'markdownH5', 'markdownH6' },
},
})
Expand All @@ -344,7 +350,7 @@ require('render-markdown').setup({
-- Determines how code blocks & inline code are rendered:
-- none: disables all rendering
-- normal: adds highlight group to code blocks & inline code
-- language: adds language icon & name above code blocks
-- language: adds language icon to sign column and icon + name above code blocks
-- full: normal + language
style = 'full',
-- Highlight for code blocks & inline code
Expand Down
Binary file modified demo/box_dash_quote.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified demo/callout.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified demo/heading_code.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified demo/latex.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified demo/list_table.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 11 additions & 5 deletions doc/render-markdown.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Plugin to improve viewing Markdown files in Neovim
- Block quotes: replace leading `>` with provided icon
- Tables: replace border characters, does NOT automatically align
- Callouts <https://github.com/orgs/community/discussions/16925>
- Base set as well as custom ones
- Github & Obsidian out of the box, supports user defined as well
- Custom checkbox states, function similar to `callouts`
- `LaTeX` blocks: renders formulas if `latex` parser and `pylatexenc` are installed
- Disable rendering when file is larger than provided value
Expand Down Expand Up @@ -208,18 +208,21 @@ Full Default Configuration ~
-- The 'level' is used to index into the array using a cycle
-- The result is left padded with spaces to hide any additional '#'
icons = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
-- Added to the sign column
-- The 'level' is used to index into the array using a cycle
signs = { '󰫎 ' },
-- The 'level' is used to index into the array using a clamp
-- Highlight for the heading icon and extends through the entire line
backgrounds = { 'DiffAdd', 'DiffChange', 'DiffDelete' },
-- The 'level' is used to index into the array using a clamp
-- Highlight for the heading icon only
-- Highlight for the heading and sign icons
foregrounds = { 'markdownH1', 'markdownH2', 'markdownH3', 'markdownH4', 'markdownH5', 'markdownH6' },
},
code = {
-- Determines how code blocks & inline code are rendered:
-- none: disables all rendering
-- normal: adds highlight group to code blocks & inline code
-- language: adds language icon & name above code blocks
-- language: adds language icon to sign column and icon + name above code blocks
-- full: normal + language
style = 'full',
-- Highlight for code blocks & inline code
Expand Down Expand Up @@ -361,11 +364,14 @@ HEADINGS *render-markdown-setup-headings*
-- The 'level' is used to index into the array using a cycle
-- The result is left padded with spaces to hide any additional '#'
icons = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
-- Added to the sign column
-- The 'level' is used to index into the array using a cycle
signs = { '󰫎 ' },
-- The 'level' is used to index into the array using a clamp
-- Highlight for the heading icon and extends through the entire line
backgrounds = { 'DiffAdd', 'DiffChange', 'DiffDelete' },
-- The 'level' is used to index into the array using a clamp
-- Highlight for the heading icon only
-- Highlight for the heading and sign icons
foregrounds = { 'markdownH1', 'markdownH2', 'markdownH3', 'markdownH4', 'markdownH5', 'markdownH6' },
},
})
Expand All @@ -380,7 +386,7 @@ CODE BLOCKS *render-markdown-setup-code-blocks*
-- Determines how code blocks & inline code are rendered:
-- none: disables all rendering
-- normal: adds highlight group to code blocks & inline code
-- language: adds language icon & name above code blocks
-- language: adds language icon to sign column and icon + name above code blocks
-- full: normal + language
style = 'full',
-- Highlight for code blocks & inline code
Expand Down
21 changes: 17 additions & 4 deletions lua/render-markdown/handler/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ M.render_node = function(namespace, buf, capture, node)
virt_text_pos = 'overlay',
hl_eol = true,
})

vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
end_row = end_row,
end_col = end_col,
sign_text = list.cycle(heading.signs, level),
sign_hl_group = foreground,
})
elseif capture == 'dash' then
local dash = state.config.dash
local width = vim.api.nvim_win_get_width(util.buf_to_win(buf))
Expand All @@ -72,14 +79,20 @@ M.render_node = function(namespace, buf, capture, node)
if not vim.tbl_contains({ 'language', 'full' }, code.style) then
return
end
-- Requires inline extmarks
if not util.has_10 then
return
end
local icon, icon_highlight = icons.get(value)
if icon == nil or icon_highlight == nil then
return
end
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
end_row = end_row,
end_col = end_col,
sign_text = icon,
sign_hl_group = icon_highlight,
})
-- Requires inline extmarks
if not util.has_10 then
return
end
local highlight = { icon_highlight }
if code.style == 'full' then
highlight = { icon_highlight, code.highlight }
Expand Down
2 changes: 2 additions & 0 deletions lua/render-markdown/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,12 @@ function M.check_config(config)
local heading = config.heading
append_errors('heading', {
icons = { heading.icons, 'table' },
signs = { heading.signs, 'table' },
backgrounds = { heading.backgrounds, 'table' },
foregrounds = { heading.foregrounds, 'table' },
})
all_strings('heading.icons', heading.icons)
all_strings('heading.signs', heading.signs)
all_strings('heading.backgrounds', heading.backgrounds)
all_strings('heading.foregrounds', heading.foregrounds)

Expand Down
8 changes: 6 additions & 2 deletions lua/render-markdown/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ local M = {}

---@class render.md.UserHeading
---@field public icons? string[]
---@field public signs? string[]
---@field public backgrounds? string[]
---@field public foregrounds? string[]

Expand Down Expand Up @@ -145,18 +146,21 @@ M.default_config = {
-- The 'level' is used to index into the array using a cycle
-- The result is left padded with spaces to hide any additional '#'
icons = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
-- Added to the sign column
-- The 'level' is used to index into the array using a cycle
signs = { '󰫎 ' },
-- The 'level' is used to index into the array using a clamp
-- Highlight for the heading icon and extends through the entire line
backgrounds = { 'DiffAdd', 'DiffChange', 'DiffDelete' },
-- The 'level' is used to index into the array using a clamp
-- Highlight for the heading icon only
-- Highlight for the heading and sign icons
foregrounds = { 'markdownH1', 'markdownH2', 'markdownH3', 'markdownH4', 'markdownH5', 'markdownH6' },
},
code = {
-- Determines how code blocks & inline code are rendered:
-- none: disables all rendering
-- normal: adds highlight group to code blocks & inline code
-- language: adds language icon & name above code blocks
-- language: adds language icon to sign column and icon + name above code blocks
-- full: normal + language
style = 'full',
-- Highlight for code blocks & inline code
Expand Down
1 change: 1 addition & 0 deletions lua/render-markdown/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

---@class render.md.Heading
---@field public icons string[]
---@field public signs string[]
---@field public backgrounds string[]
---@field public foregrounds string[]

Expand Down
5 changes: 2 additions & 3 deletions scripts/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ def update_readme(init_file: Path, readme_file: Path) -> None:
parameters: list[str] = ["heading", "code", "dash", "bullet"]
parameters.extend(["checkbox", "quote", "pipe_table", "callout"])
for parameter in parameters:
parameter = f"{parameter} = "
old_param = get_code_block(readme_file, parameter, 2)
new_param = wrap_setup(get_config_for(new_config, parameter))
old_param = get_code_block(readme_file, f"\n {parameter} = ", 2)
new_param = wrap_setup(get_config_for(new_config, f"{parameter} = "))
text = text.replace(old_param, new_param)

readme_file.write_text(text)
Expand Down
6 changes: 6 additions & 0 deletions tests/box_dash_quote_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ async_tests.describe('box_dash_quote.md', function()
virt_text = { { '󰲡 ', { 'markdownH1', 'DiffAdd' } } },
virt_text_pos = 'overlay',
},
{
row = { 0, 0 },
col = { 0, 1 },
sign_text = '󰫎 ',
sign_hl_group = 'markdownH1',
},
})

-- Checkboxes
Expand Down
42 changes: 42 additions & 0 deletions tests/callout_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ async_tests.describe('callout.md', function()
virt_text = { { '󰲡 ', { 'markdownH1', 'DiffAdd' } } },
virt_text_pos = 'overlay',
},
{
row = { note_start, note_start },
col = { 0, 1 },
sign_text = '󰫎 ',
sign_hl_group = 'markdownH1',
},
-- Quote start
{
row = { note_start + 2, note_start + 2 },
Expand Down Expand Up @@ -71,6 +77,12 @@ async_tests.describe('callout.md', function()
virt_text = { { '󰲡 ', { 'markdownH1', 'DiffAdd' } } },
virt_text_pos = 'overlay',
},
{
row = { tip_start, tip_start },
col = { 0, 1 },
sign_text = '󰫎 ',
sign_hl_group = 'markdownH1',
},
-- Quote start
{
row = { tip_start + 2, tip_start + 2 },
Expand Down Expand Up @@ -104,6 +116,12 @@ async_tests.describe('callout.md', function()
hl_eol = true,
hl_group = 'ColorColumn',
},
{
row = { tip_start + 4, tip_start + 4 },
col = { 5, 8 },
sign_text = '󰢱 ',
sign_hl_group = 'MiniIconsAzure',
},
{
row = { tip_start + 4 },
col = { 5 },
Expand Down Expand Up @@ -135,6 +153,12 @@ async_tests.describe('callout.md', function()
virt_text = { { '󰲡 ', { 'markdownH1', 'DiffAdd' } } },
virt_text_pos = 'overlay',
},
{
row = { important_start, important_start },
col = { 0, 1 },
sign_text = '󰫎 ',
sign_hl_group = 'markdownH1',
},
-- Quote start
{
row = { important_start + 2, important_start + 2 },
Expand Down Expand Up @@ -169,6 +193,12 @@ async_tests.describe('callout.md', function()
virt_text = { { '󰲡 ', { 'markdownH1', 'DiffAdd' } } },
virt_text_pos = 'overlay',
},
{
row = { warning_start, warning_start },
col = { 0, 1 },
sign_text = '󰫎 ',
sign_hl_group = 'markdownH1',
},
-- Quote start
{
row = { warning_start + 2, warning_start + 2 },
Expand Down Expand Up @@ -203,6 +233,12 @@ async_tests.describe('callout.md', function()
virt_text = { { '󰲡 ', { 'markdownH1', 'DiffAdd' } } },
virt_text_pos = 'overlay',
},
{
row = { caution_start, caution_start },
col = { 0, 1 },
sign_text = '󰫎 ',
sign_hl_group = 'markdownH1',
},
-- Quote start
{
row = { caution_start + 2, caution_start + 2 },
Expand Down Expand Up @@ -237,6 +273,12 @@ async_tests.describe('callout.md', function()
virt_text = { { '󰲡 ', { 'markdownH1', 'DiffAdd' } } },
virt_text_pos = 'overlay',
},
{
row = { bug_start, bug_start },
col = { 0, 1 },
sign_text = '󰫎 ',
sign_hl_group = 'markdownH1',
},
-- Quote start
{
row = { bug_start + 2, bug_start + 2 },
Expand Down
18 changes: 18 additions & 0 deletions tests/custom_handler_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ async_tests.describe('custom_handler.md', function()
virt_text = { { '󰲡 ', { 'markdownH1', 'DiffAdd' } } },
virt_text_pos = 'overlay',
},
{
row = { 0, 0 },
col = { 0, 1 },
sign_text = '󰫎 ',
sign_hl_group = 'markdownH1',
},
{
row = { 0, 0 },
col = { 9, 18 },
Expand Down Expand Up @@ -62,6 +68,12 @@ async_tests.describe('custom_handler.md', function()
virt_text = { { '󰲡 ', { 'markdownH1', 'DiffAdd' } } },
virt_text_pos = 'overlay',
},
{
row = { 0, 0 },
col = { 0, 1 },
sign_text = '󰫎 ',
sign_hl_group = 'markdownH1',
},
})

-- Backslash escapes
Expand Down Expand Up @@ -104,6 +116,12 @@ async_tests.describe('custom_handler.md', function()
virt_text = { { '󰲡 ', { 'markdownH1', 'DiffAdd' } } },
virt_text_pos = 'overlay',
},
{
row = { 0, 0 },
col = { 0, 1 },
sign_text = '󰫎 ',
sign_hl_group = 'markdownH1',
},
{
row = { 0, 0 },
col = { 9, 18 },
Expand Down
Loading

0 comments on commit 7acc1bf

Please sign in to comment.