Skip to content

Commit 2f36ac1

Browse files
committedOct 17, 2024
feat: support scope_highlight in custom checkboxes
## Details Request: #207 Functions the same way as scope_highlight for checked and unchecked checkboxes, but for all the user defined ones.
1 parent 7808306 commit 2f36ac1

File tree

9 files changed

+76
-49
lines changed

9 files changed

+76
-49
lines changed
 

Diff for: ‎README.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,12 @@ require('render-markdown').setup({
392392
-- As a result this requires neovim >= 0.10.0 since it relies on 'inline' extmarks
393393
-- Can specify as many additional states as you like following the 'todo' pattern below
394394
-- The key in this case 'todo' is for healthcheck and to allow users to change its values
395-
-- 'raw': Matched against the raw text of a 'shortcut_link'
396-
-- 'rendered': Replaces the 'raw' value when rendering
397-
-- 'highlight': Highlight for the 'rendered' icon
395+
-- 'raw': Matched against the raw text of a 'shortcut_link'
396+
-- 'rendered': Replaces the 'raw' value when rendering
397+
-- 'highlight': Highlight for the 'rendered' icon
398+
-- 'scope_highlight': Highlight for item associated with custom checkbox
398399
custom = {
399-
todo = { raw = '[-]', rendered = '󰥔 ', highlight = 'RenderMarkdownTodo' },
400+
todo = { raw = '[-]', rendered = '󰥔 ', highlight = 'RenderMarkdownTodo', scope_highlight = nil },
400401
},
401402
},
402403
quote = {
@@ -858,11 +859,12 @@ require('render-markdown').setup({
858859
-- As a result this requires neovim >= 0.10.0 since it relies on 'inline' extmarks
859860
-- Can specify as many additional states as you like following the 'todo' pattern below
860861
-- The key in this case 'todo' is for healthcheck and to allow users to change its values
861-
-- 'raw': Matched against the raw text of a 'shortcut_link'
862-
-- 'rendered': Replaces the 'raw' value when rendering
863-
-- 'highlight': Highlight for the 'rendered' icon
862+
-- 'raw': Matched against the raw text of a 'shortcut_link'
863+
-- 'rendered': Replaces the 'raw' value when rendering
864+
-- 'highlight': Highlight for the 'rendered' icon
865+
-- 'scope_highlight': Highlight for item associated with custom checkbox
864866
custom = {
865-
todo = { raw = '[-]', rendered = '󰥔 ', highlight = 'RenderMarkdownTodo' },
867+
todo = { raw = '[-]', rendered = '󰥔 ', highlight = 'RenderMarkdownTodo', scope_highlight = nil },
866868
},
867869
},
868870
})

Diff for: ‎doc/render-markdown.txt

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 October 16
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 October 17
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -441,11 +441,12 @@ Default Configuration ~
441441
-- As a result this requires neovim >= 0.10.0 since it relies on 'inline' extmarks
442442
-- Can specify as many additional states as you like following the 'todo' pattern below
443443
-- The key in this case 'todo' is for healthcheck and to allow users to change its values
444-
-- 'raw': Matched against the raw text of a 'shortcut_link'
445-
-- 'rendered': Replaces the 'raw' value when rendering
446-
-- 'highlight': Highlight for the 'rendered' icon
444+
-- 'raw': Matched against the raw text of a 'shortcut_link'
445+
-- 'rendered': Replaces the 'raw' value when rendering
446+
-- 'highlight': Highlight for the 'rendered' icon
447+
-- 'scope_highlight': Highlight for item associated with custom checkbox
447448
custom = {
448-
todo = { raw = '[-]', rendered = '󰥔 ', highlight = 'RenderMarkdownTodo' },
449+
todo = { raw = '[-]', rendered = '󰥔 ', highlight = 'RenderMarkdownTodo', scope_highlight = nil },
449450
},
450451
},
451452
quote = {
@@ -895,11 +896,12 @@ Checkbox Configuration ~
895896
-- As a result this requires neovim >= 0.10.0 since it relies on 'inline' extmarks
896897
-- Can specify as many additional states as you like following the 'todo' pattern below
897898
-- The key in this case 'todo' is for healthcheck and to allow users to change its values
898-
-- 'raw': Matched against the raw text of a 'shortcut_link'
899-
-- 'rendered': Replaces the 'raw' value when rendering
900-
-- 'highlight': Highlight for the 'rendered' icon
899+
-- 'raw': Matched against the raw text of a 'shortcut_link'
900+
-- 'rendered': Replaces the 'raw' value when rendering
901+
-- 'highlight': Highlight for the 'rendered' icon
902+
-- 'scope_highlight': Highlight for item associated with custom checkbox
901903
custom = {
902-
todo = { raw = '[-]', rendered = '󰥔 ', highlight = 'RenderMarkdownTodo' },
904+
todo = { raw = '[-]', rendered = '󰥔 ', highlight = 'RenderMarkdownTodo', scope_highlight = nil },
903905
},
904906
},
905907
})

