mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 22:40:13 +01:00
Reimplement MD026/no-trailing-punctuation using micromark tokens, ignore trailing GitHub emoji codes, improve tests (fixes #457).
This commit is contained in:
parent
272c15ed39
commit
c06506c317
9 changed files with 157 additions and 32 deletions
31
lib/md026.js
31
lib/md026.js
|
|
@ -2,10 +2,10 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const { addError, allPunctuationNoQuestion, escapeForRegExp, forEachHeading } =
|
||||
require("../helpers");
|
||||
const { addError, allPunctuationNoQuestion, endOfLineGemojiCodeRe,
|
||||
endOfLineHtmlEntityRe, escapeForRegExp } = require("../helpers");
|
||||
const { filterByTypes } = require("../helpers/micromark.cjs");
|
||||
|
||||
const endOfLineHtmlEntityRe = /&#?[\da-zA-Z]+;$/;
|
||||
|
||||
module.exports = {
|
||||
"names": [ "MD026", "no-trailing-punctuation" ],
|
||||
|
|
@ -18,19 +18,26 @@ module.exports = {
|
|||
);
|
||||
const trailingPunctuationRe =
|
||||
new RegExp("\\s*[" + escapeForRegExp(punctuation) + "]+$");
|
||||
forEachHeading(params, (heading) => {
|
||||
const { line, lineNumber } = heading;
|
||||
const trimmedLine = line.replace(/([^\s#])[\s#]+$/, "$1");
|
||||
const match = trailingPunctuationRe.exec(trimmedLine);
|
||||
if (match && !endOfLineHtmlEntityRe.test(trimmedLine)) {
|
||||
const headings = filterByTypes(
|
||||
params.parsers.micromark.tokens,
|
||||
[ "atxHeadingText", "setextHeadingText" ]
|
||||
);
|
||||
for (const heading of headings) {
|
||||
const { endLine, startColumn, text } = heading;
|
||||
const match = trailingPunctuationRe.exec(text);
|
||||
if (
|
||||
match &&
|
||||
!endOfLineHtmlEntityRe.test(text) &&
|
||||
!endOfLineGemojiCodeRe.test(text)
|
||||
) {
|
||||
const fullMatch = match[0];
|
||||
const column = match.index + 1;
|
||||
const column = startColumn + match.index;
|
||||
const length = fullMatch.length;
|
||||
addError(
|
||||
onError,
|
||||
lineNumber,
|
||||
endLine,
|
||||
`Punctuation: '${fullMatch}'`,
|
||||
null,
|
||||
undefined,
|
||||
[ column, length ],
|
||||
{
|
||||
"editColumn": column,
|
||||
|
|
@ -38,6 +45,6 @@ module.exports = {
|
|||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue