From 91b4fcd55f5f930e10e98dec93f30651614365ca Mon Sep 17 00:00:00 2001 From: David Anson Date: Fri, 26 Jul 2019 23:03:56 -0700 Subject: [PATCH] Update MD026/no-trailing-punctuation to handle empty punctuation paramater, escape for RegExp (fixes #205). --- doc/Rules.md | 15 ++++++++------- lib/md026.js | 14 +++++++++----- test/heading_trailing_punctuation_customized.json | 2 +- test/heading_trailing_punctuation_customized.md | 2 ++ test/heading_trailing_punctuation_empty.json | 6 ++++++ test/heading_trailing_punctuation_empty.md | 13 +++++++++++++ 6 files changed, 39 insertions(+), 13 deletions(-) create mode 100644 test/heading_trailing_punctuation_empty.json create mode 100644 test/heading_trailing_punctuation_empty.md diff --git a/doc/Rules.md b/doc/Rules.md index 01fd7452..4a86c1a7 100644 --- a/doc/Rules.md +++ b/doc/Rules.md @@ -792,23 +792,24 @@ Aliases: no-trailing-punctuation Parameters: punctuation (string; default ".,;:!?。,;:!?") -This rule is triggered on any heading that has a normal or full-width punctuation -character as the last character in the line: +This rule is triggered on any heading that has one of the specified normal or +full-width punctuation characters as the last character in the line: ```markdown # This is a heading. ``` -To fix this, remove any trailing punctuation: +To fix this, remove the trailing punctuation: ```markdown # This is a heading ``` -Note: The punctuation parameter can be used to specify what characters class -as punctuation at the end of the heading. For example, you can set it to -`".,;:!"` to allow headings with question marks in them, such as might be used -in an FAQ. +Note: The `punctuation` parameter can be used to specify what characters count +as punctuation at the end of a heading. For example, you can change it to +`".,;:!"` to allow headings that end with a question mark, such as in an FAQ. +Setting the `punctuation` parameter to `""` allows all characters - and is +equivalent to disabling the rule. diff --git a/lib/md026.js b/lib/md026.js index cc7a93ee..88881255 100644 --- a/lib/md026.js +++ b/lib/md026.js @@ -2,17 +2,21 @@ "use strict"; -const { addError, allPunctuation, forEachHeading, rangeFromRegExp } = - require("../helpers"); +const { addError, allPunctuation, escapeForRegExp, forEachHeading, + rangeFromRegExp } = require("../helpers"); module.exports = { "names": [ "MD026", "no-trailing-punctuation" ], "description": "Trailing punctuation in heading", "tags": [ "headings", "headers" ], "function": function MD026(params, onError) { - const punctuation = params.config.punctuation || allPunctuation; - const trailingPunctuationRe = new RegExp("[" + punctuation + "]$"); - forEachHeading(params, function forHeading(heading, content) { + let punctuation = params.config.punctuation; + if (punctuation === undefined) { + punctuation = allPunctuation; + } + const trailingPunctuationRe = + new RegExp("[" + escapeForRegExp(punctuation) + "]$"); + forEachHeading(params, (heading, content) => { const match = trailingPunctuationRe.exec(content); if (match) { addError(onError, heading.lineNumber, diff --git a/test/heading_trailing_punctuation_customized.json b/test/heading_trailing_punctuation_customized.json index df827f91..91ca4506 100644 --- a/test/heading_trailing_punctuation_customized.json +++ b/test/heading_trailing_punctuation_customized.json @@ -1,6 +1,6 @@ { "default": true, "MD026": { - "punctuation": ".,;:!" + "punctuation": ".,;:!]" } } diff --git a/test/heading_trailing_punctuation_customized.md b/test/heading_trailing_punctuation_customized.md index e44f5482..69679422 100644 --- a/test/heading_trailing_punctuation_customized.md +++ b/test/heading_trailing_punctuation_customized.md @@ -10,5 +10,7 @@ ## Heading 6? +## Heading 7 {MD026}] + The rule has been customized to allow question marks while disallowing everything else. diff --git a/test/heading_trailing_punctuation_empty.json b/test/heading_trailing_punctuation_empty.json new file mode 100644 index 00000000..84f341a2 --- /dev/null +++ b/test/heading_trailing_punctuation_empty.json @@ -0,0 +1,6 @@ +{ + "default": true, + "MD026": { + "punctuation": "" + } +} diff --git a/test/heading_trailing_punctuation_empty.md b/test/heading_trailing_punctuation_empty.md new file mode 100644 index 00000000..ab7d7090 --- /dev/null +++ b/test/heading_trailing_punctuation_empty.md @@ -0,0 +1,13 @@ +# Heading Trailing Punctuation + +## Heading . + +## Heading , + +## Heading ; + +## Heading : + +## Heading ! + +## Heading ?