-
Notifications
You must be signed in to change notification settings - Fork 86
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
Extend the images' checker to check API docs #2426
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,11 +11,43 @@ | |
// that they have been altered from the originals. | ||
|
||
import { globby } from "globby"; | ||
import yargs from "yargs/yargs"; | ||
import { hideBin } from "yargs/helpers"; | ||
import { collectInvalidImageErrors } from "../lib/markdownImages.js"; | ||
import { readMarkdown } from "../lib/markdownReader.js"; | ||
|
||
// Known APIs containing images without alt text that will be skipped | ||
// by the checker. | ||
const APIS_TO_IGNORE__SHOULD_FIX: string[] = [ | ||
"qiskit", | ||
"qiskit-ibm-runtime", | ||
"qiskit-addon-cutting", | ||
"qiskit-addon-mpf", | ||
"qiskit-addon-obp", | ||
"qiskit-addon-utils", | ||
]; | ||
|
||
interface Arguments { | ||
[x: string]: unknown; | ||
apis: boolean; | ||
} | ||
|
||
const readArgs = (): Arguments => { | ||
return yargs(hideBin(process.argv)) | ||
.version(false) | ||
.option("apis", { | ||
type: "boolean", | ||
default: false, | ||
description: | ||
"Check the images in the current and dev versions of the API docs.", | ||
arnaucasau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
.parseSync(); | ||
}; | ||
|
||
async function main() { | ||
const files = await globby(["docs/**/*.{ipynb,mdx}", "!docs/api/**/*.mdx"]); | ||
const args = readArgs(); | ||
|
||
const files = await determineTocFiles(args); | ||
const fileErrors: string[] = []; | ||
|
||
for (const file of files) { | ||
|
@@ -24,19 +56,38 @@ async function main() { | |
|
||
if (imageErrors.size) { | ||
fileErrors.push( | ||
`Error in file '${file}':\n\t- ${[...imageErrors].join("\n\t- ")}`, | ||
`Error in file '${file}':\n\t- ${[...imageErrors].join("\n\t- ")}\n`, | ||
); | ||
} | ||
} | ||
|
||
if (fileErrors.length) { | ||
fileErrors.forEach((error) => console.log(error)); | ||
console.error( | ||
"\n💔 Some images have problems. See https://github.com/Qiskit/documentation#images for instructions.\n", | ||
"💔 Some images have problems. See https://github.com/Qiskit/documentation#images for instructions.\n", | ||
); | ||
process.exit(1); | ||
} | ||
console.log("✅ All images are valid.\n"); | ||
} | ||
|
||
async function determineTocFiles(args: Arguments): Promise<string[]> { | ||
// We always skip historical versions to simplify the code and to have a faster script. | ||
// Even though it is possible for someone to add a new image without alt text to a | ||
// historical version that wasn't in the original release, that's very unlikely. | ||
// If it happens, it would probably be a backport from latest or dev, and the linter in | ||
// the API repo should catch it. | ||
// | ||
// If an image is missed by the API repo's linter, it will still have an alt text defined, | ||
// although it won't be very useful. | ||
Comment on lines
+81
to
+82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be helpful to add
|
||
const globs = [ | ||
"docs/**/*.{ipynb,mdx}", | ||
"!docs/api/qiskit/release-notes/*.mdx", | ||
args.apis ? "!docs/api/*/([0-9]*)/*.mdx" : "!docs/api/**/*.mdx", | ||
arnaucasau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
...APIS_TO_IGNORE__SHOULD_FIX.map((api) => `!docs/api/${api}/**/*.mdx`), | ||
]; | ||
|
||
return await globby(globs); | ||
} | ||
|
||
main().then(() => process.exit()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ import { visit } from "unist-util-visit"; | |
import remarkParse from "remark-parse"; | ||
import remarkGfm from "remark-gfm"; | ||
import remarkStringify from "remark-stringify"; | ||
import { last, split } from "lodash-es"; | ||
|
||
export async function collectInvalidImageErrors( | ||
markdown: string, | ||
|
@@ -28,7 +29,10 @@ export async function collectInvalidImageErrors( | |
.use(remarkGfm) | ||
.use(() => (tree: Root) => { | ||
visit(tree, "image", (node) => { | ||
if (!node.alt) { | ||
// Sphinx uses the image path as alt text if it wasn't defined using the | ||
// :alt: option. | ||
const imageName = last(split(node.url, "/")); | ||
if (!node.alt || node.alt.endsWith(imageName!)) { | ||
Comment on lines
+32
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call |
||
imagesErrors.add(`The image '${node.url}' does not have alt text.`); | ||
} | ||
}); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Insted, this should go into https://github.com/Qiskit/documentation/blob/main/.github/workflows/api-checks.yml. We want the main job to be as fast as possible for non-API changes. We're okay with API changes being slightly slower (repeating the check of non-API)