Skip to content

Commit 09d02ff

Browse files
committed
Add new rule
1 parent 012612f commit 09d02ff

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/rules/no-empty-string-alt.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module.exports = {
2+
names: ["GH003", "no-empty-string-alt"],
3+
description: "Please provide an alternative text for the image.",
4+
information: new URL(
5+
"https://github.com/github/markdownlint-github/blob/main/docs/rules/GH003-no-empty-string-alt.md",
6+
),
7+
tags: ["accessibility", "images"],
8+
function: function GH003(params, onError) {
9+
const htmlTagsWithImages = params.parsers.markdownit.tokens.filter(
10+
(token) => {
11+
return token.type === "html_block" && token.content.includes("<img");
12+
},
13+
);
14+
const inlineImages = params.parsers.markdownit.tokens.filter(
15+
(token) =>
16+
token.type === "inline" &&
17+
token.children.some((child) => child.type === "image"),
18+
);
19+
20+
const htmlAltRegex = new RegExp(/alt=""|alt=''/i);
21+
const markdownAltRegex = new RegExp(/!\[""\]|!\[''\]/i);
22+
23+
for (const token of [...htmlTagsWithImages, ...inlineImages]) {
24+
const lineRange = token.map;
25+
const lineNumber = token.lineNumber;
26+
const lines = params.lines.slice(lineRange[0], lineRange[1]);
27+
28+
for (let i = 0; i < lines.length; i++) {
29+
const line = lines[i];
30+
let fails;
31+
if (token.type === "inline") {
32+
fails = markdownAltRegex.test(line);
33+
} else {
34+
fails = htmlAltRegex.test(line);
35+
}
36+
if (fails) {
37+
onError({
38+
lineNumber: lineNumber + i,
39+
});
40+
}
41+
}
42+
}
43+
},
44+
};

0 commit comments

Comments
 (0)