2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2023-10-18 23:45:39 -07:00
|
|
|
const { addError, getHtmlAttributeRe, nextLinesRe } = require("../helpers");
|
2023-10-18 23:20:19 -07:00
|
|
|
const { filterByTypes, getHtmlTagInfo } = require("../helpers/micromark.cjs");
|
|
|
|
|
2023-10-18 23:45:39 -07:00
|
|
|
const altRe = getHtmlAttributeRe("alt");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2024-02-27 20:42:09 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("./markdownlint").Rule */
|
2018-01-21 21:44:25 -08:00
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD045", "no-alt-text" ],
|
|
|
|
"description": "Images should have alternate text (alt text)",
|
|
|
|
"tags": [ "accessibility", "images" ],
|
|
|
|
"function": function MD045(params, onError) {
|
2024-02-28 21:01:23 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("../helpers/micromark.cjs").Token[] */
|
|
|
|
const micromarkTokens =
|
|
|
|
// @ts-ignore
|
|
|
|
params.parsers.micromark.tokens;
|
2023-10-18 23:20:19 -07:00
|
|
|
|
|
|
|
// Process Markdown images
|
2024-02-28 21:01:23 -08:00
|
|
|
const images = filterByTypes(micromarkTokens, [ "image" ]);
|
2023-07-28 20:19:30 -07:00
|
|
|
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
|
|
|
|
);
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2023-07-28 20:19:30 -07:00
|
|
|
}
|
2023-10-18 23:20:19 -07:00
|
|
|
|
|
|
|
// Process HTML images
|
2024-02-28 21:01:23 -08:00
|
|
|
const htmlTexts = filterByTypes(micromarkTokens, [ "htmlText" ]);
|
2023-10-18 23:20:19 -07:00
|
|
|
for (const htmlText of htmlTexts) {
|
|
|
|
const { startColumn, startLine, text } = htmlText;
|
|
|
|
const htmlTagInfo = getHtmlTagInfo(htmlText);
|
|
|
|
if (
|
|
|
|
htmlTagInfo &&
|
2023-10-18 23:45:39 -07:00
|
|
|
!htmlTagInfo.close &&
|
2023-10-18 23:20:19 -07:00
|
|
|
(htmlTagInfo.name.toLowerCase() === "img") &&
|
|
|
|
!altRe.test(text)
|
|
|
|
) {
|
|
|
|
const range = [
|
|
|
|
startColumn,
|
|
|
|
text.replace(nextLinesRe, "").length
|
|
|
|
];
|
|
|
|
addError(
|
|
|
|
onError,
|
|
|
|
startLine,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
range
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|