Add MD050/strong-style (fixes #150).

This commit is contained in:
Sébastien Règne 2021-10-21 06:42:48 +02:00 committed by GitHub
parent 6294ad3ef0
commit ab9e5875a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 255 additions and 19 deletions

View file

@ -4,3 +4,4 @@ demo/markdownlint-browser.min.js
demo/markdownlint-rule-helpers-browser.js
demo/markdownlint-rule-helpers-browser.min.js
example/typescript/type-check.js
test-repos/

View file

@ -102,6 +102,7 @@ playground for learning and exploring.
* **[MD046](doc/Rules.md#md046)** *code-block-style* - Code block style
* **[MD047](doc/Rules.md#md047)** *single-trailing-newline* - Files should end with a single newline character
* **[MD048](doc/Rules.md#md048)** *code-fence-style* - Code fence style
* **[MD050](doc/Rules.md#md050)** *strong-style* - Strong style should be consistent
<!-- markdownlint-restore -->
@ -125,7 +126,7 @@ rules at once.
* **blockquote** - MD027, MD028
* **bullet** - MD004, MD005, MD006, MD007, MD032
* **code** - MD014, MD031, MD038, MD040, MD046, MD048
* **emphasis** - MD036, MD037
* **emphasis** - MD036, MD037, MD050
* **hard_tab** - MD010
* **headers** - MD001, MD002, MD003, MD018, MD019, MD020, MD021, MD022,
MD023, MD024, MD025, MD026, MD036, MD041, MD043

View file

@ -190,6 +190,21 @@ module.exports.fencedCodeBlockStyleFor =
return "backtick";
}
};
/**
* Return the string representation of a emphasis or strong markup character.
*
* @param {string} markup Emphasis or strong string.
* @returns {string} String representation.
*/
module.exports.emphasisOrStrongStyleFor =
function emphasisOrStrongStyleFor(markup) {
switch (markup[0]) {
case "*":
return "asterisk";
default:
return "underscore";
}
};
/**
* Return the number of characters of indent for a token.
*
@ -4024,6 +4039,36 @@ module.exports = {
};
/***/ }),
/***/ "../lib/md050.js":
/*!***********************!*\
!*** ../lib/md050.js ***!
\***********************/
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
"use strict";
// @ts-check
var _a = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addErrorDetailIf = _a.addErrorDetailIf, emphasisOrStrongStyleFor = _a.emphasisOrStrongStyleFor, forEachInlineChild = _a.forEachInlineChild;
module.exports = {
"names": ["MD050", "strong-style"],
"description": "Strong style should be consistent",
"tags": ["emphasis"],
"function": function MD050(params, onError) {
var expectedStyle = String(params.config.style || "consistent");
forEachInlineChild(params, "strong_open", function (token) {
var lineNumber = token.lineNumber, markup = token.markup;
var markupStyle = emphasisOrStrongStyleFor(markup);
if (expectedStyle === "consistent") {
expectedStyle = markupStyle;
}
addErrorDetailIf(onError, lineNumber, expectedStyle, markupStyle);
});
}
};
/***/ }),
/***/ "../lib/rules.js":
@ -4082,7 +4127,8 @@ var rules = [
__webpack_require__(/*! ./md045 */ "../lib/md045.js"),
__webpack_require__(/*! ./md046 */ "../lib/md046.js"),
__webpack_require__(/*! ./md047 */ "../lib/md047.js"),
__webpack_require__(/*! ./md048 */ "../lib/md048.js")
__webpack_require__(/*! ./md048 */ "../lib/md048.js"),
__webpack_require__(/*! ./md050 */ "../lib/md050.js")
];
rules.forEach(function (rule) {
var name = rule.names[0].toLowerCase();

View file

@ -1911,3 +1911,33 @@ The configured list style can be a specific symbol to use (backtick, tilde), or
can require that usage be consistent within the document.
Rationale: Consistent formatting makes it easier to understand a document.
<a name="md050"></a>
## MD050 - Strong style should be consistent
Tags: emphasis
Aliases: strong-style
Parameters: style ("consistent", "asterisk", "underscore"; default "consistent")
This rule is triggered when the symbols used in the document for strong do not
match the configured strong style:
```markdown
**Text**
__Text__
```
To fix this issue, use the configured strong style throughout the document:
```markdown
**Text**
**Text**
```
The configured strong style can be a specific symbol to use ("asterisk",
"underscore"), or can require that usage be consistent within the document.
Rationale: Consistent formatting makes it easier to understand a document.

View file

@ -181,6 +181,22 @@ module.exports.fencedCodeBlockStyleFor =
}
};
/**
* Return the string representation of a emphasis or strong markup character.
*
* @param {string} markup Emphasis or strong string.
* @returns {string} String representation.
*/
module.exports.emphasisOrStrongStyleFor =
function emphasisOrStrongStyleFor(markup) {
switch (markup[0]) {
case "*":
return "asterisk";
default:
return "underscore";
}
};
/**
* Return the number of characters of indent for a token.
*

28
lib/md050.js Normal file
View file

@ -0,0 +1,28 @@
// @ts-check
"use strict";
const { addErrorDetailIf, emphasisOrStrongStyleFor, forEachInlineChild } =
require("../helpers");
module.exports = {
"names": [ "MD050", "strong-style" ],
"description": "Strong style should be consistent",
"tags": [ "emphasis" ],
"function": function MD050(params, onError) {
let expectedStyle = String(params.config.style || "consistent");
forEachInlineChild(params, "strong_open", (token) => {
const { lineNumber, markup } = token;
const markupStyle = emphasisOrStrongStyleFor(markup);
if (expectedStyle === "consistent") {
expectedStyle = markupStyle;
}
addErrorDetailIf(
onError,
lineNumber,
expectedStyle,
markupStyle
);
});
}
};

View file

@ -50,7 +50,8 @@ const rules = [
require("./md045"),
require("./md046"),
require("./md047"),
require("./md048")
require("./md048"),
require("./md050")
];
rules.forEach((rule) => {
const name = rule.names[0].toLowerCase();

View file

@ -248,5 +248,11 @@
"MD048": {
// Code fence style
"style": "consistent"
},
// MD050/strong-style - Strong style should be consistent
"MD050": {
// Strong style should be consistent
"style": "consistent"
}
}

View file

@ -225,3 +225,8 @@ MD047: true
MD048:
# Code fence style
style: "consistent"
# MD050/strong-style - Strong style should be consistent
MD050:
# Strong style should be consistent
style: "consistent"

View file

@ -396,6 +396,20 @@ rules.forEach(function forRule(rule) {
}
};
break;
case "MD050":
scheme.properties = {
"style": {
"description": "Strong style should be consistent",
"type": "string",
"enum": [
"consistent",
"asterisk",
"underscore"
],
"default": "consistent"
}
};
break;
default:
custom = false;
break;

View file

@ -1439,6 +1439,48 @@
},
"additionalProperties": false
},
"MD050": {
"description": "MD050/strong-style - Strong style should be consistent",
"type": [
"boolean",
"object"
],
"default": true,
"properties": {
"style": {
"description": "Strong style should be consistent",
"type": "string",
"enum": [
"consistent",
"asterisk",
"underscore"
],
"default": "consistent"
}
},
"additionalProperties": false
},
"strong-style": {
"description": "MD050/strong-style - Strong style should be consistent",
"type": [
"boolean",
"object"
],
"default": true,
"properties": {
"style": {
"description": "Strong style should be consistent",
"type": "string",
"enum": [
"consistent",
"asterisk",
"underscore"
],
"default": "consistent"
}
},
"additionalProperties": false
},
"headings": {
"description": "headings - MD001, MD002, MD003, MD018, MD019, MD020, MD021, MD022, MD023, MD024, MD025, MD026, MD036, MD041, MD043",
"type": "boolean",
@ -1535,7 +1577,7 @@
"default": true
},
"emphasis": {
"description": "emphasis - MD036, MD037",
"description": "emphasis - MD036, MD037, MD050",
"type": "boolean",
"default": true
},

View file

@ -85,4 +85,8 @@ markdownLint {MD044}
![](image.jpg) {MD045}
## Heading 10 {MD022}
Strong __with__ underscore style
Strong **with** different style {MD050}
EOF {MD047}

View file

@ -9,7 +9,7 @@ Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum.
__Section 1.1: another section {MD036}__
__Section 1.1: another section {MD036} {MD050}__
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis

View file

@ -6,5 +6,5 @@
[test *test* *test* test](www.test.com)
[test *test* *test* *test* test](www.test.com)
[test **test** test](www.test.com)
[test __test__ test](www.test.com)
[test __test__ test](www.test.com) {MD050}
[this should not raise](www.shouldnotraise.com)

View file

@ -36,7 +36,7 @@ _[This long line is comprised of an emphasized link](https://example.com "This i
**[This long line is comprised of a bolded link](https://example.com "This is the long link's title")**
__[This long line is comprised of a bolded link](https://example.com "This is the long link's title")__
__[This long line is comprised of a bolded link {MD050}](https://example.com "This is the long link's title")__
_**[This long line is comprised of an emphasized and bolded link](https://example.com "This is the long link's title")**_

View file

@ -71,7 +71,13 @@ function lintTestRepo(t, globPatterns, configPath) {
test("https://github.com/eslint/eslint", (t) => {
const rootDir = "./test-repos/eslint-eslint";
const globPatterns = [ join(rootDir, "docs/**/*.md") ];
const globPatterns = [
join(rootDir, "docs/**/*.md"),
"!" + join(
rootDir,
"docs/rules/array-callback-return.md"
)
];
const configPath = join(rootDir, ".markdownlint.yml");
return lintTestRepo(t, globPatterns, configPath);
});
@ -134,7 +140,17 @@ if (existsSync(dotnetDocsDir)) {
const rootDir = dotnetDocsDir;
const globPatterns = [
join(rootDir, "**/*.md"),
"!" + join(rootDir, "samples/**/*.md")
"!" + join(rootDir, "samples/**/*.md"),
"!" + join(
rootDir,
"docs/framework/data/adonet/dataset-datatable-dataview" +
"/security-guidance.md"
),
"!" + join(rootDir, "docs/standard/native-interop/best-practices.md"),
"!" + join(
rootDir,
"docs/standard/serialization/binaryformatter-security-guide.md"
)
];
const configPath = join(rootDir, ".markdownlint.json");
return lintTestRepo(t, globPatterns, configPath);

View file

@ -492,8 +492,9 @@ test.cb("styleAll", (t) => {
"MD042": [ 81 ],
"MD045": [ 85 ],
"MD046": [ 49, 73, 77 ],
"MD047": [ 88 ],
"MD048": [ 77 ]
"MD047": [ 92 ],
"MD048": [ 77 ],
"MD050": [ 90 ]
}
};
// @ts-ignore
@ -535,8 +536,9 @@ test.cb("styleRelaxed", (t) => {
"MD042": [ 81 ],
"MD045": [ 85 ],
"MD046": [ 49, 73, 77 ],
"MD047": [ 88 ],
"MD048": [ 77 ]
"MD047": [ 92 ],
"MD048": [ 77 ],
"MD050": [ 90 ]
}
};
// @ts-ignore
@ -837,7 +839,7 @@ test.cb("customFileSystemAsync", (t) => {
});
});
test.cb("readme", (t) => {
t.plan(115);
t.plan(117);
const tagToRules = {};
rules.forEach(function forRule(rule) {
rule.tags.forEach(function forTag(tag) {
@ -913,7 +915,7 @@ test.cb("readme", (t) => {
});
test.cb("rules", (t) => {
t.plan(336);
t.plan(344);
fs.readFile("doc/Rules.md", "utf8",
(err, contents) => {
t.falsy(err);
@ -1566,7 +1568,7 @@ test.cb("configBadFilePromise", (t) => {
});
test("allBuiltInRulesHaveValidUrl", (t) => {
t.plan(132);
t.plan(135);
rules.forEach(function forRule(rule) {
t.truthy(rule.information);
t.true(Object.getPrototypeOf(rule.information) === URL.prototype);

View file

@ -8,10 +8,10 @@ This paragraph *nests both _kinds_ of emphasis* marker.
This paragraph *nests both __kinds__ of emphasis* marker.
This paragraph **nests both __kinds__ of emphasis** marker.
This paragraph **nests both __kinds__ of emphasis** marker. {MD050}
This paragraph _nests both *kinds* of emphasis_ marker.
This paragraph _nests both **kinds** of emphasis_ marker.
This paragraph _nests both **kinds** of emphasis_ marker. {MD050}
This paragraph __nests both **kinds** of emphasis__ marker.
This paragraph __nests both **kinds** of emphasis__ marker. {MD050}

View file

@ -1,5 +1,7 @@
# Heading
<!-- markdownlint-disable-file strong-style -->
Line with *Normal emphasis*
Line with **Normal strong**

View file

@ -0,0 +1,6 @@
{
"default": true,
"MD050": {
"style": "asterisk"
}
}

View file

@ -0,0 +1,5 @@
# Strong style asterisk
This is **fine**
This is __not__ {MD050}

View file

@ -0,0 +1,6 @@
{
"default": true,
"MD050": {
"style": "underscore"
}
}

View file

@ -0,0 +1,5 @@
# Strong style underscore
This is __fine__
This is **not** {MD050}