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

fix(blog): normalize inline authors socials #10424

Merged
merged 10 commits into from
Aug 29, 2024
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,11 +272,176 @@
]);
});

it('can read authors Author', () => {
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({
frontMatter: {
authors: {
name: 'Sébastien Lorber',
title: 'maintainer',
socials: {},
OzakIOne marked this conversation as resolved.
Show resolved Hide resolved
},
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([
{
name: 'Sébastien Lorber',
title: 'maintainer',
imageURL: undefined,
socials: {},
key: null,
page: null,
},
]);
});

it('can normalize full socials from Author', () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: {
name: 'Sébastien Lorber',
title: 'maintainer',
socials: {
github: 'https://github.com/slorber',
linkedin: 'https://www.linkedin.com/in/sebastienlorber/',
stackoverflow: 'https://stackoverflow.com/users/82609',
twitter: 'https://twitter.com/sebastienlorber',
x: 'https://x.com/sebastienlorber',
},
},
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([
{
name: 'Sébastien Lorber',
title: 'maintainer',
imageURL: undefined,
key: null,
socials: {
github: 'https://github.com/slorber',
linkedin: 'https://www.linkedin.com/in/sebastienlorber/',
stackoverflow: 'https://stackoverflow.com/users/82609',
twitter: 'https://twitter.com/sebastienlorber',
x: 'https://x.com/sebastienlorber',
},
page: null,
},
]);
});

it('can normalize handle socials from Author', () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: {
name: 'Sébastien Lorber',
title: 'maintainer',
socials: {
github: 'slorber',
x: 'sebastienlorber',
linkedin: 'sebastienlorber',
stackoverflow: '82609',
twitter: 'sebastienlorber',
},
},
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([
{
name: 'Sébastien Lorber',
title: 'maintainer',
imageURL: undefined,
key: null,
socials: {
github: 'https://github.com/slorber',
linkedin: 'https://www.linkedin.com/in/sebastienlorber/',
stackoverflow: 'https://stackoverflow.com/users/82609',
twitter: 'https://twitter.com/sebastienlorber',
x: 'https://x.com/sebastienlorber',
},
page: null,
},
]);
});

it('can normalize socials from Author[]', () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: {name: 'Sébastien Lorber', title: 'maintainer'},
authors: [
{
name: 'Sébastien Lorber',
title: 'maintainer',
socials: {
github: 'slorber',
x: 'sebastienlorber',
linkedin: 'sebastienlorber',
stackoverflow: '82609',
twitter: 'sebastienlorber',
},
},
{
name: 'Seb',
socials: {
github: 'https://github.com/slorber',
linkedin: 'https://www.linkedin.com/in/sebastienlorber/',
stackoverflow: 'https://stackoverflow.com/users/82609',
twitter: 'https://twitter.com/sebastienlorber',
x: 'https://x.com/sebastienlorber',
},
},
],
},
authorsMap: undefined,
baseUrl: '/',
Expand All @@ -283,6 +452,26 @@
title: 'maintainer',
imageURL: undefined,
key: null,
socials: {
github: 'https://github.com/slorber',
linkedin: 'https://www.linkedin.com/in/sebastienlorber/',
stackoverflow: 'https://stackoverflow.com/users/82609',
twitter: 'https://twitter.com/sebastienlorber',
x: 'https://x.com/sebastienlorber',
},
page: null,
},
{
name: 'Seb',
imageURL: undefined,
key: null,
socials: {
github: 'https://github.com/slorber',
linkedin: 'https://www.linkedin.com/in/sebastienlorber/',
stackoverflow: 'https://stackoverflow.com/users/82609',
twitter: 'https://twitter.com/sebastienlorber',
x: 'https://x.com/sebastienlorber',
},
page: null,
},
]);
Expand All @@ -293,8 +482,13 @@
getBlogPostAuthors({
frontMatter: {
authors: [
{name: 'Sébastien Lorber', title: 'maintainer'},
{name: 'Yangshun Tay'},
{
name: 'Sébastien Lorber',
title: 'maintainer',
},
{
name: 'Yangshun Tay',
},
],
},
authorsMap: undefined,
Expand All @@ -306,9 +500,16 @@
title: 'maintainer',
imageURL: undefined,
key: null,
socials: {},
page: null,
},
{
name: 'Yangshun Tay',
imageURL: undefined,
socials: {},
key: null,
page: null,
},
{name: 'Yangshun Tay', imageURL: undefined, key: null, page: null},
]);
});

