Skip to content

Commit a7097f3

Browse files
feat: add default icons for ordered lists
## Details Request: #250 Previously we supported adding icons for ordered lists in much the same way as unordered, however by default the value was empty. To do an auto ordering for the pattern of using `1.` for every value we now provide a default value. However, because ordered lists can go on and we want to support arbitrary lengths we now also support setting a function for both ordered and unordered list icons. The function is called with both the level of the list and the index of the item and whatever value is returned is used as the icon. The default function just concatenates a period after the index value and ignores the level.
1 parent 7674543 commit a7097f3

File tree

9 files changed

+93
-36
lines changed

9 files changed

+93
-36
lines changed

README.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -362,14 +362,20 @@ require('render-markdown').setup({
362362
-- Turn on / off list bullet rendering
363363
enabled = true,
364364
-- Replaces '-'|'+'|'*' of 'list_item'
365-
-- How deeply nested the list is determines the 'level' which is used to index into the list using a cycle
366-
-- The item number in the list is used to index into the value using a clamp if the value is also a list
365+
-- How deeply nested the list is determines the 'level', how far down at that level determines the 'index'
366+
-- If a function is provided both of these values are passed in using 1 based indexing
367+
-- If a list is provided we index into it using a cycle based on the level
368+
-- If the value at that level is also a list we further index into it using a clamp based on the index
367369
-- If the item is a 'checkbox' a conceal is used to hide the bullet instead
368370
icons = { '', '', '', '' },
369371
-- Replaces 'n.'|'n)' of 'list_item'
370-
-- How deeply nested the list is determines the 'level' which is used to index into the list using a cycle
371-
-- The item number in the list is used to index into the value using a clamp if the value is also a list
372-
ordered_icons = {},
372+
-- How deeply nested the list is determines the 'level', how far down at that level determines the 'index'
373+
-- If a function is provided both of these values are passed in using 1 based indexing
374+
-- If a list is provided we index into it using a cycle based on the level
375+
-- If the value at that level is also a list we further index into it using a clamp based on the index
376+
ordered_icons = function(level, index)
377+
return string.format('%d.', index)
378+
end,
373379
-- Padding to add to the left of bullet point
374380
left_pad = 0,
375381
-- Padding to add to the right of bullet point
@@ -859,14 +865,20 @@ require('render-markdown').setup({
859865
-- Turn on / off list bullet rendering
860866
enabled = true,
861867
-- Replaces '-'|'+'|'*' of 'list_item'
862-
-- How deeply nested the list is determines the 'level' which is used to index into the list using a cycle
863-
-- The item number in the list is used to index into the value using a clamp if the value is also a list
868+
-- How deeply nested the list is determines the 'level', how far down at that level determines the 'index'
869+
-- If a function is provided both of these values are passed in using 1 based indexing
870+
-- If a list is provided we index into it using a cycle based on the level
871+
-- If the value at that level is also a list we further index into it using a clamp based on the index
864872
-- If the item is a 'checkbox' a conceal is used to hide the bullet instead
865873
icons = { '', '', '', '' },
866874
-- Replaces 'n.'|'n)' of 'list_item'
867-
-- How deeply nested the list is determines the 'level' which is used to index into the list using a cycle
868-
-- The item number in the list is used to index into the value using a clamp if the value is also a list
869-
ordered_icons = {},
875+
-- How deeply nested the list is determines the 'level', how far down at that level determines the 'index'
876+
-- If a function is provided both of these values are passed in using 1 based indexing
877+
-- If a list is provided we index into it using a cycle based on the level
878+
-- If the value at that level is also a list we further index into it using a clamp based on the index
879+
ordered_icons = function(level, index)
880+
return string.format('%d.', index)
881+
end,
870882
-- Padding to add to the left of bullet point
871883
left_pad = 0,
872884
-- Padding to add to the right of bullet point

doc/render-markdown.txt

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 December 04
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 December 05
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -409,14 +409,20 @@ Default Configuration ~
409409
-- Turn on / off list bullet rendering
410410
enabled = true,
411411
-- Replaces '-'|'+'|'*' of 'list_item'
412-
-- How deeply nested the list is determines the 'level' which is used to index into the list using a cycle
413-
-- The item number in the list is used to index into the value using a clamp if the value is also a list
412+
-- How deeply nested the list is determines the 'level', how far down at that level determines the 'index'
413+
-- If a function is provided both of these values are passed in using 1 based indexing
414+
-- If a list is provided we index into it using a cycle based on the level
415+
-- If the value at that level is also a list we further index into it using a clamp based on the index
414416
-- If the item is a 'checkbox' a conceal is used to hide the bullet instead
415417
icons = { '●', '○', '◆', '◇' },
416418
-- Replaces 'n.'|'n)' of 'list_item'
417-
-- How deeply nested the list is determines the 'level' which is used to index into the list using a cycle
418-
-- The item number in the list is used to index into the value using a clamp if the value is also a list
419-
ordered_icons = {},
419+
-- How deeply nested the list is determines the 'level', how far down at that level determines the 'index'
420+
-- If a function is provided both of these values are passed in using 1 based indexing
421+
-- If a list is provided we index into it using a cycle based on the level
422+
-- If the value at that level is also a list we further index into it using a clamp based on the index
423+
ordered_icons = function(level, index)
424+
return string.format('%d.', index)
425+
end,
420426
-- Padding to add to the left of bullet point
421427
left_pad = 0,
422428
-- Padding to add to the right of bullet point
@@ -896,14 +902,20 @@ Bullet Point Configuration ~
896902
-- Turn on / off list bullet rendering
897903
enabled = true,
898904
-- Replaces '-'|'+'|'*' of 'list_item'
899-
-- How deeply nested the list is determines the 'level' which is used to index into the list using a cycle
900-
-- The item number in the list is used to index into the value using a clamp if the value is also a list
905+
-- How deeply nested the list is determines the 'level', how far down at that level determines the 'index'
906+
-- If a function is provided both of these values are passed in using 1 based indexing
907+
-- If a list is provided we index into it using a cycle based on the level
908+
-- If the value at that level is also a list we further index into it using a clamp based on the index
901909
-- If the item is a 'checkbox' a conceal is used to hide the bullet instead
902910
icons = { '●', '○', '◆', '◇' },
903911
-- Replaces 'n.'|'n)' of 'list_item'
904-
-- How deeply nested the list is determines the 'level' which is used to index into the list using a cycle
905-
-- The item number in the list is used to index into the value using a clamp if the value is also a list
906-
ordered_icons = {},
912+
-- How deeply nested the list is determines the 'level', how far down at that level determines the 'index'
913+
-- If a function is provided both of these values are passed in using 1 based indexing
914+
-- If a list is provided we index into it using a cycle based on the level
915+
-- If the value at that level is also a list we further index into it using a clamp based on the index
916+
ordered_icons = function(level, index)
917+
return string.format('%d.', index)
918+
end,
907919
-- Padding to add to the left of bullet point
908920
left_pad = 0,
909921
-- Padding to add to the right of bullet point

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local state = require('render-markdown.state')
44
local M = {}
55

66
---@private
7-
M.version = '7.6.10'
7+
M.version = '7.6.11'
88

99
function M.check()
1010
M.start('version')

lua/render-markdown/init.lua

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,12 @@ local M = {}
127127
---@field public checked? render.md.UserCheckboxComponent
128128
---@field public custom? table<string, render.md.UserCustomCheckbox>
129129

130+
---@alias render.md.bullet.Icons string[]|string[][]|fun(level: integer, index: integer): string?
131+
130132
---@class (exact) render.md.UserBullet
131133
---@field public enabled? boolean
132-
---@field public icons? (string|string[])[]
133-
---@field public ordered_icons? (string|string[])[]
134+
---@field public icons? render.md.bullet.Icons
135+
---@field public ordered_icons? render.md.bullet.Icons
134136
---@field public left_pad? integer
135137
---@field public right_pad? integer
136138
---@field public highlight? string
@@ -494,14 +496,20 @@ M.default_config = {
494496
-- Turn on / off list bullet rendering
495497
enabled = true,
496498
-- Replaces '-'|'+'|'*' of 'list_item'
497-
-- How deeply nested the list is determines the 'level' which is used to index into the list using a cycle
498-
-- The item number in the list is used to index into the value using a clamp if the value is also a list
499+
-- How deeply nested the list is determines the 'level', how far down at that level determines the 'index'
500+
-- If a function is provided both of these values are passed in using 1 based indexing
501+
-- If a list is provided we index into it using a cycle based on the level
502+
-- If the value at that level is also a list we further index into it using a clamp based on the index
499503
-- If the item is a 'checkbox' a conceal is used to hide the bullet instead
500504
icons = { '', '', '', '' },
501505
-- Replaces 'n.'|'n)' of 'list_item'
502-
-- How deeply nested the list is determines the 'level' which is used to index into the list using a cycle
503-
-- The item number in the list is used to index into the value using a clamp if the value is also a list
504-
ordered_icons = {},
506+
-- How deeply nested the list is determines the 'level', how far down at that level determines the 'index'
507+
-- If a function is provided both of these values are passed in using 1 based indexing
508+
-- If a list is provided we index into it using a cycle based on the level
509+
-- If the value at that level is also a list we further index into it using a clamp based on the index
510+
ordered_icons = function(level, index)
511+
return string.format('%d.', index)
512+
end,
505513
-- Padding to add to the left of bullet point
506514
left_pad = 0,
507515
-- Padding to add to the right of bullet point

lua/render-markdown/render/list_item.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,16 @@ end
8787
---@private
8888
---@param level integer
8989
function Render:icon(level)
90+
local index = self.node:sibling_count('list_item')
9091
local icons = self.data.ordered and self.bullet.ordered_icons or self.bullet.icons
91-
local icon = List.cycle(icons, level)
92-
if type(icon) == 'table' then
93-
icon = List.clamp(icon, self.node:sibling_count('list_item'))
92+
local icon = nil
93+
if type(icons) == 'function' then
94+
icon = icons(level, index)
95+
else
96+
icon = List.cycle(icons, level)
97+
if type(icon) == 'table' then
98+
icon = List.clamp(icon, index)
99+
end
94100
end
95101
if icon == nil then
96102
return

lua/render-markdown/state.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ function M.validate()
189189
:type('enabled', 'boolean')
190190
:type({ 'left_pad', 'right_pad' }, 'number')
191191
:type('highlight', 'string')
192-
:list_or_list_of_list({ 'icons', 'ordered_icons' }, 'string')
192+
:list_or_list_of_list({ 'icons', 'ordered_icons' }, 'string', 'function')
193193
:check()
194194
end)
195195
:nested('checkbox', function(checkbox)

lua/render-markdown/types.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@
110110

111111
---@class (exact) render.md.Bullet
112112
---@field public enabled boolean
113-
---@field public icons (string|string[])[]
114-
---@field public ordered_icons (string|string[])[]
113+
---@field public icons render.md.bullet.Icons
114+
---@field public ordered_icons render.md.bullet.Icons
115115
---@field public left_pad integer
116116
---@field public right_pad integer
117117
---@field public highlight string

tests/list_table_spec.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ describe('list_table.md', function()
2626

2727
vim.list_extend(expected, util.heading(row:increment(2), 1))
2828

29-
vim.list_extend(expected, util.heading(row:increment(5), 1))
29+
vim.list_extend(expected, {
30+
util.ordered(row:increment(2), 0, '1.'),
31+
util.ordered(row:increment(1), 0, '2.'),
32+
})
33+
34+
vim.list_extend(expected, util.heading(row:increment(2), 1))
3035

3136
vim.list_extend(expected, {
3237
util.table_border(row:increment(2), true, { 8, 15, 7, 6 }),

tests/util.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,20 @@ function M.bullet(row, col, level, spaces)
165165
}
166166
end
167167

168+
---@param row integer
169+
---@param col integer
170+
---@param text string
171+
---@return render.md.MarkInfo
172+
function M.ordered(row, col, text)
173+
---@type render.md.MarkInfo
174+
return {
175+
row = { row, row },
176+
col = { col, col + 3 },
177+
virt_text = { { text, M.hl('Bullet') } },
178+
virt_text_pos = 'overlay',
179+
}
180+
end
181+
168182
---@param row integer
169183
---@param start_col integer
170184
---@param end_col integer

0 commit comments

Comments
 (0)