Skip to content

Commit 46add6c

Browse files
authored
feat: Add (optin) support for formatting tables without borders (#643)
1 parent 66ed2e1 commit 46add6c

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,27 @@ The following options control which syntax extensions will be turned on. They ar
349349

350350
let g:vim_markdown_edit_url_in = 'tab'
351351

352+
### Borderless tables
353+
354+
- `g:vim_markdown_borderless_table`
355+
356+
Add support for borderless tables, such as:
357+
```
358+
header 1|header 2
359+
--|--
360+
data 1|data 2
361+
```
362+
if set to `1`:
363+
364+
let g:vim_markdown_borderless_table = 1
365+
366+
the table would be formatted as usual:
367+
```
368+
| header 1 | header 2 |
369+
|----------|----------|
370+
| data 1 | data 2 |
371+
```
372+
352373
## Mappings
353374
354375
The following work on normal and visual modes:

doc/vim-markdown.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,30 @@ Change how to open new files ~
490490
>
491491
let g:vim_markdown_edit_url_in = 'tab'
492492
<
493+
-------------------------------------------------------------------------------
494+
*vim-markdown-support-borderless-tables*
495+
Support borderless tables ~
496+
497+
*g:vim_markdown_borderless_table*
498+
- 'g:vim_markdown_borderless_table'
499+
500+
Add support for borderless tables, such as:
501+
>
502+
header 1|header 2
503+
--|--
504+
data 1|data 2
505+
<
506+
if set to 1:
507+
>
508+
let g:vim_markdown_borderless_table = 1
509+
<
510+
the table would be formatted as usual:
511+
>
512+
| header 1 | header 2 |
513+
|----------|----------|
514+
| data 1 | data 2 |
515+
<
516+
493517
===============================================================================
494518
*vim-markdown-mappings*
495519
Mappings ~

ftplugin/markdown.vim

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,19 @@ endfunction
538538
"
539539
function! s:TableFormat()
540540
let l:pos = getpos('.')
541+
542+
if get(g:, 'vim_markdown_borderless_table', 0)
543+
" add `|` to the beginning of the line if it isn't present
544+
normal! {
545+
call search('|')
546+
execute 'silent .,''}s/\v^(\s{0,})\|?([^\|])/\1|\2/e'
547+
548+
" add `|` to the end of the line if it isn't present
549+
normal! {
550+
call search('|')
551+
execute 'silent .,''}s/\v([^\|])\|?(\s{0,})$/\1|\2/e'
552+
endif
553+
541554
normal! {
542555
" Search instead of `normal! j` because of the table at beginning of file edge case.
543556
call search('|')
@@ -765,7 +778,7 @@ endif
765778
command! -buffer -range=% HeaderDecrease call s:HeaderDecrease(<line1>, <line2>)
766779
command! -buffer -range=% HeaderIncrease call s:HeaderDecrease(<line1>, <line2>, 1)
767780
command! -buffer -range=% SetexToAtx call s:SetexToAtx(<line1>, <line2>)
768-
command! -buffer TableFormat call s:TableFormat()
781+
command! -buffer -range TableFormat call s:TableFormat()
769782
command! -buffer Toc call s:Toc()
770783
command! -buffer Toch call s:Toc('horizontal')
771784
command! -buffer Tocv call s:Toc('vertical')

test/table-format.vader

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,18 @@ Expect (preserve colons to align text):
6868
| left | right | center | |
6969
|:-----|------:|:------:|:--|
7070
| left | right | center | |
71+
72+
Given markdown (borderless table);
73+
left |right| center
74+
:- | --: |:---:
75+
left |right| center
76+
77+
Execute (format borderless table):
78+
let g:vim_markdown_borderless_table = 1
79+
TableFormat
80+
unlet g:vim_markdown_borderless_table
81+
82+
Expect (table with borders):
83+
| left | right | center |
84+
|:-----|------:|:------:|
85+
| left | right | center |

0 commit comments

Comments
 (0)