2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2024-08-20 21:36:17 -07:00
|
|
|
const { addErrorDetailIf } = require("../helpers");
|
2024-09-28 16:26:38 -07:00
|
|
|
const { getDescendantsByType, getParentOfType } = require("../helpers/micromark-helpers.cjs");
|
2024-08-24 22:05:16 -07:00
|
|
|
const { filterByTypesCached } = require("./cache");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2024-08-20 21:36:17 -07:00
|
|
|
const markerToStyle = {
|
|
|
|
"-": "dash",
|
|
|
|
"+": "plus",
|
|
|
|
"*": "asterisk"
|
|
|
|
};
|
|
|
|
const styleToMarker = {
|
2021-02-06 15:49:02 -08:00
|
|
|
"dash": "-",
|
|
|
|
"plus": "+",
|
|
|
|
"asterisk": "*"
|
|
|
|
};
|
|
|
|
const differentItemStyle = {
|
|
|
|
"dash": "plus",
|
|
|
|
"plus": "asterisk",
|
|
|
|
"asterisk": "dash"
|
|
|
|
};
|
2024-08-20 21:36:17 -07:00
|
|
|
const validStyles = new Set([
|
|
|
|
"asterisk",
|
|
|
|
"consistent",
|
|
|
|
"dash",
|
|
|
|
"plus",
|
|
|
|
"sublist"
|
|
|
|
]);
|
2021-02-06 15:49:02 -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": [ "MD004", "ul-style" ],
|
|
|
|
"description": "Unordered list style",
|
|
|
|
"tags": [ "bullet", "ul" ],
|
2024-08-20 21:36:17 -07:00
|
|
|
"parser": "micromark",
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD004(params, onError) {
|
2020-01-25 18:40:39 -08:00
|
|
|
const style = String(params.config.style || "consistent");
|
2024-08-20 21:36:17 -07:00
|
|
|
let expectedStyle = validStyles.has(style) ? style : "dash";
|
2018-04-27 22:05:34 -07:00
|
|
|
const nestingStyles = [];
|
2024-08-24 22:05:16 -07:00
|
|
|
for (const listUnordered of filterByTypesCached([ "listUnordered" ])) {
|
2024-08-20 21:36:17 -07:00
|
|
|
let nesting = 0;
|
|
|
|
if (style === "sublist") {
|
2024-09-28 16:26:38 -07:00
|
|
|
/** @type {import("../helpers/micromark-helpers.cjs").Token | null} */
|
2024-08-20 21:36:17 -07:00
|
|
|
let parent = listUnordered;
|
2024-08-24 22:05:16 -07:00
|
|
|
// @ts-ignore
|
2024-09-28 15:55:26 -07:00
|
|
|
while ((parent = getParentOfType(parent, [ "listOrdered", "listUnordered" ]))) {
|
2024-08-20 21:36:17 -07:00
|
|
|
nesting++;
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2024-08-20 21:36:17 -07:00
|
|
|
}
|
|
|
|
const listItemMarkers = getDescendantsByType(listUnordered, [ "listItemPrefix", "listItemMarker" ]);
|
|
|
|
for (const listItemMarker of listItemMarkers) {
|
|
|
|
const itemStyle = markerToStyle[listItemMarker.text];
|
|
|
|
if (style === "sublist") {
|
|
|
|
if (!nestingStyles[nesting]) {
|
|
|
|
nestingStyles[nesting] =
|
|
|
|
(itemStyle === nestingStyles[nesting - 1]) ?
|
|
|
|
differentItemStyle[itemStyle] :
|
|
|
|
itemStyle;
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2024-08-20 21:36:17 -07:00
|
|
|
expectedStyle = nestingStyles[nesting];
|
|
|
|
} else if (expectedStyle === "consistent") {
|
|
|
|
expectedStyle = itemStyle;
|
2022-06-08 22:10:27 -07:00
|
|
|
}
|
2024-08-20 21:36:17 -07:00
|
|
|
const column = listItemMarker.startColumn;
|
|
|
|
const length = listItemMarker.endColumn - listItemMarker.startColumn;
|
|
|
|
addErrorDetailIf(
|
|
|
|
onError,
|
|
|
|
listItemMarker.startLine,
|
|
|
|
expectedStyle,
|
|
|
|
itemStyle,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
[ column, length ],
|
|
|
|
{
|
|
|
|
"editColumn": column,
|
|
|
|
"deleteCount": length,
|
|
|
|
"insertText": styleToMarker[expectedStyle]
|
|
|
|
}
|
|
|
|
);
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2022-06-08 22:10:27 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|