Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Check images for alt text #1800

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

shraddha-aangiras
Copy link
Contributor

Implemented to ensure all images have some alt text
Fixes #1651

@shraddha-aangiras
Copy link
Contributor Author

@Eric-Arellano please review this when you're free, thanks!

Copy link
Collaborator

@Eric-Arellano Eric-Arellano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this!

@@ -284,6 +284,20 @@ npm run check:metadata -- --apis
npm run check
```

## Check image alt text

Every image needs to have `alt text` for accessibility. The `lint` job in CI will fail if images do not have alt text.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, "alt text" isn't code so no need to escape it.

Suggested change
Every image needs to have `alt text` for accessibility. The `lint` job in CI will fail if images do not have alt text.
Every image needs to have alt text for accessibility. The `lint` job in CI will fail if images do not have alt text.

You can also check for valid metadata locally:

```bash
# Only check file metadata
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Only check file metadata
# Only check alt text

# Only check file metadata
npm run check:alt-text

# Or, run all the checks. Although this only checks non-API docs.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Or, run all the checks. Although this only checks non-API docs.
# Or, run all the checks

Comment on lines +20 to +28
interface Arguments {
[x: string]: unknown;
}

const readArgs = (): Arguments => {
return yargs(hideBin(process.argv))
.version(false)
.parseSync();
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this. This program doesn't need to take arguments

Comment on lines +31 to +33
const mdGlobs = ["docs/guides/*.mdx", "docs/migration-guides/*.mdx"];
const notebookGlobs = ["docs/guides/*.ipynb", "docs/migration-guides/*.ipynb"];
return [await globby(mdGlobs), await globby(notebookGlobs)];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to differentiate between MDX and ipynb. We do that for checkMetadata because you set up the metadata very differently there, but we don't do that for alt text.

Also, we want to check all folders other than docs/api. You can do something like:

return await globby(["docs/**/*.{ipynb,mdx}", "!docs/api/**/*.mdx"])

At that point, this function isn't really "pulling its weight". It would be cleaner to inline the code, meaning you get rid of the helper function and directly have const files = await globby(["docs/**/*.{ipynb,mdx}", "!docs/api/**/*.mdx"]) in your main function

// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.

import { expect, test } from "@jest/globals";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use Jest anymore. We've now migrated to Playwright. You can look at some other test files for inspiration.

Comment on lines +21 to +29
export interface AltText {
imageName: Set<string>;
altText: Set<string | null | undefined>;
};


export async function extractAlt(
markdown: string,
): Promise<AltText> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simply this functionality by making it more focused. All we care about is finding which images are missing alt text. We don't need to keep track of which images do have alt text. Note that if you are missing alt text, then altText would be empty. So, all we need is to return a string[] for the image names we're missing. You could rename the function something like findImagesWithoutAltText

}


function getNullAlt(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You won't need this function if you take my recommendation in extractAltText.ts

arnaucasau added a commit to arnaucasau/documentation that referenced this pull request Nov 20, 2024
Co-authored-by: Eric Arellano <[email protected]>
Co-authored-by: Shraddha Aangiras <[email protected]>
github-merge-queue bot pushed a commit that referenced this pull request Nov 21, 2024
This pull request adds an image checker to ensure all images have an alt
text defined, which is crucial for accessibility, and that we don't have
any `<img>` HTML tag. The output of the check shows the file name that
contains invalid images and the images' names. Ex:
```
Error in file 'docs/guides/custom-transpiler-pass.ipynb':
	- The image '/images/guides/custom-transpiler-pass/DAG.png' does not have alt text.

Invalid images found 💔 See https://github.com/Qiskit/documentation#images for instructions.
```
The PR builds on the work done by @shraddha-aangiras on
#1800. I have made some
changes to simplify the code and incorporated @Eric-Arellano's feedback.
Thank you both for the work you've done!

Closes #1651

---------

Co-authored-by: Eric Arellano <[email protected]>
Co-authored-by: Shraddha Aangiras <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

Accessibility: new check for image alt text
2 participants