+
+ let l:bufnr = bufnr('%')
+ let l:cursor_line = line('.')
+ let l:cursor_header = 0
+ let l:fenced_block = 0
+ let l:front_matter = 0
+ let l:header_list = []
+ let l:header_max_len = 0
+ let l:vim_markdown_toc_autofit = get(g:, "vim_markdown_toc_autofit", 0)
+ let l:vim_markdown_frontmatter = get(g:, "vim_markdown_frontmatter", 0)
+ for i in range(1, line('$'))
+ let l:lineraw = getline(i)
+ let l:l1 = getline(i+1)
+ let l:line = substitute(l:lineraw, "#", "\\\#", "g")
+ if l:line =~ '````*' || l:line =~ '\~\~\~\~*'
+ if l:fenced_block == 0
+ let l:fenced_block = 1
+ elseif l:fenced_block == 1
+ let l:fenced_block = 0
+ endif
+ elseif l:vim_markdown_frontmatter == 1
+ if l:front_matter == 1
+ if l:line == '---'
+ let l:front_matter = 0
+ endif
+ elseif i == 1
+ if l:line == '---'
+ let l:front_matter = 1
+ endif
+ endif
+ endif
+ if l:line =~ '^#\+' || (l:l1 =~ '^=\+\s*$' || l:l1 =~ '^-\+\s*$') && l:line =~ '^\S'
+ let l:is_header = 1
+ else
+ let l:is_header = 0
+ endif
+ if l:is_header == 1 && l:fenced_block == 0 && l:front_matter == 0
+ " append line to location list
+ let l:item = {'lnum': i, 'text': l:line, 'valid': 1, 'bufnr': l:bufnr, 'col': 1}
+ let l:header_list = l:header_list + [l:item]
+ " set header number of the cursor position
+ if l:cursor_header == 0
+ if i == l:cursor_line
+ let l:cursor_header = len(l:header_list)
+ elseif i > l:cursor_line
+ let l:cursor_header = len(l:header_list) - 1
+ endif
+ endif
+ " keep track of the longest header size (heading level + title)
+ let l:total_len = stridx(l:line, ' ') + len(l:line)
+ if l:total_len > l:header_max_len
+ let l:header_max_len = l:total_len
+ endif
+ endif
+ endfor
+ if len(l:header_list) == 0