mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Reimplement MD037/no-space-in-emphasis to better handle multiple fixes on a line and more scenarios (fixes #251).
This commit is contained in:
parent
de86a26e4e
commit
29f16bf402
4 changed files with 121 additions and 40 deletions
|
|
@ -234,17 +234,22 @@ module.exports.getLineMetadata = function getLineMetadata(params) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
filterTokens(params, "list_item_open", function forToken(token) {
|
filterTokens(params, "list_item_open", function forToken(token) {
|
||||||
|
let count = 1;
|
||||||
for (let i = token.map[0]; i < token.map[1]; i++) {
|
for (let i = token.map[0]; i < token.map[1]; i++) {
|
||||||
lineMetadata[i][5] = true;
|
lineMetadata[i][5] = count;
|
||||||
|
count++;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
filterTokens(params, "hr", function forToken(token) {
|
||||||
|
lineMetadata[token.map[0]][6] = true;
|
||||||
|
});
|
||||||
return lineMetadata;
|
return lineMetadata;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Calls the provided function for each line (with context)
|
// Calls the provided function for each line (with context)
|
||||||
module.exports.forEachLine = function forEachLine(lineMetadata, handler) {
|
module.exports.forEachLine = function forEachLine(lineMetadata, handler) {
|
||||||
lineMetadata.forEach(function forMetadata(metadata) {
|
lineMetadata.forEach(function forMetadata(metadata) {
|
||||||
// Parameters: line, lineIndex, inCode, onFence, inTable
|
// Parameters: line, lineIndex, inCode, onFence, inTable, inBreak
|
||||||
handler(...metadata);
|
handler(...metadata);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
||||||
84
lib/md037.js
84
lib/md037.js
|
|
@ -2,39 +2,67 @@
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { addErrorContext, forEachInlineChild } = require("../helpers");
|
const { addErrorContext, forEachLine } = require("../helpers");
|
||||||
|
const { lineMetadata } = require("./cache");
|
||||||
|
|
||||||
const leftSpaceRe = /(?:^|\s)(\*\*?\*?|__?_?)\s.*[^\\]\1/g;
|
const emphasisRe = /(^|[^\\])(?:(\*\*?\*?)|(__?_?))/g;
|
||||||
const rightSpaceRe = /(?:^|[^\\])(\*\*?\*?|__?_?).+\s\1(?:\s|$)/g;
|
const asteriskListItemMarkerRe = /^(\s*)\*(\s+)/;
|
||||||
|
const leftSpaceRe = /^\s+/;
|
||||||
|
const rightSpaceRe = /\s+$/;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
"names": [ "MD037", "no-space-in-emphasis" ],
|
"names": [ "MD037", "no-space-in-emphasis" ],
|
||||||
"description": "Spaces inside emphasis markers",
|
"description": "Spaces inside emphasis markers",
|
||||||
"tags": [ "whitespace", "emphasis" ],
|
"tags": [ "whitespace", "emphasis" ],
|
||||||
"function": function MD037(params, onError) {
|
"function": function MD037(params, onError) {
|
||||||
forEachInlineChild(params, "text", (token) => {
|
forEachLine(
|
||||||
const { content, lineNumber } = token;
|
lineMetadata(),
|
||||||
const columnsReported = [];
|
(line, lineIndex, inCode, onFence, inTable, inItem, inBreak) => {
|
||||||
[ leftSpaceRe, rightSpaceRe ].forEach((spaceRe, index) => {
|
if (inCode || inBreak) {
|
||||||
|
// Emphasis has no meaning here
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (inItem === 1) {
|
||||||
|
// Trim overlapping '*' list item marker
|
||||||
|
line = line.replace(asteriskListItemMarkerRe, "$1 $2");
|
||||||
|
}
|
||||||
let match = null;
|
let match = null;
|
||||||
while ((match = spaceRe.exec(content)) !== null) {
|
let emphasisIndex = -1;
|
||||||
const [ fullText, marker ] = match;
|
let emphasisLength = 0;
|
||||||
const line = params.lines[lineNumber - 1];
|
// Match all emphasis-looking runs in the line...
|
||||||
if (line.includes(fullText)) {
|
while ((match = emphasisRe.exec(line))) {
|
||||||
const text = fullText.trim();
|
const matchIndex = match.index + match[1].length;
|
||||||
const column = line.indexOf(text) + 1;
|
const matchLength = match[0].length - match[1].length;
|
||||||
if (!columnsReported.includes(column)) {
|
if (emphasisIndex === -1) {
|
||||||
const length = text.length;
|
// New run
|
||||||
const markerLength = marker.length;
|
emphasisLength = matchLength;
|
||||||
const emphasized =
|
emphasisIndex = matchIndex + emphasisLength;
|
||||||
text.slice(markerLength, length - markerLength);
|
} else {
|
||||||
const fixedText = `${marker}${emphasized.trim()}${marker}`;
|
// Already in a run
|
||||||
|
if (matchLength !== emphasisLength) {
|
||||||
|
// Looks like a run within a run, reset to embedded run
|
||||||
|
emphasisLength += matchLength;
|
||||||
|
emphasisIndex = matchIndex + emphasisLength;
|
||||||
|
}
|
||||||
|
// Extract emphasized content
|
||||||
|
const content = line.substring(emphasisIndex, matchIndex);
|
||||||
|
const leftSpace = leftSpaceRe.test(content);
|
||||||
|
const rightSpace = rightSpaceRe.test(content);
|
||||||
|
if (leftSpace || rightSpace) {
|
||||||
|
// Report the violation
|
||||||
|
const contextStart = emphasisIndex - emphasisLength;
|
||||||
|
const contextEnd = matchIndex + emphasisLength;
|
||||||
|
const context = line.substring(contextStart, contextEnd);
|
||||||
|
const column = contextStart + 1;
|
||||||
|
const length = contextEnd - contextStart;
|
||||||
|
const marker = match[2] || match[3];
|
||||||
|
const fixedText = `${marker}${content.trim()}${marker}`;
|
||||||
addErrorContext(
|
addErrorContext(
|
||||||
onError,
|
onError,
|
||||||
lineNumber,
|
lineIndex + 1,
|
||||||
text,
|
context,
|
||||||
index === 0,
|
leftSpace,
|
||||||
index !== 0,
|
rightSpace,
|
||||||
[ column, length ],
|
[ column, length ],
|
||||||
{
|
{
|
||||||
"editColumn": column,
|
"editColumn": column,
|
||||||
|
|
@ -42,11 +70,15 @@ module.exports = {
|
||||||
"insertText": fixedText
|
"insertText": fixedText
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
columnsReported.push(column);
|
}
|
||||||
|
// Update the run
|
||||||
|
emphasisLength -= matchLength;
|
||||||
|
if (!emphasisLength) {
|
||||||
|
emphasisIndex = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
});
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,28 +1,42 @@
|
||||||
# Heading
|
# Heading
|
||||||
|
|
||||||
|
## Single-character markers
|
||||||
|
|
||||||
|
None are valid emphasis without spaces.
|
||||||
|
|
||||||
Escaped asterisks \* should \* be ignored by MD037.
|
Escaped asterisks \* should \* be ignored by MD037.
|
||||||
|
|
||||||
Escaped asterisks \* should * be ignored by MD037.
|
Escaped asterisks \* should * be ignored by MD037.
|
||||||
|
|
||||||
Escaped asterisks * should \* be ignored by MD037.
|
Escaped asterisks * should \* be ignored by MD037.
|
||||||
|
|
||||||
Escaped asterisks \** should ** be ignored by MD037.
|
|
||||||
|
|
||||||
Escaped asterisks *\* should ** be ignored by MD037.
|
|
||||||
|
|
||||||
Escaped asterisks ** should \** be ignored by MD037.
|
|
||||||
|
|
||||||
Escaped asterisks ** should *\* be ignored by MD037.
|
|
||||||
|
|
||||||
Escaped underscores \_ should \_ be ignored by MD037.
|
Escaped underscores \_ should \_ be ignored by MD037.
|
||||||
|
|
||||||
Escaped underscores \_ should _ be ignored by MD037.
|
Escaped underscores \_ should _ be ignored by MD037.
|
||||||
|
|
||||||
Escaped underscores _ should \_ be ignored by MD037.
|
Escaped underscores _ should \_ be ignored by MD037.
|
||||||
|
|
||||||
Escaped underscores \__ should __ be ignored by MD037.
|
## Double-character markers, start
|
||||||
|
|
||||||
Escaped underscores _\_ should __ be ignored by MD037.
|
All should be reported because they are valid single-character
|
||||||
|
marker emphasis without spaces.
|
||||||
|
|
||||||
|
Escaped asterisks \** should ** be ignored by MD037. {MD037}
|
||||||
|
|
||||||
|
Escaped asterisks *\* should ** be ignored by MD037. {MD037}
|
||||||
|
|
||||||
|
Escaped underscores \__ should __ be ignored by MD037. {MD037}
|
||||||
|
|
||||||
|
Escaped underscores _\_ should __ be ignored by MD037. {MD037}
|
||||||
|
|
||||||
|
## Double-character markers, end
|
||||||
|
|
||||||
|
All should be reported, but are ignored because they look like
|
||||||
|
the start of an embedded emphasis.
|
||||||
|
|
||||||
|
Escaped asterisks ** should \** be ignored by MD037.
|
||||||
|
|
||||||
|
Escaped asterisks ** should *\* be ignored by MD037.
|
||||||
|
|
||||||
Escaped underscores __ should \__ be ignored by MD037.
|
Escaped underscores __ should \__ be ignored by MD037.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -44,9 +44,11 @@ One-sided *broken emphasis * {MD037}
|
||||||
|
|
||||||
One-sided * broken emphasis* {MD037}
|
One-sided * broken emphasis* {MD037}
|
||||||
|
|
||||||
Don't _flag on _words with underscores before them.
|
Will _flag on _words with underscores before them. {MD037}
|
||||||
|
|
||||||
The same goes for words* with asterisks* after them.
|
The same goes for words* with asterisks* after them. {MD037}
|
||||||
|
|
||||||
|
But not with escaped\* asterisks\* \_and \_underscores.
|
||||||
|
|
||||||
* Emphasis* with left space is recognized as a list
|
* Emphasis* with left space is recognized as a list
|
||||||
|
|
||||||
|
|
@ -95,3 +97,31 @@ ___Strong and emphasis ___ with right space {MD037}
|
||||||
{MD037} Right space __strong __
|
{MD037} Right space __strong __
|
||||||
|
|
||||||
{MD037} Right space ___strong and emphasis ___
|
{MD037} Right space ___strong and emphasis ___
|
||||||
|
|
||||||
|
**Multiple ** spaces **in ** emphasis **at ** once. {MD037}
|
||||||
|
|
||||||
|
**Multiple ** spaces ** in** emphasis ** at ** once. {MD037}
|
||||||
|
|
||||||
|
This is * an ambiguous * scenario {MD037}
|
||||||
|
|
||||||
|
* List item *with emphasis* on the
|
||||||
|
first and *second lines*.
|
||||||
|
* List * item* {MD037}
|
||||||
|
* List *item * {MD037}
|
||||||
|
* List * item * {MD037}
|
||||||
|
* List item with
|
||||||
|
*hanging* emphasis
|
||||||
|
and * some* lines {MD037}
|
||||||
|
with *space * problems {MD037}
|
||||||
|
throughout * the * content {MD037}
|
||||||
|
|
||||||
|
Uncommon scenarios from the CommonMark specification:
|
||||||
|
***strong emph***
|
||||||
|
***strong** in emph*
|
||||||
|
***emph* in strong**
|
||||||
|
**in strong *emph***
|
||||||
|
*in emph **strong***
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
Violations * are * allowed in code blocks where emphasis does not apply.
|
||||||
|
```
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue