Update MD037/no-space-in-emphasis to ignore violations in tables that include the table pipe character to avoid spanning cells.

This commit is contained in:
David Anson 2021-01-23 20:47:27 -08:00
parent 238781506a
commit d6cd840e7d
3 changed files with 50 additions and 8 deletions

View file

@ -10,6 +10,7 @@ const emphasisRe = /(^|[^\\]|\\\\)(?:(\*\*?\*?)|(__?_?))/g;
const asteriskListItemMarkerRe = /^([\s>]*)\*(\s+)/;
const leftSpaceRe = /^\s+/;
const rightSpaceRe = /\s+$/;
const tablePipeRe = /\|/;
module.exports = {
"names": [ "MD037", "no-space-in-emphasis" ],
@ -28,7 +29,9 @@ module.exports = {
pendingError = null;
}
// eslint-disable-next-line jsdoc/require-jsdoc
function handleRunEnd(line, lineIndex, contextLength, match, matchIndex) {
function handleRunEnd(
line, lineIndex, contextLength, match, matchIndex, inTable
) {
// Close current run
let content = line.substring(emphasisIndex, matchIndex);
if (!emphasisLength) {
@ -39,7 +42,10 @@ module.exports = {
}
const leftSpace = leftSpaceRe.test(content);
const rightSpace = rightSpaceRe.test(content);
if (leftSpace || rightSpace) {
if (
(leftSpace || rightSpace) &&
(!inTable || !tablePipeRe.test(content))
) {
// Report the violation
const contextStart = emphasisIndex - emphasisLength;
const contextEnd = matchIndex + contextLength;
@ -111,7 +117,13 @@ module.exports = {
pendingError = null;
}
const error = handleRunEnd(
line, lineIndex, effectiveEmphasisLength, match, matchIndex);
line,
lineIndex,
effectiveEmphasisLength,
match,
matchIndex,
inTable
);
if (error) {
// @ts-ignore
addErrorContext(...error);
@ -141,7 +153,7 @@ module.exports = {
}
if (emphasisIndex !== -1) {
pendingError = pendingError ||
handleRunEnd(line, lineIndex, 0, null, line.length);
handleRunEnd(line, lineIndex, 0, null, line.length, inTable);
// Adjust for pending run on new line
emphasisIndex = 0;
emphasisLength = 0;