Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for tables without borders #643

Merged
merged 7 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
Loading