Skip to content

Commit

Permalink
fix #544 use sed for improving highting performance
Browse files Browse the repository at this point in the history
  • Loading branch information
lotabout committed Sep 9, 2021
1 parent 8e5d86f commit 71f0b6b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
13 changes: 13 additions & 0 deletions doc/vim-markdown.txt
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,19 @@ Fenced code block languages ~
<
Default is "['c++=cpp', 'viml=vim', 'bash=sh', 'ini=dosini']".

*g:vim_markdown_highlight_with_sed*
- 'g:vim_markdown_highlight_with_sed'

Vim-markdown needs to search all fenced code blocks for the filetypes to
highlight. The default code uses vim's builtin regex engine which is slow
dealing with large markdown documents. Enabling this option would use
command line utility 'sed' for searching. It's expected to improve performance
around 10x.
>
let g:vim_markdown_highlight_with_sed = 1
<
Default is '0'.

-------------------------------------------------------------------------------
*vim-markdown-follow-named-anchors*
Follow named anchors ~
Expand Down
17 changes: 13 additions & 4 deletions ftplugin/markdown.vim
Original file line number Diff line number Diff line change
Expand Up @@ -787,10 +787,19 @@ function! s:MarkdownHighlightSources(force)
" Syntax highlight source code embedded in notes.
" Look for code blocks in the current file
let filetypes = {}
for line in getline(1, '$')
let ft = matchstr(line, '```\s*\zs[0-9A-Za-z_+-]*\ze.*')
if !empty(ft) && ft !~ '^\d*$' | let filetypes[ft] = 1 | endif
endfor

if get(g:, 'vim_markdown_highlight_with_sed', 0)
let l:ft_lines = systemlist('sed -n ' . "'" . '/^[[:space:]]*```[[:space:]]*\([0-9A-Za-z_+-]*\).*$/s//\1/p' . "'", bufnr())
for ft in l:ft_lines
if !empty(ft) && ft !~ '^\d*$' | let filetypes[ft] = 1 | endif
endfor
else
for line in getline(1, '$')
let ft = matchstr(line, '```\s*\zs[0-9A-Za-z_+-]*\ze.*')
if !empty(ft) && ft !~ '^\d*$' | let filetypes[ft] = 1 | endif
endfor
endif

if !exists('b:mkd_known_filetypes')
let b:mkd_known_filetypes = {}
endif
Expand Down

0 comments on commit 71f0b6b

Please sign in to comment.