mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 22:40:13 +01:00
Update MD037/no-space-in-emphasis to ignore emphasis markers in code spans (fixes #278).
This commit is contained in:
parent
bdc0246b34
commit
f5a71521d4
5 changed files with 195 additions and 76 deletions
|
|
@ -40,6 +40,7 @@
|
||||||
"multiline-comment-style": ["error", "separate-lines"],
|
"multiline-comment-style": ["error", "separate-lines"],
|
||||||
"multiline-ternary": "off",
|
"multiline-ternary": "off",
|
||||||
"newline-per-chained-call": "off",
|
"newline-per-chained-call": "off",
|
||||||
|
"no-continue": "off",
|
||||||
"no-empty-function": "off",
|
"no-empty-function": "off",
|
||||||
"no-extra-parens": "off",
|
"no-extra-parens": "off",
|
||||||
"no-implicit-coercion": "off",
|
"no-implicit-coercion": "off",
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,9 @@ module.exports.bareUrlRe = /(?:http|ftp)s?:\/\/[^\s\]"']*/ig;
|
||||||
module.exports.listItemMarkerRe = /^([\s>]*)(?:[*+-]|\d+[.)])\s+/;
|
module.exports.listItemMarkerRe = /^([\s>]*)(?:[*+-]|\d+[.)])\s+/;
|
||||||
module.exports.orderedListItemMarkerRe = /^[\s>]*0*(\d+)[.)]/;
|
module.exports.orderedListItemMarkerRe = /^[\s>]*0*(\d+)[.)]/;
|
||||||
|
|
||||||
|
// Regular expression for emphasis markers
|
||||||
|
const emphasisMarkersRe = /[_*]+/g;
|
||||||
|
|
||||||
// readFile options for reading with the UTF-8 encoding
|
// readFile options for reading with the UTF-8 encoding
|
||||||
module.exports.utf8Encoding = { "encoding": "utf8" };
|
module.exports.utf8Encoding = { "encoding": "utf8" };
|
||||||
|
|
||||||
|
|
@ -330,8 +333,13 @@ module.exports.forEachHeading = function forEachHeading(params, handler) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Calls the provided function for each inline code span's content
|
/**
|
||||||
module.exports.forEachInlineCodeSpan =
|
* Calls the provided function for each inline code span's content.
|
||||||
|
*
|
||||||
|
* @param {string} input Markdown content.
|
||||||
|
* @param {Function} handler Callback function.
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
function forEachInlineCodeSpan(input, handler) {
|
function forEachInlineCodeSpan(input, handler) {
|
||||||
let currentLine = 0;
|
let currentLine = 0;
|
||||||
let currentColumn = 0;
|
let currentColumn = 0;
|
||||||
|
|
@ -407,7 +415,8 @@ module.exports.forEachInlineCodeSpan =
|
||||||
currentColumn = startColumn;
|
currentColumn = startColumn;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
module.exports.forEachInlineCodeSpan = forEachInlineCodeSpan;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a generic error object via the onError callback.
|
* Adds a generic error object via the onError callback.
|
||||||
|
|
@ -484,6 +493,41 @@ module.exports.frontMatterHasTitle =
|
||||||
frontMatterLines.some((line) => frontMatterTitleRe.test(line));
|
frontMatterLines.some((line) => frontMatterTitleRe.test(line));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of emphasis markers in code spans.
|
||||||
|
*
|
||||||
|
* @param {Object} params RuleParams instance.
|
||||||
|
* @returns {number[][]} List of markers.
|
||||||
|
*/
|
||||||
|
function emphasisMarkersInCodeSpans(params) {
|
||||||
|
const { lines } = params;
|
||||||
|
const byLine = new Array(lines.length);
|
||||||
|
filterTokens(params, "inline", (token) => {
|
||||||
|
const { children, lineNumber, map } = token;
|
||||||
|
if (children.some((child) => child.type === "code_inline")) {
|
||||||
|
const tokenLines = lines.slice(map[0], map[1]);
|
||||||
|
forEachInlineCodeSpan(
|
||||||
|
tokenLines.join("\n"),
|
||||||
|
(code, lineIndex, column, tickCount) => {
|
||||||
|
const codeLines = code.split(newLineRe);
|
||||||
|
codeLines.forEach((codeLine, codeLineIndex) => {
|
||||||
|
let match = null;
|
||||||
|
while ((match = emphasisMarkersRe.exec(codeLine))) {
|
||||||
|
const byLineIndex = lineNumber - 1 + lineIndex + codeLineIndex;
|
||||||
|
const inLine = byLine[byLineIndex] || [];
|
||||||
|
const codeLineOffset = codeLineIndex ? 0 : column - 1 + tickCount;
|
||||||
|
inLine.push(codeLineOffset + match.index);
|
||||||
|
byLine[byLineIndex] = inLine;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return byLine;
|
||||||
|
}
|
||||||
|
module.exports.emphasisMarkersInCodeSpans = emphasisMarkersInCodeSpans;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the most common line ending, falling back to the platform default.
|
* Gets the most common line ending, falling back to the platform default.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,8 @@
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { addErrorContext, forEachLine, isBlankLine } = require("../helpers");
|
const { addErrorContext, emphasisMarkersInCodeSpans, forEachLine,
|
||||||
|
includesSorted, isBlankLine } = require("../helpers");
|
||||||
const { lineMetadata } = require("./cache");
|
const { lineMetadata } = require("./cache");
|
||||||
|
|
||||||
const emphasisRe = /(^|[^\\])(?:(\*\*?\*?)|(__?_?))/g;
|
const emphasisRe = /(^|[^\\])(?:(\*\*?\*?)|(__?_?))/g;
|
||||||
|
|
@ -63,6 +64,7 @@ module.exports = {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
// Initialize
|
// Initialize
|
||||||
|
const ignoreMarkersByLine = emphasisMarkersInCodeSpans(params);
|
||||||
resetRunTracking();
|
resetRunTracking();
|
||||||
forEachLine(
|
forEachLine(
|
||||||
lineMetadata(),
|
lineMetadata(),
|
||||||
|
|
@ -83,7 +85,12 @@ module.exports = {
|
||||||
let match = null;
|
let match = null;
|
||||||
// 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 ignoreMarkersForLine = ignoreMarkersByLine[lineIndex] || [];
|
||||||
const matchIndex = match.index + match[1].length;
|
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;
|
const matchLength = match[0].length - match[1].length;
|
||||||
if (emphasisIndex === -1) {
|
if (emphasisIndex === -1) {
|
||||||
// New run
|
// New run
|
||||||
|
|
|
||||||
|
|
@ -73,3 +73,50 @@ emphasis _ text {MD037}
|
||||||
|
|
||||||
Text ** bold {MD037}
|
Text ** bold {MD037}
|
||||||
bold ** text {MD037}
|
bold ** text {MD037}
|
||||||
|
|
||||||
|
Emphasis `inside
|
||||||
|
of * code *
|
||||||
|
blocks` is okay.
|
||||||
|
|
||||||
|
Emphasis `* inside`
|
||||||
|
code
|
||||||
|
`blocks *` is okay.
|
||||||
|
|
||||||
|
Emphasis `inside *`
|
||||||
|
code
|
||||||
|
`* blocks` is okay.
|
||||||
|
|
||||||
|
Emphasis `inside
|
||||||
|
_ code _
|
||||||
|
blocks` is okay.
|
||||||
|
|
||||||
|
Emphasis `_ inside`
|
||||||
|
code
|
||||||
|
`blocks _` is okay.
|
||||||
|
|
||||||
|
Emphasis `inside _`
|
||||||
|
code
|
||||||
|
`_ blocks` is okay.
|
||||||
|
|
||||||
|
Mixed `code_span`
|
||||||
|
scenarios
|
||||||
|
are _also_ okay.
|
||||||
|
|
||||||
|
Mixed `code*span`
|
||||||
|
scenarios
|
||||||
|
are *also* okay.
|
||||||
|
|
||||||
|
This paragraph
|
||||||
|
contains *a* mix
|
||||||
|
of `*` emphasis
|
||||||
|
scenarios and *should*
|
||||||
|
not trigger `*` any
|
||||||
|
violations at *all*.
|
||||||
|
|
||||||
|
This paragraph
|
||||||
|
contains `a * slightly
|
||||||
|
more complicated
|
||||||
|
multi-line emphasis
|
||||||
|
scenario * that
|
||||||
|
should * not trigger
|
||||||
|
violations * either`.
|
||||||
|
|
|
||||||
|
|
@ -154,3 +154,23 @@ Text *emph***strong ** text {MD037}
|
||||||
```markdown
|
```markdown
|
||||||
Violations * are * allowed in code blocks where emphasis does not apply.
|
Violations * are * allowed in code blocks where emphasis does not apply.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Emphasis `inside * code * blocks` is okay.
|
||||||
|
|
||||||
|
Emphasis `* inside` code `blocks *` is okay.
|
||||||
|
|
||||||
|
Emphasis `inside *` code `* blocks` is okay.
|
||||||
|
|
||||||
|
Emphasis `inside _ code _ blocks` is okay.
|
||||||
|
|
||||||
|
Emphasis `_ inside` code `blocks _` is okay.
|
||||||
|
|
||||||
|
Emphasis `inside _` code `_ blocks` is okay.
|
||||||
|
|
||||||
|
Mixed `code_span` scenarios are _also_ okay.
|
||||||
|
|
||||||
|
Mixed `code*span` scenarios are *also* okay.
|
||||||
|
|
||||||
|
Mixed `code*span` scenarios are _also_ okay.
|
||||||
|
|
||||||
|
Mixed `code_span` scenarios are *also* okay.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue