Skip to content

Commit

Permalink
Breaking Change: more configurable table behavior
Browse files Browse the repository at this point in the history
# Details

* fat_tables option removed
* table_style option added
* If fat_tables option was not being set default behavior
  remains the same
* If fat_tables option was set to false setting table_style
  option to 'normal' will result in the same behavior
* Setting table_style to 'none' is new behavior and will stop
  rendering markdown tables: MeanderingProgrammer#21
  • Loading branch information
MeanderingProgrammer committed May 21, 2024
1 parent 3811dbf commit 49f4597
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 28 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,11 @@ require('render-markdown').setup({
-- conceallevel used for buffer when being rendered
rendered = 3,
},
-- Add a line above and below tables to complete look, ends up like a window
fat_tables = true,
-- Determines how tables are rendered
-- full: adds a line above and below tables + normal behavior
-- normal: renders the rows of tables
-- none: disables rendering, use this if you prefer having cell highlights
table_style = 'full',
-- Define the highlight groups to use when rendering various components
highlights = {
heading = {
Expand Down
7 changes: 5 additions & 2 deletions doc/render-markdown.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,11 @@ modified by the user.
-- conceallevel used for buffer when being rendered
rendered = 3,
},
-- Add a line above and below tables to complete look, ends up like a window
fat_tables = true,
-- Determines how tables are rendered
-- full: adds a line above and below tables + normal behavior
-- normal: renders the rows of tables
-- none: disables rendering, use this if you prefer having cell highlights
table_style = 'full',
-- Define the highlight groups to use when rendering various components
highlights = {
heading = {
Expand Down
44 changes: 23 additions & 21 deletions lua/render-markdown/handler/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ M.render = function(namespace, root, buf)
})
end
elseif capture == 'table' then
if state.config.fat_tables then
if state.config.table_style == 'full' then
local lines = vim.api.nvim_buf_get_lines(buf, start_row, end_row, false)
local table_head = list.first(lines)
local table_tail = list.last(lines)
Expand All @@ -123,28 +123,30 @@ M.render = function(namespace, root, buf)
end
end
elseif vim.tbl_contains({ 'table_head', 'table_delim', 'table_row' }, capture) then
local row = value:gsub('|', '')
if capture == 'table_delim' then
-- Order matters here, in particular handling inner intersections before left & right
row = row:gsub('-', '')
:gsub(' ', '')
:gsub('─│─', '─┼─')
:gsub('│─', '├─')
:gsub('─│', '─┤')
end
if vim.tbl_contains({ 'full', 'normal' }, state.config.table_style) then
local row = value:gsub('|', '')
if capture == 'table_delim' then
-- Order matters here, in particular handling inner intersections before left & right
row = row:gsub('-', '')
:gsub(' ', '')
:gsub('─│─', '─┼─')
:gsub('│─', '├─')
:gsub('─│', '─┤')
end

local highlight = highlights.table.head
if capture == 'table_row' then
highlight = highlights.table.row
end
local highlight = highlights.table.head
if capture == 'table_row' then
highlight = highlights.table.row
end

local virt_text = { row, highlight }
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
end_row = end_row,
end_col = end_col,
virt_text = { virt_text },
virt_text_pos = 'overlay',
})
local virt_text = { row, highlight }
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
end_row = end_row,
end_col = end_col,
virt_text = { virt_text },
virt_text_pos = 'overlay',
})
end
else
-- Should only get here if user provides custom capture, currently unhandled
logger.error('Unhandled markdown capture: ' .. capture)
Expand Down
4 changes: 2 additions & 2 deletions lua/render-markdown/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ local M = {}
---@field public checkbox? render.md.UserCheckbox
---@field public quote? string
---@field public conceal? render.md.UserConceal
---@field public fat_tables? boolean
---@field public table_style? 'full'|'normal'|'none'
---@field public highlights? render.md.UserHighlights

---@param opts? render.md.UserConfig
Expand Down Expand Up @@ -106,7 +106,7 @@ function M.setup(opts)
default = vim.opt.conceallevel:get(),
rendered = 3,
},
fat_tables = true,
table_style = 'full',
highlights = {
heading = {
backgrounds = { 'DiffAdd', 'DiffChange', 'DiffDelete' },
Expand Down
2 changes: 1 addition & 1 deletion lua/render-markdown/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
---@field public checkbox render.md.Checkbox
---@field public quote string
---@field public conceal render.md.Conceal
---@field public fat_tables boolean
---@field public table_style 'full'|'normal'|'none'
---@field public highlights render.md.Highlights

---@class render.md.State
Expand Down

0 comments on commit 49f4597

Please sign in to comment.