markdownlint/lib/md026.js

41 lines
1.2 KiB
JavaScript

// @ts-check
"use strict";
const { addError, allPunctuationNoQuestion, escapeForRegExp, forEachHeading } =
require("../helpers");
module.exports = {
"names": [ "MD026", "no-trailing-punctuation" ],
"description": "Trailing punctuation in heading",
"tags": [ "headings", "headers" ],
"function": function MD026(params, onError) {
let punctuation = params.config.punctuation;
punctuation = String(
(punctuation === undefined) ? allPunctuationNoQuestion : punctuation
);
const trailingPunctuationRe =
new RegExp("\\s*[" + escapeForRegExp(punctuation) + "]+$");
forEachHeading(params, (heading) => {
const { line, lineNumber } = heading;
const trimmedLine = line.replace(/[\s#]*$/, "");
const match = trailingPunctuationRe.exec(trimmedLine);
if (match) {
const fullMatch = match[0];
const column = match.index + 1;
const length = fullMatch.length;
addError(
onError,
lineNumber,
`Punctuation: '${fullMatch}'`,
null,
[ column, length ],
{
"editColumn": column,
"deleteCount": length
}
);
}
});
}
};