mirror of
https://github.com/neovim/neovim.git
synced 2025-09-05 19:08:15 +00:00
fix: bug in stylize_markdown
`stripped` and `markdown_lines` are iterated together so must have the same length.
This commit is contained in:

committed by
Lewis Russell

parent
98f5aa2564
commit
379c37fa0b
@@ -1138,10 +1138,8 @@ function M.stylize_markdown(bufnr, contents, opts)
|
|||||||
-- Clean up
|
-- Clean up
|
||||||
contents = vim.split(table.concat(contents, '\n'), '\n', { trimempty = true })
|
contents = vim.split(table.concat(contents, '\n'), '\n', { trimempty = true })
|
||||||
|
|
||||||
local stripped = {}
|
local stripped = {} --- @type string[]
|
||||||
local highlights = {} --- @type {ft:string,start:integer,finish:integer}[]
|
local highlights = {} --- @type {ft:string,start:integer,finish:integer}[]
|
||||||
-- keep track of lnums that contain markdown
|
|
||||||
local markdown_lines = {} --- @type table<integer,boolean>
|
|
||||||
|
|
||||||
local i = 1
|
local i = 1
|
||||||
while i <= #contents do
|
while i <= #contents do
|
||||||
@@ -1156,7 +1154,7 @@ function M.stylize_markdown(bufnr, contents, opts)
|
|||||||
i = i + 1
|
i = i + 1
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
table.insert(stripped, line)
|
stripped[#stripped + 1] = line
|
||||||
i = i + 1
|
i = i + 1
|
||||||
end
|
end
|
||||||
table.insert(highlights, {
|
table.insert(highlights, {
|
||||||
@@ -1166,30 +1164,23 @@ function M.stylize_markdown(bufnr, contents, opts)
|
|||||||
})
|
})
|
||||||
-- add a separator, but not on the last line
|
-- add a separator, but not on the last line
|
||||||
if opts.separator and i < #contents then
|
if opts.separator and i < #contents then
|
||||||
table.insert(stripped, '---')
|
stripped[#stripped + 1] = '---'
|
||||||
markdown_lines[#stripped] = true
|
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
-- strip any empty lines or separators prior to this separator in actual markdown
|
-- strip any empty lines or separators prior to this separator in actual markdown
|
||||||
if line:match('^---+$') then
|
if line:match('^---+$') then
|
||||||
while
|
while
|
||||||
markdown_lines[#stripped]
|
stripped[#stripped]
|
||||||
and (stripped[#stripped]:match('^%s*$') or stripped[#stripped]:match('^---+$'))
|
and (stripped[#stripped]:match('^%s*$') or stripped[#stripped]:match('^---+$'))
|
||||||
do
|
do
|
||||||
markdown_lines[#stripped] = false
|
stripped[#stripped] = nil
|
||||||
table.remove(stripped, #stripped)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- add the line if its not an empty line following a separator
|
-- add the line if its not an empty line following a separator
|
||||||
if
|
if
|
||||||
not (
|
not (line:match('^%s*$') and stripped[#stripped] and stripped[#stripped]:match('^---+$'))
|
||||||
line:match('^%s*$')
|
|
||||||
and markdown_lines[#stripped]
|
|
||||||
and stripped[#stripped]:match('^---+$')
|
|
||||||
)
|
|
||||||
then
|
then
|
||||||
table.insert(stripped, line)
|
stripped[#stripped + 1] = line
|
||||||
markdown_lines[#stripped] = true
|
|
||||||
end
|
end
|
||||||
i = i + 1
|
i = i + 1
|
||||||
end
|
end
|
||||||
@@ -1220,7 +1211,7 @@ function M.stylize_markdown(bufnr, contents, opts)
|
|||||||
|
|
||||||
local sep_line = string.rep('─', math.min(width, opts.wrap_at or width))
|
local sep_line = string.rep('─', math.min(width, opts.wrap_at or width))
|
||||||
|
|
||||||
for l in pairs(markdown_lines) do
|
for l in ipairs(stripped) do
|
||||||
if stripped[l]:match('^---+$') then
|
if stripped[l]:match('^---+$') then
|
||||||
stripped[l] = sep_line
|
stripped[l] = sep_line
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user