From 7e3e671eeb5373031cd211686b556deaaf590d89 Mon Sep 17 00:00:00 2001 From: Duncan Mackenzie Date: Fri, 29 Dec 2017 17:01:21 -0800 Subject: [PATCH 1/3] Adding a new rule, no-alt-text as per Issue #75 --- README.md | 3 +++ doc/Rules.md | 10 ++++++++++ lib/rules.js | 18 +++++++++++++++++ schema/markdownlint-config-schema.json | 10 ++++++++++ test/detailed-results-MD041-MD050.md | 4 ++++ .../detailed-results-MD041-MD050.results.json | 20 ++++++++++++++++++- test/markdownlint-test.js | 8 ++++---- test/no_alt_text.json | 4 ++++ test/no_alt_text.md | 3 +++ 9 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 test/no_alt_text.json create mode 100644 test/no_alt_text.md diff --git a/README.md b/README.md index 3a257530..c0a4d413 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/doc/Rules.md b/doc/Rules.md index 3f515fb2..1cd483c5 100644 --- a/doc/Rules.md +++ b/doc/Rules.md @@ -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. + + + +## 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). diff --git a/lib/rules.js b/lib/rules.js index 36f7558a..bdf12bca 100644 --- a/lib/rules.js +++ b/lib/rules.js @@ -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); + } + }); + } + }); + } } ]; diff --git a/schema/markdownlint-config-schema.json b/schema/markdownlint-config-schema.json index da53e01b..155af00d 100644 --- a/schema/markdownlint-config-schema.json +++ b/schema/markdownlint-config-schema.json @@ -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", diff --git a/test/detailed-results-MD041-MD050.md b/test/detailed-results-MD041-MD050.md index a19515d3..3139cb8a 100644 --- a/test/detailed-results-MD041-MD050.md +++ b/test/detailed-results-MD041-MD050.md @@ -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) diff --git a/test/detailed-results-MD041-MD050.results.json b/test/detailed-results-MD041-MD050.results.json index 2a74d2a9..8a2effec 100644 --- a/test/detailed-results-MD041-MD050.results.json +++ b/test/detailed-results-MD041-MD050.results.json @@ -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 } ] \ No newline at end of file diff --git a/test/markdownlint-test.js b/test/markdownlint-test.js index 3e5846c1..e6a171da 100644 --- a/test/markdownlint-test.js +++ b/test/markdownlint-test.js @@ -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); diff --git a/test/no_alt_text.json b/test/no_alt_text.json new file mode 100644 index 00000000..aa286a3b --- /dev/null +++ b/test/no_alt_text.json @@ -0,0 +1,4 @@ +{ + "default": true, + "MD045": true +} diff --git a/test/no_alt_text.md b/test/no_alt_text.md new file mode 100644 index 00000000..a05e2094 --- /dev/null +++ b/test/no_alt_text.md @@ -0,0 +1,3 @@ +# This is an image link without any alt text + +![](image.jpg) {MD045} \ No newline at end of file From d3b0e1eea229eb15dd35ba2054bdcd6c0ded3bcb Mon Sep 17 00:00:00 2001 From: Duncan Mackenzie Date: Mon, 1 Jan 2018 13:16:39 -0800 Subject: [PATCH 2/3] Update as per feedback from maintainer --- README.md | 4 ++-- doc/Rules.md | 2 +- schema/markdownlint-config-schema.json | 16 +++++++++++++--- test/{no_alt_text.md => no-alt-text.md} | 0 test/no_alt_text.json | 4 ---- 5 files changed, 16 insertions(+), 10 deletions(-) rename test/{no_alt_text.md => no-alt-text.md} (100%) delete mode 100644 test/no_alt_text.json diff --git a/README.md b/README.md index c0a4d413..a4ed243e 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ See [Rules.md](doc/Rules.md) for more details. ## Tags +* **accessibility** - MD045 * **atx** - MD018, MD019 * **atx_closed** - MD020, MD021 * **blank_lines** - MD012, MD022, MD031, MD032 @@ -104,6 +105,7 @@ See [Rules.md](doc/Rules.md) for more details. MD024, MD025, MD026, MD036, MD041, MD043 * **hr** - MD035 * **html** - MD033 +* **images** - MD045 * **indentation** - MD005, MD006, MD007, MD027 * **language** - MD040 * **line_length** - MD013 @@ -114,8 +116,6 @@ 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 diff --git a/doc/Rules.md b/doc/Rules.md index 1cd483c5..d6d5e802 100644 --- a/doc/Rules.md +++ b/doc/Rules.md @@ -1241,7 +1241,7 @@ Set the `code_blocks` parameter to `false` to disable this rule for code blocks. ## MD045 - Images should have ALT Text attribute -Tags: images, accessibility +Tags: accessibility, images Aliases: no-alt-text diff --git a/schema/markdownlint-config-schema.json b/schema/markdownlint-config-schema.json index 155af00d..18660b81 100644 --- a/schema/markdownlint-config-schema.json +++ b/schema/markdownlint-config-schema.json @@ -955,14 +955,14 @@ "additionalProperties": false }, "MD045": { - "description": "MD045/no-alt-text - Images should have non-empty Alt Text", + "description": "MD045/no-alt-text - Images should have ALT Text attribute", "type": "boolean", "default": true }, "no-alt-text": { - "description": "MD045/no-alt-text - Images should have non-empty Alt Text", + "description": "MD045/no-alt-text - Images should have ALT Text attribute", "type": "boolean", - "default": true + "default": true }, "headers": { "description": "headers - MD001, MD002, MD003, MD018, MD019, MD020, MD021, MD022, MD023, MD024, MD025, MD026, MD036, MD041, MD043", @@ -1068,6 +1068,16 @@ "description": "spelling - MD044", "type": "boolean", "default": true + }, + "images": { + "description": "images - MD045", + "type": "boolean", + "default": true + }, + "accessibility": { + "description": "accessibility - MD045", + "type": "boolean", + "default": true } }, "additionalProperties": false diff --git a/test/no_alt_text.md b/test/no-alt-text.md similarity index 100% rename from test/no_alt_text.md rename to test/no-alt-text.md diff --git a/test/no_alt_text.json b/test/no_alt_text.json deleted file mode 100644 index aa286a3b..00000000 --- a/test/no_alt_text.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "default": true, - "MD045": true -} From ae443f02addef206e88e0a27e5881cfec2d2cff0 Mon Sep 17 00:00:00 2001 From: Duncan Mackenzie Date: Tue, 2 Jan 2018 14:12:34 -0800 Subject: [PATCH 3/3] add additional test for reference syntax of images (fixes #75) --- lib/rules.js | 2 +- test/detailed-results-MD041-MD050.md | 4 ++++ test/detailed-results-MD041-MD050.results.json | 11 ++++++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/rules.js b/lib/rules.js index bdf12bca..4a50eb52 100644 --- a/lib/rules.js +++ b/lib/rules.js @@ -1206,7 +1206,7 @@ module.exports = [ { "name": "MD045", "desc": "Images should have ALT Text attribute", - "tags": [ "images", "accessibility" ], + "tags": [ "accessibility", "images" ], "aliases": [ "no-alt-text" ], "regexp": null, "func": function MD045(params, errors) { diff --git a/test/detailed-results-MD041-MD050.md b/test/detailed-results-MD041-MD050.md index 3139cb8a..ad2911ce 100644 --- a/test/detailed-results-MD041-MD050.md +++ b/test/detailed-results-MD041-MD050.md @@ -19,3 +19,7 @@ A [normal](link) and an [empty one]() and a [fragment](#one). An image without alt text ![](image.jpg) ![](image.jpg) + +A reference image without alt text ![][reference] + +[reference]: image.jpg "title" \ No newline at end of file diff --git a/test/detailed-results-MD041-MD050.results.json b/test/detailed-results-MD041-MD050.results.json index 8a2effec..9558a3d9 100644 --- a/test/detailed-results-MD041-MD050.results.json +++ b/test/detailed-results-MD041-MD050.results.json @@ -45,7 +45,7 @@ "errorRange": [25, 13] }, { - "lineNumber": 22, + "lineNumber": 25, "ruleName": "MD043", "ruleAlias": "required-headers", "ruleDescription": "Required header structure", @@ -97,5 +97,14 @@ "errorDetail": null, "errorContext": null, "errorRange": null + }, + { + "lineNumber": 23, + "ruleName": "MD045", + "ruleAlias": "no-alt-text", + "ruleDescription": "Images should have ALT Text attribute", + "errorDetail": null, + "errorContext": null, + "errorRange": null } ] \ No newline at end of file