mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10: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,84 +333,90 @@ 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.
|
||||||
function forEachInlineCodeSpan(input, handler) {
|
*
|
||||||
let currentLine = 0;
|
* @param {string} input Markdown content.
|
||||||
let currentColumn = 0;
|
* @param {Function} handler Callback function.
|
||||||
let index = 0;
|
* @returns {void}
|
||||||
while (index < input.length) {
|
*/
|
||||||
let startIndex = -1;
|
function forEachInlineCodeSpan(input, handler) {
|
||||||
let startLine = -1;
|
let currentLine = 0;
|
||||||
let startColumn = -1;
|
let currentColumn = 0;
|
||||||
let tickCount = 0;
|
let index = 0;
|
||||||
let currentTicks = 0;
|
while (index < input.length) {
|
||||||
let state = "normal";
|
let startIndex = -1;
|
||||||
// Deliberate <= so trailing 0 completes the last span (ex: "text `code`")
|
let startLine = -1;
|
||||||
for (; index <= input.length; index++) {
|
let startColumn = -1;
|
||||||
const char = input[index];
|
let tickCount = 0;
|
||||||
// Ignore backticks in link destination
|
let currentTicks = 0;
|
||||||
if ((char === "[") && (state === "normal")) {
|
let state = "normal";
|
||||||
state = "linkTextOpen";
|
// Deliberate <= so trailing 0 completes the last span (ex: "text `code`")
|
||||||
} else if ((char === "]") && (state === "linkTextOpen")) {
|
for (; index <= input.length; index++) {
|
||||||
state = "linkTextClosed";
|
const char = input[index];
|
||||||
} else if ((char === "(") && (state === "linkTextClosed")) {
|
// Ignore backticks in link destination
|
||||||
state = "linkDestinationOpen";
|
if ((char === "[") && (state === "normal")) {
|
||||||
} else if (
|
state = "linkTextOpen";
|
||||||
((char === "(") && (state === "linkDestinationOpen")) ||
|
} else if ((char === "]") && (state === "linkTextOpen")) {
|
||||||
((char === ")") && (state === "linkDestinationOpen")) ||
|
state = "linkTextClosed";
|
||||||
(state === "linkTextClosed")) {
|
} else if ((char === "(") && (state === "linkTextClosed")) {
|
||||||
state = "normal";
|
state = "linkDestinationOpen";
|
||||||
}
|
} else if (
|
||||||
// Parse backtick open/close
|
((char === "(") && (state === "linkDestinationOpen")) ||
|
||||||
if ((char === "`") && (state !== "linkDestinationOpen")) {
|
((char === ")") && (state === "linkDestinationOpen")) ||
|
||||||
// Count backticks at start or end of code span
|
(state === "linkTextClosed")) {
|
||||||
currentTicks++;
|
state = "normal";
|
||||||
if ((startIndex === -1) || (startColumn === -1)) {
|
|
||||||
startIndex = index + 1;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if ((startIndex >= 0) &&
|
|
||||||
(startColumn >= 0) &&
|
|
||||||
(tickCount === currentTicks)) {
|
|
||||||
// Found end backticks; invoke callback for code span
|
|
||||||
handler(
|
|
||||||
input.substring(startIndex, index - currentTicks),
|
|
||||||
startLine, startColumn, tickCount);
|
|
||||||
startIndex = -1;
|
|
||||||
startColumn = -1;
|
|
||||||
} else if ((startIndex >= 0) && (startColumn === -1)) {
|
|
||||||
// Found start backticks
|
|
||||||
tickCount = currentTicks;
|
|
||||||
startLine = currentLine;
|
|
||||||
startColumn = currentColumn;
|
|
||||||
}
|
|
||||||
// Not in backticks
|
|
||||||
currentTicks = 0;
|
|
||||||
}
|
|
||||||
if (char === "\n") {
|
|
||||||
// On next line
|
|
||||||
currentLine++;
|
|
||||||
currentColumn = 0;
|
|
||||||
} else if ((char === "\\") &&
|
|
||||||
((startIndex === -1) || (startColumn === -1)) &&
|
|
||||||
(input[index + 1] !== "\n")) {
|
|
||||||
// Escape character outside code, skip next
|
|
||||||
index++;
|
|
||||||
currentColumn += 2;
|
|
||||||
} else {
|
|
||||||
// On next column
|
|
||||||
currentColumn++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (startIndex >= 0) {
|
// Parse backtick open/close
|
||||||
// Restart loop after unmatched start backticks (ex: "`text``code``")
|
if ((char === "`") && (state !== "linkDestinationOpen")) {
|
||||||
index = startIndex;
|
// Count backticks at start or end of code span
|
||||||
currentLine = startLine;
|
currentTicks++;
|
||||||
currentColumn = startColumn;
|
if ((startIndex === -1) || (startColumn === -1)) {
|
||||||
|
startIndex = index + 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ((startIndex >= 0) &&
|
||||||
|
(startColumn >= 0) &&
|
||||||
|
(tickCount === currentTicks)) {
|
||||||
|
// Found end backticks; invoke callback for code span
|
||||||
|
handler(
|
||||||
|
input.substring(startIndex, index - currentTicks),
|
||||||
|
startLine, startColumn, tickCount);
|
||||||
|
startIndex = -1;
|
||||||
|
startColumn = -1;
|
||||||
|
} else if ((startIndex >= 0) && (startColumn === -1)) {
|
||||||
|
// Found start backticks
|
||||||
|
tickCount = currentTicks;
|
||||||
|
startLine = currentLine;
|
||||||
|
startColumn = currentColumn;
|
||||||
|
}
|
||||||
|
// Not in backticks
|
||||||
|
currentTicks = 0;
|
||||||
|
}
|
||||||
|
if (char === "\n") {
|
||||||
|
// On next line
|
||||||
|
currentLine++;
|
||||||
|
currentColumn = 0;
|
||||||
|
} else if ((char === "\\") &&
|
||||||
|
((startIndex === -1) || (startColumn === -1)) &&
|
||||||
|
(input[index + 1] !== "\n")) {
|
||||||
|
// Escape character outside code, skip next
|
||||||
|
index++;
|
||||||
|
currentColumn += 2;
|
||||||
|
} else {
|
||||||
|
// On next column
|
||||||
|
currentColumn++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
if (startIndex >= 0) {
|
||||||
|
// Restart loop after unmatched start backticks (ex: "`text``code``")
|
||||||
|
index = startIndex;
|
||||||
|
currentLine = startLine;
|
||||||
|
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