diff --git a/src/index.js b/src/index.js index 8e40bf8..3e260d3 100644 --- a/src/index.js +++ b/src/index.js @@ -23,7 +23,7 @@ function encodeAttr(str) { /** Parse Markdown into an HTML String. */ export default function parse(md, prevLinks) { - let tokenizer = /((?:^|\n+)(?:\n---+|\* \*(?: \*)+)\n)|(?:^``` *(\w*)\n([\s\S]*?)\n```$)|((?:(?:^|\n+)(?:\t| {2,}).+)+\n*)|((?:(?:^|\n)([>*+-]|\d+\.)\s+.*)+)|(?:\!\[([^\]]*?)\]\(([^\)]+?)\))|(\[)|(\](?:\(([^\)]+?)\))?)|(?:(?:^|\n+)([^\s].*)\n(\-{3,}|={3,})(?:\n+|$))|(?:(?:^|\n+)(#{1,6})\s*(.+)(?:\n+|$))|(?:`([^`].*?)`)|( \n\n*|\n{2,}|__|\*\*|[_*]|~~)/gm, + let tokenizer = /((?:^|\n+)(?:\n---+|\* \*(?: \*)+)\n)|(?:^``` *(\w*)\n([\s\S]*?)\n```$)|((?:(?:^|\n+)(?:\t| {2,}).+)+\n*)|((?:(?:^|\n)([>*+-]|\d+\.)\s+.*)+)|(?:\!\[([^\]]*?)\]\(([^\)]+?)\))|(\[)|(\](?:\(([^\)]+?)\))?)|(?:(?:^|\n+)([^\s].*)\n(\-{3,}|={3,})(?:\n+|$))|(?:(?:^|\n+)(#{1,3})\s*(.+)(?:\n+|$))|(?:`([^`].*?)`)|( \n\n*|\n{2,}|__|\*\*|[_*]|~~)|((?:(?:^|\n+)(?:\|.*))+)/gm, context = [], out = '', links = prevLinks || {}, @@ -100,6 +100,28 @@ export default function parse(md, prevLinks) { else if (token[17] || token[1]) { chunk = tag(token[17] || '--'); } + // Table parser + else if (token[18]) { + var l = token[18].split('\n'), + i = l.length, + table = '', + r = 'td>'; + while ( i-- ) { + if(l[i].match(/^\|\s+---+.*$/)) { + r = 'th>'; + continue; + } + var c = l[i].split(/\|\s*/), + j = c.length, + tr = ''; + while (j--) { + tr = (c[j] ? `<${r+parse(c[j])}${tr}` + table; + r = 'td>'; + } + chunk = `${table}
`; + } out += prev; out += chunk; } diff --git a/test/index.js b/test/index.js index 8e8aaac..f9b92c8 100644 --- a/test/index.js +++ b/test/index.js @@ -67,9 +67,9 @@ describe('snarkdown()', () => { expect(snarkdown('\nhello [World]!\n[world]: http://world.com')).to.equal('hello World!'); }); - it('parses reference links without creating excessive linebreaks', () => { - expect(snarkdown('\nhello [World]!\n\n[world]: http://world.com')).to.equal('hello World!'); - }); + it('parses reference links without creating excessive linebreaks', () => { + expect(snarkdown('\nhello [World]!\n\n[world]: http://world.com')).to.equal('hello World!'); + }); }); describe('lists', () => { @@ -167,4 +167,25 @@ describe('snarkdown()', () => { expect(snarkdown('`')).to.equal('`'); }); }); + + describe('tables', () => { + it('should parse content', () => { + expect(snarkdown('| a | hallo welt | c |')).to.equal('
ahallo weltc
'); + expect(snarkdown('| a | b |')).to.equal('
ab
'); + expect(snarkdown('| a | b \n| c | d')).to.equal('
ab
cd
'); + expect(snarkdown('| a | b \n| c | d \n| e | f')).to.equal('
ab
cd
ef
'); + expect(snarkdown('| a')).to.equal('
a
'); + }); + + it('should parse header', () => { + expect(snarkdown('| a | hallo welt | c |\n| ---')).to.equal('
ahallo weltc
'); + expect(snarkdown('| a | b \n| --- | --- \n| e | f')).to.equal('
ab
ef
'); + }); + + it('should allow inline styles', () => { + expect(snarkdown('| [Example](#example) | **strong** |')).to.equal('
Examplestrong
'); + expect(snarkdown('| a | # hallo welt | c |\n| ---')).to.equal('
a

hallo welt

c
'); + expect(snarkdown('| [some **bold text](#winning) | b \n| --- | --- \n| > To be or not to be | f')).to.equal('
some bold textb
To be or not to be
f
'); + }); + }); });