Skip to content

Commit

Permalink
wip: refactor & add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OzakIOne committed Nov 24, 2023
1 parent 0d0f858 commit 59494e0
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,66 @@ describe('getBlogPostAuthors', () => {
baseUrl: '/',
}),
).toEqual([{key: 'slorber', name: 'Sébastien Lorber'}]);
expect(
getBlogPostAuthors({
frontMatter: {
authors: 'slorber',
},
authorsMap: {
slorber: {
name: 'Sébastien Lorber',
imageURL: 'https://github.com/slorber.png',
},
},
baseUrl: '/',
}),
).toEqual([
{
key: 'slorber',
name: 'Sébastien Lorber',
imageURL: 'https://github.com/slorber.png',
},
]);
expect(
getBlogPostAuthors({
frontMatter: {
authors: 'slorber',
},
authorsMap: {
slorber: {
name: 'Sébastien Lorber',
imageURL: '/img/slorber.png',
},
},
baseUrl: '/',
}),
).toEqual([
{
key: 'slorber',
name: 'Sébastien Lorber',
imageURL: '/img/slorber.png',
},
]);
expect(
getBlogPostAuthors({
frontMatter: {
authors: 'slorber',
},
authorsMap: {
slorber: {
name: 'Sébastien Lorber',
imageURL: '/img/slorber.png',
},
},
baseUrl: '/baseUrl',
}),
).toEqual([

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

View workflow job for this annotation

GitHub Actions / Windows Tests (18.0)

getBlogPostAuthors › can read authors string

expect(received).toEqual(expected) // deep equality - Expected - 1 + Received + 1 Array [ Object { - "imageURL": "/baseUrl/img/slorber.png", + "imageURL": "\\baseUrl\\img\\slorber.png", "key": "slorber", "name": "Sébastien Lorber", }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:169:7)

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

View workflow job for this annotation

GitHub Actions / Windows Tests (18)

getBlogPostAuthors › can read authors string

expect(received).toEqual(expected) // deep equality - Expected - 1 + Received + 1 Array [ Object { - "imageURL": "/baseUrl/img/slorber.png", + "imageURL": "\\baseUrl\\img\\slorber.png", "key": "slorber", "name": "Sébastien Lorber", }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:169:7)
{
key: 'slorber',
name: 'Sébastien Lorber',
imageURL: '/baseUrl/img/slorber.png',
},
]);
});

it('can read authors string[]', () => {
Expand Down
24 changes: 14 additions & 10 deletions packages/docusaurus-plugin-content-blog/src/authors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,20 @@ type AuthorsParam = {

// Legacy v1/early-v2 front matter fields
// We may want to deprecate those in favor of using only frontMatter.authors
function getFrontMatterAuthorLegacy(params: AuthorsParam): Author | undefined {
const name = params.frontMatter.author;
const title =
params.frontMatter.author_title ?? params.frontMatter.authorTitle;
const url = params.frontMatter.author_url ?? params.frontMatter.authorURL;
let imageURL =
params.frontMatter.author_image_url ?? params.frontMatter.authorImageURL;

if (params.baseUrl !== '/' && imageURL && !imageURL.startsWith('http')) {
imageURL = path.join(params.baseUrl, imageURL);
function getFrontMatterAuthorLegacy({
baseUrl,
frontMatter,
}: {
baseUrl: string;
frontMatter: BlogPostFrontMatter;
}): Author | undefined {
const name = frontMatter.author;
const title = frontMatter.author_title ?? frontMatter.authorTitle;
const url = frontMatter.author_url ?? frontMatter.authorURL;
let imageURL = frontMatter.author_image_url ?? frontMatter.authorImageURL;

if (baseUrl !== '/' && imageURL && !imageURL.startsWith('http')) {
imageURL = path.join(baseUrl, imageURL);
}

if (name || title || url || imageURL) {
Expand Down

0 comments on commit 59494e0

Please sign in to comment.