diff --git a/src/index.js b/src/index.js index 3938c5c..820ec70 100644 --- a/src/index.js +++ b/src/index.js @@ -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|^---+(?:\n|$))|(?:^``` *(\w*)\n([\s\S]*?)\n```$)|((?:(?:^|\n+)(?:\t| {2,}).+)+\n*)|((?:(?:^|\n)([>*+-]|\d+\.)\s+.*)+)|(?:!\[([^\]]*?)\]\(([^)]+?)\))|(\[)|(\](?:\(([^)]+?)\))?)|(?:(?:^|\n+)([^\s].*)\n(-{3,}|={3,})(?:\n+|$))|(?:(?:^|\n+)(#{1,6})[ \t]+(.+)(?:\n+|$))|(?:`([^`].*?)`)|( \n\n*|\n{2,}|__|\*\*|[_*]|~~)/gm, context = [], out = '', links = prevLinks || {}, diff --git a/test/index.js b/test/index.js index 5262b16..a3974df 100644 --- a/test/index.js +++ b/test/index.js @@ -37,6 +37,16 @@ describe('snarkdown()', () => { expect(snarkdown('### I like tiny libraries')).to.equal('

I like tiny libraries

'); }); + it('requires a space after the hashes', () => { + // #64: "#sth" is plain text (e.g. a hashtag), not a heading. + expect(snarkdown('#sth')).to.equal('#sth'); + expect(snarkdown('##sth')).to.equal('##sth'); + expect(snarkdown('####### seven')).to.equal('####### seven'); + expect(snarkdown('#\nfoo')).to.equal('#\nfoo'); + // a tab counts as the required whitespace, too: + expect(snarkdown('#\ttitle')).to.equal('

title

'); + }); + it('parses titles with reference links', () => { expect( snarkdown('# I like [tiny libraries]\n\n[tiny libraries]: https://github.com/developit/snarkdown') @@ -144,6 +154,22 @@ describe('snarkdown()', () => { expect(snarkdown('> foo\n\n---\nbar')).to.equal('
foo

bar'); }); + it('should parse --- after a heading', () => { + // #112: the heading swallows the blank line that follows it, + // which used to leave the rule without the blank line it needs. + expect(snarkdown('### Title\n\n---\n\ntext')).to.equal('

Title


\ntext'); + expect(snarkdown('---\n\nhello'), 'at the start of a document').to.equal('
\nhello'); + expect(snarkdown('foo\n\n---'), 'at the end of a document').to.equal('foo

'); + }); + + it('should parse --- after an indented code block', () => { + // #103: same cause as above, with an indented code block + // swallowing the blank line before the rule. + expect( + snarkdown('Text into\n\n---\n\nBut, not this\n\n Sample Code Block\n\n---\n\ntest') + ).to.equal('Text into
\nBut, not this
Sample Code Block

\ntest'); + }); + it('should parse * * *', () => { expect(snarkdown('foo\n* * *\nbar')).to.equal('foo
bar'); expect(snarkdown('foo\n* * * *\nbar'), '* * * *').to.equal('foo
bar');