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 { getDescendantsByType } from "../helpers/micromark-helpers.cjs";
|
|
|
|
|
import { filterByTypesCached } from "./cache.mjs";
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2019-03-06 21:57:19 -08:00
|
|
|
const listStyleExamples = {
|
|
|
|
|
"one": "1/1/1",
|
|
|
|
|
"ordered": "1/2/3",
|
|
|
|
|
"zero": "0/0/0"
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-20 21:36:17 -07:00
|
|
|
/**
|
2025-08-20 16:24:46 +00:00
|
|
|
* Gets the column and text of an ordered list item prefix token.
|
2024-08-20 21:36:17 -07:00
|
|
|
*
|
2024-12-03 19:58:28 -08:00
|
|
|
* @param {import("markdownlint").MicromarkToken} listItemPrefix List item prefix token.
|
2025-08-20 16:24:46 +00:00
|
|
|
* @returns {{column: number, value: number}} List item value column and text.
|
2024-08-20 21:36:17 -07:00
|
|
|
*/
|
|
|
|
|
function getOrderedListItemValue(listItemPrefix) {
|
2025-08-20 16:24:46 +00:00
|
|
|
const listItemValue = getDescendantsByType(listItemPrefix, [ "listItemValue" ])[0];
|
|
|
|
|
return {
|
|
|
|
|
"column": listItemValue.startColumn,
|
|
|
|
|
"value": Number(listItemValue.text)
|
|
|
|
|
};
|
2024-08-20 21:36:17 -07:00
|
|
|
}
|
|
|
|
|
|
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": [ "MD029", "ol-prefix" ],
|
|
|
|
|
"description": "Ordered list item prefix",
|
|
|
|
|
"tags": [ "ol" ],
|
2024-08-20 21:36:17 -07:00
|
|
|
"parser": "micromark",
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD029(params, onError) {
|
2025-10-11 16:36:47 -07:00
|
|
|
const style = String(params.config.style);
|
2024-08-24 22:05:16 -07:00
|
|
|
for (const listOrdered of filterByTypesCached([ "listOrdered" ])) {
|
2024-08-20 21:36:17 -07:00
|
|
|
const listItemPrefixes = getDescendantsByType(listOrdered, [ "listItemPrefix" ]);
|
|
|
|
|
let expected = 1;
|
2020-03-18 21:50:10 -07:00
|
|
|
let incrementing = false;
|
|
|
|
|
// Check for incrementing number pattern 1/2/3 or 0/1/2
|
2024-08-20 21:36:17 -07:00
|
|
|
if (listItemPrefixes.length >= 2) {
|
2025-08-20 16:24:46 +00:00
|
|
|
const first = getOrderedListItemValue(listItemPrefixes[0]);
|
|
|
|
|
const second = getOrderedListItemValue(listItemPrefixes[1]);
|
|
|
|
|
if ((second.value !== 1) || (first.value === 0)) {
|
2024-08-20 21:36:17 -07:00
|
|
|
incrementing = true;
|
2025-08-20 16:24:46 +00:00
|
|
|
if (first.value === 0) {
|
2024-08-20 21:36:17 -07:00
|
|
|
expected = 0;
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2020-03-18 21:50:10 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Determine effective style
|
2025-10-11 16:36:47 -07:00
|
|
|
const listStyle = ((style === "one") || (style === "ordered") || (style === "zero")) ?
|
|
|
|
|
style :
|
|
|
|
|
(incrementing ? "ordered" : "one");
|
|
|
|
|
if (listStyle === "zero") {
|
2024-08-20 21:36:17 -07:00
|
|
|
expected = 0;
|
2020-03-18 21:50:10 -07:00
|
|
|
} else if (listStyle === "one") {
|
2024-08-20 21:36:17 -07:00
|
|
|
expected = 1;
|
2020-03-18 21:50:10 -07:00
|
|
|
}
|
|
|
|
|
// Validate each list item marker
|
2024-08-20 21:36:17 -07:00
|
|
|
for (const listItemPrefix of listItemPrefixes) {
|
2025-08-20 16:24:46 +00:00
|
|
|
const orderedListItemValue = getOrderedListItemValue(listItemPrefix);
|
|
|
|
|
const actual = orderedListItemValue.value;
|
|
|
|
|
const fixInfo = {
|
|
|
|
|
"editColumn": orderedListItemValue.column,
|
|
|
|
|
"deleteCount": orderedListItemValue.value.toString().length,
|
|
|
|
|
"insertText": expected.toString()
|
|
|
|
|
};
|
2024-08-20 21:36:17 -07:00
|
|
|
addErrorDetailIf(
|
|
|
|
|
onError,
|
|
|
|
|
listItemPrefix.startLine,
|
|
|
|
|
expected,
|
|
|
|
|
actual,
|
|
|
|
|
"Style: " + listStyleExamples[listStyle],
|
|
|
|
|
undefined,
|
2025-08-20 16:24:46 +00:00
|
|
|
[ listItemPrefix.startColumn, listItemPrefix.endColumn - listItemPrefix.startColumn ],
|
|
|
|
|
fixInfo
|
2024-08-20 21:36:17 -07:00
|
|
|
);
|
|
|
|
|
if (listStyle === "ordered") {
|
|
|
|
|
expected++;
|
2020-03-18 21:50:10 -07:00
|
|
|
}
|
2022-06-08 22:10:27 -07:00
|
|
|
}
|
|
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
|
};
|