Skip to content

Commit

Permalink
feature: Allow each individual component to be disabled
Browse files Browse the repository at this point in the history
## Details

Now that each component is defined in its own configuration
we can add an `enabled` flag to each to allow them to be
enabled / disabled.

This has been added to:

- heading
- code
- dash
- bullet
- checkbox
- quote
- pipe_table

There are a couple of slightly unintuitive behaviors that I'll
note down here.

- Disabling quote also disables callouts. This is because callouts
  are essentially a special block quote. So if block quote rendering
  is disabled it makes no sense to render callouts. This does mean
  you can't have quote rendering without callouts, though this seems
  like an unlikely feature request.
- Disabling checkboxes re-enables their list like behavior. Meaning
  bullet points will now be rendered at the start of checkboxes
  like normal list items rather than being concealed.
- Disabling both checkboxes and bullets will then disable any rendering
  on checkbox elements. This is the only feature that requires disabling
  two things to get a `raw` view.
  • Loading branch information
MeanderingProgrammer committed Jul 9, 2024
1 parent 501e5e0 commit b84a788
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 14 deletions.
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ require('render-markdown').setup({
highlight = '@markup.math',
},
heading = {
-- Turn on / off heading icon & background rendering
enabled = true,
-- Replaces '#+' of 'atx_h._marker'
-- The number of '#' in the heading determines the 'level'
-- The 'level' is used to index into the array using a cycle
Expand All @@ -188,6 +190,8 @@ require('render-markdown').setup({
foregrounds = { 'markdownH1', 'markdownH2', 'markdownH3', 'markdownH4', 'markdownH5', 'markdownH6' },
},
code = {
-- Turn on / off code block & inline code rendering
enabled = true,
-- Determines how code blocks & inline code are rendered:
-- none: disables all rendering
-- normal: adds highlight group to code blocks & inline code
Expand All @@ -198,13 +202,17 @@ require('render-markdown').setup({
highlight = 'ColorColumn',
},
dash = {
-- Turn on / off thematic break rendering
enabled = true,
-- Replaces '---'|'***'|'___'|'* * *' of 'thematic_break'
-- The icon gets repeated across the window's width
icon = '',
-- Highlight for the whole line generated from the icon
highlight = 'LineNr',
},
bullet = {
-- Turn on / off list bullet rendering
enabled = true,
-- Replaces '-'|'+'|'*' of 'list_item'
-- How deeply nested the list is determines the 'level'
-- The 'level' is used to index into the array using a cycle
Expand All @@ -216,6 +224,8 @@ require('render-markdown').setup({
-- Checkboxes are a special instance of a 'list_item' that start with a 'shortcut_link'
-- There are two special states for unchecked & checked defined in the markdown grammar
checkbox = {
-- Turn on / off checkbox state rendering
enabled = true,
unchecked = {
-- Replaces '[ ]' of 'task_list_marker_unchecked'
icon = '󰄱 ',
Expand All @@ -240,12 +250,16 @@ require('render-markdown').setup({
},
},
quote = {
-- Turn on / off block quote & callout rendering
enabled = true,
-- Replaces '>' of 'block_quote'
icon = '',
-- Highlight for the quote icon
highlight = '@markup.quote',
},
pipe_table = {
-- Turn on / off pipe table rendering
enabled = true,
-- Determines how the table as a whole is rendered:
-- none: disables all rendering
-- normal: applies the 'cell' style rendering to each row of the table
Expand Down Expand Up @@ -293,7 +307,7 @@ require('render-markdown').setup({
quote = { raw = '[!QUOTE]', rendered = '󱆨 Quote', highlight = '@markup.quote' },
},
link = {
-- Turn on / off inline link icon behavior
-- Turn on / off inline link icon rendering
enabled = true,
-- Inlined with 'image' elements
image = '󰥶 ',
Expand Down Expand Up @@ -339,6 +353,8 @@ We use the following definitions when discussing indexing into arrays:
```lua
require('render-markdown').setup({
heading = {
-- Turn on / off heading icon & background rendering
enabled = true,
-- Replaces '#+' of 'atx_h._marker'
-- The number of '#' in the heading determines the 'level'
-- The 'level' is used to index into the array using a cycle
Expand All @@ -362,6 +378,8 @@ require('render-markdown').setup({
```lua
require('render-markdown').setup({
code = {
-- Turn on / off code block & inline code rendering
enabled = true,
-- Determines how code blocks & inline code are rendered:
-- none: disables all rendering
-- normal: adds highlight group to code blocks & inline code
Expand All @@ -379,6 +397,8 @@ require('render-markdown').setup({
```lua
require('render-markdown').setup({
dash = {
-- Turn on / off thematic break rendering
enabled = true,
-- Replaces '---'|'***'|'___'|'* * *' of 'thematic_break'
-- The icon gets repeated across the window's width
icon = '',
Expand All @@ -393,6 +413,8 @@ require('render-markdown').setup({
```lua
require('render-markdown').setup({
bullet = {
-- Turn on / off list bullet rendering
enabled = true,
-- Replaces '-'|'+'|'*' of 'list_item'
-- How deeply nested the list is determines the 'level'
-- The 'level' is used to index into the array using a cycle
Expand All @@ -411,6 +433,8 @@ require('render-markdown').setup({
-- Checkboxes are a special instance of a 'list_item' that start with a 'shortcut_link'
-- There are two special states for unchecked & checked defined in the markdown grammar
checkbox = {
-- Turn on / off checkbox state rendering
enabled = true,
unchecked = {
-- Replaces '[ ]' of 'task_list_marker_unchecked'
icon = '󰄱 ',
Expand Down Expand Up @@ -442,6 +466,8 @@ require('render-markdown').setup({
```lua
require('render-markdown').setup({
quote = {
-- Turn on / off block quote & callout rendering
enabled = true,
-- Replaces '>' of 'block_quote'
icon = '',
-- Highlight for the quote icon
Expand All @@ -455,6 +481,8 @@ require('render-markdown').setup({
```lua
require('render-markdown').setup({
pipe_table = {
-- Turn on / off pipe table rendering
enabled = true,
-- Determines how the table as a whole is rendered:
-- none: disables all rendering
-- normal: applies the 'cell' style rendering to each row of the table
Expand Down Expand Up @@ -516,7 +544,7 @@ require('render-markdown').setup({
```lua
require('render-markdown').setup({
link = {
-- Turn on / off inline link icon behavior
-- Turn on / off inline link icon rendering
enabled = true,
-- Inlined with 'image' elements
image = '󰥶 ',
Expand Down
32 changes: 30 additions & 2 deletions doc/render-markdown.txt
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ Full Default Configuration ~
highlight = '@markup.math',
},
heading = {
-- Turn on / off heading icon & background rendering
enabled = true,
-- Replaces '#+' of 'atx_h._marker'
-- The number of '#' in the heading determines the 'level'
-- The 'level' is used to index into the array using a cycle
Expand All @@ -225,6 +227,8 @@ Full Default Configuration ~
foregrounds = { 'markdownH1', 'markdownH2', 'markdownH3', 'markdownH4', 'markdownH5', 'markdownH6' },
},
code = {
-- Turn on / off code block & inline code rendering
enabled = true,
-- Determines how code blocks & inline code are rendered:
-- none: disables all rendering
-- normal: adds highlight group to code blocks & inline code
Expand All @@ -235,13 +239,17 @@ Full Default Configuration ~
highlight = 'ColorColumn',
},
dash = {
-- Turn on / off thematic break rendering
enabled = true,
-- Replaces '---'|'***'|'___'|'* * *' of 'thematic_break'
-- The icon gets repeated across the window's width
icon = '─',
-- Highlight for the whole line generated from the icon
highlight = 'LineNr',
},
bullet = {
-- Turn on / off list bullet rendering
enabled = true,
-- Replaces '-'|'+'|'*' of 'list_item'
-- How deeply nested the list is determines the 'level'
-- The 'level' is used to index into the array using a cycle
Expand All @@ -253,6 +261,8 @@ Full Default Configuration ~
-- Checkboxes are a special instance of a 'list_item' that start with a 'shortcut_link'
-- There are two special states for unchecked & checked defined in the markdown grammar
checkbox = {
-- Turn on / off checkbox state rendering
enabled = true,
unchecked = {
-- Replaces '[ ]' of 'task_list_marker_unchecked'
icon = '󰄱 ',
Expand All @@ -277,12 +287,16 @@ Full Default Configuration ~
},
},
quote = {
-- Turn on / off block quote & callout rendering
enabled = true,
-- Replaces '>' of 'block_quote'
icon = '▋',
-- Highlight for the quote icon
highlight = '@markup.quote',
},
pipe_table = {
-- Turn on / off pipe table rendering
enabled = true,
-- Determines how the table as a whole is rendered:
-- none: disables all rendering
-- normal: applies the 'cell' style rendering to each row of the table
Expand Down Expand Up @@ -330,7 +344,7 @@ Full Default Configuration ~
quote = { raw = '[!QUOTE]', rendered = '󱆨 Quote', highlight = '@markup.quote' },
},
link = {
-- Turn on / off inline link icon behavior
-- Turn on / off inline link icon rendering
enabled = true,
-- Inlined with 'image' elements
image = '󰥶 ',
Expand Down Expand Up @@ -375,6 +389,8 @@ HEADINGS *render-markdown-setup-headings*
>lua
require('render-markdown').setup({
heading = {
-- Turn on / off heading icon & background rendering
enabled = true,
-- Replaces '#+' of 'atx_h._marker'
-- The number of '#' in the heading determines the 'level'
-- The 'level' is used to index into the array using a cycle
Expand All @@ -399,6 +415,8 @@ CODE BLOCKS *render-markdown-setup-code-blocks*
>lua
require('render-markdown').setup({
code = {
-- Turn on / off code block & inline code rendering
enabled = true,
-- Determines how code blocks & inline code are rendered:
-- none: disables all rendering
-- normal: adds highlight group to code blocks & inline code
Expand All @@ -417,6 +435,8 @@ DASHED LINE *render-markdown-setup-dashed-line*
>lua
require('render-markdown').setup({
dash = {
-- Turn on / off thematic break rendering
enabled = true,
-- Replaces '---'|'***'|'___'|'* * *' of 'thematic_break'
-- The icon gets repeated across the window's width
icon = '─',
Expand All @@ -432,6 +452,8 @@ LIST BULLETS *render-markdown-setup-list-bullets*
>lua
require('render-markdown').setup({
bullet = {
-- Turn on / off list bullet rendering
enabled = true,
-- Replaces '-'|'+'|'*' of 'list_item'
-- How deeply nested the list is determines the 'level'
-- The 'level' is used to index into the array using a cycle
Expand All @@ -451,6 +473,8 @@ CHECKBOXES *render-markdown-setup-checkboxes*
-- Checkboxes are a special instance of a 'list_item' that start with a 'shortcut_link'
-- There are two special states for unchecked & checked defined in the markdown grammar
checkbox = {
-- Turn on / off checkbox state rendering
enabled = true,
unchecked = {
-- Replaces '[ ]' of 'task_list_marker_unchecked'
icon = '󰄱 ',
Expand Down Expand Up @@ -483,6 +507,8 @@ BLOCK QUOTES *render-markdown-setup-block-quotes*
>lua
require('render-markdown').setup({
quote = {
-- Turn on / off block quote & callout rendering
enabled = true,
-- Replaces '>' of 'block_quote'
icon = '▋',
-- Highlight for the quote icon
Expand All @@ -497,6 +523,8 @@ TABLES *render-markdown-setup-tables*
>lua
require('render-markdown').setup({
pipe_table = {
-- Turn on / off pipe table rendering
enabled = true,
-- Determines how the table as a whole is rendered:
-- none: disables all rendering
-- normal: applies the 'cell' style rendering to each row of the table
Expand Down Expand Up @@ -560,7 +588,7 @@ LINKS *render-markdown-setup-links*
>lua
require('render-markdown').setup({
link = {
-- Turn on / off inline link icon behavior
-- Turn on / off inline link icon rendering
enabled = true,
-- Inlined with 'image' elements
image = '󰥶 ',
Expand Down
38 changes: 34 additions & 4 deletions lua/render-markdown/handler/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ M.render_node = function(namespace, buf, capture, node)

if capture == 'heading' then
local heading = state.config.heading
if not heading.enabled then
return
end
local level = vim.fn.strdisplaywidth(value)

local icon = list.cycle(heading.icons, level)
Expand Down Expand Up @@ -57,14 +60,19 @@ M.render_node = function(namespace, buf, capture, node)
})
elseif capture == 'dash' then
local dash = state.config.dash
if not dash.enabled then
return
end
local width = vim.api.nvim_win_get_width(util.buf_to_win(buf))

vim.api.nvim_buf_set_extmark(buf, namespace, start_row, 0, {
virt_text = { { dash.icon:rep(width), dash.highlight } },
virt_text_pos = 'overlay',
})
elseif capture == 'code' then
local code = state.config.code
if not code.enabled then
return
end
if not vim.tbl_contains({ 'normal', 'full' }, code.style) then
return
end
Expand All @@ -76,6 +84,9 @@ M.render_node = function(namespace, buf, capture, node)
})
elseif capture == 'language' then
local code = state.config.code
if not code.enabled then
return
end
if not vim.tbl_contains({ 'language', 'full' }, code.style) then
return
end
Expand Down Expand Up @@ -111,6 +122,9 @@ M.render_node = function(namespace, buf, capture, node)
})
else
local bullet = state.config.bullet
if not bullet.enabled then
return
end
-- List markers from tree-sitter should have leading spaces removed, however there are known
-- edge cases in the parser: https://github.com/tree-sitter-grammars/tree-sitter-markdown/issues/127
-- As a result we handle leading spaces here, can remove if this gets fixed upstream
Expand All @@ -132,6 +146,9 @@ M.render_node = function(namespace, buf, capture, node)
end
elseif capture == 'quote_marker' then
local quote = state.config.quote
if not quote.enabled then
return
end
local highlight = quote.highlight
local quote_node = ts.parent_in_section(node, 'block_quote')
if quote_node ~= nil then
Expand All @@ -147,18 +164,25 @@ M.render_node = function(namespace, buf, capture, node)
virt_text_pos = 'overlay',
})
elseif vim.tbl_contains({ 'checkbox_unchecked', 'checkbox_checked' }, capture) then
local checkbox = state.config.checkbox.unchecked
local checkbox = state.config.checkbox
if not checkbox.enabled then
return
end
local checkbox_state = checkbox.unchecked
if capture == 'checkbox_checked' then
checkbox = state.config.checkbox.checked
checkbox_state = checkbox.checked
end
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
end_row = end_row,
end_col = end_col,
virt_text = { { str.pad_to(value, checkbox.icon), checkbox.highlight } },
virt_text = { { str.pad_to(value, checkbox_state.icon), checkbox_state.highlight } },
virt_text_pos = 'overlay',
})
elseif capture == 'table' then
local pipe_table = state.config.pipe_table
if not pipe_table.enabled then
return
end
if pipe_table.style ~= 'full' then
return
end
Expand Down Expand Up @@ -207,6 +231,9 @@ M.render_node = function(namespace, buf, capture, node)
end
elseif vim.tbl_contains({ 'table_head', 'table_delim', 'table_row' }, capture) then
local pipe_table = state.config.pipe_table
if not pipe_table.enabled then
return
end
if pipe_table.style == 'none' then
return
end
Expand Down Expand Up @@ -261,6 +288,9 @@ end
---@param node TSNode
---@return boolean
M.sibling_checkbox = function(buf, node)
if not state.config.checkbox.enabled then
return false
end
if ts.sibling(node, { 'task_list_marker_unchecked', 'task_list_marker_checked' }) ~= nil then
return true
end
Expand Down
Loading

0 comments on commit b84a788

Please sign in to comment.