Reimplement MD045/no-alt-text using micromark tokens, add range information.

This commit is contained in:
David Anson 2023-07-28 20:19:30 -07:00
parent 3dedc1cda1
commit e8cc7eb3cb
6 changed files with 98 additions and 19 deletions

View file

@ -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
);
}
});
}
}
};