mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Update MD037/MD038/MD039 to report fixInfo for violations.
This commit is contained in:
parent
620853f200
commit
c8a74bd72c
3 changed files with 66 additions and 29 deletions
46
lib/md037.js
46
lib/md037.js
|
@ -4,29 +4,43 @@
|
|||
|
||||
const { addErrorContext, forEachInlineChild } = require("../helpers");
|
||||
|
||||
const leftSpaceRe = /(?:^|\s)(\*\*?|__?)\s.*[^\\]\1/g;
|
||||
const rightSpaceRe = /(?:^|[^\\])(\*\*?|__?).+\s\1(?:\s|$)/g;
|
||||
|
||||
module.exports = {
|
||||
"names": [ "MD037", "no-space-in-emphasis" ],
|
||||
"description": "Spaces inside emphasis markers",
|
||||
"tags": [ "whitespace", "emphasis" ],
|
||||
"function": function MD037(params, onError) {
|
||||
forEachInlineChild(params, "text", (token) => {
|
||||
let left = true;
|
||||
let match = /(?:^|\s)(\*\*?|__?)\s.*[^\\]\1/.exec(token.content);
|
||||
if (!match) {
|
||||
left = false;
|
||||
match = /(?:^|[^\\])(\*\*?|__?).+\s\1(?:\s|$)/.exec(token.content);
|
||||
}
|
||||
if (match) {
|
||||
const fullText = match[0];
|
||||
const line = params.lines[token.lineNumber - 1];
|
||||
if (line.includes(fullText)) {
|
||||
const text = fullText.trim();
|
||||
const column = line.indexOf(text) + 1;
|
||||
const length = text.length;
|
||||
addErrorContext(onError, token.lineNumber,
|
||||
text, left, !left, [ column, length ]);
|
||||
[ leftSpaceRe, rightSpaceRe ].forEach((spaceRe, index) => {
|
||||
let match = null;
|
||||
while ((match = spaceRe.exec(token.content)) !== null) {
|
||||
const [ fullText, marker ] = match;
|
||||
const line = params.lines[token.lineNumber - 1];
|
||||
if (line.includes(fullText)) {
|
||||
const text = fullText.trim();
|
||||
const column = line.indexOf(text) + 1;
|
||||
const length = text.length;
|
||||
const markerLength = marker.length;
|
||||
const emphasized = text.slice(markerLength, length - markerLength);
|
||||
const fixedText = `${marker}${emphasized.trim()}${marker}`;
|
||||
addErrorContext(
|
||||
onError,
|
||||
token.lineNumber,
|
||||
text,
|
||||
index === 0,
|
||||
index !== 0,
|
||||
[ column, length ],
|
||||
{
|
||||
"editColumn": column,
|
||||
"deleteCount": length,
|
||||
"insertText": fixedText
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue