Update MD037/no-space-in-emphasis to ignore emphasis markers in code spans (fixes #278).

This commit is contained in:
David Anson 2020-04-25 15:10:07 -07:00
parent bdc0246b34
commit f5a71521d4
5 changed files with 195 additions and 76 deletions

View file

@ -2,7 +2,8 @@
"use strict";
const { addErrorContext, forEachLine, isBlankLine } = require("../helpers");
const { addErrorContext, emphasisMarkersInCodeSpans, forEachLine,
includesSorted, isBlankLine } = require("../helpers");
const { lineMetadata } = require("./cache");
const emphasisRe = /(^|[^\\])(?:(\*\*?\*?)|(__?_?))/g;
@ -63,6 +64,7 @@ module.exports = {
return null;
}
// Initialize
const ignoreMarkersByLine = emphasisMarkersInCodeSpans(params);
resetRunTracking();
forEachLine(
lineMetadata(),
@ -83,7 +85,12 @@ module.exports = {
let match = null;
// Match all emphasis-looking runs in the line...
while ((match = emphasisRe.exec(line))) {
const ignoreMarkersForLine = ignoreMarkersByLine[lineIndex] || [];
const matchIndex = match.index + match[1].length;
if (includesSorted(ignoreMarkersForLine, matchIndex)) {
// Ignore emphasis markers inside code spans
continue;
}
const matchLength = match[0].length - match[1].length;
if (emphasisIndex === -1) {
// New run