From 1d8b9e7e62837ffcc187ab36236a3d3a7081d4e1 Mon Sep 17 00:00:00 2001 From: David Anson Date: Thu, 6 Jun 2019 22:21:31 -0700 Subject: [PATCH] Add full-width punctuation characters to MD026/no-trailing-punctuation and MD036/no-emphasis-as-heading (fixes #192). --- doc/Rules.md | 18 +++++++++--------- helpers/helpers.js | 3 +++ lib/md026.js | 5 +++-- lib/md036.js | 4 ++-- schema/build-config-schema.js | 2 +- schema/markdownlint-config-schema.json | 10 +++++----- test/emphasis_instead_of_headings.md | 2 ++ test/heading_trailing_punctuation.md | 26 ++++++++++++++++++++------ 8 files changed, 45 insertions(+), 25 deletions(-) diff --git a/doc/Rules.md b/doc/Rules.md index a3ed4a7a..7d28564f 100644 --- a/doc/Rules.md +++ b/doc/Rules.md @@ -788,10 +788,10 @@ Tags: headings, headers 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 -last character in the line: +This rule is triggered on any heading that has a normal or full-width punctuation +character as the last character in the line: ```markdown # This is a heading. @@ -1207,7 +1207,7 @@ Tags: headings, headers, emphasis 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 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. ``` -Note: this rule looks for single line paragraphs that consist entirely of -emphasized text. It won't fire on emphasis used within regular text, -multi-line emphasized paragraphs, and paragraphs ending in punctuation. -Similarly to rule MD026, you can configure what characters are recognized as -punctuation. +Note: This rule looks for single line paragraphs that consist entirely +of emphasized text. It won't fire on emphasis used within regular text, +multi-line emphasized paragraphs, or paragraphs ending in punctuation +(normal or full-width). Similarly to rule MD026, you can configure what +characters are recognized as punctuation. diff --git a/helpers/helpers.js b/helpers/helpers.js index d4a02bf1..8f21e800 100644 --- a/helpers/helpers.js +++ b/helpers/helpers.js @@ -24,6 +24,9 @@ module.exports.orderedListItemMarkerRe = /^[\s>]*0*(\d+)[.)]/; // readFile options for reading with the UTF-8 encoding module.exports.utf8Encoding = { "encoding": "utf8" }; +// All punctuation characters (normal and full-width) +module.exports.allPunctuation = ".,;:!?。,;:!?"; + // Returns true iff the input is a number module.exports.isNumber = function isNumber(obj) { return typeof obj === "number"; diff --git a/lib/md026.js b/lib/md026.js index 9469def0..cc7a93ee 100644 --- a/lib/md026.js +++ b/lib/md026.js @@ -2,14 +2,15 @@ "use strict"; -const { addError, forEachHeading, rangeFromRegExp } = require("../helpers"); +const { addError, allPunctuation, 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 || ".,;:!?"; + const punctuation = params.config.punctuation || allPunctuation; const trailingPunctuationRe = new RegExp("[" + punctuation + "]$"); forEachHeading(params, function forHeading(heading, content) { const match = trailingPunctuationRe.exec(content); diff --git a/lib/md036.js b/lib/md036.js index 44ab0944..cea7b1ac 100644 --- a/lib/md036.js +++ b/lib/md036.js @@ -2,14 +2,14 @@ "use strict"; -const { addErrorContext } = require("../helpers"); +const { addErrorContext, allPunctuation } = require("../helpers"); module.exports = { "names": [ "MD036", "no-emphasis-as-heading", "no-emphasis-as-header" ], "description": "Emphasis used instead of a heading", "tags": [ "headings", "headers", "emphasis" ], "function": function MD036(params, onError) { - const punctuation = params.config.punctuation || ".,;:!?"; + const punctuation = params.config.punctuation || allPunctuation; const re = new RegExp("[" + punctuation + "]$"); function base(token) { if (token.type === "paragraph_open") { diff --git a/schema/build-config-schema.js b/schema/build-config-schema.js index ecf13ad2..217d43f2 100644 --- a/schema/build-config-schema.js +++ b/schema/build-config-schema.js @@ -190,7 +190,7 @@ rules.forEach(function forRule(rule) { "punctuation": { "description": "Punctuation characters", "type": "string", - "default": ".,;:!?" + "default": ".,;:!?。,;:!?" } }; break; diff --git a/schema/markdownlint-config-schema.json b/schema/markdownlint-config-schema.json index 46485e66..a0df8653 100644 --- a/schema/markdownlint-config-schema.json +++ b/schema/markdownlint-config-schema.json @@ -708,7 +708,7 @@ "punctuation": { "description": "Punctuation characters", "type": "string", - "default": ".,;:!?" + "default": ".,;:!?。,;:!?" } }, "additionalProperties": false @@ -724,7 +724,7 @@ "punctuation": { "description": "Punctuation characters", "type": "string", - "default": ".,;:!?" + "default": ".,;:!?。,;:!?" } }, "additionalProperties": false @@ -966,7 +966,7 @@ "punctuation": { "description": "Punctuation characters", "type": "string", - "default": ".,;:!?" + "default": ".,;:!?。,;:!?" } }, "additionalProperties": false @@ -982,7 +982,7 @@ "punctuation": { "description": "Punctuation characters", "type": "string", - "default": ".,;:!?" + "default": ".,;:!?。,;:!?" } }, "additionalProperties": false @@ -998,7 +998,7 @@ "punctuation": { "description": "Punctuation characters", "type": "string", - "default": ".,;:!?" + "default": ".,;:!?。,;:!?" } }, "additionalProperties": false diff --git a/test/emphasis_instead_of_headings.md b/test/emphasis_instead_of_headings.md index 5bda62b8..2af778f5 100644 --- a/test/emphasis_instead_of_headings.md +++ b/test/emphasis_instead_of_headings.md @@ -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 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)** diff --git a/test/heading_trailing_punctuation.md b/test/heading_trailing_punctuation.md index 3aedc267..0286b5b2 100644 --- a/test/heading_trailing_punctuation.md +++ b/test/heading_trailing_punctuation.md @@ -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} ?