mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Update MD026/no-trailing-punctuation to handle empty punctuation paramater, escape for RegExp (fixes #205).
This commit is contained in:
parent
d336a11665
commit
91b4fcd55f
6 changed files with 39 additions and 13 deletions
15
doc/Rules.md
15
doc/Rules.md
|
@ -792,23 +792,24 @@ Aliases: no-trailing-punctuation
|
||||||
|
|
||||||
Parameters: punctuation (string; default ".,;:!?。,;:!?")
|
Parameters: punctuation (string; default ".,;:!?。,;:!?")
|
||||||
|
|
||||||
This rule is triggered on any heading that has a normal or full-width punctuation
|
This rule is triggered on any heading that has one of the specified normal or
|
||||||
character as the last character in the line:
|
full-width punctuation characters as the last character in the line:
|
||||||
|
|
||||||
```markdown
|
```markdown
|
||||||
# This is a heading.
|
# This is a heading.
|
||||||
```
|
```
|
||||||
|
|
||||||
To fix this, remove any trailing punctuation:
|
To fix this, remove the trailing punctuation:
|
||||||
|
|
||||||
```markdown
|
```markdown
|
||||||
# This is a heading
|
# This is a heading
|
||||||
```
|
```
|
||||||
|
|
||||||
Note: The punctuation parameter can be used to specify what characters class
|
Note: The `punctuation` parameter can be used to specify what characters count
|
||||||
as punctuation at the end of the heading. For example, you can set it to
|
as punctuation at the end of a heading. For example, you can change it to
|
||||||
`".,;:!"` to allow headings with question marks in them, such as might be used
|
`".,;:!"` to allow headings that end with a question mark, such as in an FAQ.
|
||||||
in an FAQ.
|
Setting the `punctuation` parameter to `""` allows all characters - and is
|
||||||
|
equivalent to disabling the rule.
|
||||||
|
|
||||||
<a name="md027"></a>
|
<a name="md027"></a>
|
||||||
|
|
||||||
|
|
14
lib/md026.js
14
lib/md026.js
|
@ -2,17 +2,21 @@
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { addError, allPunctuation, forEachHeading, rangeFromRegExp } =
|
const { addError, allPunctuation, escapeForRegExp, forEachHeading,
|
||||||
require("../helpers");
|
rangeFromRegExp } = require("../helpers");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
"names": [ "MD026", "no-trailing-punctuation" ],
|
"names": [ "MD026", "no-trailing-punctuation" ],
|
||||||
"description": "Trailing punctuation in heading",
|
"description": "Trailing punctuation in heading",
|
||||||
"tags": [ "headings", "headers" ],
|
"tags": [ "headings", "headers" ],
|
||||||
"function": function MD026(params, onError) {
|
"function": function MD026(params, onError) {
|
||||||
const punctuation = params.config.punctuation || allPunctuation;
|
let punctuation = params.config.punctuation;
|
||||||
const trailingPunctuationRe = new RegExp("[" + punctuation + "]$");
|
if (punctuation === undefined) {
|
||||||
forEachHeading(params, function forHeading(heading, content) {
|
punctuation = allPunctuation;
|
||||||
|
}
|
||||||
|
const trailingPunctuationRe =
|
||||||
|
new RegExp("[" + escapeForRegExp(punctuation) + "]$");
|
||||||
|
forEachHeading(params, (heading, content) => {
|
||||||
const match = trailingPunctuationRe.exec(content);
|
const match = trailingPunctuationRe.exec(content);
|
||||||
if (match) {
|
if (match) {
|
||||||
addError(onError, heading.lineNumber,
|
addError(onError, heading.lineNumber,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"default": true,
|
"default": true,
|
||||||
"MD026": {
|
"MD026": {
|
||||||
"punctuation": ".,;:!"
|
"punctuation": ".,;:!]"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,5 +10,7 @@
|
||||||
|
|
||||||
## Heading 6?
|
## Heading 6?
|
||||||
|
|
||||||
|
## Heading 7 {MD026}]
|
||||||
|
|
||||||
The rule has been customized to allow question marks while disallowing
|
The rule has been customized to allow question marks while disallowing
|
||||||
everything else.
|
everything else.
|
||||||
|
|
6
test/heading_trailing_punctuation_empty.json
Normal file
6
test/heading_trailing_punctuation_empty.json
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"default": true,
|
||||||
|
"MD026": {
|
||||||
|
"punctuation": ""
|
||||||
|
}
|
||||||
|
}
|
13
test/heading_trailing_punctuation_empty.md
Normal file
13
test/heading_trailing_punctuation_empty.md
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
# Heading Trailing Punctuation
|
||||||
|
|
||||||
|
## Heading .
|
||||||
|
|
||||||
|
## Heading ,
|
||||||
|
|
||||||
|
## Heading ;
|
||||||
|
|
||||||
|
## Heading :
|
||||||
|
|
||||||
|
## Heading !
|
||||||
|
|
||||||
|
## Heading ?
|
Loading…
Add table
Add a link
Reference in a new issue