2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2019-09-02 15:35:43 -07:00
|
|
|
const { addErrorContext, filterTokens } = require("../helpers");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2022-12-19 21:36:24 -08:00
|
|
|
const spaceBeforeHeadingRe = /^(\s+|[>\s]+\s\s)[^>\s]/;
|
2018-01-21 21:44:25 -08:00
|
|
|
|
|
|
|
module.exports = {
|
2023-11-09 20:05:30 -08:00
|
|
|
"names": [ "MD023", "heading-start-left" ],
|
2018-03-19 23:39:42 +01:00
|
|
|
"description": "Headings must start at the beginning of the line",
|
2023-11-09 20:05:30 -08:00
|
|
|
"tags": [ "headings", "spaces" ],
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD023(params, onError) {
|
2019-04-13 11:18:57 -07:00
|
|
|
filterTokens(params, "heading_open", function forToken(token) {
|
2019-09-02 15:35:43 -07:00
|
|
|
const { lineNumber, line } = token;
|
|
|
|
const match = line.match(spaceBeforeHeadingRe);
|
|
|
|
if (match) {
|
|
|
|
const [ prefixAndFirstChar, prefix ] = match;
|
|
|
|
let deleteCount = prefix.length;
|
2021-02-06 19:55:22 -08:00
|
|
|
const prefixLengthNoSpace = prefix.trimEnd().length;
|
2019-09-02 15:35:43 -07:00
|
|
|
if (prefixLengthNoSpace) {
|
|
|
|
deleteCount -= prefixLengthNoSpace - 1;
|
|
|
|
}
|
|
|
|
addErrorContext(
|
|
|
|
onError,
|
|
|
|
lineNumber,
|
|
|
|
line,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
[ 1, prefixAndFirstChar.length ],
|
|
|
|
{
|
|
|
|
"editColumn": prefixLengthNoSpace + 1,
|
|
|
|
"deleteCount": deleteCount
|
|
|
|
});
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|