Skip to content

Commit

Permalink
Fix bug where images within code blocks are being flagged (#83)
Browse files Browse the repository at this point in the history
Fix bug where fenced img is being flagged
  • Loading branch information
khiga8 authored Oct 4, 2023
1 parent 139cdac commit 8a36c24
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
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

0 comments on commit 8a36c24

Please sign in to comment.