Expand All @@ -323,7 +524,12 @@
title: 'Yangshun title local override',
extra: 42,
},
{name: 'Alexey'},
{
name: 'Alexey',
socials: {
github: 'lex111',
},
},
],
},
authorsMap: {
Expand Down Expand Up @@ -355,10 +561,19 @@
name: 'Yangshun Tay',
title: 'Yangshun title local override',
extra: 42,
socials: {},
imageURL: undefined,
page: null,
},
{name: 'Alexey', imageURL: undefined, key: null, page: null},
{
name: 'Alexey',
imageURL: undefined,
key: null,
page: null,
socials: {
github: 'https://github.com/lex111',
},
},
]);
});

Expand Down Expand Up @@ -622,7 +837,7 @@
baseUrl: '/',
});
expect(() => baseUrlTest).not.toThrow();
expect(baseUrlTest).toEqual([

Check failure on line 840 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Tests (18.0)

getBlogPostAuthors › getBlogPostAuthors do not throws if inline author imageURL is a link to a file

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "./ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:840:25)

Check failure on line 840 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Tests (20)

getBlogPostAuthors › getBlogPostAuthors do not throws if inline author imageURL is a link to a file

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "./ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:840:25)

Check failure on line 840 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Tests (22)

getBlogPostAuthors › getBlogPostAuthors do not throws if inline author imageURL is a link to a file

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "./ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:840:25)

Check failure on line 840 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Windows Tests (18.0)

getBlogPostAuthors › getBlogPostAuthors do not throws if inline author imageURL is a link to a file

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "./ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:840:25)

Check failure on line 840 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Windows Tests (22)

getBlogPostAuthors › getBlogPostAuthors do not throws if inline author imageURL is a link to a file

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "./ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:840:25)
{
imageURL: './ozaki.png',
key: null,
Expand All @@ -649,7 +864,7 @@
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([

Check failure on line 867 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Tests (18.0)

getBlogPostAuthors › getBlogPostAuthors can return imageURL without baseUrl for inline authors

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "/ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:867:7)

Check failure on line 867 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Tests (20)

getBlogPostAuthors › getBlogPostAuthors can return imageURL without baseUrl for inline authors

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "/ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:867:7)

Check failure on line 867 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Tests (22)

getBlogPostAuthors › getBlogPostAuthors can return imageURL without baseUrl for inline authors

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "/ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:867:7)

Check failure on line 867 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Windows Tests (18.0)

getBlogPostAuthors › getBlogPostAuthors can return imageURL without baseUrl for inline authors

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "/ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:867:7)

Check failure on line 867 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Windows Tests (22)

getBlogPostAuthors › getBlogPostAuthors can return imageURL without baseUrl for inline authors

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "/ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:867:7)
{
imageURL: '/ozaki.png',
key: null,
Expand All @@ -668,7 +883,7 @@
authorsMap: undefined,
baseUrl: '/img/',
}),
).toEqual([

Check failure on line 886 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Tests (18.0)

getBlogPostAuthors › getBlogPostAuthors normalize imageURL with baseUrl for inline authors

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "/img/ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:886:7)

Check failure on line 886 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Tests (20)

getBlogPostAuthors › getBlogPostAuthors normalize imageURL with baseUrl for inline authors

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "/img/ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:886:7)

Check failure on line 886 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Tests (22)

getBlogPostAuthors › getBlogPostAuthors normalize imageURL with baseUrl for inline authors

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "/img/ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:886:7)

Check failure on line 886 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Windows Tests (18.0)

getBlogPostAuthors › getBlogPostAuthors normalize imageURL with baseUrl for inline authors

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "/img/ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:886:7)

Check failure on line 886 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Windows Tests (22)

getBlogPostAuthors › getBlogPostAuthors normalize imageURL with baseUrl for inline authors

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "/img/ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:886:7)
{
imageURL: '/img/ozaki.png',
key: null,
Expand All @@ -687,7 +902,7 @@
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([

Check failure on line 905 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Tests (18.0)

getBlogPostAuthors › getBlogPostAuthors normalize imageURL from subfolder without baseUrl for inline authors

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "/img/ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:905:7)

Check failure on line 905 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Tests (20)

getBlogPostAuthors › getBlogPostAuthors normalize imageURL from subfolder without baseUrl for inline authors

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "/img/ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:905:7)

Check failure on line 905 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Tests (22)

getBlogPostAuthors › getBlogPostAuthors normalize imageURL from subfolder without baseUrl for inline authors

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "/img/ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:905:7)

Check failure on line 905 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Windows Tests (18.0)

getBlogPostAuthors › getBlogPostAuthors normalize imageURL from subfolder without baseUrl for inline authors

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "/img/ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:905:7)

Check failure on line 905 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Windows Tests (22)

getBlogPostAuthors › getBlogPostAuthors normalize imageURL from subfolder without baseUrl for inline authors

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "/img/ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:905:7)
{
imageURL: '/img/ozaki.png',
key: null,
Expand All @@ -706,7 +921,7 @@
authorsMap: undefined,
baseUrl: '/img/',
}),
).toEqual([

Check failure on line 924 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Tests (18.0)

getBlogPostAuthors › getBlogPostAuthors normalize imageURL from subfolder with baseUrl for inline authors

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "/img/img/ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:924:7)

Check failure on line 924 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Tests (20)

getBlogPostAuthors › getBlogPostAuthors normalize imageURL from subfolder with baseUrl for inline authors

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "/img/img/ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:924:7)

Check failure on line 924 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Tests (22)

getBlogPostAuthors › getBlogPostAuthors normalize imageURL from subfolder with baseUrl for inline authors

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "/img/img/ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:924:7)

Check failure on line 924 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Windows Tests (18.0)

getBlogPostAuthors › getBlogPostAuthors normalize imageURL from subfolder with baseUrl for inline authors

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "/img/img/ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:924:7)

Check failure on line 924 in packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

View workflow job for this annotation

GitHub Actions / Windows Tests (22)

getBlogPostAuthors › getBlogPostAuthors normalize imageURL from subfolder with baseUrl for inline authors

expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 1 Array [ Object { "imageURL": "/img/img/ozaki.png", "key": null, "page": null, + "socials": Object {}, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:924:7)
{
imageURL: '/img/img/ozaki.png',
key: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ describe('authors socials', () => {
});

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

const socialNull: AuthorsMapInput = {
ozaki: {
name: 'ozaki',
socials: {
// @ts-expect-error: for tests
twitter: null,
},
},
};

const socialNull2: AuthorsMapInput = {
ozaki: {
name: 'ozaki',
// @ts-expect-error: for tests
socials: null,
},
};

expect(() =>
validateAuthorsMap(authorsMap),
validateAuthorsMap(socialNumber),
).toThrowErrorMatchingInlineSnapshot(
`""ozaki.socials.twitter" must be a string"`,
);
expect(() =>
validateAuthorsMap(socialNull),
).toThrowErrorMatchingInlineSnapshot(
`""ozaki.socials.twitter" must be a string"`,
);
expect(() =>
validateAuthorsMap(socialNull2),
).toThrowErrorMatchingInlineSnapshot(
`""ozaki.socials" should be an author object containing properties like name, title, and imageURL."`,
);
});

it('throw socials that are objects', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,15 @@ describe('blog plugin', () => {
imageURL: undefined,
key: null,
page: null,
socials: {},
},
{
email: '[email protected]',
key: 'slorber',
name: 'Sébastien Lorber (translated)',
title: 'Docusaurus maintainer (translated)',
imageURL: undefined,
socials: undefined,
page: {permalink: '/blog/authors/slorber-custom-permalink-localized'},
},
],
Expand Down
6 changes: 5 additions & 1 deletion packages/docusaurus-plugin-content-blog/src/authors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import _ from 'lodash';
import {normalizeUrl} from '@docusaurus/utils';
import {normalizeSocials} from './authorsSocials';
import type {
Author,
AuthorsMap,
Expand Down Expand Up @@ -107,7 +108,10 @@ function getFrontMatterAuthors(params: AuthorsParam): Author[] {
// becoming a name and may end up unnoticed
return {key: authorInput};
}
return authorInput;
return {
...authorInput,
socials: normalizeSocials(authorInput.socials ?? {}),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here if author front matter has no social key, the result will be social: {}.

If the key was absent before normalization, we'd rather keep it absent after normalization (unless it's an explicit goal of the normalization to always have an object for empty (in which case the normalized type shouldn't allow null/undefined anymore)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm +1 to make socials required—unless we plan to make socials: null and socials: {} render different things, for which I cannot conceive a reason.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we plan render those differently.

I also like to normalize to {} for convenience of frontend code, although technically this slightly increases bundle size / memory by creating useless empty objects when we could use optional chaining. Maybe always normalizing to undefined would be better? That's really a detail so I'm fine with any of those.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think neither of those would be a concern when there aren't thousands of authors (by which time the blog posts' metadata is a far bigger concern).

};
}

return Array.isArray(frontMatter.authors)
Expand Down
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
Loading