Reimplement MD037/no-space-in-emphasis to better handle multiple fixes on a line and more scenarios (fixes #251).

This commit is contained in:
David Anson 2020-03-28 14:16:28 -07:00
parent de86a26e4e
commit 29f16bf402
4 changed files with 121 additions and 40 deletions

View file

@ -2,39 +2,67 @@
"use strict";
const { addErrorContext, forEachInlineChild } = require("../helpers");
const { addErrorContext, forEachLine } = require("../helpers");
const { lineMetadata } = require("./cache");
const leftSpaceRe = /(?:^|\s)(\*\*?\*?|__?_?)\s.*[^\\]\1/g;
const rightSpaceRe = /(?:^|[^\\])(\*\*?\*?|__?_?).+\s\1(?:\s|$)/g;
const emphasisRe = /(^|[^\\])(?:(\*\*?\*?)|(__?_?))/g;
const asteriskListItemMarkerRe = /^(\s*)\*(\s+)/;
const leftSpaceRe = /^\s+/;
const rightSpaceRe = /\s+$/;
module.exports = {
"names": [ "MD037", "no-space-in-emphasis" ],
"description": "Spaces inside emphasis markers",
"tags": [ "whitespace", "emphasis" ],
"function": function MD037(params, onError) {
forEachInlineChild(params, "text", (token) => {
const { content, lineNumber } = token;
const columnsReported = [];
[ leftSpaceRe, rightSpaceRe ].forEach((spaceRe, index) => {
forEachLine(
lineMetadata(),
(line, lineIndex, inCode, onFence, inTable, inItem, inBreak) => {
if (inCode || inBreak) {
// Emphasis has no meaning here
return;
}
if (inItem === 1) {
// Trim overlapping '*' list item marker
line = line.replace(asteriskListItemMarkerRe, "$1 $2");
}
let match = null;
while ((match = spaceRe.exec(content)) !== null) {
const [ fullText, marker ] = match;
const line = params.lines[lineNumber - 1];
if (line.includes(fullText)) {
const text = fullText.trim();
const column = line.indexOf(text) + 1;
if (!columnsReported.includes(column)) {
const length = text.length;
const markerLength = marker.length;
const emphasized =
text.slice(markerLength, length - markerLength);
const fixedText = `${marker}${emphasized.trim()}${marker}`;
let emphasisIndex = -1;
let emphasisLength = 0;
// Match all emphasis-looking runs in the line...
while ((match = emphasisRe.exec(line))) {
const matchIndex = match.index + match[1].length;
const matchLength = match[0].length - match[1].length;
if (emphasisIndex === -1) {
// New run
emphasisLength = matchLength;
emphasisIndex = matchIndex + emphasisLength;
} else {
// Already in a run
if (matchLength !== emphasisLength) {
// Looks like a run within a run, reset to embedded run
emphasisLength += matchLength;
emphasisIndex = matchIndex + emphasisLength;
}
// Extract emphasized content
const content = line.substring(emphasisIndex, matchIndex);
const leftSpace = leftSpaceRe.test(content);
const rightSpace = rightSpaceRe.test(content);
if (leftSpace || rightSpace) {
// Report the violation
const contextStart = emphasisIndex - emphasisLength;
const contextEnd = matchIndex + emphasisLength;
const context = line.substring(contextStart, contextEnd);
const column = contextStart + 1;
const length = contextEnd - contextStart;
const marker = match[2] || match[3];
const fixedText = `${marker}${content.trim()}${marker}`;
addErrorContext(
onError,
lineNumber,
text,
index === 0,
index !== 0,
lineIndex + 1,
context,
leftSpace,
rightSpace,
[ column, length ],
{
"editColumn": column,
@ -42,11 +70,15 @@ module.exports = {
"insertText": fixedText
}
);
columnsReported.push(column);
}
// Update the run
emphasisLength -= matchLength;
if (!emphasisLength) {
emphasisIndex = -1;
}
}
}
});
});
}
);
}
};