From c71b2d60bb98c85debf02cf65c88a7395b4533f9 Mon Sep 17 00:00:00 2001 From: Ian Sanders Date: Wed, 26 Apr 2023 11:15:13 -0400 Subject: [PATCH 1/2] Enforce asterisk-style unordered lists (#41) * Enforce asterisk-style unordered lists * Update affected project documentation * Fix test options --- CONTRIBUTING.md | 8 ++++---- README.md | 14 +++++++------- style/accessibility.json | 4 +++- test/usage.test.js | 4 +++- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 110984a..031b9c3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -6,8 +6,8 @@ Thank you for your interest in contributing. Depending on your needs, you may have one of two development paths: -- via integrating with an existing usage (ideal for modifying the interface of our rules) -- via unit testing (ideal for adding a new rule) +* via integrating with an existing usage (ideal for modifying the interface of our rules) +* via unit testing (ideal for adding a new rule) For that reason we've included two paths to develop. Feel free to use either, or both. @@ -32,8 +32,8 @@ It may be useful to work on this in tandem with a codebase that uses the rules. If you go to the `node_modules` directory in your codebase and try to navigate into the package, you'll notice that whatever changes you make in your local development directory will be reflected in the codebase. 3. Reset symlinks at any time by reversing the steps via `npm unlink`. - - in your codebase: `npm unlink @github/markdownlint-github` - - in this directory: `npm unlink` + * in your codebase: `npm unlink @github/markdownlint-github` + * in this directory: `npm unlink` ### Unit and Interface Testing diff --git a/README.md b/README.md index 2b7193a..5614270 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,8 @@ See opinions codified in [index.js](./index.js). The following are custom rules defined in this plugin. -- [**GH001** _no-default-alt-text_](./docs/rules/GH001-no-default-alt-text.md) -- [**GH002** _no-generic-link-text_](./docs/rules/GH002-no-generic-link-text.md) +* [**GH001** _no-default-alt-text_](./docs/rules/GH001-no-default-alt-text.md) +* [**GH002** _no-generic-link-text_](./docs/rules/GH002-no-generic-link-text.md) See [`markdownlint` rules](https://github.com/DavidAnson/markdownlint#rules--aliases) for documentation on rules pulled in from `markdownlint`. @@ -29,11 +29,11 @@ See [`markdownlint` rules](https://github.com/DavidAnson/markdownlint#rules--ali 2. Install packages. - - ```bash - npm install -D markdownlint-cli2 # if updating existing package, check for updates - npm install -D @github/markdownlint-github [--@github:registry=https://registry.npmjs.org] - npm install -D markdownlint-cli2-formatter-pretty - ``` + ```bash + npm install -D markdownlint-cli2 # if updating existing package, check for updates + npm install -D @github/markdownlint-github [--@github:registry=https://registry.npmjs.org] + npm install -D markdownlint-cli2-formatter-pretty + ``` 3. Add/modify your linting script in `package.json`. diff --git a/style/accessibility.json b/style/accessibility.json index 683d4c2..046af54 100644 --- a/style/accessibility.json +++ b/style/accessibility.json @@ -8,5 +8,7 @@ "no-space-in-links": false, "ol-prefix": "ordered", "single-h1": true, - "ul-style": true + "ul-style": { + "style": "asterisk" + } } diff --git a/test/usage.test.js b/test/usage.test.js index 1d3fa4b..6a0d0ea 100644 --- a/test/usage.test.js +++ b/test/usage.test.js @@ -19,7 +19,9 @@ describe("usage", () => { "no-emphasis-as-header": true, "no-heading-increment": true, "no-generic-link-text": true, - "ul-style": true, + "ul-style": { + style: "asterisk", + }, default: true, "no-inline-html": false, "no-bare-urls": false, From 114f037a7edbe3a40a4c74e40152cc0c53b6fac8 Mon Sep 17 00:00:00 2001 From: Ian Sanders Date: Wed, 26 Apr 2023 11:34:53 -0400 Subject: [PATCH 2/2] 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]); }); }); });