mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Convert markdownlint library to an ECMAScript module, replace markdownlint-micromark with micromark, stop publishing (large) markdownlint-browser.js, see https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c for guidance.
This commit is contained in:
parent
191226f070
commit
1e71f6f44e
140 changed files with 1087 additions and 10428 deletions
59
lib/md045.mjs
Normal file
59
lib/md045.mjs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
// @ts-check
|
||||
|
||||
import { addError, getHtmlAttributeRe, nextLinesRe } from "../helpers/helpers.cjs";
|
||||
import { getHtmlTagInfo, getDescendantsByType } from "../helpers/micromark-helpers.cjs";
|
||||
import { filterByTypesCached } from "./cache.mjs";
|
||||
|
||||
const altRe = getHtmlAttributeRe("alt");
|
||||
|
||||
/** @type {import("./markdownlint.mjs").Rule} */
|
||||
export default {
|
||||
"names": [ "MD045", "no-alt-text" ],
|
||||
"description": "Images should have alternate text (alt text)",
|
||||
"tags": [ "accessibility", "images" ],
|
||||
"parser": "micromark",
|
||||
"function": function MD045(params, onError) {
|
||||
// Process Markdown images
|
||||
const images = filterByTypesCached([ "image" ]);
|
||||
for (const image of images) {
|
||||
const labelTexts = getDescendantsByType(image, [ "label", "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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Process HTML images
|
||||
const htmlTexts = filterByTypesCached([ "htmlText" ], true);
|
||||
for (const htmlText of htmlTexts) {
|
||||
const { startColumn, startLine, text } = htmlText;
|
||||
const htmlTagInfo = getHtmlTagInfo(htmlText);
|
||||
if (
|
||||
htmlTagInfo &&
|
||||
!htmlTagInfo.close &&
|
||||
(htmlTagInfo.name.toLowerCase() === "img") &&
|
||||
!altRe.test(text)
|
||||
) {
|
||||
const range = [
|
||||
startColumn,
|
||||
text.replace(nextLinesRe, "").length
|
||||
];
|
||||
addError(
|
||||
onError,
|
||||
startLine,
|
||||
undefined,
|
||||
undefined,
|
||||
range
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue