diff --git a/src/rules/no-default-alt-text.js b/src/rules/no-default-alt-text.js index c059944..9eae5d0 100644 --- a/src/rules/no-default-alt-text.js +++ b/src/rules/no-default-alt-text.js @@ -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(" + 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], + }); + } } } }, diff --git a/test/no-default-alt-text.test.js b/test/no-default-alt-text.test.js index bd391ca..feca45f 100644 --- a/test/no-default-alt-text.test.js +++ b/test/no-default-alt-text.test.js @@ -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)", ];