Skip to content

Commit

Permalink
feat: Add (optin) support for formatting tables without borders (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
gko authored Nov 1, 2023
1 parent 66ed2e1 commit 46add6c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,27 @@ The following options control which syntax extensions will be turned on. They ar

let g:vim_markdown_edit_url_in = 'tab'

### Borderless tables

- `g:vim_markdown_borderless_table`

Add support for borderless tables, such as:
```
header 1|header 2
--|--
data 1|data 2
```
if set to `1`:
let g:vim_markdown_borderless_table = 1
the table would be formatted as usual:
```
| header 1 | header 2 |
|----------|----------|
| data 1 | data 2 |
```
## Mappings
The following work on normal and visual modes:
Expand Down
24 changes: 24 additions & 0 deletions doc/vim-markdown.txt
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,30 @@ Change how to open new files ~
>
let g:vim_markdown_edit_url_in = 'tab'
<
-------------------------------------------------------------------------------
*vim-markdown-support-borderless-tables*
Support borderless tables ~

*g:vim_markdown_borderless_table*
- 'g:vim_markdown_borderless_table'

Add support for borderless tables, such as:
>
header 1|header 2
--|--
data 1|data 2
<
if set to 1:
>
let g:vim_markdown_borderless_table = 1
<
the table would be formatted as usual:
>
| header 1 | header 2 |
|----------|----------|
| data 1 | data 2 |
<

===============================================================================
*vim-markdown-mappings*
Mappings ~
Expand Down
15 changes: 14 additions & 1 deletion ftplugin/markdown.vim
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,19 @@ endfunction
"
function! s:TableFormat()
let l:pos = getpos('.')

if get(g:, 'vim_markdown_borderless_table', 0)
" add `|` to the beginning of the line if it isn't present
normal! {
call search('|')
execute 'silent .,''}s/\v^(\s{0,})\|?([^\|])/\1|\2/e'

" add `|` to the end of the line if it isn't present
normal! {
call search('|')
execute 'silent .,''}s/\v([^\|])\|?(\s{0,})$/\1|\2/e'
endif

normal! {
" Search instead of `normal! j` because of the table at beginning of file edge case.
call search('|')
Expand Down Expand Up @@ -765,7 +778,7 @@ endif
command! -buffer -range=% HeaderDecrease call s:HeaderDecrease(<line1>, <line2>)
command! -buffer -range=% HeaderIncrease call s:HeaderDecrease(<line1>, <line2>, 1)
command! -buffer -range=% SetexToAtx call s:SetexToAtx(<line1>, <line2>)
command! -buffer TableFormat call s:TableFormat()
command! -buffer -range TableFormat call s:TableFormat()
command! -buffer Toc call s:Toc()
command! -buffer Toch call s:Toc('horizontal')
command! -buffer Tocv call s:Toc('vertical')
Expand Down
15 changes: 15 additions & 0 deletions test/table-format.vader
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,18 @@ Expect (preserve colons to align text):
| left | right | center | |
|:-----|------:|:------:|:--|
| left | right | center | |

Given markdown (borderless table);
left |right| center
:- | --: |:---:
left |right| center

Execute (format borderless table):
let g:vim_markdown_borderless_table = 1
TableFormat
unlet g:vim_markdown_borderless_table

Expect (table with borders):
| left | right | center |
|:-----|------:|:------:|
| left | right | center |

0 comments on commit 46add6c

Please sign in to comment.