Update MD037/no-space-in-emphasis to ignore the content of HTML attributes (fixes #540).

This commit is contained in:
David Anson 2022-07-30 20:35:27 -07:00
parent 48f47b5214
commit 1154ab483b
5 changed files with 209 additions and 39 deletions

View file

@ -2,9 +2,9 @@
"use strict";
const { addErrorContext, emphasisMarkersInContent, forEachLine, isBlankLine } =
require("../helpers");
const { lineMetadata } = require("./cache");
const { addErrorContext, emphasisMarkersInContent, forEachLine, isBlankLine,
withinAnyRange } = require("../helpers");
const { htmlElementRanges, lineMetadata } = require("./cache");
const emphasisRe = /(^|[^\\]|\\\\)(?:(\*\*?\*?)|(__?_?))/g;
const embeddedUnderscoreRe = /([A-Za-z0-9])_([A-Za-z0-9])/g;
@ -18,6 +18,7 @@ module.exports = {
"description": "Spaces inside emphasis markers",
"tags": [ "whitespace", "emphasis" ],
"function": function MD037(params, onError) {
const exclusions = htmlElementRanges();
// eslint-disable-next-line init-declarations
let effectiveEmphasisLength, emphasisIndex, emphasisKind, emphasisLength,
pendingError = null;
@ -50,25 +51,27 @@ module.exports = {
// 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
}
];
if (!withinAnyRange(exclusions, lineIndex, column, length)) {
const context = line.substring(contextStart, contextEnd);
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;
}