From 8a36c244fd7349ea01d1c1187e0ebebb6b0fb739 Mon Sep 17 00:00:00 2001 From: Kate Higa <16447748+khiga8@users.noreply.github.com> Date: Wed, 4 Oct 2023 16:33:12 -0400 Subject: [PATCH] Fix bug where images within code blocks are being flagged (#83) Fix bug where fenced img is being flagged --- src/rules/no-default-alt-text.js | 43 +++++++++++++++++++++++--------- test/no-default-alt-text.test.js | 2 ++ 2 files changed, 33 insertions(+), 12 deletions(-) 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)", ];