From f33d2fd730974d218673262a7ceba3d94a7892dc Mon Sep 17 00:00:00 2001 From: Ultroncn <82553854+Ultroncn@users.noreply.github.com> Date: Wed, 18 Nov 2026 06:34:08 +0000 Subject: [PATCH 1/4] fix: parse *** and ___ as nested bold+italic --- src/index.js | 9 ++++++++- test/index.js | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 3938c5c..4d6ad0d 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)|(?:^``` *(\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 = '', links = prevLinks || {}, @@ -32,6 +32,13 @@ export default function parse(md, prevLinks) { chunk, prev, token, inner, t; function tag(token) { + // `***` / `___` is a combined pair, not two separate toggles: + if (token === '***' || token === '___') { + let end = context[context.length-1] == token; + if (end) context.pop(); + else context.push(token); + return end ? '' : ''; + } let desc = TAGS[token[1] || '']; let end = context[context.length-1] == token; if (!desc) return token; diff --git a/test/index.js b/test/index.js index 5262b16..2775087 100644 --- a/test/index.js +++ b/test/index.js @@ -18,6 +18,14 @@ 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'); + }); }); describe('titles', () => { From e1c57f119be3d36addfab1ccc721c2680a4b5ef1 Mon Sep 17 00:00:00 2001 From: Ultroncn <82553854+Ultroncn@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:30:27 +0000 Subject: [PATCH 2/4] fix: keep triple emphasis from swallowing following text --- src/index.js | 41 ++++++++++++++++++++++++++++++++++++----- test/index.js | 8 ++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 4d6ad0d..5b564b0 100644 --- a/src/index.js +++ b/src/index.js @@ -32,12 +32,43 @@ export default function parse(md, prevLinks) { chunk, prev, token, inner, t; function tag(token) { - // `***` / `___` is a combined pair, not two separate toggles: + // `***` / `___` is a combined pair, not two separate toggles. + // A triple run also closes a single+double pair that is already open + // (`*italic **bold***` / `**bold *italic***`), and a single/double run + // closes just its half of an open triple pair when it cannot open a + // nested run (followed by whitespace or end of input). if (token === '***' || token === '___') { - let end = context[context.length-1] == token; - if (end) context.pop(); - else context.push(token); - return end ? '' : ''; + let top = context[context.length-1], second = context[context.length-2]; + if (top === token) { + context.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 ''; + } + context.push(token); + return ''; + } + // Single/double run against an open triple pair of the same marker: + let top = context[context.length-1]; + if ((top === '***' || top === '___') && token[0] === top[0]) { + let next = md[tokenizer.lastIndex]; + if (next === undefined || /\s/.test(next)) { + if (token.length === 1) { + context[context.length-1] = top[0] + top[0]; + return ''; + } + if (token.length === 2) { + context[context.length-1] = top[0]; + return ''; + } + } } let desc = TAGS[token[1] || '']; let end = context[context.length-1] == token; diff --git a/test/index.js b/test/index.js index 2775087..7621adb 100644 --- a/test/index.js +++ b/test/index.js @@ -26,6 +26,14 @@ describe('snarkdown()', () => { 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'); + }); }); describe('titles', () => { From 599d887b7b0fbd441fc73293abfc0bfb2d4f2278 Mon Sep 17 00:00:00 2001 From: Ultron <82553854+Ultroncn@users.noreply.github.com> Date: Thu, 19 Nov 2026 03:30:12 +0800 Subject: [PATCH 3/4] fix: preserve adjacent and partial emphasis delimiters --- src/index.js | 37 ++++++++++++++++++++----------------- test/index.js | 22 +++++++++++++++++++++- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/src/index.js b/src/index.js index 5b564b0..b66e465 100644 --- a/src/index.js +++ b/src/index.js @@ -26,21 +26,19 @@ function encodeAttr(str) { 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, context = [], - out = '', + tripleStarts = [], + out = [], links = prevLinks || {}, last = 0, chunk, prev, token, inner, t; function tag(token) { - // `***` / `___` is a combined pair, not two separate toggles. - // A triple run also closes a single+double pair that is already open - // (`*italic **bold***` / `**bold *italic***`), and a single/double run - // closes just its half of an open triple pair when it cannot open a - // nested run (followed by whitespace or end of input). + // 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; @@ -52,22 +50,28 @@ export default function parse(md, prevLinks) { 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 ''; } - // Single/double run against an open triple pair of the same marker: + // 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 next = md[tokenizer.lastIndex]; - if (next === undefined || /\s/.test(next)) { + 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 ''; } - if (token.length === 2) { - context[context.length-1] = top[0]; - return ''; - } + context[context.length-1] = top[0]; + return ''; } } let desc = TAGS[token[1] || '']; @@ -120,8 +124,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 = ''; @@ -139,9 +143,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 7621adb..ef2197e 100644 --- a/test/index.js +++ b/test/index.js @@ -32,7 +32,27 @@ describe('snarkdown()', () => { }); it('closes each half of *** with single and double runs', () => { - expect(snarkdown('***bold* italic** normal')).to.equal('bold italic normal'); + 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('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'); }); }); From b3906f1d72e2c4da251ea0134143813a986c4ce5 Mon Sep 17 00:00:00 2001 From: Ultron <82553854+Ultroncn@users.noreply.github.com> Date: Thu, 19 Nov 2026 03:57:58 +0800 Subject: [PATCH 4/4] fix: preserve double delimiters in longer emphasis runs --- src/index.js | 10 +++++++++- test/index.js | 11 +++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index b66e465..2674746 100644 --- a/src/index.js +++ b/src/index.js @@ -96,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 } diff --git a/test/index.js b/test/index.js index ef2197e..e9c66aa 100644 --- a/test/index.js +++ b/test/index.js @@ -42,6 +42,17 @@ describe('snarkdown()', () => { 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');