2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2023-10-12 21:43:31 -07:00
|
|
|
const { addError, addErrorDetailIf } = require("../helpers");
|
2023-10-21 22:03:11 -07:00
|
|
|
const { filterByTypes, inHtmlFlow } = require("../helpers/micromark.cjs");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2024-02-27 20:42:09 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("./markdownlint").Rule */
|
2018-01-21 21:44:25 -08:00
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD005", "list-indent" ],
|
|
|
|
"description": "Inconsistent indentation for list items at the same level",
|
|
|
|
"tags": [ "bullet", "ul", "indentation" ],
|
|
|
|
"function": function MD005(params, onError) {
|
2024-02-28 21:01:23 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("../helpers/micromark.cjs").Token[] */
|
|
|
|
const micromarkTokens =
|
|
|
|
// @ts-ignore
|
|
|
|
params.parsers.micromark.tokens;
|
2023-10-12 21:43:31 -07:00
|
|
|
const lists = filterByTypes(
|
2024-02-28 21:01:23 -08:00
|
|
|
micromarkTokens,
|
2023-10-12 21:43:31 -07:00
|
|
|
[ "listOrdered", "listUnordered" ]
|
2023-10-21 22:03:11 -07:00
|
|
|
).filter((list) => !inHtmlFlow(list));
|
2023-10-12 21:43:31 -07:00
|
|
|
for (const list of lists) {
|
|
|
|
const expectedIndent = list.startColumn - 1;
|
2018-06-15 22:37:12 -07:00
|
|
|
let expectedEnd = 0;
|
|
|
|
let endMatching = false;
|
2023-10-12 21:43:31 -07:00
|
|
|
const listItemPrefixes =
|
|
|
|
list.children.filter((token) => (token.type === "listItemPrefix"));
|
|
|
|
for (const listItemPrefix of listItemPrefixes) {
|
|
|
|
const lineNumber = listItemPrefix.startLine;
|
|
|
|
const actualIndent = listItemPrefix.startColumn - 1;
|
2023-12-31 21:51:34 -08:00
|
|
|
const range = [ 1, listItemPrefix.endColumn - 1 ];
|
2023-10-12 21:43:31 -07:00
|
|
|
if (list.type === "listUnordered") {
|
2020-01-15 22:06:41 -08:00
|
|
|
addErrorDetailIf(
|
|
|
|
onError,
|
|
|
|
lineNumber,
|
|
|
|
expectedIndent,
|
|
|
|
actualIndent,
|
|
|
|
null,
|
|
|
|
null,
|
2023-10-12 21:43:31 -07:00
|
|
|
range
|
2020-01-15 22:06:41 -08:00
|
|
|
// No fixInfo; MD007 handles this scenario better
|
|
|
|
);
|
2023-10-12 21:43:31 -07:00
|
|
|
} else {
|
2023-12-31 21:51:34 -08:00
|
|
|
const markerLength = listItemPrefix.text.trim().length;
|
|
|
|
const actualEnd = listItemPrefix.startColumn + markerLength - 1;
|
2018-06-15 22:37:12 -07:00
|
|
|
expectedEnd = expectedEnd || actualEnd;
|
|
|
|
if ((expectedIndent !== actualIndent) || endMatching) {
|
|
|
|
if (expectedEnd === actualEnd) {
|
|
|
|
endMatching = true;
|
|
|
|
} else {
|
|
|
|
const detail = endMatching ?
|
|
|
|
`Expected: (${expectedEnd}); Actual: (${actualEnd})` :
|
|
|
|
`Expected: ${expectedIndent}; Actual: ${actualIndent}`;
|
2020-01-15 22:06:41 -08:00
|
|
|
const expected = endMatching ?
|
|
|
|
expectedEnd - markerLength :
|
|
|
|
expectedIndent;
|
|
|
|
const actual = endMatching ?
|
|
|
|
actualEnd - markerLength :
|
|
|
|
actualIndent;
|
|
|
|
addError(
|
|
|
|
onError,
|
|
|
|
lineNumber,
|
|
|
|
detail,
|
2023-10-12 21:43:31 -07:00
|
|
|
undefined,
|
|
|
|
range,
|
2020-01-15 22:06:41 -08:00
|
|
|
{
|
|
|
|
"editColumn": Math.min(actual, expected) + 1,
|
|
|
|
"deleteCount": Math.max(actual - expected, 0),
|
|
|
|
"insertText": "".padEnd(Math.max(expected - actual, 0))
|
|
|
|
}
|
|
|
|
);
|
2018-06-15 22:37:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-08 22:10:27 -07:00
|
|
|
}
|
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|