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
|
|
|
/**
|
|
|
|
* Gets the value of an ordered list item prefix token.
|
|
|
|
*
|
2024-12-03 19:58:28 -08:00
|
|
|
* @param {import("markdownlint").MicromarkToken} listItemPrefix List item prefix token.
|
2024-08-20 21:36:17 -07:00
|
|
|
* @returns {number} List item value.
|
|
|
|
*/
|
|
|
|
function getOrderedListItemValue(listItemPrefix) {
|
2024-09-24 22:48:14 -07:00
|
|
|
return Number(getDescendantsByType(listItemPrefix, [ "listItemValue" ])[0].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) {
|
2020-01-25 18:40:39 -08:00
|
|
|
const style = String(params.config.style || "one_or_ordered");
|
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) {
|
|
|
|
const firstValue = getOrderedListItemValue(listItemPrefixes[0]);
|
|
|
|
const secondValue = getOrderedListItemValue(listItemPrefixes[1]);
|
|
|
|
if ((secondValue !== 1) || (firstValue === 0)) {
|
|
|
|
incrementing = true;
|
|
|
|
if (firstValue === 0) {
|
|
|
|
expected = 0;
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2020-03-18 21:50:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Determine effective style
|
|
|
|
let listStyle = style;
|
|
|
|
if (listStyle === "one_or_ordered") {
|
|
|
|
listStyle = incrementing ? "ordered" : "one";
|
2024-08-20 21:36:17 -07:00
|
|
|
} else if (listStyle === "zero") {
|
|
|
|
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) {
|
|
|
|
const actual = getOrderedListItemValue(listItemPrefix);
|
|
|
|
addErrorDetailIf(
|
|
|
|
onError,
|
|
|
|
listItemPrefix.startLine,
|
|
|
|
expected,
|
|
|
|
actual,
|
|
|
|
"Style: " + listStyleExamples[listStyle],
|
|
|
|
undefined,
|
|
|
|
[ listItemPrefix.startColumn, listItemPrefix.endColumn - listItemPrefix.startColumn ]
|
|
|
|
);
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|