Diff for: ‎lua/render-markdown/health.lua

+1-1
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.4.0'
7+
M.version = '7.4.1'
88

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

Diff for: ‎lua/render-markdown/init.lua

+6-4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ local M = {}
9292
---@field public raw? string
9393
---@field public rendered? string
9494
---@field public highlight? string
95+
---@field public scope_highlight? string
9596

9697
---@class (exact) render.md.UserCheckboxComponent
9798
---@field public icon? string
@@ -506,11 +507,12 @@ M.default_config = {
506507
-- As a result this requires neovim >= 0.10.0 since it relies on 'inline' extmarks
507508
-- Can specify as many additional states as you like following the 'todo' pattern below
508509
-- The key in this case 'todo' is for healthcheck and to allow users to change its values
509-
-- 'raw': Matched against the raw text of a 'shortcut_link'
510-
-- 'rendered': Replaces the 'raw' value when rendering
511-
-- 'highlight': Highlight for the 'rendered' icon
510+
-- 'raw': Matched against the raw text of a 'shortcut_link'
511+
-- 'rendered': Replaces the 'raw' value when rendering
512+
-- 'highlight': Highlight for the 'rendered' icon
513+
-- 'scope_highlight': Highlight for item associated with custom checkbox
512514
custom = {
513-
todo = { raw = '[-]', rendered = '󰥔 ', highlight = 'RenderMarkdownTodo' },
515+
todo = { raw = '[-]', rendered = '󰥔 ', highlight = 'RenderMarkdownTodo', scope_highlight = nil },
514516
},
515517
},
516518
quote = {

Diff for: ‎lua/render-markdown/render/base.lua

+21
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@ function Base:sign(text, highlight)
4444
})
4545
end
4646

47+
---@protected
48+
---@param highlight? string
49+
function Base:checkbox_scope(highlight)
50+
if highlight == nil then
51+
return
52+
end
53+
local paragraph = self.info:sibling('paragraph')
54+
if paragraph == nil then
55+
return
56+
end
57+
paragraph = paragraph:child('inline')
58+
if paragraph == nil then
59+
return
60+
end
61+
self.marks:add('check_scope', paragraph.start_row, paragraph.start_col, {
62+
end_row = paragraph.end_row,
63+
end_col = paragraph.end_col,
64+
hl_group = highlight,
65+
})
66+
end
67+
4768
---@protected
4869
---@param destination string
4970
---@return render.md.LinkComponent?

Diff for: ‎lua/render-markdown/render/checkbox.lua

+1-18
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ end
3939

4040
function Render:render()
4141
self:icon()
42-
self:highlight_scope()
42+
self:checkbox_scope(self.checkbox.scope_highlight)
4343
end
4444

4545
---@private
@@ -55,21 +55,4 @@ function Render:icon()
5555
})
5656
end
5757

58-
---@private
59-
function Render:highlight_scope()
60-
local highlight = self.checkbox.scope_highlight
61-
if highlight == nil then
62-
return
63-
end
64-
local paragraph = self.info:sibling('paragraph')
65-
if paragraph == nil then
66-
return
67-
end
68-
self.marks:add('check_scope', paragraph.start_row, paragraph.start_col, {
69-
end_row = paragraph.end_row,
70-
end_col = paragraph.end_col,
71-
hl_group = highlight,
72-
})
73-
end
74-
7558
return Render

Diff for: ‎lua/render-markdown/render/list_marker.lua

