Skip to content

Commit c5d8614

Browse files
committed
Added support for rendering bullet icons depending on item level.
- 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 #1
1 parent a61f50b commit c5d8614

File tree

5 files changed

+33
-8
lines changed

5 files changed

+33
-8
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ require('render-markdown').setup({
8585
render_modes = { 'n', 'c' },
8686
-- Characters that will replace the # at the start of headings
8787
headings = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
88-
-- Character to use for the bullet point in lists
89-
bullet = '',
88+
-- Character to use for the bullet points in lists
89+
bullets = {"","","",""},
9090
-- Character that will replace the > at the start of block quotes
9191
quote = '',
9292
-- See :h 'conceallevel' for more information about meaning of values

doc/render-markdown.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ modified by the user.
112112
render_modes = { 'n', 'c' },
113113
-- Characters that will replace the # at the start of headings
114114
headings = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
115-
-- Character to use for the bullet point in lists
116-
bullet = '○',
115+
-- Character to use for the bullet points in lists
116+
bullets = {"●","○","◆","◇"},
117117
-- Character that will replace the > at the start of block quotes
118118
quote = '┃',
119119
-- See :h 'conceallevel' for more information about meaning of values

lua/render-markdown/init.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ local M = {}
2929
---@field public file_types? string[]
3030
---@field public render_modes? string[]
3131
---@field public headings? string[]
32-
---@field public bullet? string
32+
---@field public bullets? string[]
3333
---@field public quote? string
3434
---@field public conceal? UserConceal
3535
---@field public fat_tables? boolean
@@ -71,7 +71,7 @@ function M.setup(opts)
7171
file_types = { 'markdown' },
7272
render_modes = { 'n', 'c' },
7373
headings = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
74-
bullet = '',
74+
bullets = {"","","",""},
7575
quote = '',
7676
conceal = {
7777
default = vim.opt.conceallevel:get(),

lua/render-markdown/state.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
---@field public file_types string[]
2525
---@field public render_modes string[]
2626
---@field public headings string[]
27-
---@field public bullet string
27+
---@field public bullets string[]
2828
---@field public quote string
2929
---@field public conceal Conceal
3030
---@field public fat_tables boolean

lua/render-markdown/ui.lua

+26-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ M.refresh = function()
3737
end)
3838
end
3939

40+
--- Walk through all parent nodes and count the number of nodes with type list
41+
--- to calculate the level of the given node
42+
---@param node TSNode
43+
---@return integer
44+
M.calculate_list_level = function(node)
45+
local candidate = node:parent()
46+
local level = 0
47+
while candidate ~= nil do
48+
local candidate_type = candidate:type()
49+
if candidate_type == "section" or candidate_type == "document" then
50+
-- when reaching a section or the document we are clearly at the
51+
-- top of the list
52+
break
53+
elseif candidate_type == "list" then
54+
-- found a list increase the level and continue
55+
level = level + 1
56+
end
57+
candidate = candidate:parent()
58+
end
59+
return level
60+
end
61+
4062
---@param root TSNode
4163
M.markdown = function(root)
4264
local highlights = state.config.highlights
@@ -73,7 +95,10 @@ M.markdown = function(root)
7395
-- edge cases in the parser: https://github.com/tree-sitter-grammars/tree-sitter-markdown/issues/127
7496
-- As a result we handle leading spaces here, can remove if this gets fixed upstream
7597
local _, leading_spaces = value:find('^%s*')
76-
local virt_text = { string.rep(' ', leading_spaces or 0) .. state.config.bullet, highlights.bullet }
98+
-- Get the level of the list_marker by counting the number of parent list nodes
99+
local level = M.calculate_list_level(node)
100+
local bullet_marker = list.cycle(state.config.bullets, level)
101+
local virt_text = { string.rep(' ', leading_spaces or 0) .. bullet_marker, highlights.bullet }
77102
vim.api.nvim_buf_set_extmark(0, M.namespace, start_row, start_col, {
78103
end_row = end_row,
79104
end_col = end_col,

0 commit comments

Comments
 (0)