-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix: pedantic **foo:** and **"word"** emphasis parsing #3999
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
Open
waiter
wants to merge
1
commit into
markedjs:master
Choose a base branch
from
waiter:fix_strong
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+89
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -285,6 +285,13 @@ const _notPunctuationOrSpace = /[^\s\p{P}\p{S}]/u; | |
| const punctuation = edit(/^((?![*_])punctSpace)/, 'u') | ||
| .replace(/punctSpace/g, _punctuationOrSpace).getRegex(); | ||
|
|
||
| /** Quote chars for pedantic rule 6 lookbehind; includes both straight and curly forms. */ | ||
| const closeQuoteClass = '["\u201c\u201d\u2018\u2019\']'; | ||
| /** Quote chars excluded from pedantic left-delim captured punct so `**"` / `**'` yields an empty nextChar. */ | ||
| const openQuoteClass = '["\'\u2018\u2019\u201c]'; | ||
| /** When lookbehind is supported, pedantic rule 6 does not match immediately after a quote (e.g. `"**` in `**"A"**`). */ | ||
| const closeQuoteLookbehind = supportsLookbehind ? `(?<!${closeQuoteClass})` : ''; | ||
|
|
||
| // GFM allows ~ inside strong and em for strikethrough | ||
| const _punctuationGfmStrongEm = /(?!~)[\p{P}\p{S}]/u; | ||
| const _punctuationOrSpaceGfmStrongEm = /(?!~)[\s\p{P}\p{S}]/u; | ||
|
|
@@ -308,6 +315,14 @@ const emStrongLDelimGfm = edit(emStrongLDelimCore, 'u') | |
| .replace(/punct/g, _punctuationGfmStrongEm) | ||
| .getRegex(); | ||
|
|
||
| // Pedantic: do not treat a quote as left-delim punct (pairs with pedantic emStrongRDelim rule 6). | ||
| const emStrongLDelimPedanticCore = /^(?:\*+(?:((?!\*)(?!openQuote)punct)|([^\s*]))?)|^_+(?:((?!_)(?!openQuote)punct)|([^\s_]))?/; | ||
|
|
||
| const emStrongLDelimPedantic = edit(emStrongLDelimPedanticCore, 'u') | ||
| .replace(/openQuote/g, openQuoteClass) | ||
| .replace(/punct/g, _punctuation) | ||
| .getRegex(); | ||
|
|
||
| const emStrongRDelimAstCore = | ||
| '^[^_*]*?__[^_*]*?\\*[^_*]*?(?=__)' // Skip orphan inside strong | ||
| + '|[^*]+(?=[^*])' // Consume to delim | ||
|
|
@@ -330,7 +345,25 @@ const emStrongRDelimAstGfm = edit(emStrongRDelimAstCore, 'gu') | |
| .replace(/punct/g, _punctuationGfmStrongEm) | ||
| .getRegex(); | ||
|
|
||
| // (6) Not allowed for _ | ||
| // Pedantic: rule 3 requires whitespace before left delim; rule 6 allows punct before close (e.g. `:**bar`). | ||
| const emStrongRDelimAstPedanticCore = | ||
| '^[^_*]*?__[^_*]*?\\*[^_*]*?(?=__)' // Skip orphan inside strong | ||
| + '|[^*]+(?=[^*])' // Consume to delim | ||
| + '|(?!\\*)punct(\\*+)(?=[\\s]|$)' // (1) #*** can only be a Right Delimiter | ||
| + '|notPunctSpace(\\*+)(?!\\*)(?=punctSpace|$)' // (2) a***#, a*** can only be a Right Delimiter | ||
| + '|(?!\\*)[\\s](\\*+)(?=notPunctSpace)' // (3) ***a can only be Left Delimiter (whitespace required; not #***a) | ||
| + '|[\\s](\\*+)(?!\\*)(?=punct)' // (4) ***# can only be Left Delimiter | ||
| + '|(?!\\*)punct(\\*+)(?!\\*)(?=punct)' // (5) #***# can be either Left or Right Delimiter | ||
| + '|closeQuoteLookbehind(?:(?!\\*)punct|notPunctSpace)(\\*+)(?!\\*)(?=notPunctSpace)'; // (6) a***a, #***a, and :** (e.g. **foo:**bar) | ||
|
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. This changes pedantic parsing for some "aa-style edge cases. The new behavior may well be preferable, but since it's a silent regression, I'd add a regression test to explicitly pin the intended behavior |
||
|
|
||
| const emStrongRDelimAstPedantic = edit(emStrongRDelimAstPedanticCore, 'gu') | ||
| .replace(/closeQuoteLookbehind/g, closeQuoteLookbehind) | ||
| .replace(/notPunctSpace/g, _notPunctuationOrSpace) | ||
| .replace(/punctSpace/g, _punctuationOrSpace) | ||
| .replace(/punct/g, _punctuation) | ||
| .getRegex(); | ||
|
|
||
| // Normal: no rule 6 for _ (a___a cannot close as emphasis). | ||
| const emStrongRDelimUnd = edit( | ||
| '^[^_*]*?\\*\\*[^_*]*?_[^_*]*?(?=\\*\\*)' // Skip orphan inside strong | ||
| + '|[^_]+(?=[^_])' // Consume to delim | ||
|
|
@@ -344,6 +377,24 @@ const emStrongRDelimUnd = edit( | |
| .replace(/punct/g, _punctuation) | ||
| .getRegex(); | ||
|
|
||
| // Pedantic: same rule 3/6 adjustments as emStrongRDelimAstPedantic (e.g. `_foo:_bar`). | ||
| const emStrongRDelimUndPedanticCore = | ||
| '^[^_*]*?\\*\\*[^_*]*?_[^_*]*?(?=\\*\\*)' // Skip orphan inside strong | ||
| + '|[^_]+(?=[^_])' // Consume to delim | ||
| + '|(?!_)punct(_+)(?=[\\s]|$)' // (1) #___ can only be a Right Delimiter | ||
| + '|notPunctSpace(_+)(?!_)(?=punctSpace|$)' // (2) a___#, a___ can only be a Right Delimiter | ||
| + '|(?!_)[\\s](_+)(?=notPunctSpace)' // (3) ___a can only be Left Delimiter (whitespace required; not #___a) | ||
| + '|[\\s](_+)(?!_)(?=punct)' // (4) ___# can only be Left Delimiter | ||
| + '|(?!_)punct(_+)(?!_)(?=punct)' // (5) #___# can be either Left or Right Delimiter | ||
| + '|closeQuoteLookbehind(?:(?!_)punct|notPunctSpace)(_+)(?!_)(?=notPunctSpace)'; // (6) a___a, #___a, and :__ (e.g. _foo:_bar) | ||
|
|
||
| const emStrongRDelimUndPedantic = edit(emStrongRDelimUndPedanticCore, 'gu') | ||
| .replace(/closeQuoteLookbehind/g, closeQuoteLookbehind) | ||
| .replace(/notPunctSpace/g, _notPunctuationOrSpace) | ||
| .replace(/punctSpace/g, _punctuationOrSpace) | ||
| .replace(/punct/g, _punctuation) | ||
| .getRegex(); | ||
|
|
||
| // Tilde left delimiter for strikethrough (similar to emStrongLDelim for asterisk) | ||
| const delLDelim = edit(/^~~?(?:((?!~)punct)|[^\s~])/, 'u') | ||
| .replace(/punct/g, _punctuation) | ||
|
|
@@ -446,6 +497,9 @@ type InlineKeys = keyof typeof inlineNormal; | |
|
|
||
| const inlinePedantic: Record<InlineKeys, RegExp> = { | ||
| ...inlineNormal, | ||
| emStrongLDelim: emStrongLDelimPedantic, | ||
| emStrongRDelimAst: emStrongRDelimAstPedantic, | ||
| emStrongRDelimUnd: emStrongRDelimUndPedantic, | ||
| link: edit(/^!?\[(label)\]\((.*?)\)/) | ||
| .replace('label', _inlineLabel) | ||
| .getRegex(), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <p><strong>foo:</strong>bar</p> | ||
|
|
||
| <p><strong>foo</strong>bar</p> | ||
|
|
||
| <p><em>foo:</em>bar</p> | ||
|
|
||
| <p><em>foo:</em>bar</p> | ||
|
|
||
| <p><strong>foo:</strong>bar</p> | ||
|
|
||
| <p>aa<strong>"A"</strong>and<strong>"B"</strong>aa</p> | ||
|
|
||
| <p>aa<strong>'A'</strong>and<strong>'B'</strong>aa</p> | ||
|
|
||
| <p>We can compare <strong>"direction"</strong> and <strong>"distance"</strong> in two ways</p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| --- | ||
| pedantic: true | ||
| --- | ||
|
|
||
| **foo:**bar | ||
|
|
||
| **foo**bar | ||
|
|
||
| *foo:*bar | ||
|
|
||
| _foo:_bar | ||
|
|
||
| __foo:__bar | ||
|
|
||
| aa**"A"**and**"B"**aa | ||
|
|
||
| aa**'A'**and**'B'**aa | ||
|
|
||
| We can compare **"direction"** and **"distance"** in two ways |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
openQuoteClass and closeQuoteClass are asymmetric (” only exists in the latter). If that's intentional, I'd add a test to pin the behavior; otherwise it looks like an easy regression candidate.