From 114f037a7edbe3a40a4c74e40152cc0c53b6fac8 Mon Sep 17 00:00:00 2001 From: Ian Sanders Date: Wed, 26 Apr 2023 11:34:53 -0400 Subject: [PATCH] Improve the no-default-alt-text rule (#40) * Improve the no-default-alt-text rule * Fix lint errors * Update node version for tests * Change example comment to JSDoc * Format --- .github/workflows/ci.yml | 2 +- src/rules/no-default-alt-text.js | 63 ++++++++++++-------------------- test/no-default-alt-text.test.js | 12 ++---- 3 files changed, 29 insertions(+), 48 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3a88ef5..08aee24 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,6 +11,6 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-node@v3 with: - node-version: 14 + node-version: 19 - run: npm install - run: npm test diff --git a/src/rules/no-default-alt-text.js b/src/rules/no-default-alt-text.js index 083f86d..33937fc 100644 --- a/src/rules/no-default-alt-text.js +++ b/src/rules/no-default-alt-text.js @@ -1,55 +1,40 @@ -// Regex to match alt text that is the same as the default image filename -// e.g. "Screen Shot 2020-10-20 at 2 52 27 PM" -// e.g. "Screenshot 2020-10-20 at 2 52 27 PM" -// e.g. "Clean Shot 2020-10-20 @45x" -// e.g. "image" -const defaultMacOsScreenshotMarkdownRegex = - /^(Screen|Clean) ?[S|s]hot \d{4}-\d{2}-\d{2}/gi; -const imageMarkdownRegex = /^image$/i; +/** + * Examples: + * * "Screen Shot 2020-10-20 at 2 52 27 PM" + * * "Screenshot 2020-10-20 at 2 52 27 PM" + * * "Clean Shot 2020-10-20 @45x" + */ +const defaultScreenshotRegex = + "(?:screen|clean) ?shot \\d{4}-\\d{2}-\\d{2}[^'\"\\]]*"; -const defaultMacOsScreenshotHtmlRegex = - /alt="(Screen|Clean) ?[S|s]hot \d{4}-\d{2}-\d{2}/gi; -const imageHtmlRegex = /alt="image"/i; +const imageRegex = "image"; +const combinedRegex = `(${[defaultScreenshotRegex, imageRegex].join("|")})`; + +const markdownAltRegex = new RegExp(`!\\[${combinedRegex}\\]\\(.*\\)`, "gid"); +const htmlAltRegex = new RegExp(`alt=["']${combinedRegex}["']`, "gid"); module.exports = { names: ["GH001", "no-default-alt-text"], - description: - "Images should set meaningful alternative text (alt text), and not use the macOS default screenshot filename or `Image`.", + description: "Images should have meaningful alternative text (alt text)", information: new URL( "https://github.com/github/markdownlint-github/blob/main/docs/rules/GH001-no-default-alt-text.md" ), tags: ["accessibility", "images"], function: function GH001(params, onError) { - // markdown syntax - const inlineTokens = params.tokens.filter((t) => t.type === "inline"); - for (const token of inlineTokens) { - const imageTokens = token.children.filter((t) => t.type === "image"); - for (const image of imageTokens) { - if ( - image.content.match(defaultMacOsScreenshotMarkdownRegex) || - image.content.match(imageMarkdownRegex) - ) { - onError({ - lineNumber: image.lineNumber, - detail: `For image: ${image.content}`, - }); - } - } - } + 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]; - // html syntax - let lineNumber = 1; - for (const line of params.lines) { - if ( - line.match(defaultMacOsScreenshotHtmlRegex) || - line.match(imageHtmlRegex) - ) { onError({ - lineNumber, - detail: `For image: ${line}`, + lineNumber: lineIndex + 1, + range: [startIndex + 1, altText.length], }); } - lineNumber++; } }, }; diff --git a/test/no-default-alt-text.test.js b/test/no-default-alt-text.test.js index 63c55ca..93f2b34 100644 --- a/test/no-default-alt-text.test.js +++ b/test/no-default-alt-text.test.js @@ -84,17 +84,13 @@ describe("GH001: No Default Alt Text", () => { const results = await runTest(strings, altTextRule); expect(results[0].ruleDescription).toMatch( - "Images should set meaningful alternative text (alt text), and not use the macOS default screenshot filename or `Image`." - ); - expect(results[0].errorDetail).toBe( - "For image: Screen Shot 2022-06-26 at 7 41 30 PM" + "Images should have meaningful alternative text (alt text)" ); + expect(results[0].errorRange).toEqual([3, 36]); expect(results[1].ruleDescription).toMatch( - "Images should set meaningful alternative text (alt text), and not use the macOS default screenshot filename or `Image`." - ); - expect(results[1].errorDetail).toBe( - 'For image: Screen Shot 2022-06-26 at 7 41 30 PM' + "Images should have meaningful alternative text (alt text)" ); + expect(results[1].errorRange).toEqual([11, 36]); }); }); });