mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 14:00:13 +01:00
Reimplement MD045/no-alt-text using micromark tokens, add range information.
This commit is contained in:
parent
3dedc1cda1
commit
e8cc7eb3cb
6 changed files with 98 additions and 19 deletions
|
|
@ -5834,19 +5834,37 @@ module.exports = {
|
|||
|
||||
|
||||
|
||||
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
||||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
||||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
||||
var _require = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"),
|
||||
addError = _require.addError,
|
||||
forEachInlineChild = _require.forEachInlineChild;
|
||||
addError = _require.addError;
|
||||
var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"),
|
||||
filterByTypes = _require2.filterByTypes;
|
||||
module.exports = {
|
||||
"names": ["MD045", "no-alt-text"],
|
||||
"description": "Images should have alternate text (alt text)",
|
||||
"tags": ["accessibility", "images"],
|
||||
"function": function MD045(params, onError) {
|
||||
forEachInlineChild(params, "image", function forToken(token) {
|
||||
if (token.content === "") {
|
||||
addError(onError, token.lineNumber);
|
||||
var images = filterByTypes(params.parsers.micromark.tokens, ["image"]);
|
||||
var _iterator = _createForOfIteratorHelper(images),
|
||||
_step;
|
||||
try {
|
||||
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
||||
var image = _step.value;
|
||||
var labelTexts = filterByTypes(image.children, ["labelText"]);
|
||||
if (labelTexts.some(function (labelText) {
|
||||
return labelText.text.length === 0;
|
||||
})) {
|
||||
var range = image.startLine === image.endLine ? [image.startColumn, image.endColumn - image.startColumn] : undefined;
|
||||
addError(onError, image.startLine, undefined, undefined, range);
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
_iterator.e(err);
|
||||
} finally {
|
||||
_iterator.f();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
22
lib/md045.js
22
lib/md045.js
|
|
@ -2,17 +2,29 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const { addError, forEachInlineChild } = require("../helpers");
|
||||
const { addError } = require("../helpers");
|
||||
const { filterByTypes } = require("../helpers/micromark.cjs");
|
||||
|
||||
module.exports = {
|
||||
"names": [ "MD045", "no-alt-text" ],
|
||||
"description": "Images should have alternate text (alt text)",
|
||||
"tags": [ "accessibility", "images" ],
|
||||
"function": function MD045(params, onError) {
|
||||
forEachInlineChild(params, "image", function forToken(token) {
|
||||
if (token.content === "") {
|
||||
addError(onError, token.lineNumber);
|
||||
const images = filterByTypes(params.parsers.micromark.tokens, [ "image" ]);
|
||||
for (const image of images) {
|
||||
const labelTexts = filterByTypes(image.children, [ "labelText" ]);
|
||||
if (labelTexts.some((labelText) => labelText.text.length === 0)) {
|
||||
const range = (image.startLine === image.endLine) ?
|
||||
[ image.startColumn, image.endColumn - image.startColumn ] :
|
||||
undefined;
|
||||
addError(
|
||||
onError,
|
||||
image.startLine,
|
||||
undefined,
|
||||
undefined,
|
||||
range
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -22,5 +22,11 @@ Link to image with alternate text [](image.jpg)
|
|||
|
||||
Link to image without alternate text [](image.jpg) {MD045}
|
||||
|
||||
Multi-line image with alternate text 
|
||||
|
||||
Multi-line image without alternate text  {MD045:28}
|
||||
|
||||
[notitle]: image.jpg
|
||||
[title]: image.jpg "Title"
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -1860,7 +1860,10 @@ Generated by [AVA](https://avajs.dev).
|
|||
{
|
||||
errorContext: null,
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
errorRange: [
|
||||
33,
|
||||
14,
|
||||
],
|
||||
fixInfo: null,
|
||||
lineNumber: 19,
|
||||
ruleDescription: 'Images should have alternate text (alt text)',
|
||||
|
|
@ -6741,7 +6744,10 @@ Generated by [AVA](https://avajs.dev).
|
|||
{
|
||||
errorContext: null,
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
errorRange: [
|
||||
1,
|
||||
14,
|
||||
],
|
||||
fixInfo: null,
|
||||
lineNumber: 85,
|
||||
ruleDescription: 'Images should have alternate text (alt text)',
|
||||
|
|
@ -33064,7 +33070,10 @@ Generated by [AVA](https://avajs.dev).
|
|||
{
|
||||
errorContext: null,
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
errorRange: [
|
||||
1,
|
||||
14,
|
||||
],
|
||||
fixInfo: null,
|
||||
lineNumber: 5,
|
||||
ruleDescription: 'Images should have alternate text (alt text)',
|
||||
|
|
@ -33077,7 +33086,10 @@ Generated by [AVA](https://avajs.dev).
|
|||
{
|
||||
errorContext: null,
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
errorRange: [
|
||||
1,
|
||||
22,
|
||||
],
|
||||
fixInfo: null,
|
||||
lineNumber: 9,
|
||||
ruleDescription: 'Images should have alternate text (alt text)',
|
||||
|
|
@ -33090,7 +33102,10 @@ Generated by [AVA](https://avajs.dev).
|
|||
{
|
||||
errorContext: null,
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
errorRange: [
|
||||
30,
|
||||
14,
|
||||
],
|
||||
fixInfo: null,
|
||||
lineNumber: 11,
|
||||
ruleDescription: 'Images should have alternate text (alt text)',
|
||||
|
|
@ -33103,7 +33118,10 @@ Generated by [AVA](https://avajs.dev).
|
|||
{
|
||||
errorContext: null,
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
errorRange: [
|
||||
40,
|
||||
12,
|
||||
],
|
||||
fixInfo: null,
|
||||
lineNumber: 15,
|
||||
ruleDescription: 'Images should have alternate text (alt text)',
|
||||
|
|
@ -33116,7 +33134,10 @@ Generated by [AVA](https://avajs.dev).
|
|||
{
|
||||
errorContext: null,
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
errorRange: [
|
||||
50,
|
||||
10,
|
||||
],
|
||||
fixInfo: null,
|
||||
lineNumber: 19,
|
||||
ruleDescription: 'Images should have alternate text (alt text)',
|
||||
|
|
@ -33129,7 +33150,10 @@ Generated by [AVA](https://avajs.dev).
|
|||
{
|
||||
errorContext: null,
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
errorRange: [
|
||||
39,
|
||||
14,
|
||||
],
|
||||
fixInfo: null,
|
||||
lineNumber: 23,
|
||||
ruleDescription: 'Images should have alternate text (alt text)',
|
||||
|
|
@ -33139,6 +33163,19 @@ Generated by [AVA](https://avajs.dev).
|
|||
'no-alt-text',
|
||||
],
|
||||
},
|
||||
{
|
||||
errorContext: null,
|
||||
errorDetail: null,
|
||||
errorRange: null,
|
||||
fixInfo: null,
|
||||
lineNumber: 28,
|
||||
ruleDescription: 'Images should have alternate text (alt text)',
|
||||
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md045.md',
|
||||
ruleNames: [
|
||||
'MD045',
|
||||
'no-alt-text',
|
||||
],
|
||||
},
|
||||
],
|
||||
fixed: `# Images with and without alternate text␊
|
||||
␊
|
||||
|
|
@ -33164,6 +33201,12 @@ Generated by [AVA](https://avajs.dev).
|
|||
␊
|
||||
Link to image without alternate text [](image.jpg) {MD045}␊
|
||||
␊
|
||||
Multi-line image with alternate text ␊
|
||||
␊
|
||||
Multi-line image without alternate text  {MD045:28}␊
|
||||
␊
|
||||
[notitle]: image.jpg␊
|
||||
[title]: image.jpg "Title"␊
|
||||
`,
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue