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(content-blog): add baseUrl for author.image_url #9581

Merged
merged 4 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
getBlogPostAuthors({
frontMatter: {},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([]);
expect(
Expand All @@ -27,6 +28,7 @@
authors: [],
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([]);
});
Expand All @@ -38,6 +40,7 @@
author: 'Sébastien Lorber',
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([{name: 'Sébastien Lorber'}]);
expect(
Expand All @@ -46,6 +49,7 @@
authorTitle: 'maintainer',
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([{title: 'maintainer'}]);
expect(
Expand All @@ -54,8 +58,27 @@
authorImageURL: 'https://github.com/slorber.png',
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([{imageURL: 'https://github.com/slorber.png'}]);
expect(
getBlogPostAuthors({
frontMatter: {
authorImageURL: '/img/slorber.png',
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([{imageURL: '/img/slorber.png'}]);
expect(
getBlogPostAuthors({
frontMatter: {
authorImageURL: '/img/slorber.png',
},
authorsMap: undefined,
baseUrl: '/baseURL',
}),
).toEqual([{imageURL: '/baseURL/img/slorber.png'}]);

Check failure on line 81 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 author from legacy front matter

expect(received).toEqual(expected) // deep equality - Expected - 1 + Received + 4 Array [ Object { - "imageURL": "/baseURL/img/slorber.png", + "imageURL": "\\baseURL\\img\\slorber.png", + "name": undefined, + "title": undefined, + "url": undefined, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:81:7)

Check failure on line 81 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 author from legacy front matter

expect(received).toEqual(expected) // deep equality - Expected - 1 + Received + 4 Array [ Object { - "imageURL": "/baseURL/img/slorber.png", + "imageURL": "\\baseURL\\img\\slorber.png", + "name": undefined, + "title": undefined, + "url": undefined, }, ] at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts:81:7)
expect(
getBlogPostAuthors({
frontMatter: {
Expand All @@ -68,6 +91,7 @@
authorURL: 'https://github.com/slorber2',
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([
{
Expand All @@ -86,8 +110,69 @@
authors: 'slorber',
},
authorsMap: {slorber: {name: 'Sébastien Lorber'}},
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 All @@ -100,6 +185,7 @@
slorber: {name: 'Sébastien Lorber', title: 'maintainer'},
yangshun: {name: 'Yangshun Tay'},
},
baseUrl: '/',
}),
).toEqual([
{key: 'slorber', name: 'Sébastien Lorber', title: 'maintainer'},
Expand All @@ -114,6 +200,7 @@
authors: {name: 'Sébastien Lorber', title: 'maintainer'},
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([{name: 'Sébastien Lorber', title: 'maintainer'}]);
});
Expand All @@ -128,6 +215,7 @@
],
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([
{name: 'Sébastien Lorber', title: 'maintainer'},
Expand All @@ -153,6 +241,7 @@
slorber: {name: 'Sébastien Lorber', title: 'maintainer'},
yangshun: {name: 'Yangshun Tay', title: 'Yangshun title original'},
},
baseUrl: '/',
}),
).toEqual([
{key: 'slorber', name: 'Sébastien Lorber', title: 'maintainer'},
Expand All @@ -173,6 +262,7 @@
authors: 'slorber',
},
authorsMap: undefined,
baseUrl: '/',
}),
).toThrowErrorMatchingInlineSnapshot(`
"Can't reference blog post authors by a key (such as 'slorber') because no authors map file could be loaded.
Expand All @@ -187,6 +277,7 @@
authors: 'slorber',
},
authorsMap: {},
baseUrl: '/',
}),
).toThrowErrorMatchingInlineSnapshot(`
"Can't reference blog post authors by a key (such as 'slorber') because no authors map file could be loaded.
Expand All @@ -205,6 +296,7 @@
yangshun: {name: 'Yangshun Tay'},
jmarcey: {name: 'Joel Marcey'},
},
baseUrl: '/',
}),
).toThrowErrorMatchingInlineSnapshot(`
"Blog author with key "slorber" not found in the authors map file.
Expand All @@ -225,6 +317,7 @@
yangshun: {name: 'Yangshun Tay'},
jmarcey: {name: 'Joel Marcey'},
},
baseUrl: '/',
}),
).toThrowErrorMatchingInlineSnapshot(`
"Blog author with key "slorber" not found in the authors map file.
Expand All @@ -245,6 +338,7 @@
yangshun: {name: 'Yangshun Tay'},
jmarcey: {name: 'Joel Marcey'},
},
baseUrl: '/',
}),
).toThrowErrorMatchingInlineSnapshot(`
"Blog author with key "slorber" not found in the authors map file.
Expand All @@ -262,6 +356,7 @@
author: 'Yangshun Tay',
},
authorsMap: undefined,
baseUrl: '/',
}),
).toThrowErrorMatchingInlineSnapshot(`
"To declare blog post authors, use the 'authors' front matter in priority.
Expand All @@ -275,6 +370,7 @@
author_title: 'legacy title',
},
authorsMap: {slorber: {name: 'Sébastien Lorber'}},
baseUrl: '/',
}),
).toThrowErrorMatchingInlineSnapshot(`
"To declare blog post authors, use the 'authors' front matter in priority.
Expand Down
38 changes: 33 additions & 5 deletions packages/docusaurus-plugin-content-blog/src/authors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import path from 'path';
import {getDataFileData} from '@docusaurus/utils';
import {Joi, URISchema} from '@docusaurus/utils-validation';
import type {BlogContentPaths} from './types';
Expand Down Expand Up @@ -68,17 +69,26 @@ export async function getAuthorsMap(params: {
type AuthorsParam = {
frontMatter: BlogPostFrontMatter;
authorsMap: AuthorsMap | undefined;
baseUrl: string;
};

// Legacy v1/early-v2 front matter fields
// We may want to deprecate those in favor of using only frontMatter.authors
function getFrontMatterAuthorLegacy(
frontMatter: BlogPostFrontMatter,
): Author | undefined {
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;
const imageURL = frontMatter.author_image_url ?? frontMatter.authorImageURL;
let imageURL = frontMatter.author_image_url ?? frontMatter.authorImageURL;

if (baseUrl !== '/' && imageURL && !imageURL.startsWith('http')) {
imageURL = path.join(baseUrl, imageURL);
}
OzakIOne marked this conversation as resolved.
Show resolved Hide resolved

if (name || title || url || imageURL) {
return {
Expand Down Expand Up @@ -148,10 +158,28 @@ ${Object.keys(authorsMap)
return frontMatterAuthors.map(toAuthor);
}

function fixAuthorImageBaseURL(
authors: Author[],
{baseUrl}: {baseUrl: string},
) {
authors.forEach((author) => {
OzakIOne marked this conversation as resolved.
Show resolved Hide resolved
if (
!author.imageURL ||
baseUrl === '/' ||
author.imageURL.startsWith('http')
) {
return;
}
author.imageURL = path.join(baseUrl, author.imageURL);
OzakIOne marked this conversation as resolved.
Show resolved Hide resolved
});
}

export function getBlogPostAuthors(params: AuthorsParam): Author[] {
const authorLegacy = getFrontMatterAuthorLegacy(params.frontMatter);
const authorLegacy = getFrontMatterAuthorLegacy(params);
const authors = getFrontMatterAuthors(params);

fixAuthorImageBaseURL(authors, params);

if (authorLegacy) {
// Technically, we could allow mixing legacy/authors front matter, but do we
// really want to?
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-content-blog/src/blogUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ async function processBlogSourceFile(
routeBasePath,
tagsRouteBasePath,
]);
const authors = getBlogPostAuthors({authorsMap, frontMatter});
const authors = getBlogPostAuthors({authorsMap, frontMatter, baseUrl});

return {
id: slug,
Expand Down
Loading