Update rewritten MD037/no-space-in-emphasis to handle multiple-line emphasis (fixes #272).

This commit is contained in:
David Anson 2020-04-12 20:40:05 -07:00
parent 190716da39
commit f775b9d4fb
5 changed files with 174 additions and 37 deletions

View file

@ -2,7 +2,7 @@
"use strict"; "use strict";
const { addErrorContext, forEachLine } = require("../helpers"); const { addErrorContext, forEachLine, isBlankLine } = require("../helpers");
const { lineMetadata } = require("./cache"); const { lineMetadata } = require("./cache");
const emphasisRe = /(^|[^\\])(?:(\*\*?\*?)|(__?_?))/g; const emphasisRe = /(^|[^\\])(?:(\*\*?\*?)|(__?_?))/g;
@ -15,21 +15,72 @@ module.exports = {
"description": "Spaces inside emphasis markers", "description": "Spaces inside emphasis markers",
"tags": [ "whitespace", "emphasis" ], "tags": [ "whitespace", "emphasis" ],
"function": function MD037(params, onError) { "function": function MD037(params, onError) {
// eslint-disable-next-line init-declarations
let effectiveEmphasisLength, emphasisIndex, emphasisLength, pendingError;
// eslint-disable-next-line jsdoc/require-jsdoc
function resetRunTracking() {
emphasisIndex = -1;
emphasisLength = 0;
effectiveEmphasisLength = 0;
pendingError = null;
}
// eslint-disable-next-line jsdoc/require-jsdoc
function handleRunEnd(line, lineIndex, contextLength, match, matchIndex) {
// Close current run
let content = line.substring(emphasisIndex, matchIndex);
if (!emphasisLength) {
content = content.trimStart();
}
if (!match) {
content = content.trimEnd();
}
const leftSpace = leftSpaceRe.test(content);
const rightSpace = rightSpaceRe.test(content);
if (leftSpace || rightSpace) {
// Report the violation
const contextStart = emphasisIndex - emphasisLength;
const contextEnd = matchIndex + contextLength;
const context = line.substring(contextStart, contextEnd);
const column = contextStart + 1;
const length = contextEnd - contextStart;
const leftMarker = line.substring(contextStart, emphasisIndex);
const rightMarker = match ? (match[2] || match[3]) : "";
const fixedText = `${leftMarker}${content.trim()}${rightMarker}`;
return [
onError,
lineIndex + 1,
context,
leftSpace,
rightSpace,
[ column, length ],
{
"editColumn": column,
"deleteCount": length,
"insertText": fixedText
}
];
}
return null;
}
// Initialize
resetRunTracking();
forEachLine( forEachLine(
lineMetadata(), lineMetadata(),
(line, lineIndex, inCode, onFence, inTable, inItem, inBreak) => { (line, lineIndex, inCode, onFence, inTable, inItem, onBreak) => {
if (inCode || inBreak) { const onItemStart = (inItem === 1);
if (inCode || inTable || onBreak || onItemStart || isBlankLine(line)) {
// Emphasis resets when leaving a block
resetRunTracking();
}
if (inCode || onBreak) {
// Emphasis has no meaning here // Emphasis has no meaning here
return; return;
} }
if (inItem === 1) { if (onItemStart) {
// Trim overlapping '*' list item marker // Trim overlapping '*' list item marker
line = line.replace(asteriskListItemMarkerRe, "$1 $2"); line = line.replace(asteriskListItemMarkerRe, "$1 $2");
} }
let match = null; let match = null;
let emphasisIndex = -1;
let emphasisLength = 0;
let effectiveEmphasisLength = 0;
// Match all emphasis-looking runs in the line... // Match all emphasis-looking runs in the line...
while ((match = emphasisRe.exec(line))) { while ((match = emphasisRe.exec(line))) {
const matchIndex = match.index + match[1].length; const matchIndex = match.index + match[1].length;
@ -40,38 +91,18 @@ module.exports = {
emphasisLength = matchLength; emphasisLength = matchLength;
effectiveEmphasisLength = matchLength; effectiveEmphasisLength = matchLength;
} else if (matchLength === effectiveEmphasisLength) { } else if (matchLength === effectiveEmphasisLength) {
// Close current run // Ending an existing run, report any pending error
const content = line.substring(emphasisIndex, matchIndex); if (pendingError) {
const leftSpace = leftSpaceRe.test(content); addErrorContext(...pendingError);
const rightSpace = rightSpaceRe.test(content); pendingError = null;
if (leftSpace || rightSpace) { }
// Report the violation const error = handleRunEnd(
const contextStart = emphasisIndex - emphasisLength; line, lineIndex, effectiveEmphasisLength, match, matchIndex);
const contextEnd = matchIndex + effectiveEmphasisLength; if (error) {
const context = line.substring(contextStart, contextEnd); addErrorContext(...error);
const column = contextStart + 1;
const length = contextEnd - contextStart;
const leftMarker = line.substring(contextStart, emphasisIndex);
const rightMarker = match[2] || match[3];
const fixedText = `${leftMarker}${content.trim()}${rightMarker}`;
addErrorContext(
onError,
lineIndex + 1,
context,
leftSpace,
rightSpace,
[ column, length ],
{
"editColumn": column,
"deleteCount": length,
"insertText": fixedText
}
);
} }
// Reset // Reset
emphasisIndex = -1; resetRunTracking();
emphasisLength = 0;
effectiveEmphasisLength = 0;
} else if (matchLength === 3) { } else if (matchLength === 3) {
// Swap internal run length (1->2 or 2->1) // Swap internal run length (1->2 or 2->1)
effectiveEmphasisLength = matchLength - effectiveEmphasisLength; effectiveEmphasisLength = matchLength - effectiveEmphasisLength;
@ -83,6 +114,13 @@ module.exports = {
effectiveEmphasisLength += matchLength; effectiveEmphasisLength += matchLength;
} }
} }
if (emphasisIndex !== -1) {
pendingError = pendingError ||
handleRunEnd(line, lineIndex, 0, null, line.length);
// Adjust for pending run on new line
emphasisIndex = 0;
emphasisLength = 0;
}
} }
); );
} }

View file

@ -58,3 +58,6 @@ span```` text
text. text.
Text [ space](link) text [space ](link) text [ space ](link) text. Text [ space](link) text [space ](link) text [ space ](link) text.
Space * inside
multi-line * emphasis.

View file

@ -60,3 +60,6 @@ span```` text
text. text.
Text [space](link) text [space](link) text [space](link) text. Text [space](link) text [space](link) text [space](link) text.
Space *inside
multi-line* emphasis.

View file

@ -116,6 +116,24 @@
"errorContext": "__ some __", "errorContext": "__ some __",
"errorRange": [ 19, 10 ] "errorRange": [ 19, 10 ]
}, },
{
"lineNumber": 62,
"ruleNames": [ "MD037", "no-space-in-emphasis" ],
"ruleDescription": "Spaces inside emphasis markers",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md037",
"errorDetail": null,
"errorContext": "* inside",
"errorRange": [ 7, 8 ]
},
{
"lineNumber": 63,
"ruleNames": [ "MD037", "no-space-in-emphasis" ],
"ruleDescription": "Spaces inside emphasis markers",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md037",
"errorDetail": null,
"errorContext": "multi-line *",
"errorRange": [ 1, 12 ]
},
{ {
"lineNumber": 17, "lineNumber": 17,
"ruleNames": [ "MD038", "no-space-in-code" ], "ruleNames": [ "MD038", "no-space-in-code" ],

View file

@ -0,0 +1,75 @@
# Space Inside Emphasis Markers, Multiple Lines
Text *emphasis
emphasis* text
Text *emphasis* *emphasis
emphasis* *emphasis* text
Text *emphasis* text *emphasis
emphasis* text *emphasis* text
Text *emphasis* *emphasis
emphasis* *emphasis* *emphasis
emphasis* text *emphasis
emphasis* text *emphasis* text
Text text
text *emphasis
emphasis emphasis
emphasis* text
text text
Text * asterisk
Text * asterisk
* Item *emphasis* item
* Item *emphasis* item
* Item *emphasis
emphasis* item
* Item *emphasis* item
* Item * asterisk
* Item * asterisk
Text * emphasis {MD037}
emphasis* text
Text *emphasis
emphasis * text {MD037}
Text * emphasis {MD037}
emphasis * text {MD037}
Text *emphasis * *emphasis {MD037}
emphasis* * emphasis* text {MD037}
Text *emphasis* * emphasis {MD037}
emphasis * *emphasis* text {MD037}
Text * emphasis * * emphasis {MD037}
emphasis * * emphasis * text {MD037}
Text text
text * emphasis {MD037}
emphasis emphasis
emphasis * text {MD037}
text text
* Item *emphasis* item
* Item * emphasis {MD037}
emphasis* item
* Item *emphasis
emphasis * item {MD037}
* Item * emphasis {MD037}
emphasis * item {MD037}
* Item *emphasis* item
* Item item item
item * emphasis * item {MD037}
Text _ emphasis {MD037}
emphasis _ text {MD037}
Text ** bold {MD037}
bold ** text {MD037}