mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Reimplement MD023/heading-start-left using micromark tokens.
This commit is contained in:
parent
26a0084515
commit
3b9a7fb2dd
5 changed files with 70 additions and 85 deletions
|
@ -4521,9 +4521,8 @@ module.exports = {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const { addErrorContext, filterTokens } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
const { addErrorContext } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
||||||
|
const { filterByTypes } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
||||||
const spaceBeforeHeadingRe = /^(\s+|[>\s]+\s\s)[^>\s]/;
|
|
||||||
|
|
||||||
// eslint-disable-next-line jsdoc/valid-types
|
// eslint-disable-next-line jsdoc/valid-types
|
||||||
/** @type import("./markdownlint").Rule */
|
/** @type import("./markdownlint").Rule */
|
||||||
|
@ -4531,31 +4530,34 @@ module.exports = {
|
||||||
"names": [ "MD023", "heading-start-left" ],
|
"names": [ "MD023", "heading-start-left" ],
|
||||||
"description": "Headings must start at the beginning of the line",
|
"description": "Headings must start at the beginning of the line",
|
||||||
"tags": [ "headings", "spaces" ],
|
"tags": [ "headings", "spaces" ],
|
||||||
"parser": "markdownit",
|
"parser": "micromark",
|
||||||
"function": function MD023(params, onError) {
|
"function": function MD023(params, onError) {
|
||||||
filterTokens(params, "heading_open", function forToken(token) {
|
const headings = filterByTypes(
|
||||||
const { lineNumber, line } = token;
|
params.parsers.micromark.tokens,
|
||||||
const match = line.match(spaceBeforeHeadingRe);
|
[ "atxHeading", "linePrefix", "setextHeading" ]
|
||||||
if (match) {
|
);
|
||||||
const [ prefixAndFirstChar, prefix ] = match;
|
for (let i = 0; i < headings.length - 1; i++) {
|
||||||
let deleteCount = prefix.length;
|
if (
|
||||||
const prefixLengthNoSpace = prefix.trimEnd().length;
|
(headings[i].type === "linePrefix") &&
|
||||||
if (prefixLengthNoSpace) {
|
(headings[i + 1].type !== "linePrefix") &&
|
||||||
deleteCount -= prefixLengthNoSpace - 1;
|
(headings[i].startLine === headings[i + 1].startLine)
|
||||||
}
|
) {
|
||||||
|
const { endColumn, startColumn, startLine } = headings[i];
|
||||||
|
const length = endColumn - startColumn;
|
||||||
addErrorContext(
|
addErrorContext(
|
||||||
onError,
|
onError,
|
||||||
lineNumber,
|
startLine,
|
||||||
line,
|
params.lines[startLine - 1],
|
||||||
null,
|
true,
|
||||||
null,
|
false,
|
||||||
[ 1, prefixAndFirstChar.length ],
|
[ startColumn, length ],
|
||||||
{
|
{
|
||||||
"editColumn": prefixLengthNoSpace + 1,
|
"editColumn": startColumn,
|
||||||
"deleteCount": deleteCount
|
"deleteCount": length
|
||||||
});
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
48
lib/md023.js
48
lib/md023.js
|
@ -2,9 +2,8 @@
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { addErrorContext, filterTokens } = require("../helpers");
|
const { addErrorContext } = require("../helpers");
|
||||||
|
const { filterByTypes } = require("../helpers/micromark.cjs");
|
||||||
const spaceBeforeHeadingRe = /^(\s+|[>\s]+\s\s)[^>\s]/;
|
|
||||||
|
|
||||||
// eslint-disable-next-line jsdoc/valid-types
|
// eslint-disable-next-line jsdoc/valid-types
|
||||||
/** @type import("./markdownlint").Rule */
|
/** @type import("./markdownlint").Rule */
|
||||||
|
@ -12,30 +11,33 @@ module.exports = {
|
||||||
"names": [ "MD023", "heading-start-left" ],
|
"names": [ "MD023", "heading-start-left" ],
|
||||||
"description": "Headings must start at the beginning of the line",
|
"description": "Headings must start at the beginning of the line",
|
||||||
"tags": [ "headings", "spaces" ],
|
"tags": [ "headings", "spaces" ],
|
||||||
"parser": "markdownit",
|
"parser": "micromark",
|
||||||
"function": function MD023(params, onError) {
|
"function": function MD023(params, onError) {
|
||||||
filterTokens(params, "heading_open", function forToken(token) {
|
const headings = filterByTypes(
|
||||||
const { lineNumber, line } = token;
|
params.parsers.micromark.tokens,
|
||||||
const match = line.match(spaceBeforeHeadingRe);
|
[ "atxHeading", "linePrefix", "setextHeading" ]
|
||||||
if (match) {
|
);
|
||||||
const [ prefixAndFirstChar, prefix ] = match;
|
for (let i = 0; i < headings.length - 1; i++) {
|
||||||
let deleteCount = prefix.length;
|
if (
|
||||||
const prefixLengthNoSpace = prefix.trimEnd().length;
|
(headings[i].type === "linePrefix") &&
|
||||||
if (prefixLengthNoSpace) {
|
(headings[i + 1].type !== "linePrefix") &&
|
||||||
deleteCount -= prefixLengthNoSpace - 1;
|
(headings[i].startLine === headings[i + 1].startLine)
|
||||||
}
|
) {
|
||||||
|
const { endColumn, startColumn, startLine } = headings[i];
|
||||||
|
const length = endColumn - startColumn;
|
||||||
addErrorContext(
|
addErrorContext(
|
||||||
onError,
|
onError,
|
||||||
lineNumber,
|
startLine,
|
||||||
line,
|
params.lines[startLine - 1],
|
||||||
null,
|
true,
|
||||||
null,
|
false,
|
||||||
[ 1, prefixAndFirstChar.length ],
|
[ startColumn, length ],
|
||||||
{
|
{
|
||||||
"editColumn": prefixLengthNoSpace + 1,
|
"editColumn": startColumn,
|
||||||
"deleteCount": deleteCount
|
"deleteCount": length
|
||||||
});
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -16,7 +16,7 @@ Some text
|
||||||
```
|
```
|
||||||
|
|
||||||
* This is another case where MD023 shouldn't be triggered
|
* This is another case where MD023 shouldn't be triggered
|
||||||
# Test {MD022} {MD023} Valid heading for CommonMark (see section 5.2)
|
# Test {MD022} Valid heading for CommonMark (see section 5.2)
|
||||||
# Test {MD022} {MD023} Also valid heading for CommonMark
|
# Test {MD022} {MD023} Also valid heading for CommonMark
|
||||||
|
|
||||||
<!-- markdownlint-configure-file {
|
<!-- markdownlint-configure-file {
|
||||||
|
|
|
@ -623,7 +623,7 @@ Generated by [AVA](https://avajs.dev).
|
||||||
errorDetail: null,
|
errorDetail: null,
|
||||||
errorRange: [
|
errorRange: [
|
||||||
1,
|
1,
|
||||||
2,
|
1,
|
||||||
],
|
],
|
||||||
fixInfo: {
|
fixInfo: {
|
||||||
deleteCount: 1,
|
deleteCount: 1,
|
||||||
|
@ -5602,12 +5602,12 @@ Generated by [AVA](https://avajs.dev).
|
||||||
errorContext: '> ## Quoted indented sub-head...',
|
errorContext: '> ## Quoted indented sub-head...',
|
||||||
errorDetail: null,
|
errorDetail: null,
|
||||||
errorRange: [
|
errorRange: [
|
||||||
|
3,
|
||||||
1,
|
1,
|
||||||
4,
|
|
||||||
],
|
],
|
||||||
fixInfo: {
|
fixInfo: {
|
||||||
deleteCount: 3,
|
deleteCount: 1,
|
||||||
editColumn: 2,
|
editColumn: 3,
|
||||||
},
|
},
|
||||||
lineNumber: 17,
|
lineNumber: 17,
|
||||||
ruleDescription: 'Headings must start at the beginning of the line',
|
ruleDescription: 'Headings must start at the beginning of the line',
|
||||||
|
@ -5621,12 +5621,12 @@ Generated by [AVA](https://avajs.dev).
|
||||||
errorContext: ' > ## Quoted indented sub-he...',
|
errorContext: ' > ## Quoted indented sub-he...',
|
||||||
errorDetail: null,
|
errorDetail: null,
|
||||||
errorRange: [
|
errorRange: [
|
||||||
|
5,
|
||||||
1,
|
1,
|
||||||
6,
|
|
||||||
],
|
],
|
||||||
fixInfo: {
|
fixInfo: {
|
||||||
deleteCount: 3,
|
deleteCount: 1,
|
||||||
editColumn: 4,
|
editColumn: 5,
|
||||||
},
|
},
|
||||||
lineNumber: 33,
|
lineNumber: 33,
|
||||||
ruleDescription: 'Headings must start at the beginning of the line',
|
ruleDescription: 'Headings must start at the beginning of the line',
|
||||||
|
@ -7141,7 +7141,7 @@ Generated by [AVA](https://avajs.dev).
|
||||||
errorDetail: null,
|
errorDetail: null,
|
||||||
errorRange: [
|
errorRange: [
|
||||||
1,
|
1,
|
||||||
2,
|
1,
|
||||||
],
|
],
|
||||||
fixInfo: {
|
fixInfo: {
|
||||||
deleteCount: 1,
|
deleteCount: 1,
|
||||||
|
@ -16650,7 +16650,7 @@ Generated by [AVA](https://avajs.dev).
|
||||||
{
|
{
|
||||||
errors: [
|
errors: [
|
||||||
{
|
{
|
||||||
errorContext: '# Test {MD022} {MD023} Valid heading for CommonMark (see section 5.2)',
|
errorContext: '# Test {MD022} Valid heading for CommonMark (see section 5.2)',
|
||||||
errorDetail: 'Expected: 1; Actual: 0; Above',
|
errorDetail: 'Expected: 1; Actual: 0; Above',
|
||||||
errorRange: null,
|
errorRange: null,
|
||||||
fixInfo: {
|
fixInfo: {
|
||||||
|
@ -16666,7 +16666,7 @@ Generated by [AVA](https://avajs.dev).
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
errorContext: '# Test {MD022} {MD023} Valid heading for CommonMark (see section 5.2)',
|
errorContext: '# Test {MD022} Valid heading for CommonMark (see section 5.2)',
|
||||||
errorDetail: 'Expected: 1; Actual: 0; Below',
|
errorDetail: 'Expected: 1; Actual: 0; Below',
|
||||||
errorRange: null,
|
errorRange: null,
|
||||||
fixInfo: {
|
fixInfo: {
|
||||||
|
@ -16703,7 +16703,7 @@ Generated by [AVA](https://avajs.dev).
|
||||||
errorDetail: null,
|
errorDetail: null,
|
||||||
errorRange: [
|
errorRange: [
|
||||||
1,
|
1,
|
||||||
2,
|
1,
|
||||||
],
|
],
|
||||||
fixInfo: {
|
fixInfo: {
|
||||||
deleteCount: 1,
|
deleteCount: 1,
|
||||||
|
@ -16722,7 +16722,7 @@ Generated by [AVA](https://avajs.dev).
|
||||||
errorDetail: null,
|
errorDetail: null,
|
||||||
errorRange: [
|
errorRange: [
|
||||||
1,
|
1,
|
||||||
2,
|
1,
|
||||||
],
|
],
|
||||||
fixInfo: {
|
fixInfo: {
|
||||||
deleteCount: 1,
|
deleteCount: 1,
|
||||||
|
@ -16741,7 +16741,7 @@ Generated by [AVA](https://avajs.dev).
|
||||||
errorDetail: null,
|
errorDetail: null,
|
||||||
errorRange: [
|
errorRange: [
|
||||||
1,
|
1,
|
||||||
2,
|
1,
|
||||||
],
|
],
|
||||||
fixInfo: {
|
fixInfo: {
|
||||||
deleteCount: 1,
|
deleteCount: 1,
|
||||||
|
@ -16755,35 +16755,16 @@ Generated by [AVA](https://avajs.dev).
|
||||||
'heading-start-left',
|
'heading-start-left',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
|
||||||
errorContext: ' # Test {MD022} {MD023} Valid...',
|
|
||||||
errorDetail: null,
|
|
||||||
errorRange: [
|
|
||||||
1,
|
|
||||||
3,
|
|
||||||
],
|
|
||||||
fixInfo: {
|
|
||||||
deleteCount: 2,
|
|
||||||
editColumn: 1,
|
|
||||||
},
|
|
||||||
lineNumber: 19,
|
|
||||||
ruleDescription: 'Headings must start at the beginning of the line',
|
|
||||||
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md023.md',
|
|
||||||
ruleNames: [
|
|
||||||
'MD023',
|
|
||||||
'heading-start-left',
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
errorContext: ' # Test {MD022} {MD023} Als...',
|
errorContext: ' # Test {MD022} {MD023} Als...',
|
||||||
errorDetail: null,
|
errorDetail: null,
|
||||||
errorRange: [
|
errorRange: [
|
||||||
1,
|
3,
|
||||||
5,
|
2,
|
||||||
],
|
],
|
||||||
fixInfo: {
|
fixInfo: {
|
||||||
deleteCount: 4,
|
deleteCount: 2,
|
||||||
editColumn: 1,
|
editColumn: 3,
|
||||||
},
|
},
|
||||||
lineNumber: 20,
|
lineNumber: 20,
|
||||||
ruleDescription: 'Headings must start at the beginning of the line',
|
ruleDescription: 'Headings must start at the beginning of the line',
|
||||||
|
@ -16813,9 +16794,9 @@ Generated by [AVA](https://avajs.dev).
|
||||||
␊
|
␊
|
||||||
* This is another case where MD023 shouldn't be triggered␊
|
* This is another case where MD023 shouldn't be triggered␊
|
||||||
␊
|
␊
|
||||||
# Test {MD022} {MD023} Valid heading for CommonMark (see section 5.2)␊
|
# Test {MD022} Valid heading for CommonMark (see section 5.2)␊
|
||||||
␊
|
␊
|
||||||
# Test {MD022} {MD023} Also valid heading for CommonMark␊
|
# Test {MD022} {MD023} Also valid heading for CommonMark␊
|
||||||
␊
|
␊
|
||||||
<!-- markdownlint-configure-file {␊
|
<!-- markdownlint-configure-file {␊
|
||||||
"heading-style": false,␊
|
"heading-style": false,␊
|
||||||
|
|
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue