Skip to content

Commit 01ad32f

Browse files
Change default dash character for thematic breaks
# Details Updates the default thematic break dash to one that creates a continuous line, same one used for tables. Minor refactor on `ts.lua` module to be more consistent in behavior across different methods.
1 parent 6f64bf6 commit 01ad32f

File tree

6 files changed

+36
-34
lines changed

6 files changed

+36
-34
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ require('render-markdown').setup({
137137
-- Characters that will replace the # at the start of headings
138138
headings = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
139139
-- Character to use for the horizontal break
140-
dash = '',
140+
dash = '',
141141
-- Character to use for the bullet points in lists
142142
bullets = { '', '', '', '' },
143143
checkbox = {

demo/box_dash_quote.gif

-7.4 KB
Loading

doc/render-markdown.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ modified by the user.
174174
-- Characters that will replace the # at the start of headings
175175
headings = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
176176
-- Character to use for the horizontal break
177-
dash = '',
177+
dash = '',
178178
-- Character to use for the bullet points in lists
179179
bullets = { '●', '○', '◆', '◇' },
180180
checkbox = {

lua/render-markdown/handler/markdown.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ M.render_node = function(namespace, buf, capture, node)
6262
hl_eol = true,
6363
})
6464
elseif capture == 'list_marker' then
65-
if ts.is_sibling(node, { 'task_list_marker_unchecked', 'task_list_marker_checked' }) then
65+
if ts.sibling(node, { 'task_list_marker_unchecked', 'task_list_marker_checked' }) ~= nil then
6666
-- Hide the list marker for checkboxes rather than replacing with a bullet point
6767
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
6868
end_row = end_row,
@@ -74,7 +74,7 @@ M.render_node = function(namespace, buf, capture, node)
7474
-- edge cases in the parser: https://github.com/tree-sitter-grammars/tree-sitter-markdown/issues/127
7575
-- As a result we handle leading spaces here, can remove if this gets fixed upstream
7676
local _, leading_spaces = value:find('^%s*')
77-
local level = ts.get_list_level(node)
77+
local level = ts.level_in_section(node, 'list')
7878
local bullet = list.cycle(state.config.bullets, level)
7979

8080
local list_marker_text = { string.rep(' ', leading_spaces or 0) .. bullet, highlights.bullet }
@@ -87,7 +87,7 @@ M.render_node = function(namespace, buf, capture, node)
8787
end
8888
elseif capture == 'quote_marker' then
8989
local highlight = highlights.quote
90-
local quote = ts.get_parent(node, 'block_quote')
90+
local quote = ts.parent_in_section(node, 'block_quote')
9191
if quote ~= nil then
9292
local quote_value = vim.treesitter.get_node_text(quote, buf)
9393
local key = callout.get_key_contains(quote_value)
@@ -137,7 +137,7 @@ M.render_node = function(namespace, buf, capture, node)
137137
return result
138138
end
139139

140-
local delim = ts.get_child(node, 'pipe_table_delimiter_row')
140+
local delim = ts.child(node, 'pipe_table_delimiter_row')
141141
if delim == nil then
142142
return
143143
end

lua/render-markdown/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ M.default_config = {
130130
-- Characters that will replace the # at the start of headings
131131
headings = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
132132
-- Character to use for the horizontal break
133-
dash = '',
133+
dash = '',
134134
-- Character to use for the bullet points in lists
135135
bullets = { '', '', '', '' },
136136
checkbox = {

lua/render-markdown/ts.lua

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,61 @@
1-
local M = {}
2-
31
---@param node TSNode
4-
---@param targets string[]
52
---@return boolean
6-
M.is_sibling = function(node, targets)
7-
local sibling = node:next_sibling()
8-
if sibling == nil then
9-
return false
10-
end
11-
return vim.tbl_contains(targets, sibling:type())
3+
local function in_section(node)
4+
-- reaching a section or document means we are outside section
5+
return not vim.tbl_contains({ 'section', 'document' }, node:type())
126
end
137

14-
--- Walk through all parent nodes and count the number of list nodes
8+
local M = {}
9+
10+
---Walk through parent nodes, count the number of target nodes
1511
---@param node TSNode
12+
---@param target string
1613
---@return integer
17-
M.get_list_level = function(node)
14+
M.level_in_section = function(node, target)
1815
local level = 0
1916
local parent = node:parent()
20-
while parent ~= nil do
21-
local parent_type = parent:type()
22-
if vim.tbl_contains({ 'section', 'document' }, parent_type) then
23-
-- reaching a section or document means we are clearly at the top of the list
24-
break
25-
elseif parent_type == 'list' then
26-
-- found a list increase the level and continue
17+
while parent ~= nil and in_section(parent) do
18+
if parent:type() == target then
2719
level = level + 1
2820
end
2921
parent = parent:parent()
3022
end
3123
return level
3224
end
3325

34-
--- Walk through parent nodes until target, return first found
26+
---Walk through parent nodes, return first target node
3527
---@param node TSNode
3628
---@param target string
3729
---@return TSNode?
38-
M.get_parent = function(node, target)
30+
M.parent_in_section = function(node, target)
3931
local parent = node:parent()
40-
while parent ~= nil do
41-
local parent_type = parent:type()
42-
if vim.tbl_contains({ 'section', 'document' }, parent_type) then
43-
-- reaching a section or document means we are clearly outside our target
44-
break
45-
elseif parent_type == target then
32+
while parent ~= nil and in_section(parent) do
33+
if parent:type() == target then
4634
return parent
4735
end
4836
parent = parent:parent()
4937
end
5038
return nil
5139
end
5240

41+
---@param node TSNode
42+
---@param targets string[]
43+
---@return TSNode?
44+
M.sibling = function(node, targets)
45+
local sibling = node:next_sibling()
46+
while sibling ~= nil do
47+
if vim.tbl_contains(targets, sibling:type()) then
48+
return sibling
49+
end
50+
sibling = sibling:next_sibling()
51+
end
52+
return nil
53+
end
54+
5355
---@param node TSNode
5456
---@param target string
5557
---@return TSNode?
56-
M.get_child = function(node, target)
58+
M.child = function(node, target)
5759
for child in node:iter_children() do
5860
if child:type() == target then
5961
return child

0 commit comments

Comments
 (0)