Skip to content

Commit

Permalink
Added support for rendering bullet icons depending on item level.
Browse files Browse the repository at this point in the history
- To calculate the level of the list_item the number of parent nodes
  with type "list" is counted.
- Replaced config option bullet string with bullets string[]

Closes MeanderingProgrammer#1
  • Loading branch information
redimp committed Mar 31, 2024
1 parent a61f50b commit c5d8614
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ require('render-markdown').setup({
render_modes = { 'n', 'c' },
-- Characters that will replace the # at the start of headings
headings = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
-- Character to use for the bullet point in lists
bullet = '',
-- Character to use for the bullet points in lists
bullets = {"","","",""},
-- Character that will replace the > at the start of block quotes
quote = '',
-- See :h 'conceallevel' for more information about meaning of values
Expand Down
4 changes: 2 additions & 2 deletions doc/render-markdown.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ modified by the user.
render_modes = { 'n', 'c' },
-- Characters that will replace the # at the start of headings
headings = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
-- Character to use for the bullet point in lists
bullet = '○',
-- Character to use for the bullet points in lists
bullets = {"●","○","◆","◇"},
-- Character that will replace the > at the start of block quotes
quote = '┃',
-- See :h 'conceallevel' for more information about meaning of values
Expand Down
4 changes: 2 additions & 2 deletions lua/render-markdown/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ local M = {}
---@field public file_types? string[]
---@field public render_modes? string[]
---@field public headings? string[]
---@field public bullet? string
---@field public bullets? string[]
---@field public quote? string
---@field public conceal? UserConceal
---@field public fat_tables? boolean
Expand Down Expand Up @@ -71,7 +71,7 @@ function M.setup(opts)
file_types = { 'markdown' },
render_modes = { 'n', 'c' },
headings = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
bullet = '',
bullets = {"","","",""},
quote = '',
conceal = {
default = vim.opt.conceallevel:get(),
Expand Down
2 changes: 1 addition & 1 deletion lua/render-markdown/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
---@field public file_types string[]
---@field public render_modes string[]
---@field public headings string[]
---@field public bullet string
---@field public bullets string[]
---@field public quote string
---@field public conceal Conceal
---@field public fat_tables boolean
Expand Down
27 changes: 26 additions & 1 deletion lua/render-markdown/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ M.refresh = function()
end)
end

--- Walk through all parent nodes and count the number of nodes with type list
--- to calculate the level of the given node
---@param node TSNode
---@return integer
M.calculate_list_level = function(node)
local candidate = node:parent()
local level = 0
while candidate ~= nil do
local candidate_type = candidate:type()
if candidate_type == "section" or candidate_type == "document" then
-- when reaching a section or the document we are clearly at the
-- top of the list
break
elseif candidate_type == "list" then
-- found a list increase the level and continue
level = level + 1
end
candidate = candidate:parent()
end
return level
end

---@param root TSNode
M.markdown = function(root)
local highlights = state.config.highlights
Expand Down Expand Up @@ -73,7 +95,10 @@ M.markdown = function(root)
-- 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
local _, leading_spaces = value:find('^%s*')
local virt_text = { string.rep(' ', leading_spaces or 0) .. state.config.bullet, highlights.bullet }
-- Get the level of the list_marker by counting the number of parent list nodes
local level = M.calculate_list_level(node)
local bullet_marker = list.cycle(state.config.bullets, level)
local virt_text = { string.rep(' ', leading_spaces or 0) .. bullet_marker, highlights.bullet }
vim.api.nvim_buf_set_extmark(0, M.namespace, start_row, start_col, {
end_row = end_row,
end_col = end_col,
Expand Down

0 comments on commit c5d8614

Please sign in to comment.