mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Update MD037/no-space-in-emphasis to handle more scenarios (refs #286).
This commit is contained in:
parent
37f1d6b64b
commit
f607a49a5b
3 changed files with 101 additions and 28 deletions
|
@ -25,8 +25,8 @@ module.exports.bareUrlRe = /(?:http|ftp)s?:\/\/[^\s\]"']*/ig;
|
||||||
module.exports.listItemMarkerRe = /^([\s>]*)(?:[*+-]|\d+[.)])\s+/;
|
module.exports.listItemMarkerRe = /^([\s>]*)(?:[*+-]|\d+[.)])\s+/;
|
||||||
module.exports.orderedListItemMarkerRe = /^[\s>]*0*(\d+)[.)]/;
|
module.exports.orderedListItemMarkerRe = /^[\s>]*0*(\d+)[.)]/;
|
||||||
|
|
||||||
// Regular expression for emphasis markers
|
// Regular expression for all instances of emphasis markers
|
||||||
const emphasisMarkersRe = /[_*]+/g;
|
const emphasisMarkersRe = /[_*]/g;
|
||||||
|
|
||||||
// Regular expression for links
|
// Regular expression for links
|
||||||
const linkRe = /\[(?:[^\]]|\[[^\]]*\])*\]\(\S*\)/g;
|
const linkRe = /\[(?:[^\]]|\[[^\]]*\])*\]\(\S*\)/g;
|
||||||
|
@ -255,7 +255,7 @@ module.exports.getLineMetadata = function getLineMetadata(params) {
|
||||||
// Calls the provided function for each line (with context)
|
// Calls the provided function for each line (with context)
|
||||||
module.exports.forEachLine = function forEachLine(lineMetadata, handler) {
|
module.exports.forEachLine = function forEachLine(lineMetadata, handler) {
|
||||||
lineMetadata.forEach(function forMetadata(metadata) {
|
lineMetadata.forEach(function forMetadata(metadata) {
|
||||||
// Parameters: line, lineIndex, inCode, onFence, inTable, inBreak
|
// Parameters: line, lineIndex, inCode, onFence, inTable, inItem, inBreak
|
||||||
handler(...metadata);
|
handler(...metadata);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
57
lib/md037.js
57
lib/md037.js
|
@ -6,8 +6,8 @@ const { addErrorContext, emphasisMarkersInContent, forEachLine, isBlankLine } =
|
||||||
require("../helpers");
|
require("../helpers");
|
||||||
const { lineMetadata } = require("./cache");
|
const { lineMetadata } = require("./cache");
|
||||||
|
|
||||||
const emphasisRe = /(^|[^\\])(?:(\*\*?\*?)|(__?_?))/g;
|
const emphasisRe = /(^|[^\\]|\\\\)(?:(\*\*?\*?)|(__?_?))/g;
|
||||||
const asteriskListItemMarkerRe = /^(\s*)\*(\s+)/;
|
const asteriskListItemMarkerRe = /^([\s>]*)\*(\s+)/;
|
||||||
const leftSpaceRe = /^\s+/;
|
const leftSpaceRe = /^\s+/;
|
||||||
const rightSpaceRe = /\s+$/;
|
const rightSpaceRe = /\s+$/;
|
||||||
|
|
||||||
|
@ -101,31 +101,38 @@ module.exports = {
|
||||||
emphasisLength = matchLength;
|
emphasisLength = matchLength;
|
||||||
emphasisKind = matchKind;
|
emphasisKind = matchKind;
|
||||||
effectiveEmphasisLength = matchLength;
|
effectiveEmphasisLength = matchLength;
|
||||||
} else if (
|
} else if (matchKind === emphasisKind) {
|
||||||
(matchLength === effectiveEmphasisLength) &&
|
// Matching emphasis markers
|
||||||
(matchKind === emphasisKind)
|
if (matchLength === effectiveEmphasisLength) {
|
||||||
) {
|
// Ending an existing run, report any pending error
|
||||||
// Ending an existing run, report any pending error
|
if (pendingError) {
|
||||||
if (pendingError) {
|
addErrorContext(...pendingError);
|
||||||
addErrorContext(...pendingError);
|
pendingError = null;
|
||||||
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(
|
// Back up one character so RegExp has a chance to match the
|
||||||
line, lineIndex, effectiveEmphasisLength, match, matchIndex);
|
// next marker (ex: "**star**_underscore_")
|
||||||
if (error) {
|
emphasisRe.lastIndex--;
|
||||||
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 {
|
} else {
|
||||||
// Upgrade to internal run (1->3 or 2->3)
|
// Back up one character so RegExp has a chance to match the
|
||||||
effectiveEmphasisLength += matchLength;
|
// mis-matched marker (ex: "*text_*")
|
||||||
|
emphasisRe.lastIndex--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (emphasisIndex !== -1) {
|
if (emphasisIndex !== -1) {
|
||||||
|
|
|
@ -188,3 +188,69 @@ Mixed `code_span` scenarios are *also* okay.
|
||||||
Text [Link](under_score) text _underscore_ text [Link](st*ar) text *star* text
|
Text [Link](under_score) text _underscore_ text [Link](st*ar) text *star* text
|
||||||
|
|
||||||
[Link [link] link](under_score) followed by _underscore_
|
[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 *******`
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue