Skip to content

Commit

Permalink
feat: enhance NewsArticleJsonLd and ArticleJsonLd author (#1105)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyichv authored Dec 3, 2022
1 parent 7b2ad73 commit 9409826
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/jsonld/newsarticle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { setAuthor } from 'src/utils/schema/setAuthor';
import { setPublisher } from 'src/utils/schema/setPublisher';

import { JsonLd, JsonLdProps } from './jsonld';
import { ArticleAuthor } from '../types';

export interface NewsArticleJsonLdProps extends JsonLdProps {
url: string;
Expand All @@ -13,7 +14,7 @@ export interface NewsArticleJsonLdProps extends JsonLdProps {
dateCreated: string;
datePublished: string;
dateModified?: string;
authorName: string | string[];
authorName: string | string[] | ArticleAuthor | ArticleAuthor[];
description: string;
body: string;
publisherName: string;
Expand All @@ -32,6 +33,7 @@ function NewsArticleJsonLd({
datePublished,
dateModified,
authorName,
authorType,
publisherName,
publisherLogo,
body,
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,8 @@ export type Author = {

export type ArticleAuthor = {
name: string;
url: string;
url?: string;
type?: 'Person' | 'Organization';
};

export type Publisher = {
Expand Down
79 changes: 79 additions & 0 deletions src/utils/schema/__tests__/setAuthor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { ArticleAuthor } from 'src/types';
import { setAuthor } from '../setAuthor';

describe('setAuthor', () => {
test('should return undefined if author is undefined', () => {
expect(setAuthor(undefined)).toBeUndefined();
});

test('works correctly when ArticleAuthor is passed', () => {
const author: ArticleAuthor = {
name: 'Acme',
type: 'Organization',
url: '/acme',
};

const data = setAuthor(author);

expect(data).toEqual({
'@type': 'Organization',
name: 'Acme',
url: '/acme',
});
});

test('works correctly when just ArticleAuthor name is passed', () => {
const data = setAuthor({ name: 'John Doe' });

expect(data).toEqual({
'@type': 'Person',
name: 'John Doe',
});
});

test('works correctly when an array of ArticleAuthor is passed', () => {
const author: ArticleAuthor[] = [
{
name: 'Acme',
type: 'Organization',
url: '/acme',
},
{ name: 'John Doe' },
];

const data = setAuthor(author);

expect(data).toEqual([
{
'@type': 'Organization',
name: 'Acme',
url: '/acme',
},
{ '@type': 'Person', name: 'John Doe' },
]);
});

test('works correctly when string is passed', () => {
const data = setAuthor('John Doe');

expect(data).toEqual({
'@type': 'Person',
name: 'John Doe',
});
});

test('works correctly when an array string is passed', () => {
const data = setAuthor(['John Doe', 'Foo Bar']);

expect(data).toEqual([
{
'@type': 'Person',
name: 'John Doe',
},
{
'@type': 'Person',
name: 'Foo Bar',
},
]);
});
});
15 changes: 3 additions & 12 deletions src/utils/schema/setAuthor.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { ArticleAuthor } from '../../types';

/**
* Include name and url
* @param author
* @returns
*/
function includeNameAndUrl(author: ArticleAuthor): boolean {
return typeof author === 'object' && !!(author.name && author.url);
}

/**
* Generate author information
* @param author
Expand All @@ -20,11 +11,11 @@ function generateAuthorInfo(author: string | ArticleAuthor) {
'@type': 'Person',
name: author,
};
} else if (includeNameAndUrl(author)) {
} else if (!!author.name) {
return {
'@type': 'Person',
'@type': author?.type ?? 'Person',
name: author.name,
url: author.url,
url: author?.url,
};
}

Expand Down

0 comments on commit 9409826

Please sign in to comment.