+24-8
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ local Base = require('render-markdown.render.base')
22
local list = require('render-markdown.core.list')
33
local str = require('render-markdown.core.str')
44

5+
---@class render.md.data.ListMarker
6+
---@field leading_spaces integer
7+
---@field checkbox? render.md.CustomCheckbox
8+
59
---@class render.md.render.ListMarker: render.md.Renderer
610
---@field private bullet render.md.Bullet
7-
---@field private leading_spaces integer
11+
---@field private data render.md.data.ListMarker
812
local Render = setmetatable({}, Base)
913
Render.__index = Render
1014

@@ -21,10 +25,13 @@ end
2125
function Render:setup()
2226
self.bullet = self.config.bullet
2327

24-
-- List markers from tree-sitter should have leading spaces removed, however there are edge
25-
-- cases in the parser: https://github.com/tree-sitter-grammars/tree-sitter-markdown/issues/127
26-
-- As a result we account for leading spaces here, can remove if this gets fixed upstream
27-
self.leading_spaces = str.spaces('start', self.info.text)
28+
self.data = {
29+
-- List markers from tree-sitter should have leading spaces removed, however there are edge
30+
-- cases in the parser: https://github.com/tree-sitter-grammars/tree-sitter-markdown/issues/127
31+
-- As a result we account for leading spaces here, can remove if this gets fixed upstream
32+
leading_spaces = str.spaces('start', self.info.text),
33+
checkbox = self.context:get_checkbox(self.info),
34+
}
2835

2936
return true
3037
end
@@ -33,6 +40,7 @@ function Render:render()
3340
if self:sibling_checkbox() then
3441
-- Hide the list marker for checkboxes rather than replacing with a bullet point
3542
self:hide_marker()
43+
self:highlight_scope()
3644
else
3745
if not self.bullet.enabled then
3846
return
@@ -49,7 +57,7 @@ function Render:sibling_checkbox()
4957
if not self.config.checkbox.enabled then
5058
return false
5159
end
52-
if self.context:get_checkbox(self.info) ~= nil then
60+
if self.data.checkbox ~= nil then
5361
return true
5462
end
5563
if self.info:sibling('task_list_marker_unchecked') ~= nil then
@@ -63,13 +71,21 @@ end
6371

6472
---@private
6573
function Render:hide_marker()
66-
self.marks:add('check_icon', self.info.start_row, self.info.start_col + self.leading_spaces, {
74+
self.marks:add('check_icon', self.info.start_row, self.info.start_col + self.data.leading_spaces, {
6775
end_row = self.info.end_row,
6876
end_col = self.info.end_col,
6977
conceal = '',
7078
})
7179
end
7280

81+
---@private
82+
function Render:highlight_scope()
83+
if self.data.checkbox == nil then
84+
return
85+
end
86+
self:checkbox_scope(self.data.checkbox.scope_highlight)
87+
end
88+
7389
---@private
7490
---@param level integer
7591
function Render:icon(level)
@@ -80,7 +96,7 @@ function Render:icon(level)
8096
self.marks:add('bullet', self.info.start_row, self.info.start_col, {
8197
end_row = self.info.end_row,
8298
end_col = self.info.end_col,
83-
virt_text = { { str.pad(self.leading_spaces) .. icon, self.bullet.highlight } },
99+
virt_text = { { str.pad(self.data.leading_spaces) .. icon, self.bullet.highlight } },
84100
virt_text_pos = 'overlay',
85101
})
86102
end

Diff for: ‎lua/render-markdown/state.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ function M.validate()
200200
:type('scope_highlight', { 'string', 'nil' })
201201
:check()
202202
get_spec({ 'checkbox', 'custom' }):for_each(false, function(spec)
203-
spec:type({ 'raw', 'rendered', 'highlight' }, 'string')
203+
spec:type({ 'raw', 'rendered', 'highlight' }, 'string'):type('scope_highlight', { 'string', 'nil' })
204204
end)
205205

206206
get_spec('quote')

Diff for: ‎lua/render-markdown/types.lua

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
---@field public raw string
7676
---@field public rendered string
7777
---@field public highlight string
78+
---@field public scope_highlight? string
7879

7980
---@class (exact) render.md.CheckboxComponent
8081
---@field public icon string

0 commit comments

Comments
 (0)
Please sign in to comment.