Improve markdown

This commit is contained in:
Knyffen 2021-03-31 16:48:25 +02:00
parent 3ac8878b0e
commit 13bfac82df

View File

@ -54,6 +54,9 @@
call plug#begin(g:plugged_home) call plug#begin(g:plugged_home)
" vim-plug bundles {{{ " vim-plug bundles {{{
" VimScript Library
Plug 'inkarkat/vim-ingo-library'
" UI related " UI related
Plug 'chriskempson/base16-vim' Plug 'chriskempson/base16-vim'
Plug 'itchyny/lightline.vim' Plug 'itchyny/lightline.vim'
@ -209,6 +212,9 @@ tnoremap <Esc> <C-\><C-n>
" autocmd BufWinEnter,InsertLeave,CursorMovedI * if index(HighlightWhitespacesBlacklist, &ft) < 0 | match ExtraWhitespace /\s\+\%#\@<!$\| \+\ze\t/ " autocmd BufWinEnter,InsertLeave,CursorMovedI * if index(HighlightWhitespacesBlacklist, &ft) < 0 | match ExtraWhitespace /\s\+\%#\@<!$\| \+\ze\t/
" autocmd FileType markdown autocmd BufWinEnter,InsertLeave,CursorMovedI * " autocmd FileType markdown autocmd BufWinEnter,InsertLeave,CursorMovedI *
augroup END augroup END
augroup DontItalicMarkdownLatexUnderscore
autocmd filetype markdown syntax match markdownIgnore "\$.*_.*\$" containedin=markdown
augroup END
" " Colorscheme " " Colorscheme
" let base16colorspace=256 " let base16colorspace=256
@ -452,7 +458,7 @@ endif
" Full config: when writing or reading a buffer, and on changes in insert and " Full config: when writing or reading a buffer, and on changes in insert and
" normal mode (after 1s; no delay when writing). " normal mode (after 1s; no delay when writing).
call neomake#configure#automake('nrwi', 500) call neomake#configure#automake('nrwi', 2000)
" Autoopen error list " Autoopen error list
let g:neomake_open_list = 2 " 0: show hints, 1: show list and move cursor to list, 2: show list, but don't move cursor let g:neomake_open_list = 2 " 0: show hints, 1: show list and move cursor to list, 2: show list, but don't move cursor
@ -719,7 +725,7 @@ augroup END
let g:instant_markdown_mathjax = 1 let g:instant_markdown_mathjax = 1
let g:instant_markdown_browser = "firefox --new-window" let g:instant_markdown_browser = "firefox --new-window"
"let g:instant_markdown_port = 8888 "let g:instant_markdown_port = 8888
"let g:instant_markdown_autoscroll = 0 let g:instant_markdown_autoscroll = 1
"let g:instant_markdown_python = 1 "let g:instant_markdown_python = 1
" }} " }}
@ -805,11 +811,11 @@ nnoremap * *zz
nnoremap # #zz nnoremap # #zz
nnoremap g* g*zz nnoremap g* g*zz
nnoremap g# g#zz nnoremap g# g#zz
function! ZZWrap(...) function! ZZWrap(...)
exec ':set so=999' exec ':set so=999'
exec join(a:000, ' ') exec join(a:000, ' ')
exec ':set so=0' exec ':set so=0'
endfunction endfunction
command! -nargs=* ZZ call ZZWrap(<f-args>) command! -nargs=* ZZ call ZZWrap(<f-args>)
nnoremap <Space> :nohl<CR> nnoremap <Space> :nohl<CR>
@ -836,3 +842,61 @@ function! InsertSnip(snip_name)
endfunction endfunction
command! -nargs=1 R call InsertSnip(<f-args>) command! -nargs=1 R call InsertSnip(<f-args>)
function! GenerateMarkdownHeaderAnchors()
let l:winview = winsaveview() " save cursor position
exec 'normal! gg'
let l:match = ingo#area#frompattern#Get(0, line('$'), '\v(^#+\s+)@<=.*')
for i in range(0, len(l:match)-1)
let l:line_nr = l:match[i][0][0]
let l:valid = 0 == len(ingo#area#frompattern#Get(l:line_nr, l:line_nr, '\v<a\s+.*name\=.*>'))
if l:valid
let l:line = getbufline(bufname(), l:line_nr)[0]
let l:title = join(split(l:line)[1:-1], '-')
let l:title = tolower(l:title)
let l:title = substitute(l:title, '\V%', '%25', 'g') " MUST be first!
let l:title = substitute(l:title, '\V\', '%5C', 'g')
let l:title = substitute(l:title, '\V^', '%5E', 'g')
let l:title = substitute(l:title, '\V&\|.\|/\|,\|=\|!\|@\|#\|(\|)\|*\|$\|<\|>\|"\|', '', 'g')
let l:title = substitute(l:title, "'", '', 'g')
let l:anchor = '<a name="' . l:title . '"></a>'
let l:next_line = l:line_nr+1
let l:failed = append(l:line_nr, l:anchor)
exec ':' . l:line_nr . ',' . l:next_line .'j'
" use append for appending to the line?
endif
endfor
call winrestview(l:winview) " restore cursor position
endfunction
function! RemoveMarkdownHeaderAnchors()
let l:winview = winsaveview() " save cursor position
exec 'normal! gg'
let l:match = ingo#area#frompattern#Get(0, line('$'), '\v(^#+\s+.*)@<=\s\<a\sname\=.*\>\</a\>')
for i in range(0, len(l:match)-1)
call winrestview({'lnum': l:match[i][0][0], 'col': l:match[i][0][1]-1})
let l:string_length = l:match[i][1][1] - l:match[i][0][1] + 1
exec "normal" '"_d' . l:string_length . "l"
endfor
call winrestview(l:winview) " restore cursor position
endfunction
function! GenerateMarkdownTOC()
" We need remove the anchors before we run doctoc. Otherwise it would
" include the anchors in the link, and the anchors would become incorrect
call RemoveMarkdownHeaderAnchors()
exec ":w"
silent exec "!doctoc " . bufname()
exec ":e"
call GenerateMarkdownHeaderAnchors()
endfunction
augroup MarkdownKeyBinds
autocmd!
autocmd FileType markdown nmap <Leader>lt :call GenerateMarkdownTOC()<CR>
augroup END