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

⚠️ Add warnings for missing alt-text and auto-generated alt-text #1814

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
6 changes: 6 additions & 0 deletions .changeset/big-cooks-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"myst-transforms": patch
"myst-common": patch
---

Add warnings for missing alt-text and auto-generated alt-text.
2 changes: 2 additions & 0 deletions packages/myst-common/src/ruleids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export enum RuleId {
imageFormatConverts = 'image-format-converts',
imageCopied = 'image-copied',
imageFormatOptimizes = 'image-format-optimizes',
imageHasAltText = 'image-has-alt-text',
imageAltTextGenerated = 'image-alt-text-generated',
// Math rules
mathLabelLifted = 'math-label-lifted',
mathEquationEnvRemoved = 'math-equation-env-removed',
Expand Down
3 changes: 2 additions & 1 deletion packages/myst-transforms/src/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { captionParagraphTransform } from './caption.js';
import { admonitionBlockquoteTransform, admonitionHeadersTransform } from './admonitions.js';
import { blockMetadataTransform, blockNestingTransform, blockToFigureTransform } from './blocks.js';
import { htmlIdsTransform } from './htmlIds.js';
import { imageAltTextTransform } from './images.js';
import { imageAltTextTransform, imageNoAltTextTransform } from './images.js';
import { mathLabelTransform, mathNestingTransform, subequationTransform } from './math.js';
import { blockquoteTransform } from './blockquote.js';
import { codeBlockToDirectiveTransform, inlineCodeFlattenTransform } from './code.js';
Expand Down Expand Up @@ -40,6 +40,7 @@ export function basicTransformations(tree: GenericParent, file: VFile, opts?: Re
containerChildrenTransform(tree, file);
htmlIdsTransform(tree);
imageAltTextTransform(tree);
imageNoAltTextTransform(tree, file);
blockquoteTransform(tree);
removeUnicodeTransform(tree);
headingDepthTransform(tree, file, opts);
Expand Down
29 changes: 27 additions & 2 deletions packages/myst-transforms/src/images.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { Plugin } from 'unified';
import type { Container, Paragraph, PhrasingContent, Image } from 'myst-spec';
import type { VFile } from 'vfile';
import { select, selectAll } from 'unist-util-select';
import type { GenericParent } from 'myst-common';
import { toText } from 'myst-common';
import { fileWarn, toText, RuleId } from 'myst-common';

const TRANSFORM_SOURCE = 'myst-transforms:images';

/**
* Generate image alt text from figure caption
Expand All @@ -29,6 +32,28 @@ export function imageAltTextTransform(tree: GenericParent) {
});
}

export const imageAltTextPlugin: Plugin<[], GenericParent, GenericParent> = () => (tree) => {
export function imageNoAltTextTransform(tree: GenericParent, file: VFile) {
const imageNodes = selectAll('image', tree) as Image[];
imageNodes.forEach((image, index) => {
if (image.alt == null) {
fileWarn(file, `missing alt text for ${image.url}`, {
ruleId: RuleId.imageHasAltText,
node: image,
source: TRANSFORM_SOURCE,
});
}
if (image.data?.altTextIsAutoGenerated) {
fileWarn(file, `alt text for ${image.url} was auto-generated`, {
ruleId: RuleId.imageAltTextGenerated,
node: image,
source: TRANSFORM_SOURCE,
note: 'You can remove this warning by writing your own altxtext',
});
}
});
}

export const imageAltTextPlugin: Plugin<[], GenericParent, GenericParent> = () => (tree, file) => {
imageAltTextTransform(tree);
imageNoAltTextTransform(tree, file);
};
Loading