2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
2024-11-28 20:36:44 -08:00
|
|
|
import { addErrorDetailIf } from "../helpers/helpers.cjs";
|
|
|
|
import { getParentOfType } from "../helpers/micromark-helpers.cjs";
|
|
|
|
import { filterByTypesCached } from "./cache.mjs";
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2024-11-28 20:36:44 -08:00
|
|
|
/** @type {import("micromark-util-types").TokenType[]} */
|
2023-10-21 22:03:11 -07:00
|
|
|
const unorderedListTypes =
|
|
|
|
[ "blockQuotePrefix", "listItemPrefix", "listUnordered" ];
|
2024-11-28 20:36:44 -08:00
|
|
|
/** @type {import("micromark-util-types").TokenType[]} */
|
2023-10-21 22:03:11 -07:00
|
|
|
const unorderedParentTypes =
|
|
|
|
[ "blockQuote", "listOrdered", "listUnordered" ];
|
|
|
|
|
2024-12-03 19:58:28 -08:00
|
|
|
/** @type {import("markdownlint").Rule} */
|
2024-11-28 20:36:44 -08:00
|
|
|
export default {
|
2018-01-21 21:44:25 -08:00
|
|
|
"names": [ "MD007", "ul-indent" ],
|
|
|
|
"description": "Unordered list indentation",
|
|
|
|
"tags": [ "bullet", "ul", "indentation" ],
|
2024-03-09 16:17:50 -08:00
|
|
|
"parser": "micromark",
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD007(params, onError) {
|
2020-01-25 18:40:39 -08:00
|
|
|
const indent = Number(params.config.indent || 2);
|
2020-01-01 15:53:03 -05:00
|
|
|
const startIndented = !!params.config.start_indented;
|
2021-12-13 21:49:43 -08:00
|
|
|
const startIndent = Number(params.config.start_indent || indent);
|
2023-10-15 21:24:59 -07:00
|
|
|
const unorderedListNesting = new Map();
|
|
|
|
let lastBlockQuotePrefix = null;
|
2024-08-24 22:05:16 -07:00
|
|
|
const tokens = filterByTypesCached(unorderedListTypes);
|
2023-10-15 21:24:59 -07:00
|
|
|
for (const token of tokens) {
|
2023-12-31 21:51:34 -08:00
|
|
|
const { endColumn, parent, startColumn, startLine, type } = token;
|
2023-10-15 21:24:59 -07:00
|
|
|
if (type === "blockQuotePrefix") {
|
|
|
|
lastBlockQuotePrefix = token;
|
|
|
|
} else if (type === "listUnordered") {
|
|
|
|
let nesting = 0;
|
2024-12-03 19:58:28 -08:00
|
|
|
/** @type {import("markdownlint").MicromarkToken | null} */
|
2023-10-15 21:24:59 -07:00
|
|
|
let current = token;
|
2023-10-21 22:03:11 -07:00
|
|
|
while (
|
2024-08-24 22:05:16 -07:00
|
|
|
// @ts-ignore
|
2024-09-28 15:55:26 -07:00
|
|
|
(current = getParentOfType(current, unorderedParentTypes))
|
2023-10-21 22:03:11 -07:00
|
|
|
) {
|
2023-10-15 21:24:59 -07:00
|
|
|
if (current.type === "listUnordered") {
|
|
|
|
nesting++;
|
2024-01-03 22:12:23 -08:00
|
|
|
// eslint-disable-next-line no-continue
|
2023-10-21 22:03:11 -07:00
|
|
|
continue;
|
2023-10-15 21:24:59 -07:00
|
|
|
} else if (current.type === "listOrdered") {
|
|
|
|
nesting = -1;
|
2019-12-01 17:30:47 -08:00
|
|
|
}
|
2023-10-21 22:03:11 -07:00
|
|
|
break;
|
2023-10-15 21:24:59 -07:00
|
|
|
}
|
|
|
|
if (nesting >= 0) {
|
|
|
|
unorderedListNesting.set(token, nesting);
|
|
|
|
}
|
2024-06-09 17:09:03 -07:00
|
|
|
} else {
|
2023-10-15 21:24:59 -07:00
|
|
|
// listItemPrefix
|
2023-10-19 23:01:31 -07:00
|
|
|
const nesting = unorderedListNesting.get(parent);
|
2023-10-15 21:24:59 -07:00
|
|
|
if (nesting !== undefined) {
|
|
|
|
// listItemPrefix for listUnordered
|
2025-08-03 15:38:25 -07:00
|
|
|
const baseIndent = (getParentOfType(token, [ "gfmFootnoteDefinition" ])) ? 4 : 0;
|
2023-10-15 21:24:59 -07:00
|
|
|
const expectedIndent =
|
2025-08-03 15:38:25 -07:00
|
|
|
baseIndent + (startIndented ? startIndent : 0) + (nesting * indent);
|
2023-10-15 21:24:59 -07:00
|
|
|
const blockQuoteAdjustment =
|
|
|
|
(lastBlockQuotePrefix?.endLine === startLine) ?
|
|
|
|
(lastBlockQuotePrefix.endColumn - 1) :
|
|
|
|
0;
|
|
|
|
const actualIndent = startColumn - 1 - blockQuoteAdjustment;
|
2023-12-31 21:51:34 -08:00
|
|
|
const range = [ 1, endColumn - 1 ];
|
2023-10-15 21:24:59 -07:00
|
|
|
const fixInfo = {
|
|
|
|
"editColumn": startColumn - actualIndent,
|
|
|
|
"deleteCount": Math.max(actualIndent - expectedIndent, 0),
|
|
|
|
"insertText": "".padEnd(Math.max(expectedIndent - actualIndent, 0))
|
|
|
|
};
|
2019-12-01 17:30:47 -08:00
|
|
|
addErrorDetailIf(
|
|
|
|
onError,
|
2023-10-15 21:24:59 -07:00
|
|
|
startLine,
|
2019-12-01 17:30:47 -08:00
|
|
|
expectedIndent,
|
|
|
|
actualIndent,
|
2023-10-15 21:24:59 -07:00
|
|
|
undefined,
|
|
|
|
undefined,
|
2019-12-01 17:30:47 -08:00
|
|
|
range,
|
2023-10-15 21:24:59 -07:00
|
|
|
fixInfo
|
|
|
|
);
|
2022-06-08 22:10:27 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2022-06-08 22:10:27 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|