Update MD037/no-space-in-emphasis to handle more scenarios (refs #286).

This commit is contained in:
David Anson 2020-05-10 17:06:07 -07:00
parent 37f1d6b64b
commit f607a49a5b
3 changed files with 101 additions and 28 deletions

View file

@ -25,8 +25,8 @@ module.exports.bareUrlRe = /(?:http|ftp)s?:\/\/[^\s\]"']*/ig;
module.exports.listItemMarkerRe = /^([\s>]*)(?:[*+-]|\d+[.)])\s+/;
module.exports.orderedListItemMarkerRe = /^[\s>]*0*(\d+)[.)]/;
// Regular expression for emphasis markers
const emphasisMarkersRe = /[_*]+/g;
// Regular expression for all instances of emphasis markers
const emphasisMarkersRe = /[_*]/g;
// Regular expression for links
const linkRe = /\[(?:[^\]]|\[[^\]]*\])*\]\(\S*\)/g;
@ -255,7 +255,7 @@ module.exports.getLineMetadata = function getLineMetadata(params) {
// Calls the provided function for each line (with context)
module.exports.forEachLine = function forEachLine(lineMetadata, handler) {
lineMetadata.forEach(function forMetadata(metadata) {
// Parameters: line, lineIndex, inCode, onFence, inTable, inBreak
// Parameters: line, lineIndex, inCode, onFence, inTable, inItem, inBreak
handler(...metadata);
});
};

View file

@ -6,8 +6,8 @@ const { addErrorContext, emphasisMarkersInContent, forEachLine, isBlankLine } =
require("../helpers");
const { lineMetadata } = require("./cache");
const emphasisRe = /(^|[^\\])(?:(\*\*?\*?)|(__?_?))/g;
const asteriskListItemMarkerRe = /^(\s*)\*(\s+)/;
const emphasisRe = /(^|[^\\]|\\\\)(?:(\*\*?\*?)|(__?_?))/g;
const asteriskListItemMarkerRe = /^([\s>]*)\*(\s+)/;
const leftSpaceRe = /^\s+/;
const rightSpaceRe = /\s+$/;
@ -101,31 +101,38 @@ module.exports = {
emphasisLength = matchLength;
emphasisKind = matchKind;
effectiveEmphasisLength = matchLength;
} else if (
(matchLength === effectiveEmphasisLength) &&
(matchKind === emphasisKind)
) {
// Ending an existing run, report any pending error
if (pendingError) {
addErrorContext(...pendingError);
pendingError = null;
} else if (matchKind === emphasisKind) {
// Matching emphasis markers
if (matchLength === effectiveEmphasisLength) {
// Ending an existing run, report any pending error
if (pendingError) {
addErrorContext(...pendingError);
pendingError = null;
}
const error = handleRunEnd(
line, lineIndex, effectiveEmphasisLength, match, matchIndex);
if (error) {
addErrorContext(...error);
}
// Reset
resetRunTracking();
} else if (matchLength === 3) {
// Swap internal run length (1->2 or 2->1)
effectiveEmphasisLength = matchLength - effectiveEmphasisLength;
} else if (effectiveEmphasisLength === 3) {
// Downgrade internal run (3->1 or 3->2)
effectiveEmphasisLength -= matchLength;
} else {
// Upgrade to internal run (1->3 or 2->3)
effectiveEmphasisLength += matchLength;
}
const error = handleRunEnd(
line, lineIndex, effectiveEmphasisLength, match, matchIndex);
if (error) {
addErrorContext(...error);
}
// Reset
resetRunTracking();
} else if (matchLength === 3) {
// Swap internal run length (1->2 or 2->1)
effectiveEmphasisLength = matchLength - effectiveEmphasisLength;
} else if (effectiveEmphasisLength === 3) {
// Downgrade internal run (3->1 or 3->2)
effectiveEmphasisLength -= matchLength;
// Back up one character so RegExp has a chance to match the
// next marker (ex: "**star**_underscore_")
emphasisRe.lastIndex--;
} else {
// Upgrade to internal run (1->3 or 2->3)
effectiveEmphasisLength += matchLength;
// Back up one character so RegExp has a chance to match the
// mis-matched marker (ex: "*text_*")
emphasisRe.lastIndex--;
}
}
if (emphasisIndex !== -1) {

View file

@ -188,3 +188,69 @@ Mixed `code_span` scenarios are *also* okay.
Text [Link](under_score) text _underscore_ text [Link](st*ar) text *star* text
[Link [link] link](under_score) followed by _underscore_
**under_score** text *under_score*
*under_score* text **under_score**
__star*star__ text _star*star_
_star*star_ text __star*star__
*_emphasis* text *emphasis*
*emphasis_* text *emphasis*
*emphasis* text *_emphasis*
*emphasis* text *emphasis_*
text \\*emphasis* text *emphasis* text
text *emphasis\\* text *emphasis* text
text *emphasis* text \\*emphasis* text
text *emphasis* text *emphasis\\* text
text *star*_underscore_ text **star**_underscore_ text
text **star**_underscore_ text *star*_underscore_ text
text **star**_underscore_ text **star**_underscore_ text
text *star*_underscore_ text *star*__underscore__ text
text *star*__underscore__ text *star*_underscore_ text
text *star*__underscore__ text *star*__underscore__ text
text _underscore_*star* text __underscore__*star* text
text __underscore__*star* text _underscore_*star* text
text __underscore__*star* text __underscore__*star* text
text _underscore_*star* text _underscore_**star** text
text _underscore_**star** text _underscore_*star* text
text _underscore_**star** text _underscore_**star** text
> * List with *emphasis* in blockquote
>
> > * List with *emphasis* in blockquote
`* text *`
`** text **`
`*** text ***`
`**** text ****`
`***** text *****`
`****** text ******`
`******* text *******`