From 6bcc89581174fa1a93a6d6904420147802ed5b65 Mon Sep 17 00:00:00 2001 From: Ian Sanders Date: Fri, 21 Apr 2023 15:57:18 +0000 Subject: [PATCH 1/5] Improve the no-default-alt-text rule --- src/rules/no-default-alt-text.js | 50 ++++++++++---------------------- test/no-default-alt-text.test.js | 12 +++----- 2 files changed, 19 insertions(+), 43 deletions(-) diff --git a/src/rules/no-default-alt-text.js b/src/rules/no-default-alt-text.js index 083f86d..1b4584f 100644 --- a/src/rules/no-default-alt-text.js +++ b/src/rules/no-default-alt-text.js @@ -3,53 +3,33 @@ // 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; +// e.g. "14352435" +const defaultScreenshotRegex = "(?:screen|clean) ?shot \\d{4}-\\d{2}-\\d{2}[^'\"\\]]*" +const imageRegex = "image"; +const combinedRegex = `(${[defaultScreenshotRegex, imageRegex].join("|")})` -const defaultMacOsScreenshotHtmlRegex = - /alt="(Screen|Clean) ?[S|s]hot \d{4}-\d{2}-\d{2}/gi; -const imageHtmlRegex = /alt="image"/i; +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..0e79cfd 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]) }); }); }); From 6f959307cf55f7ba04af755c0eec77938e66ef15 Mon Sep 17 00:00:00 2001 From: Ian Sanders Date: Fri, 21 Apr 2023 16:04:16 +0000 Subject: [PATCH 2/5] Fix lint errors --- src/rules/no-default-alt-text.js | 20 ++++++++++++-------- test/no-default-alt-text.test.js | 4 ++-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/rules/no-default-alt-text.js b/src/rules/no-default-alt-text.js index 1b4584f..9647217 100644 --- a/src/rules/no-default-alt-text.js +++ b/src/rules/no-default-alt-text.js @@ -4,12 +4,13 @@ // e.g. "Clean Shot 2020-10-20 @45x" // e.g. "image" // e.g. "14352435" -const defaultScreenshotRegex = "(?:screen|clean) ?shot \\d{4}-\\d{2}-\\d{2}[^'\"\\]]*" +const defaultScreenshotRegex = + "(?:screen|clean) ?shot \\d{4}-\\d{2}-\\d{2}[^'\"\\]]*"; const imageRegex = "image"; -const combinedRegex = `(${[defaultScreenshotRegex, imageRegex].join("|")})` +const combinedRegex = `(${[defaultScreenshotRegex, imageRegex].join("|")})`; -const markdownAltRegex = new RegExp(`!\\[${combinedRegex}\\]\\(.*\\)`, "gid") -const htmlAltRegex = new RegExp(`alt=["']${combinedRegex}["']`, "gid") +const markdownAltRegex = new RegExp(`!\\[${combinedRegex}\\]\\(.*\\)`, "gid"); +const htmlAltRegex = new RegExp(`alt=["']${combinedRegex}["']`, "gid"); module.exports = { names: ["GH001", "no-default-alt-text"], @@ -20,15 +21,18 @@ 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)]) { + 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 altText = match[1]; + const [startIndex] = match.indices[1]; onError({ lineNumber: lineIndex + 1, range: [startIndex + 1, altText.length], - }) + }); } } }, diff --git a/test/no-default-alt-text.test.js b/test/no-default-alt-text.test.js index 0e79cfd..93f2b34 100644 --- a/test/no-default-alt-text.test.js +++ b/test/no-default-alt-text.test.js @@ -86,11 +86,11 @@ describe("GH001: No Default Alt Text", () => { expect(results[0].ruleDescription).toMatch( "Images should have meaningful alternative text (alt text)" ); - expect(results[0].errorRange).toEqual([3, 36]) + expect(results[0].errorRange).toEqual([3, 36]); expect(results[1].ruleDescription).toMatch( "Images should have meaningful alternative text (alt text)" ); - expect(results[1].errorRange).toEqual([11, 36]) + expect(results[1].errorRange).toEqual([11, 36]); }); }); }); From c97b3870d9657d553c0421431bc999eaf558df29 Mon Sep 17 00:00:00 2001 From: Ian Sanders Date: Fri, 21 Apr 2023 16:07:18 +0000 Subject: [PATCH 3/5] Update node version for tests --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 9f97dad077d8dfbbf37d6c6d6e625f62e838c582 Mon Sep 17 00:00:00 2001 From: Ian Sanders Date: Wed, 26 Apr 2023 11:30:23 -0400 Subject: [PATCH 4/5] Change example comment to JSDoc --- src/rules/no-default-alt-text.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/rules/no-default-alt-text.js b/src/rules/no-default-alt-text.js index 9647217..926f303 100644 --- a/src/rules/no-default-alt-text.js +++ b/src/rules/no-default-alt-text.js @@ -1,11 +1,12 @@ -// 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" -// e.g. "14352435" +/** + * 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}[^'\"\\]]*"; +"(?:screen|clean) ?shot \\d{4}-\\d{2}-\\d{2}[^'\"\\]]*"; + const imageRegex = "image"; const combinedRegex = `(${[defaultScreenshotRegex, imageRegex].join("|")})`; From f9afc52646f42e82ab5eff3de2fbf6039628ab1a Mon Sep 17 00:00:00 2001 From: Ian Sanders Date: Wed, 26 Apr 2023 11:32:53 -0400 Subject: [PATCH 5/5] Format --- src/rules/no-default-alt-text.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rules/no-default-alt-text.js b/src/rules/no-default-alt-text.js index 926f303..33937fc 100644 --- a/src/rules/no-default-alt-text.js +++ b/src/rules/no-default-alt-text.js @@ -3,9 +3,9 @@ * * "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}[^'\"\\]]*"; + "(?:screen|clean) ?shot \\d{4}-\\d{2}-\\d{2}[^'\"\\]]*"; const imageRegex = "image"; const combinedRegex = `(${[defaultScreenshotRegex, imageRegex].join("|")})`;