Skip to content

Commit

Permalink
refactor and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OzakIOne committed Aug 20, 2024
1 parent e2c4322 commit b89513b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,13 @@ describe('getBlogPostAuthors', () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: {name: 'Sébastien Lorber', title: 'maintainer'},
authors: {
name: 'Sébastien Lorber',
title: 'maintainer',
socials: {
github: 'slorber',
},
},
},
authorsMap: undefined,
baseUrl: '/',
Expand All @@ -282,6 +288,9 @@ describe('getBlogPostAuthors', () => {
name: 'Sébastien Lorber',
title: 'maintainer',
imageURL: undefined,
socials: {
github: 'https://github.com/slorber',
},
key: null,
page: null,
},
Expand All @@ -293,8 +302,19 @@ describe('getBlogPostAuthors', () => {
getBlogPostAuthors({
frontMatter: {
authors: [
{name: 'Sébastien Lorber', title: 'maintainer'},
{name: 'Yangshun Tay'},
{
name: 'Sébastien Lorber',
title: 'maintainer',
socials: {
github: 'slorber',
},
},
{
name: 'Yangshun Tay',
socials: {
github: 'https://github.com/yangshun',
},
},
],
},
authorsMap: undefined,
Expand All @@ -306,9 +326,20 @@ describe('getBlogPostAuthors', () => {
title: 'maintainer',
imageURL: undefined,
key: null,
socials: {
github: 'https://github.com/slorber',
},
page: null,
},
{name: 'Yangshun Tay', imageURL: undefined, key: null, page: null},
{
name: 'Yangshun Tay',
imageURL: undefined,
key: null,
page: null,
socials: {
github: 'https://github.com/yangshun',
},
},
]);
});

Expand All @@ -323,7 +354,12 @@ describe('getBlogPostAuthors', () => {
title: 'Yangshun title local override',
extra: 42,
},
{name: 'Alexey'},
{
name: 'Alexey',
socials: {
github: 'lex111',
},
},
],
},
authorsMap: {
Expand Down Expand Up @@ -358,7 +394,15 @@ describe('getBlogPostAuthors', () => {
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
14 changes: 8 additions & 6 deletions packages/docusaurus-plugin-content-blog/src/authors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,14 @@ function getFrontMatterAuthors(params: AuthorsParam): Author[] {
// becoming a name and may end up unnoticed
return {key: authorInput};
}

return authorInput;
const normalizedSocials = normalizeSocials(authorInput.socials ?? {});

return {
...authorInput,
...(Object.keys(normalizedSocials).length > 0 && {
socials: normalizedSocials,
}),
};
}

return Array.isArray(frontMatter.authors)
Expand Down Expand Up @@ -119,16 +125,12 @@ ${Object.keys(authorsMap)
...getAuthorsMapAuthor(frontMatterAuthor.key),
...frontMatterAuthor,
};
const normalizedSocials = normalizeSocials(frontMatterAuthor.socials ?? {});

return {
...author,
key: author.key ?? null,
page: author.page ?? null,
imageURL: normalizeImageUrl({imageURL: author.imageURL, baseUrl}),
...(Object.keys(normalizedSocials).length > 0 && {
socials: normalizedSocials,
}),
};
}
}
Expand Down

0 comments on commit b89513b

Please sign in to comment.