mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Reimplement MD024/no-duplicate-heading using micromark tokens.
This commit is contained in:
parent
b1b16dabec
commit
96354678dc
6 changed files with 77 additions and 49 deletions
|
@ -1564,6 +1564,20 @@ function getHeadingStyle(heading) {
|
|||
return "atx_closed";
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the heading text of a Micromark heading token.
|
||||
*
|
||||
* @param {Token} heading Micromark heading token.
|
||||
* @returns {string} Heading text.
|
||||
*/
|
||||
function getHeadingText(heading) {
|
||||
const headingTexts = filterByTypes(
|
||||
heading.children,
|
||||
[ "atxHeadingText", "setextHeadingText" ]
|
||||
);
|
||||
return headingTexts[0]?.text.replace(/[\r\n]+/g, " ") || "";
|
||||
}
|
||||
|
||||
/**
|
||||
* HTML tag information.
|
||||
*
|
||||
|
@ -1679,6 +1693,7 @@ module.exports = {
|
|||
filterByTypes,
|
||||
getHeadingLevel,
|
||||
getHeadingStyle,
|
||||
getHeadingText,
|
||||
getHtmlTagInfo,
|
||||
getMicromarkEvents,
|
||||
getTokenParentOfType,
|
||||
|
@ -4575,7 +4590,8 @@ module.exports = {
|
|||
|
||||
|
||||
|
||||
const { addErrorContext, forEachHeading } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
||||
const { addErrorContext } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
||||
const { filterByTypes, getHeadingLevel, getHeadingText } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
||||
|
||||
// eslint-disable-next-line jsdoc/valid-types
|
||||
/** @type import("./markdownlint").Rule */
|
||||
|
@ -4583,15 +4599,20 @@ module.exports = {
|
|||
"names": [ "MD024", "no-duplicate-heading" ],
|
||||
"description": "Multiple headings with the same content",
|
||||
"tags": [ "headings" ],
|
||||
"parser": "markdownit",
|
||||
"parser": "micromark",
|
||||
"function": function MD024(params, onError) {
|
||||
const siblingsOnly = !!params.config.siblings_only || false;
|
||||
const knownContents = [ null, [] ];
|
||||
let lastLevel = 1;
|
||||
let knownContent = knownContents[lastLevel];
|
||||
forEachHeading(params, (heading, content) => {
|
||||
const headings = filterByTypes(
|
||||
params.parsers.micromark.tokens,
|
||||
[ "atxHeading", "setextHeading" ]
|
||||
);
|
||||
for (const heading of headings) {
|
||||
const headingText = getHeadingText(heading);
|
||||
if (siblingsOnly) {
|
||||
const newLevel = heading.tag.slice(1);
|
||||
const newLevel = getHeadingLevel(heading);
|
||||
while (lastLevel < newLevel) {
|
||||
lastLevel++;
|
||||
knownContents[lastLevel] = [];
|
||||
|
@ -4603,17 +4624,17 @@ module.exports = {
|
|||
knownContent = knownContents[newLevel];
|
||||
}
|
||||
// @ts-ignore
|
||||
if (knownContent.includes(content)) {
|
||||
if (knownContent.includes(headingText)) {
|
||||
addErrorContext(
|
||||
onError,
|
||||
heading.lineNumber,
|
||||
heading.line.trim()
|
||||
heading.startLine,
|
||||
headingText.trim()
|
||||
);
|
||||
} else {
|
||||
// @ts-ignore
|
||||
knownContent.push(content);
|
||||
knownContent.push(headingText);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -4632,7 +4653,7 @@ module.exports = {
|
|||
|
||||
|
||||
const { addErrorContext, frontMatterHasTitle } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
||||
const { filterByTypes, getHeadingLevel } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
||||
const { filterByTypes, getHeadingLevel, getHeadingText } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
||||
|
||||
// eslint-disable-next-line jsdoc/valid-types
|
||||
/** @type import("./markdownlint").Rule */
|
||||
|
@ -4657,14 +4678,7 @@ module.exports = {
|
|||
const headingLevel = getHeadingLevel(heading);
|
||||
if (headingLevel === level) {
|
||||
if (hasTopLevelHeading || foundFrontMatterTitle) {
|
||||
const headingTexts = filterByTypes(
|
||||
heading.children,
|
||||
[ "atxHeadingText", "setextHeadingText" ]
|
||||
);
|
||||
const headingText = headingTexts.
|
||||
map((token) => token.text).
|
||||
join(" ").
|
||||
replace(/[\r\n]+/g, " ");
|
||||
const headingText = getHeadingText(heading);
|
||||
addErrorContext(
|
||||
onError,
|
||||
heading.startLine,
|
||||
|
|
|
@ -345,6 +345,20 @@ function getHeadingStyle(heading) {
|
|||
return "atx_closed";
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the heading text of a Micromark heading token.
|
||||
*
|
||||
* @param {Token} heading Micromark heading token.
|
||||
* @returns {string} Heading text.
|
||||
*/
|
||||
function getHeadingText(heading) {
|
||||
const headingTexts = filterByTypes(
|
||||
heading.children,
|
||||
[ "atxHeadingText", "setextHeadingText" ]
|
||||
);
|
||||
return headingTexts[0]?.text.replace(/[\r\n]+/g, " ") || "";
|
||||
}
|
||||
|
||||
/**
|
||||
* HTML tag information.
|
||||
*
|
||||
|
@ -460,6 +474,7 @@ module.exports = {
|
|||
filterByTypes,
|
||||
getHeadingLevel,
|
||||
getHeadingStyle,
|
||||
getHeadingText,
|
||||
getHtmlTagInfo,
|
||||
getMicromarkEvents,
|
||||
getTokenParentOfType,
|
||||
|
|
24
lib/md024.js
24
lib/md024.js
|
@ -2,7 +2,8 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const { addErrorContext, forEachHeading } = require("../helpers");
|
||||
const { addErrorContext } = require("../helpers");
|
||||
const { filterByTypes, getHeadingLevel, getHeadingText } = require("../helpers/micromark.cjs");
|
||||
|
||||
// eslint-disable-next-line jsdoc/valid-types
|
||||
/** @type import("./markdownlint").Rule */
|
||||
|
@ -10,15 +11,20 @@ module.exports = {
|
|||
"names": [ "MD024", "no-duplicate-heading" ],
|
||||
"description": "Multiple headings with the same content",
|
||||
"tags": [ "headings" ],
|
||||
"parser": "markdownit",
|
||||
"parser": "micromark",
|
||||
"function": function MD024(params, onError) {
|
||||
const siblingsOnly = !!params.config.siblings_only || false;
|
||||
const knownContents = [ null, [] ];
|
||||
let lastLevel = 1;
|
||||
let knownContent = knownContents[lastLevel];
|
||||
forEachHeading(params, (heading, content) => {
|
||||
const headings = filterByTypes(
|
||||
params.parsers.micromark.tokens,
|
||||
[ "atxHeading", "setextHeading" ]
|
||||
);
|
||||
for (const heading of headings) {
|
||||
const headingText = getHeadingText(heading);
|
||||
if (siblingsOnly) {
|
||||
const newLevel = heading.tag.slice(1);
|
||||
const newLevel = getHeadingLevel(heading);
|
||||
while (lastLevel < newLevel) {
|
||||
lastLevel++;
|
||||
knownContents[lastLevel] = [];
|
||||
|
@ -30,16 +36,16 @@ module.exports = {
|
|||
knownContent = knownContents[newLevel];
|
||||
}
|
||||
// @ts-ignore
|
||||
if (knownContent.includes(content)) {
|
||||
if (knownContent.includes(headingText)) {
|
||||
addErrorContext(
|
||||
onError,
|
||||
heading.lineNumber,
|
||||
heading.line.trim()
|
||||
heading.startLine,
|
||||
headingText.trim()
|
||||
);
|
||||
} else {
|
||||
// @ts-ignore
|
||||
knownContent.push(content);
|
||||
knownContent.push(headingText);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
11
lib/md025.js
11
lib/md025.js
|
@ -3,7 +3,7 @@
|
|||
"use strict";
|
||||
|
||||
const { addErrorContext, frontMatterHasTitle } = require("../helpers");
|
||||
const { filterByTypes, getHeadingLevel } = require("../helpers/micromark.cjs");
|
||||
const { filterByTypes, getHeadingLevel, getHeadingText } = require("../helpers/micromark.cjs");
|
||||
|
||||
// eslint-disable-next-line jsdoc/valid-types
|
||||
/** @type import("./markdownlint").Rule */
|
||||
|
@ -28,14 +28,7 @@ module.exports = {
|
|||
const headingLevel = getHeadingLevel(heading);
|
||||
if (headingLevel === level) {
|
||||
if (hasTopLevelHeading || foundFrontMatterTitle) {
|
||||
const headingTexts = filterByTypes(
|
||||
heading.children,
|
||||
[ "atxHeadingText", "setextHeadingText" ]
|
||||
);
|
||||
const headingText = headingTexts.
|
||||
map((token) => token.text).
|
||||
join(" ").
|
||||
replace(/[\r\n]+/g, " ");
|
||||
const headingText = getHeadingText(heading);
|
||||
addErrorContext(
|
||||
onError,
|
||||
heading.startLine,
|
||||
|
|
|
@ -638,7 +638,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
],
|
||||
},
|
||||
{
|
||||
errorContext: '# Heading',
|
||||
errorContext: 'Heading',
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
fixInfo: null,
|
||||
|
@ -7156,7 +7156,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
],
|
||||
},
|
||||
{
|
||||
errorContext: '# Heading 8',
|
||||
errorContext: 'Heading 8',
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
fixInfo: null,
|
||||
|
@ -14804,7 +14804,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
],
|
||||
},
|
||||
{
|
||||
errorContext: '## B',
|
||||
errorContext: 'B',
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
fixInfo: null,
|
||||
|
@ -14817,7 +14817,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
],
|
||||
},
|
||||
{
|
||||
errorContext: '### C',
|
||||
errorContext: 'C',
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
fixInfo: null,
|
||||
|
@ -14830,7 +14830,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
],
|
||||
},
|
||||
{
|
||||
errorContext: '#### G',
|
||||
errorContext: 'G',
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
fixInfo: null,
|
||||
|
@ -14843,7 +14843,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
],
|
||||
},
|
||||
{
|
||||
errorContext: '### E',
|
||||
errorContext: 'E',
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
fixInfo: null,
|
||||
|
@ -14856,7 +14856,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
],
|
||||
},
|
||||
{
|
||||
errorContext: '# A',
|
||||
errorContext: 'A',
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
fixInfo: null,
|
||||
|
@ -14869,7 +14869,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
],
|
||||
},
|
||||
{
|
||||
errorContext: '## B',
|
||||
errorContext: 'B',
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
fixInfo: null,
|
||||
|
@ -14882,7 +14882,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
],
|
||||
},
|
||||
{
|
||||
errorContext: '# Heading duplicate content si...',
|
||||
errorContext: 'Heading duplicate content sibl...',
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
fixInfo: null,
|
||||
|
@ -14908,7 +14908,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
],
|
||||
},
|
||||
{
|
||||
errorContext: '## BBB ##',
|
||||
errorContext: 'BBB',
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
fixInfo: null,
|
||||
|
@ -15104,7 +15104,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
{
|
||||
errors: [
|
||||
{
|
||||
errorContext: '## Heading 1',
|
||||
errorContext: 'Heading 1',
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
fixInfo: null,
|
||||
|
@ -15117,7 +15117,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
],
|
||||
},
|
||||
{
|
||||
errorContext: '### Heading 2',
|
||||
errorContext: 'Heading 2',
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
fixInfo: null,
|
||||
|
@ -15177,7 +15177,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
{
|
||||
errors: [
|
||||
{
|
||||
errorContext: '### Bug fixes',
|
||||
errorContext: 'Bug fixes',
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
fixInfo: null,
|
||||
|
|
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue