2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2019-04-13 11:18:57 -07:00
|
|
|
const { addErrorContext, forEachHeading } = require("../helpers");
|
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 = {
|
2023-11-09 20:05:30 -08:00
|
|
|
"names": [ "MD024", "no-duplicate-heading" ],
|
2018-03-19 23:39:42 +01:00
|
|
|
"description": "Multiple headings with the same content",
|
2023-11-09 20:05:30 -08:00
|
|
|
"tags": [ "headings" ],
|
2024-03-09 16:17:50 -08:00
|
|
|
"parser": "markdownit",
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD024(params, onError) {
|
2023-12-05 19:23:17 -08:00
|
|
|
const siblingsOnly = !!params.config.siblings_only || false;
|
2018-07-19 21:49:30 -07:00
|
|
|
const knownContents = [ null, [] ];
|
|
|
|
let lastLevel = 1;
|
|
|
|
let knownContent = knownContents[lastLevel];
|
2019-03-28 22:06:42 -07:00
|
|
|
forEachHeading(params, (heading, content) => {
|
2018-07-19 21:49:30 -07:00
|
|
|
if (siblingsOnly) {
|
|
|
|
const newLevel = heading.tag.slice(1);
|
|
|
|
while (lastLevel < newLevel) {
|
|
|
|
lastLevel++;
|
|
|
|
knownContents[lastLevel] = [];
|
|
|
|
}
|
|
|
|
while (lastLevel > newLevel) {
|
|
|
|
knownContents[lastLevel] = [];
|
|
|
|
lastLevel--;
|
|
|
|
}
|
|
|
|
knownContent = knownContents[newLevel];
|
|
|
|
}
|
2023-12-05 19:23:17 -08:00
|
|
|
// @ts-ignore
|
2019-03-28 22:06:42 -07:00
|
|
|
if (knownContent.includes(content)) {
|
2023-12-05 19:23:17 -08:00
|
|
|
addErrorContext(
|
|
|
|
onError,
|
|
|
|
heading.lineNumber,
|
|
|
|
heading.line.trim()
|
|
|
|
);
|
2019-03-28 22:06:42 -07:00
|
|
|
} else {
|
2023-12-05 19:23:17 -08:00
|
|
|
// @ts-ignore
|
2019-03-28 22:06:42 -07:00
|
|
|
knownContent.push(content);
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|