Skip to content

Commit

Permalink
add advanced support for tables
Browse files Browse the repository at this point in the history
  • Loading branch information
notslang committed Aug 13, 2014
1 parent bb07302 commit 985ab68
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,27 @@ module.exports = (dirtyMarkdown) ->
colWidth = longestStringInArray(col)
token.header[i] = pad(token.header[i], colWidth)

#TODO: add support for non-left align
token.align[i] = pad('', colWidth, '-')
alignment = token.align[i]
token.align[i] = (
switch alignment
when null then pad('', colWidth, '-')
when 'left' then ':' + pad('', colWidth - 1, '-')
when 'center' then ':' + pad('', colWidth - 2, '-') + ':'
when 'right' then pad('', colWidth - 1, '-') + ':'
)

for j in [0...token.cells.length]
token.cells[j][i] = pad(token.cells[j][i], colWidth)
token.cells[j][i] = (
if alignment is 'right'
pad(colWidth, token.cells[j][i])
else
pad(token.cells[j][i], colWidth)
)

out.push token.header.join(' | ')
out.push token.align.join(' | ')
for row in token.cells
out.push row.join(' | ').trim() # no trailing whitespace


out.push row.join(' | ').trimRight() # no trailing whitespace

previousToken = token

Expand Down
15 changes: 15 additions & 0 deletions test/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,21 @@ describe 'tables', ->
Vana'diel Chocobo Society | Chocobo Raising | FFXI:TOAU
''')

it 'should handle tables with text alignment', ->
prettyMarkdown('''
Group | Domain | First Appearance
------------------------: | :-------------: | :---------------
ShinRa | Mako Reactors | FFVII
Moogles | MogNet | FFIII
Vana'diel Chocobo Society | Chocobo Raising | FFXI:TOAU
''').should.equal('''
Group | Domain | First Appearance
------------------------: | :-------------: | :---------------
ShinRa | Mako Reactors | FFVII
Moogles | MogNet | FFIII
Vana'diel Chocobo Society | Chocobo Raising | FFXI:TOAU
''')

describe 'full documents', ->
it 'should reformat to match expected', ->
for file in fs.readdirSync('./test/cases')
Expand Down

0 comments on commit 985ab68

Please sign in to comment.