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
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ It's designed to be as minimal as possible, for constrained use-cases where a fu
- **Tiny:** it's 1kb of gzipped ES3
- **Simple:** pass a Markdown string, get back an HTML string

> **Note:** Tables are not yet supported. If you love impossible to read regular expressions, submit a PR!
>
> **Note on XSS:** Snarkdown [doesn't sanitize HTML](https://github.com/developit/snarkdown/issues/70), since its primary target usage doesn't require it.
## Demos & Examples
Expand Down
11 changes: 10 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,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,6})\s*(.+)(?:\n+|$))|(?:`([^`].*?)`)|( \n\n*|\n{2,}|__|\*\*|[_*]|~~)|((?:(?:^|\n)\|.+)+)/gm,
context = [],
out = '',
links = prevLinks || {},
Expand Down Expand Up @@ -101,6 +101,15 @@ export default function parse(md, prevLinks) {
else if (token[17] || token[1]) {
chunk = tag(token[17] || '--');
}
// Tables:
else if (token[18]) {
t = token[18].trim().split('\n').map(row => row.replace(/^\s*\|\s*|\s*\|\s*$/g, '').split(/\s*\|\s*/));
inner = t[1] && t[1].every(cell => /^:?-+:?$/.test(cell)) ? t.splice(1, 1) && 'th' : 'td';
chunk = '<table>' + t.map((row, i) => {
let cell = i ? 'td' : inner;
return '<tr>' + row.map(str => '<'+cell+'>' + parse(str, links) + '</'+cell+'>').join('') + '</tr>';
}).join('') + '</table>';
}
out += prev;
out += chunk;
}
Expand Down
18 changes: 18 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,24 @@ describe('snarkdown()', () => {
});
});

describe('tables', () => {
it('parses a table with a header', () => {
expect(snarkdown('| a | b |\n| --- | --- |\n| 1 | 2 |')).to.equal('<table><tr><th>a</th><th>b</th></tr><tr><td>1</td><td>2</td></tr></table>');
});

it('parses a table without a header', () => {
expect(snarkdown('| 1 | 2 |\n| 3 | 4 |')).to.equal('<table><tr><td>1</td><td>2</td></tr><tr><td>3</td><td>4</td></tr></table>');
});

it('parses inline formatting within cells', () => {
expect(snarkdown('| **a** | [b](#b) |\n| --- | --- |\n| `c` | d |')).to.equal('<table><tr><th><strong>a</strong></th><th><a href="#b">b</a></th></tr><tr><td><code>c</code></td><td>d</td></tr></table>');
});

it('parses a table surrounded by text', () => {
expect(snarkdown('before\n\n| a |\n| --- |\n| 1 |\n\nafter')).to.equal('before<br /><table><tr><th>a</th></tr><tr><td>1</td></tr></table><br />after');
});
});

describe('edge cases', () => {
it('should close unclosed tags', () => {
expect(snarkdown('*foo')).to.equal('<em>foo</em>');
Expand Down