Skip to content

Commit

Permalink
add support for table
Browse files Browse the repository at this point in the history
  • Loading branch information
kid-icarus committed Apr 8, 2023
1 parent 22b22ea commit 3fe4468
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 3 deletions.
46 changes: 43 additions & 3 deletions lua/jira/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ local top_level_block_nodes = Set {
local child_block_nodes = Set {
'listItem',
'media',
'table_cell',
'table_header',
'table_row',
'tableHeader',
'tableRow',
'tableCell',
}

local inline_nodes = Set {
Expand Down Expand Up @@ -187,6 +187,46 @@ local function convert_adf_to_markdown(adt)
return '📷'
end
end,
table = function(node)
node_md = ''
for _, v in ipairs(node.content) do
node_md = node_md .. convert_adf_node_to_markdown(v)
end
return node_md
end,
tableRow = function(node)
node_md = '|'
local is_header = false
for _, v in ipairs(node.content) do
if v.type == 'tableHeader' then
is_header = true
end
node_md = node_md .. convert_adf_node_to_markdown(v)
end
if is_header then
node_md = node_md .. '\n|'
for _ in ipairs(node.content) do
node_md = node_md .. ' --- |'
end
end
return node_md .. '\n'
end,
tableHeader = function(node)
node_md = ' '
for _, v in ipairs(node.content) do
-- crude way to get rid of the newlines in <p> tags
node_md = node_md .. vim.trim(convert_adf_node_to_markdown(v))
end
return node_md .. ' |'
end,
tableCell = function(node)
node_md = ' '
for _, v in ipairs(node.content) do
-- crude way to get rid of the newlines in <p> tags
node_md = node_md .. vim.trim(convert_adf_node_to_markdown(v))
end
return node_md .. ' |'
end,
}

local inline_nodes_to_markdown = {
Expand Down
83 changes: 83 additions & 0 deletions tests/utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -725,4 +725,87 @@ describe('convert_adf_to_markdown', function()
local actual = utils.convert_adf_to_markdown(code_block)
assert.are.same(expected, actual)
end)

it('should convert a table with a header', function()
local code_block = {
version = 1,
type = 'doc',
content = {
{
type = 'table',
content = {
{
type = 'tableRow',
content = {
{
type = 'tableHeader',
content = {
{
type = 'paragraph',
content = {
{
type = 'text',
text = 'Header 1',
},
},
},
},
},
{
type = 'tableHeader',
content = {
{
type = 'paragraph',
content = {
{
type = 'text',
text = 'Header 2',
},
},
},
},
},
},
},
{
type = 'tableRow',
content = {
{
type = 'tableCell',
content = {
{
type = 'paragraph',
content = {
{
type = 'text',
text = 'Cell 1',
},
},
},
},
},
{
type = 'tableCell',
content = {
{
type = 'paragraph',
content = {
{
type = 'text',
text = 'Cell 2',
},
},
},
},
},
},
},
},
},
},
}
local expected = '| Header 1 | Header 2 |\n| --- | --- |\n| Cell 1 | Cell 2 |\n'
local actual = utils.convert_adf_to_markdown(code_block)
assert.are.same(expected, actual)
end)
end)

0 comments on commit 3fe4468

Please sign in to comment.