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 movie json ld #1485

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
69 changes: 69 additions & 0 deletions cypress/e2e/movie.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { assertSchema } from '@cypress/schema-tools';
import schemas from '../schemas';

describe('Movie JSON-LD', () => {
it('matches schema', () => {
cy.visit('http://localhost:3000/jsonld/movie');
cy.get('head script[type="application/ld+json"]').then(tags => {
const jsonLD = JSON.parse(tags[0].innerHTML);
assertSchema(schemas)('Movie', '1.0.0')(jsonLD);
});
});

it('renders with all props', () => {
cy.visit('http://localhost:3000/jsonld/movie');
cy.get('head script[type="application/ld+json"]').then(tags => {
const jsonLD = JSON.parse(tags[0].innerHTML);

expect(jsonLD).to.deep.equal({
'@context': 'https://schema.org',
'@type': 'Movie',
name: 'Movie Example',
contentRating: 5,
duration: 'PT2H30M',
dateCreated: '2022-01-01',
description: 'This is a movie description.',
image: 'https://example.com/movie.jpg',
author: { '@type': 'Person', name: 'Author Name' },
director: { '@type': 'Person', name: 'Director Name' },
actor: [
{
'@type': 'Person',
name: 'John Doe',
characterName: 'John Doe',
},
{
'@type': 'Person',
name: 'Jane Doe',
characterName: 'Jane Doe',
},
],
genre: ['Action', 'Adventure'],
offers: [
{
'@type': 'Offer',
price: '12.99',
priceCurrency: 'USD',
availability: 'https://schema.org/InStock',
url: 'https://example.com/movie',
seller: {
'@type': 'Organization',
name: 'Seller Name',
},
},
],
trailer: 'https://example.com/movie-trailer',
countryOfOrigin: 'USA',
aggregateRating: {
'@type': 'AggregateRating',
ratingValue: '44',
reviewCount: '89',
ratingCount: '684',
bestRating: '100',
worstRating: '1',
},
isAccessibleForFree: true,
});
});
});
});
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export {
default as LocalBusinessJsonLd,
LocalBusinessJsonLdProps,
} from './jsonld/localBusiness';
export { default as MovieJsonLd, MovieJsonLdProps } from './jsonld/movie';
export { default as QAPageJsonLd, QAPageJsonLdProps } from './jsonld/qaPage';
export {
default as ProfilePageJsonLd,
Expand Down
60 changes: 60 additions & 0 deletions src/jsonld/movie.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from 'react';
import { Actor, AggregateRating, Offers, Video } from 'src/types';
import { JsonLd, JsonLdProps } from './jsonld';

import { setAuthor } from 'src/utils/schema/setAuthor';
import { setOffers } from 'src/utils/schema/setOffers';
import { setAggregateRating } from 'src/utils/schema/setAggregateRating';
import { setActors } from 'src/utils/schema/setActors';
import { setDirector } from 'src/utils/schema/setDirector';
import { setVideo } from 'src/utils/schema/setVideo';

export interface MovieJsonLdProps extends JsonLdProps {
keyOverride?: string;
name: string;
contentRating: number;
duration: string;
dateCreated: string;
description: string;
image: string;
authorName?: string;
directorName?: string;
actors?: Actor | Actor[];
genreName?: string | string[];
trailer?: Video;
offers?: Offers | Offers[];
countryOfOrigin?: string;
aggregateRating?: AggregateRating;
}

function MovieJsonLd({
type = 'Movie',
keyOverride,
authorName,
directorName,
actors,
genreName,
trailer,
offers,
countryOfOrigin,
aggregateRating,
...rest
}: MovieJsonLdProps) {
const data = {
...rest,
author: setAuthor(authorName),
director: setDirector(directorName),
actors: setActors(actors),
genre: genreName,
trailer: setVideo(trailer),
offers: setOffers(offers),
countryOfOrigin: countryOfOrigin,
aggregateRating: setAggregateRating(aggregateRating),
};

return (
<JsonLd type={type} keyOverride={keyOverride} {...data} scriptKey="Movie" />
);
}

export default MovieJsonLd;
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ export type Author = {
name: string;
};

export type Actor = {
actor: string;
characterName?: string;
};

export type ArticleAuthor = {
name: string;
url?: string;
Expand Down
24 changes: 24 additions & 0 deletions src/utils/schema/setActors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Actor } from 'src/types';

export function setActors(actors?: Actor | Actor[]) {
function mapOffer({ actor, characterName }: Actor) {
return {
'@type': 'PerformanceRole',
...(actors && {
actor: {
'@type': 'Person',
name: actor,
},
...(characterName && { characterName: characterName }),
}),
};
}

if (Array.isArray(actors)) {
return actors.map(mapOffer);
} else if (actors) {
return mapOffer(actors);
}

return undefined;
}
9 changes: 9 additions & 0 deletions src/utils/schema/setDirector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function setDirector(director?: string) {
if (director) {
return {
'@type': 'Person',
name: director,
};
}
return undefined;
}