Add full-width punctuation characters to MD026/no-trailing-punctuation and MD036/no-emphasis-as-heading (fixes #192).

This commit is contained in:
David Anson 2019-06-06 22:21:31 -07:00
parent fa40084994
commit 1d8b9e7e62
8 changed files with 45 additions and 25 deletions

View file

@ -788,10 +788,10 @@ Tags: headings, headers
Aliases: no-trailing-punctuation Aliases: no-trailing-punctuation
Parameters: punctuation (string; default ".,;:!?") Parameters: punctuation (string; default ".,;:!?。,;:!?")
This rule is triggered on any heading that has a punctuation character as the This rule is triggered on any heading that has a normal or full-width punctuation
last character in the line: character as the last character in the line:
```markdown ```markdown
# This is a heading. # This is a heading.
@ -1207,7 +1207,7 @@ Tags: headings, headers, emphasis
Aliases: no-emphasis-as-heading, no-emphasis-as-header Aliases: no-emphasis-as-heading, no-emphasis-as-header
Parameters: punctuation (string; default ".,;:!?") Parameters: punctuation (string; default ".,;:!?。,;:!?")
This check looks for instances where emphasized (i.e. bold or italic) text is This check looks for instances where emphasized (i.e. bold or italic) text is
used to separate sections, where a heading should be used instead: used to separate sections, where a heading should be used instead:
@ -1235,11 +1235,11 @@ Lorem ipsum dolor sit amet...
Consectetur adipiscing elit, sed do eiusmod. Consectetur adipiscing elit, sed do eiusmod.
``` ```
Note: this rule looks for single line paragraphs that consist entirely of Note: This rule looks for single line paragraphs that consist entirely
emphasized text. It won't fire on emphasis used within regular text, of emphasized text. It won't fire on emphasis used within regular text,
multi-line emphasized paragraphs, and paragraphs ending in punctuation. multi-line emphasized paragraphs, or paragraphs ending in punctuation
Similarly to rule MD026, you can configure what characters are recognized as (normal or full-width). Similarly to rule MD026, you can configure what
punctuation. characters are recognized as punctuation.
<a name="md037"></a> <a name="md037"></a>

View file

@ -24,6 +24,9 @@ module.exports.orderedListItemMarkerRe = /^[\s>]*0*(\d+)[.)]/;
// readFile options for reading with the UTF-8 encoding // readFile options for reading with the UTF-8 encoding
module.exports.utf8Encoding = { "encoding": "utf8" }; module.exports.utf8Encoding = { "encoding": "utf8" };
// All punctuation characters (normal and full-width)
module.exports.allPunctuation = ".,;:!?。,;:!?";
// Returns true iff the input is a number // Returns true iff the input is a number
module.exports.isNumber = function isNumber(obj) { module.exports.isNumber = function isNumber(obj) {
return typeof obj === "number"; return typeof obj === "number";

View file

@ -2,14 +2,15 @@
"use strict"; "use strict";
const { addError, forEachHeading, rangeFromRegExp } = require("../helpers"); const { addError, allPunctuation, forEachHeading, 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 || ".,;:!?"; const punctuation = params.config.punctuation || allPunctuation;
const trailingPunctuationRe = new RegExp("[" + punctuation + "]$"); const trailingPunctuationRe = new RegExp("[" + punctuation + "]$");
forEachHeading(params, function forHeading(heading, content) { forEachHeading(params, function forHeading(heading, content) {
const match = trailingPunctuationRe.exec(content); const match = trailingPunctuationRe.exec(content);

View file

@ -2,14 +2,14 @@
"use strict"; "use strict";
const { addErrorContext } = require("../helpers"); const { addErrorContext, allPunctuation } = require("../helpers");
module.exports = { module.exports = {
"names": [ "MD036", "no-emphasis-as-heading", "no-emphasis-as-header" ], "names": [ "MD036", "no-emphasis-as-heading", "no-emphasis-as-header" ],
"description": "Emphasis used instead of a heading", "description": "Emphasis used instead of a heading",
"tags": [ "headings", "headers", "emphasis" ], "tags": [ "headings", "headers", "emphasis" ],
"function": function MD036(params, onError) { "function": function MD036(params, onError) {
const punctuation = params.config.punctuation || ".,;:!?"; const punctuation = params.config.punctuation || allPunctuation;
const re = new RegExp("[" + punctuation + "]$"); const re = new RegExp("[" + punctuation + "]$");
function base(token) { function base(token) {
if (token.type === "paragraph_open") { if (token.type === "paragraph_open") {

View file

@ -190,7 +190,7 @@ rules.forEach(function forRule(rule) {
"punctuation": { "punctuation": {
"description": "Punctuation characters", "description": "Punctuation characters",
"type": "string", "type": "string",
"default": ".,;:!?" "default": ".,;:!?。,;:!?"
} }
}; };
break; break;

View file

@ -708,7 +708,7 @@
"punctuation": { "punctuation": {
"description": "Punctuation characters", "description": "Punctuation characters",
"type": "string", "type": "string",
"default": ".,;:!?" "default": ".,;:!?。,;:!?"
} }
}, },
"additionalProperties": false "additionalProperties": false
@ -724,7 +724,7 @@
"punctuation": { "punctuation": {
"description": "Punctuation characters", "description": "Punctuation characters",
"type": "string", "type": "string",
"default": ".,;:!?" "default": ".,;:!?。,;:!?"
} }
}, },
"additionalProperties": false "additionalProperties": false
@ -966,7 +966,7 @@
"punctuation": { "punctuation": {
"description": "Punctuation characters", "description": "Punctuation characters",
"type": "string", "type": "string",
"default": ".,;:!?" "default": ".,;:!?。,;:!?"
} }
}, },
"additionalProperties": false "additionalProperties": false
@ -982,7 +982,7 @@
"punctuation": { "punctuation": {
"description": "Punctuation characters", "description": "Punctuation characters",
"type": "string", "type": "string",
"default": ".,;:!?" "default": ".,;:!?。,;:!?"
} }
}, },
"additionalProperties": false "additionalProperties": false
@ -998,7 +998,7 @@
"punctuation": { "punctuation": {
"description": "Punctuation characters", "description": "Punctuation characters",
"type": "string", "type": "string",
"default": ".,;:!?" "default": ".,;:!?。,;:!?"
} }
}, },
"additionalProperties": false "additionalProperties": false

View file

@ -39,4 +39,6 @@ detected as a heading because it's on multiple lines**
**This also shouldn't be detected as a heading as it ends in punctuation.** **This also shouldn't be detected as a heading as it ends in punctuation.**
**This shouldn't be detected as a heading as it ends in full-width punctuation。**
**[This as well since it is a link](https://example.com)** **[This as well since it is a link](https://example.com)**

View file

@ -1,11 +1,25 @@
# Heading 1 {MD026}. # Heading Trailing Punctuation
## Heading 2 {MD026}, ## Heading {MD026} .
## Heading 3 {MD026}! ## Heading {MD026} ,
## Heading 4 {MD026}: ## Heading {MD026} ;
## Heading 5 {MD026}; ## Heading {MD026} :
## Heading 6 {MD026}? ## Heading {MD026} !
## Heading {MD026} ?
## Heading/Full-Width {MD026} 。
## Heading/Full-Width {MD026}
## Heading/Full-Width {MD026}
## Heading/Full-Width {MD026}
## Heading/Full-Width {MD026}
## Heading/Full-Width {MD026}