Adding a new rule, no-alt-text as per Issue #75

This commit is contained in:
Duncan Mackenzie 2017-12-29 17:01:21 -08:00
parent 4345423be1
commit 7e3e671eeb
9 changed files with 75 additions and 5 deletions

View file

@ -86,6 +86,7 @@ playground for learning and exploring.
* **[MD042](doc/Rules.md#md042)** *no-empty-links* - No empty links
* **[MD043](doc/Rules.md#md043)** *required-headers* - Required header structure
* **[MD044](doc/Rules.md#md044)** *proper-names* - Proper names should have the correct capitalization
* **[MD045](doc/Rules.md#md045)** *no-alt-text* - Images should have ALT Text attribute
See [Rules.md](doc/Rules.md) for more details.
@ -113,6 +114,8 @@ See [Rules.md](doc/Rules.md) for more details.
* **ul** - MD004, MD005, MD006, MD007, MD030, MD032
* **url** - MD034
* **whitespace** - MD009, MD010, MD012, MD027, MD028, MD030, MD037, MD038, MD039
* **accessibility** - MD045
* **images** - MD045
## Configuration

View file

@ -1236,3 +1236,13 @@ the proper capitalization, specify the desired letter case in the `names` array:
]
Set the `code_blocks` parameter to `false` to disable this rule for code blocks.
<a name="md045"></a>
## MD045 - Images should have ALT Text attribute
Tags: images, accessibility
Aliases: no-alt-text
This rule is triggered when an image is found with no alt text. This is a key concern for accessibility. [Guidance on how to write alt text for images](https://www.phase2technology.com/blog/no-more-excuses-definitive-guide-alt-text-field).

View file

@ -1202,5 +1202,23 @@ module.exports = [
}
});
}
},
{
"name": "MD045",
"desc": "Images should have ALT Text attribute",
"tags": [ "images", "accessibility" ],
"aliases": [ "no-alt-text" ],
"regexp": null,
"func": function MD045(params, errors) {
forEachInlineChild(params, "image", function forToken(token) {
if (token.content === "") {
token.attrs.forEach(function forAttr(attr) {
if (attr[0] === "alt" && attr[1] === "") {
errors.add(token.lineNumber);
}
});
}
});
}
}
];

View file

@ -954,6 +954,16 @@
},
"additionalProperties": false
},
"MD045": {
"description": "MD045/no-alt-text - Images should have non-empty Alt Text",
"type": "boolean",
"default": true
},
"no-alt-text": {
"description": "MD045/no-alt-text - Images should have non-empty Alt Text",
"type": "boolean",
"default": true
},
"headers": {
"description": "headers - MD001, MD002, MD003, MD018, MD019, MD020, MD021, MD022, MD023, MD024, MD025, MD026, MD036, MD041, MD043",
"type": "boolean",

View file

@ -15,3 +15,7 @@ name wrong twice:
MarkDownLint.
A [normal](link) and an [empty one]() and a [fragment](#one).
An image without alt text ![](image.jpg)
![](image.jpg)

View file

@ -45,7 +45,7 @@
"errorRange": [25, 13]
},
{
"lineNumber": 18,
"lineNumber": 22,
"ruleName": "MD043",
"ruleAlias": "required-headers",
"ruleDescription": "Required header structure",
@ -79,5 +79,23 @@
"errorDetail": "Expected: markdownlint; Actual: MarkDownLint",
"errorContext": null,
"errorRange": [1, 12]
},
{
"lineNumber": 19,
"ruleName": "MD045",
"ruleAlias": "no-alt-text",
"ruleDescription": "Images should have ALT Text attribute",
"errorDetail": null,
"errorContext": null,
"errorRange": null
},
{
"lineNumber": 21,
"ruleName": "MD045",
"ruleAlias": "no-alt-text",
"ruleDescription": "Images should have ALT Text attribute",
"errorDetail": null,
"errorContext": null,
"errorRange": null
}
]

View file

@ -966,7 +966,7 @@ module.exports.missingStringValue = function missingStringValue(test) {
};
module.exports.ruleNamesUpperCase = function ruleNamesUpperCase(test) {
test.expect(40);
test.expect(41);
rules.forEach(function forRule(rule) {
test.equal(rule.name, rule.name.toUpperCase(), "Rule name not upper-case.");
});
@ -974,7 +974,7 @@ module.exports.ruleNamesUpperCase = function ruleNamesUpperCase(test) {
};
module.exports.uniqueAliases = function uniqueAliases(test) {
test.expect(80);
test.expect(82);
var tags = [];
rules.forEach(function forRule(rule) {
Array.prototype.push.apply(tags, rule.tags);
@ -991,7 +991,7 @@ module.exports.uniqueAliases = function uniqueAliases(test) {
};
module.exports.readme = function readme(test) {
test.expect(104);
test.expect(108);
var tagToRules = {};
rules.forEach(function forRule(rule) {
rule.tags.forEach(function forTag(tag) {
@ -1053,7 +1053,7 @@ module.exports.readme = function readme(test) {
};
module.exports.doc = function doc(test) {
test.expect(303);
test.expect(310);
fs.readFile("doc/Rules.md", shared.utf8Encoding,
function readFile(err, contents) {
test.ifError(err);

4
test/no_alt_text.json Normal file
View file

@ -0,0 +1,4 @@
{
"default": true,
"MD045": true
}

3
test/no_alt_text.md Normal file
View file

@ -0,0 +1,3 @@
# This is an image link without any alt text
![](image.jpg) {MD045}