diff --git a/src/index.js b/src/index.js index 3938c5c..2674746 100644 --- a/src/index.js +++ b/src/index.js @@ -24,14 +24,56 @@ 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,}|___|\*\*\*|__|\*\*|[_*]|~~)/gm, context = [], - out = '', + tripleStarts = [], + out = [], links = prevLinks || {}, last = 0, chunk, prev, token, inner, t; function tag(token) { + // A triple run can close open emphasis or start a combined pair. + if (token === '***' || token === '___') { + let top = context[context.length-1], second = context[context.length-2]; + if (top === token) { + context.pop(); + tripleStarts.pop(); + return ''; + } + let single = token[0], dbl = single + single; + if (top === dbl && second === single) { + context.pop(); context.pop(); + return ''; + } + if (top === single && second === dbl) { + context.pop(); context.pop(); + return ''; + } + // Between spans, close the open marker and open the remainder. + if ((top === dbl || top === single) && /\S/.test(md.charAt(last-4)) && /\S/.test(md.charAt(last))) { + context[context.length-1] = top === dbl ? single : dbl; + return top === dbl ? '' : ''; + } + context.push(token); + tripleStarts.push(out.length + 1); // The opening chunk follows prev. + return ''; + } + // Choose the opening order that leaves the closing tags properly nested. + let top = context[context.length-1]; + if ((top === '***' || top === '___') && token[0] === top[0]) { + let before = md.charAt(last-token.length-1), next = md.charAt(last); + if (!next || /\s/.test(next) || (/\S/.test(before) && /[!-/:-@[-`{-~]/.test(next))) { + let start = tripleStarts.pop(); + if (token.length === 1) { + out[start] = ''; + context[context.length-1] = top[0] + top[0]; + return ''; + } + context[context.length-1] = top[0]; + return ''; + } + } let desc = TAGS[token[1] || '']; let end = context[context.length-1] == token; if (!desc) return token; @@ -54,8 +96,16 @@ export default function parse(md, prevLinks) { while ( (token=tokenizer.exec(md)) ) { prev = md.substring(last, token.index); - last = tokenizer.lastIndex; chunk = token[0]; + if (token[17] === '***' || token[17] === '___') { + let marker = chunk[0]; + if (md.charAt(token.index-1) === marker || md.charAt(tokenizer.lastIndex) === marker) { + // Preserve the original double delimiters in runs longer than three. + chunk = token[17] = chunk.slice(0, 2); + tokenizer.lastIndex--; + } + } + last = tokenizer.lastIndex; if (prev.match(/[^\\](\\\\)*\\$/)) { // escaped } @@ -82,8 +132,8 @@ export default function parse(md, prevLinks) { } // Links: else if (token[10]) { - out = out.replace('', ``); chunk = flush() + ''; + out = [out.join('').replace('', ``)]; } else if (token[9]) { chunk = ''; @@ -101,9 +151,8 @@ export default function parse(md, prevLinks) { else if (token[17] || token[1]) { chunk = tag(token[17] || '--'); } - out += prev; - out += chunk; + out.push(prev, chunk); } - return (out + md.substring(last) + flush()).replace(/^\n+|\n+$/g, ''); + return (out.join('') + md.substring(last) + flush()).replace(/^\n+|\n+$/g, ''); } diff --git a/test/index.js b/test/index.js index 5262b16..e9c66aa 100644 --- a/test/index.js +++ b/test/index.js @@ -18,6 +18,53 @@ describe('snarkdown()', () => { it('parses italics with _', () => { expect(snarkdown('I _like_ tiny libraries')).to.equal('I like tiny libraries'); }); + + it('parses nested bold+italic with ***', () => { + expect(snarkdown('I ***like*** tiny libraries')).to.equal('I like tiny libraries'); + }); + + it('parses nested bold+italic with ___', () => { + expect(snarkdown('I ___like___ tiny libraries')).to.equal('I like tiny libraries'); + }); + + it('closes an open single+double pair with ***', () => { + expect(snarkdown('*italic **bold*** normal')).to.equal('italic bold normal'); + }); + + it('closes each half of *** with single and double runs', () => { + expect(snarkdown('***bold* italic** normal')).to.equal('bold italic normal'); + expect(snarkdown('***italic** bold* normal')).to.equal('italic bold normal'); + }); + + it('splits a triple run between adjacent emphasis spans', () => { + expect(snarkdown('**bold***italic* normal')).to.equal('bolditalic normal'); + expect(snarkdown('*italic***bold** normal')).to.equal('italicbold normal'); + expect(snarkdown('__bold___italic_ normal')).to.equal('bolditalic normal'); + }); + + it('preserves adjacent bold spans separated by four delimiters', () => { + expect(snarkdown('**first****second** plain')).to.equal('firstsecond plain'); + expect(snarkdown('__first____second__ plain')).to.equal('firstsecond plain'); + }); + + it('does not treat the end of a longer run as a triple delimiter', () => { + const expected = 'firstsecond end plain'; + expect(snarkdown('**first*****second* end** plain')).to.equal(expected); + expect(snarkdown('__first_____second_ end__ plain')).to.equal(expected); + }); + + it('closes partial triple runs before punctuation', () => { + expect(snarkdown('***bold*, italic** normal')).to.equal('bold, italic normal'); + expect(snarkdown('___bold_, italic__ normal')).to.equal('bold, italic normal'); + }); + + it('keeps a triple run after whitespace nested', () => { + expect(snarkdown('**bold ***nested*** bold** normal')).to.equal('bold nested bold normal'); + }); + + it('opens emphasis before punctuation after whitespace', () => { + expect(snarkdown('***outer *!inner* outer*** normal')).to.equal('outer !inner outer normal'); + }); }); describe('titles', () => {