2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
2024-11-28 20:36:44 -08:00
|
|
|
import { addError, allPunctuationNoQuestion, endOfLineGemojiCodeRe,
|
|
|
|
endOfLineHtmlEntityRe, escapeForRegExp } from "../helpers/helpers.cjs";
|
|
|
|
import { filterByTypesCached } from "./cache.mjs";
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2024-12-03 19:58:28 -08:00
|
|
|
/** @type {import("markdownlint").Rule} */
|
2024-11-28 20:36:44 -08:00
|
|
|
export default {
|
2018-01-21 21:44:25 -08:00
|
|
|
"names": [ "MD026", "no-trailing-punctuation" ],
|
2018-03-19 23:39:42 +01:00
|
|
|
"description": "Trailing punctuation in heading",
|
2023-11-09 20:05:30 -08:00
|
|
|
"tags": [ "headings" ],
|
2024-03-09 16:17:50 -08:00
|
|
|
"parser": "micromark",
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD026(params, onError) {
|
2019-07-26 23:03:56 -07:00
|
|
|
let punctuation = params.config.punctuation;
|
2020-11-17 20:32:17 -08:00
|
|
|
punctuation = String(
|
|
|
|
(punctuation === undefined) ? allPunctuationNoQuestion : punctuation
|
|
|
|
);
|
2019-07-26 23:03:56 -07:00
|
|
|
const trailingPunctuationRe =
|
2019-09-11 22:59:42 -07:00
|
|
|
new RegExp("\\s*[" + escapeForRegExp(punctuation) + "]+$");
|
2024-08-24 22:05:16 -07:00
|
|
|
const headings = filterByTypesCached([ "atxHeadingText", "setextHeadingText" ]);
|
2023-06-24 15:45:51 -07:00
|
|
|
for (const heading of headings) {
|
2023-12-31 21:51:34 -08:00
|
|
|
const { endColumn, endLine, text } = heading;
|
2023-06-24 15:45:51 -07:00
|
|
|
const match = trailingPunctuationRe.exec(text);
|
|
|
|
if (
|
|
|
|
match &&
|
|
|
|
!endOfLineHtmlEntityRe.test(text) &&
|
|
|
|
!endOfLineGemojiCodeRe.test(text)
|
|
|
|
) {
|
2019-09-11 22:59:42 -07:00
|
|
|
const fullMatch = match[0];
|
|
|
|
const length = fullMatch.length;
|
2023-12-31 21:51:34 -08:00
|
|
|
const column = endColumn - length;
|
2019-09-11 22:59:42 -07:00
|
|
|
addError(
|
|
|
|
onError,
|
2023-06-24 15:45:51 -07:00
|
|
|
endLine,
|
2019-09-11 22:59:42 -07:00
|
|
|
`Punctuation: '${fullMatch}'`,
|
2023-06-24 15:45:51 -07:00
|
|
|
undefined,
|
2019-09-11 22:59:42 -07:00
|
|
|
[ column, length ],
|
|
|
|
{
|
|
|
|
"editColumn": column,
|
|
|
|
"deleteCount": length
|
|
|
|
}
|
|
|
|
);
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2023-06-24 15:45:51 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|