Skip to content
Open
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
24 changes: 23 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {},
Expand Down Expand Up @@ -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-- ) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might want to remove the spaces inside the parentheses for consistency with the rest of the file

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and to save some bytes ofc ;)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed them for code-styles sake
@tbjgolden since it's minified spaces don't add to final size

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])}</${r}` : '') + tr;
}
table = `<tr>${tr}</tr>` + table;
r = 'td>';
}
chunk = `<table>${table}</table>`;
}
out += prev;
out += chunk;
}
Expand Down
27 changes: 24 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ describe('snarkdown()', () => {
expect(snarkdown('\nhello [World]!\n[world]: http://world.com')).to.equal('hello <a href="http://world.com">World</a>!');
});

it('parses reference links without creating excessive linebreaks', () => {
expect(snarkdown('\nhello [World]!\n\n[world]: http://world.com')).to.equal('hello <a href="http://world.com">World</a>!');
});
it('parses reference links without creating excessive linebreaks', () => {
expect(snarkdown('\nhello [World]!\n\n[world]: http://world.com')).to.equal('hello <a href="http://world.com">World</a>!');
});
});

describe('lists', () => {
Expand Down Expand Up @@ -167,4 +167,25 @@ describe('snarkdown()', () => {
expect(snarkdown('`')).to.equal('`');
});
});

describe('tables', () => {
it('should parse content', () => {
expect(snarkdown('| a | hallo welt | c |')).to.equal('<table><tr><td>a</td><td>hallo welt</td><td>c</td></tr></table>');
expect(snarkdown('| a | b |')).to.equal('<table><tr><td>a</td><td>b</td></tr></table>');
expect(snarkdown('| a | b \n| c | d')).to.equal('<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>');
expect(snarkdown('| a | b \n| c | d \n| e | f')).to.equal('<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr><tr><td>e</td><td>f</td></tr></table>');
expect(snarkdown('| a')).to.equal('<table><tr><td>a</td></tr></table>');
});

it('should parse header', () => {
expect(snarkdown('| a | hallo welt | c |\n| ---')).to.equal('<table><tr><th>a</th><th>hallo welt</th><th>c</th></tr></table>');
expect(snarkdown('| a | b \n| --- | --- \n| e | f')).to.equal('<table><tr><th>a</th><th>b</th></tr><tr><td>e</td><td>f</td></tr></table>');
});

it('should allow inline styles', () => {
expect(snarkdown('| [Example](#example) | **strong** |')).to.equal('<table><tr><td><a href="#example">Example</a></td><td><strong>strong</strong></td></tr></table>');
expect(snarkdown('| a | # hallo welt | c |\n| ---')).to.equal('<table><tr><th>a</th><th><h1>hallo welt</h1></th><th>c</th></tr></table>');
expect(snarkdown('| [some **bold text](#winning) | b \n| --- | --- \n| > To be or not to be | f')).to.equal('<table><tr><th><a href="#winning">some <strong>bold text</strong></a></th><th>b</th></tr><tr><td><blockquote>To be or not to be</blockquote></td><td>f</td></tr></table>');
});
});
});