Skip to content

Commit 4864c6b

Browse files
committed
fix: Ignore heading patterns in fenced code blocks
- Addresses issue #145 - Fixes folding and jump-to-section behavior - If folding or jump-to-section is initiated with the cursor IN a code block, the behavior will occur in the code block itself (i.e. the cursor will jump to the next heading inside the code block, if there is one, or it will fold the section inside of the code block)
1 parent b1cea92 commit 4864c6b

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

lua/mkdnflow/cursor.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,21 @@ local go_to_heading = function(anchor_text, reverse)
144144
-- Record which line we're on; chances are the link goes to something later,
145145
-- so we'll start looking from here onwards and then circle back to the beginning
146146
local position = vim.api.nvim_win_get_cursor(0)
147-
local starting_row, continue = position[1], true
147+
local starting_row, continue, in_fenced_code_block = position[1], true, false
148148
local row = (reverse and starting_row - 1) or starting_row + 1
149149
while continue do
150150
local line = (reverse and vim.api.nvim_buf_get_lines(0, row - 1, row, false))
151151
or vim.api.nvim_buf_get_lines(0, row - 1, row, false)
152152
-- If the line has contents, do the thing
153153
if line[1] then
154+
-- Are we in a code block?
155+
if string.find(line[1], "^```") then
156+
-- Flip the truth value
157+
in_fenced_code_block = not in_fenced_code_block
158+
end
154159
-- Does the line start with a hash?
155160
local has_heading = string.find(line[1], '^#')
156-
if has_heading then
161+
if has_heading and not in_fenced_code_block then
157162
if anchor_text == nil then
158163
-- Send the cursor to the heading
159164
vim.api.nvim_win_set_cursor(0, { row, 0 })
@@ -191,6 +196,7 @@ local go_to_heading = function(anchor_text, reverse)
191196
-- If the line does not have contents, start searching from the beginning
192197
if anchor_text ~= nil or wrap == true then
193198
row = (reverse and vim.api.nvim_buf_line_count(0)) or 1
199+
in_fenced_code_block = false
194200
else
195201
continue = nil
196202
local place = (reverse and 'beginning') or 'end'

lua/mkdnflow/folds.lua

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ local get_section_range = function(start_row)
3131
vim.api.nvim_buf_line_count(0)
3232
local heading_level = M.getHeadingLevel(line)
3333
if heading_level > 0 then
34-
local continue = true
34+
local continue, in_fenced_code_block = true, false
3535
local end_row = start_row + 1
3636
while continue do
3737
local next_line = vim.api.nvim_buf_get_lines(0, end_row - 1, end_row, false)
3838
if next_line[1] then
39-
if M.getHeadingLevel(next_line[1]) <= heading_level then
39+
if string.find(next_line[1], "^```") then
40+
-- Flip the truth value
41+
in_fenced_code_block = not in_fenced_code_block
42+
end
43+
if M.getHeadingLevel(next_line[1]) <= heading_level and not in_fenced_code_block then
4044
continue = false
4145
else
4246
end_row = end_row + 1
@@ -53,10 +57,14 @@ end
5357

5458
local get_nearest_heading = function()
5559
local row = vim.api.nvim_win_get_cursor(0)[1] - 1
56-
local continue = true
60+
local continue, in_fenced_code_block = true, false
5761
while continue and row > 0 do
5862
local prev_line = vim.api.nvim_buf_get_lines(0, row - 1, row, false)[1]
59-
if M.getHeadingLevel(prev_line) < 99 then
63+
if string.find(prev_line, "^```") then
64+
-- Flip the truth value
65+
in_fenced_code_block = not in_fenced_code_block
66+
end
67+
if M.getHeadingLevel(prev_line) < 99 and not in_fenced_code_block then
6068
continue = false
6169
return row
6270
else

0 commit comments

Comments
 (0)