fix(markdown): Ensure content is followed by '#'

Before this commit, empty headings are taken into account:

   # a normal heading

   below is not a good heading:

   #

   below is also not a good heading:

   ##

   These should not be listed in the outline.
This commit is contained in:
hedy
2023-11-07 21:12:06 +08:00
parent 315fe5c256
commit cb1c108a0c
2 changed files with 49 additions and 38 deletions

View File

@@ -47,6 +47,8 @@ I do not merge PRs from the original repo that I don't personally need.
- Open handler triggering multiple times ends up in messy state with errors - Open handler triggering multiple times ends up in messy state with errors
simrat39/symbols-outline.nvim#235 simrat39/symbols-outline.nvim#235
- Fixed `_highlight_current_item` function checking provider on outline window - Fixed `_highlight_current_item` function checking provider on outline window
- Fixed behaviour of empty markdown headings `^(#+)(%s+)$` being listed in the
outline.
## 🛑 Breaking changes ## 🛑 Breaking changes

View File

@@ -15,11 +15,14 @@ function M.handle_markdown()
if string.find(value, '^```') then if string.find(value, '^```') then
is_inside_code_block = not is_inside_code_block is_inside_code_block = not is_inside_code_block
end end
if is_inside_code_block then
goto nextline
end
local next_value = lines[line+1] local next_value = lines[line+1]
local is_emtpy_line = #value:gsub("^%s*(.-)%s*$", "%1") == 0 local is_emtpy_line = #value:gsub("^%s*(.-)%s*$", "%1") == 0
local header, title = string.match(value, '^(#+)%s+(.*)$') local header, title = string.match(value, '^(#+)%s+(.+)$')
if not header and next_value and not is_emtpy_line then if not header and next_value and not is_emtpy_line then
if string.match(next_value, '^=+%s*$') then if string.match(next_value, '^=+%s*$') then
header = '#' header = '#'
@@ -29,44 +32,50 @@ function M.handle_markdown()
title = value title = value
end end
end end
if not header or not title then
if header and not is_inside_code_block then goto nextline
local depth = #header + 1
local parent
for i = depth - 1, 1, -1 do
if level_symbols[i] ~= nil then
parent = level_symbols[i].children
break
end
end
for i = depth, max_level do
if level_symbols[i] ~= nil then
level_symbols[i].selectionRange['end'].line = line - 1
level_symbols[i].range['end'].line = line - 1
level_symbols[i] = nil
end
end
max_level = depth
local entry = {
kind = 13,
name = title,
selectionRange = {
start = { character = 1, line = line - 1 },
['end'] = { character = 1, line = line - 1 },
},
range = {
start = { character = 1, line = line - 1 },
['end'] = { character = 1, line = line - 1 },
},
children = {},
}
parent[#parent + 1] = entry
level_symbols[depth] = entry
end end
-- TODO: This is not needed and it works?
-- if #header > 6 then
-- goto nextline
-- end
local depth = #header + 1
local parent
for i = depth - 1, 1, -1 do
if level_symbols[i] ~= nil then
parent = level_symbols[i].children
break
end
end
for i = depth, max_level do
if level_symbols[i] ~= nil then
level_symbols[i].selectionRange['end'].line = line - 1
level_symbols[i].range['end'].line = line - 1
level_symbols[i] = nil
end
end
max_level = depth
local entry = {
kind = 13,
name = title,
selectionRange = {
start = { character = 1, line = line - 1 },
['end'] = { character = 1, line = line - 1 },
},
range = {
start = { character = 1, line = line - 1 },
['end'] = { character = 1, line = line - 1 },
},
children = {},
}
parent[#parent + 1] = entry
level_symbols[depth] = entry
::nextline::
end end
for i = 2, max_level do for i = 2, max_level do