mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 22:40: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
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
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue