mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Reimplement MD001/heading-increment using micromark tokens.
This commit is contained in:
parent
39afe0a775
commit
f032849081
2 changed files with 36 additions and 18 deletions
|
|
@ -3307,7 +3307,8 @@ module.exports = markdownlint;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const { addErrorDetailIf, filterTokens } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
const { addErrorDetailIf } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
||||||
|
const { filterByTypes, getHeadingLevel } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
||||||
|
|
||||||
// eslint-disable-next-line jsdoc/valid-types
|
// eslint-disable-next-line jsdoc/valid-types
|
||||||
/** @type import("./markdownlint").Rule */
|
/** @type import("./markdownlint").Rule */
|
||||||
|
|
@ -3315,17 +3316,25 @@ module.exports = {
|
||||||
"names": [ "MD001", "heading-increment" ],
|
"names": [ "MD001", "heading-increment" ],
|
||||||
"description": "Heading levels should only increment by one level at a time",
|
"description": "Heading levels should only increment by one level at a time",
|
||||||
"tags": [ "headings" ],
|
"tags": [ "headings" ],
|
||||||
"parser": "markdownit",
|
"parser": "micromark",
|
||||||
"function": function MD001(params, onError) {
|
"function": function MD001(params, onError) {
|
||||||
let prevLevel = 0;
|
let prevLevel = Number.MAX_SAFE_INTEGER;
|
||||||
filterTokens(params, "heading_open", function forToken(token) {
|
const headings = filterByTypes(
|
||||||
const level = Number.parseInt(token.tag.slice(1), 10);
|
params.parsers.micromark.tokens,
|
||||||
if (prevLevel && (level > prevLevel)) {
|
[ "atxHeading", "setextHeading" ]
|
||||||
addErrorDetailIf(onError, token.lineNumber,
|
);
|
||||||
"h" + (prevLevel + 1), "h" + level);
|
for (const heading of headings) {
|
||||||
|
const level = getHeadingLevel(heading);
|
||||||
|
if (level > prevLevel) {
|
||||||
|
addErrorDetailIf(
|
||||||
|
onError,
|
||||||
|
heading.startLine,
|
||||||
|
`h${prevLevel + 1}`,
|
||||||
|
`h${level}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
prevLevel = level;
|
prevLevel = level;
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
27
lib/md001.js
27
lib/md001.js
|
|
@ -2,7 +2,8 @@
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { addErrorDetailIf, filterTokens } = require("../helpers");
|
const { addErrorDetailIf } = require("../helpers");
|
||||||
|
const { filterByTypes, getHeadingLevel } = require("../helpers/micromark.cjs");
|
||||||
|
|
||||||
// eslint-disable-next-line jsdoc/valid-types
|
// eslint-disable-next-line jsdoc/valid-types
|
||||||
/** @type import("./markdownlint").Rule */
|
/** @type import("./markdownlint").Rule */
|
||||||
|
|
@ -10,16 +11,24 @@ module.exports = {
|
||||||
"names": [ "MD001", "heading-increment" ],
|
"names": [ "MD001", "heading-increment" ],
|
||||||
"description": "Heading levels should only increment by one level at a time",
|
"description": "Heading levels should only increment by one level at a time",
|
||||||
"tags": [ "headings" ],
|
"tags": [ "headings" ],
|
||||||
"parser": "markdownit",
|
"parser": "micromark",
|
||||||
"function": function MD001(params, onError) {
|
"function": function MD001(params, onError) {
|
||||||
let prevLevel = 0;
|
let prevLevel = Number.MAX_SAFE_INTEGER;
|
||||||
filterTokens(params, "heading_open", function forToken(token) {
|
const headings = filterByTypes(
|
||||||
const level = Number.parseInt(token.tag.slice(1), 10);
|
params.parsers.micromark.tokens,
|
||||||
if (prevLevel && (level > prevLevel)) {
|
[ "atxHeading", "setextHeading" ]
|
||||||
addErrorDetailIf(onError, token.lineNumber,
|
);
|
||||||
"h" + (prevLevel + 1), "h" + level);
|
for (const heading of headings) {
|
||||||
|
const level = getHeadingLevel(heading);
|
||||||
|
if (level > prevLevel) {
|
||||||
|
addErrorDetailIf(
|
||||||
|
onError,
|
||||||
|
heading.startLine,
|
||||||
|
`h${prevLevel + 1}`,
|
||||||
|
`h${level}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
prevLevel = level;
|
prevLevel = level;
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue