Skip to content

Commit 7f90f52

Browse files
Use strdisplaywidth in place of # for string lengths
Resolves: #26 Currently there are a couple places where when calculating the length of a particular string we simply use the Lua `#`. This works well enough for most characters, but not well with more complex ones such as with Chinese, as described in the issue. Using strdisplaywidth accounts for how Neovim will actually render the characters.
1 parent 43bbefd commit 7f90f52

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

lua/render-markdown/handler/markdown.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ M.render = function(namespace, root, buf)
1717
logger.debug_node(capture, node, buf)
1818

1919
if capture == 'heading' then
20-
local level = #value
20+
local level = vim.fn.strdisplaywidth(value)
2121

2222
local heading = list.cycle(state.config.headings, level)
2323
-- Available width is level + 1, where level = number of `#` characters and one is added
@@ -54,7 +54,7 @@ M.render = function(namespace, root, buf)
5454
local list_marker_overlay = ''
5555
if M.is_sibling_checkbox(node) then
5656
-- Hide the list marker for checkboxes rather than replacing with a bullet point
57-
list_marker_overlay = string.rep(' ', #value)
57+
list_marker_overlay = string.rep(' ', vim.fn.strdisplaywidth(value))
5858
else
5959
-- List markers from tree-sitter should have leading spaces removed, however there are known
6060
-- edge cases in the parser: https://github.com/tree-sitter-grammars/tree-sitter-markdown/issues/127
@@ -103,10 +103,10 @@ M.render = function(namespace, root, buf)
103103
local lines = vim.api.nvim_buf_get_lines(buf, start_row, end_row, false)
104104
local table_head = list.first(lines)
105105
local table_tail = list.last(lines)
106-
if #table_head == #table_tail then
106+
if vim.fn.strdisplaywidth(table_head) == vim.fn.strdisplaywidth(table_tail) then
107107
local headings = vim.split(table_head, '|', { plain = true, trimempty = true })
108108
local sections = vim.tbl_map(function(part)
109-
return string.rep('', #part)
109+
return string.rep('', vim.fn.strdisplaywidth(part))
110110
end, headings)
111111

112112
local line_above = { { '' .. table.concat(sections, '') .. '', highlights.table.head } }

0 commit comments

Comments
 (0)