Repository navigation
fix: parse *** and ___ as nested bold+italic #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f33d2fd
e1c57f1
599d887
b3906f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 '</strong></em>'; | ||
| } | ||
| let single = token[0], dbl = single + single; | ||
| if (top === dbl && second === single) { | ||
| context.pop(); context.pop(); | ||
| return '</strong></em>'; | ||
| } | ||
| if (top === single && second === dbl) { | ||
| context.pop(); context.pop(); | ||
| return '</em></strong>'; | ||
| } | ||
| // 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 ? '</strong><em>' : '</em><strong>'; | ||
| } | ||
| context.push(token); | ||
| tripleStarts.push(out.length + 1); // The opening chunk follows prev. | ||
| return '<em><strong>'; | ||
|
Comment on lines
+58
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For adjacent formatting such as Useful? React with 👍 / 👎. |
||
| } | ||
| // 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))) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For valid intraword asterisk emphasis such as Useful? React with 👍 / 👎. |
||
| let start = tripleStarts.pop(); | ||
| if (token.length === 1) { | ||
| out[start] = '<strong><em>'; | ||
| context[context.length-1] = top[0] + top[0]; | ||
| return '</em>'; | ||
| } | ||
| context[context.length-1] = top[0]; | ||
| return '</strong>'; | ||
| } | ||
| } | ||
| 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('<a>', `<a href="${encodeAttr(token[11] || links[prev.toLowerCase()])}">`); | ||
| chunk = flush() + '</a>'; | ||
| out = [out.join('').replace('<a>', `<a href="${encodeAttr(token[11] || links[prev.toLowerCase()])}">`)]; | ||
| } | ||
| else if (token[9]) { | ||
| chunk = '<a>'; | ||
|
|
@@ -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, ''); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For previously supported input such as
**first****second** plain, the new alternative consumes the four-character boundary as***+*rather than**+**. The output changes from<strong>first</strong><strong>second</strong> plainto<strong>first</strong><em></em>second<strong> plain</strong>:secondloses bold and the unformatted suffix becomes bold. The underscore equivalent regresses too. Account for longer delimiter runs before treating a three-character prefix as a combined emphasis token.Useful? React with 👍 / 👎.