Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions src/rules/no-default-alt-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,38 @@ module.exports = {
),
tags: ["accessibility", "images"],
function: function GH001(params, onError) {
for (const [lineIndex, line] of params.lines.entries()) {
for (const match of [
...line.matchAll(markdownAltRegex),
...line.matchAll(htmlAltRegex),
]) {
// The alt text is contained in the first capture group
const altText = match[1];
const [startIndex] = match.indices[1];
const htmlTagsWithImages = params.parsers.markdownit.tokens.filter(
(token) => {
return token.type === "html_block" && token.content.includes("<img");
},
);
const inlineImages = params.parsers.markdownit.tokens.filter(
(token) =>
token.type === "inline" &&
token.children.some((child) => child.type === "image"),
);

onError({
lineNumber: lineIndex + 1,
range: [startIndex + 1, altText.length],
});
for (const token of [...htmlTagsWithImages, ...inlineImages]) {
const lineRange = token.map;
const lineNumber = token.lineNumber;
const lines = params.lines.slice(lineRange[0], lineRange[1]);

for (let i = 0; i < lines.length; i++) {
const line = lines[i];
let matches;
if (token.type === "inline") {
matches = line.matchAll(markdownAltRegex);
} else {
matches = line.matchAll(htmlAltRegex);
}
for (const match of matches) {
const altText = match[1];
const [startIndex] = match.indices[1];
onError({
lineNumber: lineNumber + i,
range: [startIndex + 1, altText.length],
});
}
}
}
},
Expand Down
2 changes: 2 additions & 0 deletions test/no-default-alt-text.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ describe("GH001: No Default Alt Text", () => {
describe("successes", () => {
test("inline", async () => {
const strings = [
"```![image](https://user-images.githubusercontent.com/abcdef.png)```",
"`![Image](https://user-images.githubusercontent.com/abcdef.png)`",
"![Chart with a single root node reading 'Example'](https://user-images.githubusercontent.com/abcdef.png)",
];

Expand Down