Skip to content

Commit

Permalink
Reimplement MD023/heading-start-left using micromark tokens.
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Jun 18, 2024
1 parent 26a0084 commit 3b9a7fb
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 84 deletions.
48 changes: 25 additions & 23 deletions demo/markdownlint-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4521,41 +4521,43 @@ module.exports = {



const { addErrorContext, filterTokens } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");

const spaceBeforeHeadingRe = /^(\s+|[>\s]+\s\s)[^>\s]/;
const { addErrorContext } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { filterByTypes } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");

// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */
module.exports = {
"names": [ "MD023", "heading-start-left" ],
"description": "Headings must start at the beginning of the line",
"tags": [ "headings", "spaces" ],
"parser": "markdownit",
"parser": "micromark",
"function": function MD023(params, onError) {
filterTokens(params, "heading_open", function forToken(token) {
const { lineNumber, line } = token;
const match = line.match(spaceBeforeHeadingRe);
if (match) {
const [ prefixAndFirstChar, prefix ] = match;
let deleteCount = prefix.length;
const prefixLengthNoSpace = prefix.trimEnd().length;
if (prefixLengthNoSpace) {
deleteCount -= prefixLengthNoSpace - 1;
}
const headings = filterByTypes(
params.parsers.micromark.tokens,
[ "atxHeading", "linePrefix", "setextHeading" ]
);
for (let i = 0; i < headings.length - 1; i++) {
if (
(headings[i].type === "linePrefix") &&
(headings[i + 1].type !== "linePrefix") &&
(headings[i].startLine === headings[i + 1].startLine)
) {
const { endColumn, startColumn, startLine } = headings[i];
const length = endColumn - startColumn;
addErrorContext(
onError,
lineNumber,
line,
null,
null,
[ 1, prefixAndFirstChar.length ],
startLine,
params.lines[startLine - 1],
true,
false,
[ startColumn, length ],
{
"editColumn": prefixLengthNoSpace + 1,
"deleteCount": deleteCount
});
"editColumn": startColumn,
"deleteCount": length
}
);
}
});
}
}
};

Expand Down
48 changes: 25 additions & 23 deletions lib/md023.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,42 @@

"use strict";

const { addErrorContext, filterTokens } = require("../helpers");

const spaceBeforeHeadingRe = /^(\s+|[>\s]+\s\s)[^>\s]/;
const { addErrorContext } = require("../helpers");
const { filterByTypes } = require("../helpers/micromark.cjs");

// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */
module.exports = {
"names": [ "MD023", "heading-start-left" ],
"description": "Headings must start at the beginning of the line",
"tags": [ "headings", "spaces" ],
"parser": "markdownit",
"parser": "micromark",
"function": function MD023(params, onError) {
filterTokens(params, "heading_open", function forToken(token) {
const { lineNumber, line } = token;
const match = line.match(spaceBeforeHeadingRe);
if (match) {
const [ prefixAndFirstChar, prefix ] = match;
let deleteCount = prefix.length;
const prefixLengthNoSpace = prefix.trimEnd().length;
if (prefixLengthNoSpace) {
deleteCount -= prefixLengthNoSpace - 1;
}
const headings = filterByTypes(
params.parsers.micromark.tokens,
[ "atxHeading", "linePrefix", "setextHeading" ]
);
for (let i = 0; i < headings.length - 1; i++) {
if (
(headings[i].type === "linePrefix") &&
(headings[i + 1].type !== "linePrefix") &&
(headings[i].startLine === headings[i + 1].startLine)
) {
const { endColumn, startColumn, startLine } = headings[i];
const length = endColumn - startColumn;
addErrorContext(
onError,
lineNumber,
line,
null,
null,
[ 1, prefixAndFirstChar.length ],
startLine,
params.lines[startLine - 1],
true,
false,
[ startColumn, length ],
{
"editColumn": prefixLengthNoSpace + 1,
"deleteCount": deleteCount
});
"editColumn": startColumn,
"deleteCount": length
}
);
}
});
}
}
};
2 changes: 1 addition & 1 deletion test/headings_with_spaces_at_the_beginning.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Some text
```

* 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

<!-- markdownlint-configure-file {
Expand Down
55 changes: 18 additions & 37 deletions test/snapshots/markdownlint-test-scenarios.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ Generated by [AVA](https://avajs.dev).
errorDetail: null,
errorRange: [
1,
2,
1,
],
fixInfo: {
deleteCount: 1,
Expand Down Expand Up @@ -5602,12 +5602,12 @@ Generated by [AVA](https://avajs.dev).
errorContext: '> ## Quoted indented sub-head...',
errorDetail: null,
errorRange: [
3,
1,
4,
],
fixInfo: {
deleteCount: 3,
editColumn: 2,
deleteCount: 1,
editColumn: 3,
},
lineNumber: 17,
ruleDescription: 'Headings must start at the beginning of the line',
Expand All @@ -5621,12 +5621,12 @@ Generated by [AVA](https://avajs.dev).
errorContext: ' > ## Quoted indented sub-he...',
errorDetail: null,
errorRange: [
5,
1,
6,
],
fixInfo: {
deleteCount: 3,
editColumn: 4,
deleteCount: 1,
editColumn: 5,
},
lineNumber: 33,
ruleDescription: 'Headings must start at the beginning of the line',
Expand Down Expand Up @@ -7141,7 +7141,7 @@ Generated by [AVA](https://avajs.dev).
errorDetail: null,
errorRange: [
1,
2,
1,
],
fixInfo: {
deleteCount: 1,
Expand Down Expand Up @@ -16650,7 +16650,7 @@ Generated by [AVA](https://avajs.dev).
{
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',
errorRange: null,
fixInfo: {
Expand All @@ -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',
errorRange: null,
fixInfo: {
Expand Down Expand Up @@ -16703,7 +16703,7 @@ Generated by [AVA](https://avajs.dev).
errorDetail: null,
errorRange: [
1,
2,
1,
],
fixInfo: {
deleteCount: 1,
Expand All @@ -16722,7 +16722,7 @@ Generated by [AVA](https://avajs.dev).
errorDetail: null,
errorRange: [
1,
2,
1,
],
fixInfo: {
deleteCount: 1,
Expand All @@ -16741,7 +16741,7 @@ Generated by [AVA](https://avajs.dev).
errorDetail: null,
errorRange: [
1,
2,
1,
],
fixInfo: {
deleteCount: 1,
Expand All @@ -16756,34 +16756,15 @@ Generated by [AVA](https://avajs.dev).
],
},
{
errorContext: ' # Test {MD022} {MD023} Valid...',
errorContext: ' # Test {MD022} {MD023} Als...',
errorDetail: null,
errorRange: [
1,
3,
2,
],
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...',
errorDetail: null,
errorRange: [
1,
5,
],
fixInfo: {
deleteCount: 4,
editColumn: 1,
editColumn: 3,
},
lineNumber: 20,
ruleDescription: 'Headings must start at the beginning of the line',
Expand Down Expand Up @@ -16813,9 +16794,9 @@ Generated by [AVA](https://avajs.dev).
* 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 {␊
"heading-style": false,␊
Expand Down
Binary file modified test/snapshots/markdownlint-test-scenarios.js.snap
Binary file not shown.

0 comments on commit 3b9a7fb

Please sign in to comment.