Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
OzakIOne committed Aug 22, 2024
1 parent 1e30108 commit 58a350d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

import {fromPartial, type PartialDeep} from '@total-typescript/shoehorn';
import {getBlogPostAuthors, groupBlogPostsByAuthorKey} from '../authors';
import type {AuthorsMap, BlogPost} from '@docusaurus/plugin-content-blog';
import type {
AuthorAttributes,
AuthorsMap,
BlogPost,
} from '@docusaurus/plugin-content-blog';

function post(partial: PartialDeep<BlogPost>): BlogPost {
return fromPartial(partial);
Expand Down Expand Up @@ -268,6 +272,50 @@ describe('getBlogPostAuthors', () => {
]);
});

it('read different values from socials', () => {
function testSocials(socials: AuthorAttributes['socials'] | undefined) {
return getBlogPostAuthors({
frontMatter: {
authors: {
name: 'Sébastien Lorber',
title: 'maintainer',
socials,
},
},
authorsMap: undefined,
baseUrl: '/',
});
}

// @ts-expect-error test
expect(() => testSocials(null)).not.toThrow();
// @ts-expect-error test
expect(testSocials(null)).toEqual([
{
name: 'Sébastien Lorber',
title: 'maintainer',
imageURL: undefined,
socials: {},
key: null,
page: null,
},
]);
expect(() => () => testSocials(undefined)).not.toThrow();
// @ts-expect-error test
expect(() => testSocials({twitter: undefined}))
.toThrowErrorMatchingInlineSnapshot(`
"Author socials should be usernames/userIds/handles, or fully qualified HTTP(s) absolute URLs.
Social platform 'twitter' has illegal value 'undefined'"
`);
expect(
// @ts-expect-error test
() => testSocials({twitter: null}),
).toThrowErrorMatchingInlineSnapshot(`
"Author socials should be usernames/userIds/handles, or fully qualified HTTP(s) absolute URLs.
Social platform 'twitter' has illegal value 'null'"
`);
});

it('can read empty socials', () => {
expect(
getBlogPostAuthors({
Expand Down Expand Up @@ -384,7 +432,7 @@ describe('getBlogPostAuthors', () => {
},
},
{
name: 'Slorber',
name: 'Seb',
socials: {
github: 'https://github.com/slorber',
linkedin: 'https://www.linkedin.com/in/sebastienlorber/',
Expand Down Expand Up @@ -414,7 +462,7 @@ describe('getBlogPostAuthors', () => {
page: null,
},
{
name: 'Slorber',
name: 'Seb',
imageURL: undefined,
key: null,
socials: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ describe('authors socials', () => {
});

it('throw socials that are not strings', () => {
const socialnumber: AuthorsMapInput = {
const socialNumber: AuthorsMapInput = {
ozaki: {
name: 'ozaki',
socials: {
Expand All @@ -265,7 +265,7 @@ describe('authors socials', () => {
},
};

const socialnull: AuthorsMapInput = {
const socialNull: AuthorsMapInput = {
ozaki: {
name: 'ozaki',
socials: {
Expand All @@ -275,7 +275,7 @@ describe('authors socials', () => {
},
};

const socialnull2: AuthorsMapInput = {
const socialNull2: AuthorsMapInput = {
ozaki: {
name: 'ozaki',
// @ts-expect-error: for tests
Expand All @@ -284,17 +284,17 @@ describe('authors socials', () => {
};

expect(() =>
validateAuthorsMap(socialnumber),
validateAuthorsMap(socialNumber),
).toThrowErrorMatchingInlineSnapshot(
`""ozaki.socials.twitter" must be a string"`,
);
expect(() =>
validateAuthorsMap(socialnull),
validateAuthorsMap(socialNull),
).toThrowErrorMatchingInlineSnapshot(
`""ozaki.socials.twitter" must be a string"`,
);
expect(() =>
validateAuthorsMap(socialnull2),
validateAuthorsMap(socialNull2),
).toThrowErrorMatchingInlineSnapshot(
`""ozaki.socials" should be an author object containing properties like name, title, and imageURL."`,
);
Expand Down
6 changes: 6 additions & 0 deletions packages/docusaurus-plugin-content-blog/src/authorsSocials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ type SocialEntry = [string, string];

function normalizeSocialEntry([platform, value]: SocialEntry): SocialEntry {
const normalizer = PredefinedPlatformNormalizers[platform.toLowerCase()];
if (typeof value !== 'string') {
throw new Error(
`Author socials should be usernames/userIds/handles, or fully qualified HTTP(s) absolute URLs.
Social platform '${platform}' has illegal value '${value}'`,
);
}
const isAbsoluteUrl =
value.startsWith('http://') || value.startsWith('https://');
if (isAbsoluteUrl) {
Expand Down

0 comments on commit 58a350d

Please sign in to comment.