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

Added og:image and alt text checks to linter for Stories #755

Merged
merged 6 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/linter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { SxgVaryOnAcceptAct } from "./rules/SxgVaryOnAcceptAct";
import { SxgContentNegotiationIsOk } from "./rules/SxgContentNegotiationIsOk";
import { SxgDumpSignedExchangeVerify } from "./rules/SxgDumpSignedExchangeVerify";
import { SxgAmppkgIsForwarded } from "./rules/SxgAmppkgIsForwarded";
import { MetadataIncludesOGImageSrc } from "./rules/MetadataIncludesOGImageSrc";
import { ImagesHaveAltText } from "./rules/ImagesHaveAltText";
import { RuleConstructor } from "./rule";
import { isArray } from "util";

Expand Down Expand Up @@ -100,6 +102,8 @@ function testsForMode(type: LintMode) {
StoryMetadataIsV1,
StoryIsMostlyText,
StoryMetadataThumbnailsAreOk,
MetadataIncludesOGImageSrc,
ImagesHaveAltText,
])
);
return tests.get(type) || [];
Expand Down Expand Up @@ -148,4 +152,4 @@ export async function lint(
);
}

export { cli };
export { cli };
30 changes: 30 additions & 0 deletions packages/linter/src/rules/ImagesHaveAltText.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

import { Context } from "../index";
import { Rule } from "../rule";

export class ImagesHaveAltText extends Rule {
run({ $ }: Context) {
let containsAltText = true;
let imgsWithoutAlt = "";

$('amp-img').each(function (i, elem) {
if(!(elem.attribs.alt)) {
containsAltText = false;
imgsWithoutAlt = imgsWithoutAlt + "- " + elem.attribs.src + "\n";
}

});

return !containsAltText
? this.warn(`Missing alt text from images: \n` + imgsWithoutAlt)
: this.pass();
}
meta() {
return {
url:
"https://html.spec.whatwg.org/multipage/parsing.html#determining-the-character-encoding",
Dbrown910 marked this conversation as resolved.
Show resolved Hide resolved
title: "Images contain alt text",
info: "",
};
}
}
30 changes: 30 additions & 0 deletions packages/linter/src/rules/MetadataIncludesOGImageSrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Context } from "../index";
import { Rule } from "../rule";

export class MetadataIncludesOGImageSrc extends Rule {
run({ $ }: Context) {

let hasOGImage = false;

$('meta').each(function (i, elem) {
Dbrown910 marked this conversation as resolved.
Show resolved Hide resolved
if(elem.attribs.property === 'og:image' && elem.attribs.content) {
hasOGImage = true;
return false; //break the loop
}
});


return !(hasOGImage)
? this.warn(`Missing og:image property or content source`)
: this.pass();
}
meta() {
return {
url:
"https://html.spec.whatwg.org/multipage/parsing.html#determining-the-character-encoding",
Dbrown910 marked this conversation as resolved.
Show resolved Hide resolved
title: "Metadata includes og:image and src",
info: "",
};
}
}