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 all 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 @@ describe('getBlogPostAuthors', () => {
getBlogPostAuthors({
frontMatter: {},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([]);
expect(
Expand All @@ -27,6 +28,7 @@ describe('getBlogPostAuthors', () => {
authors: [],
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([]);
});
Expand All @@ -38,6 +40,7 @@ describe('getBlogPostAuthors', () => {
author: 'Sébastien Lorber',
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([{name: 'Sébastien Lorber'}]);
expect(
Expand All @@ -46,6 +49,7 @@ describe('getBlogPostAuthors', () => {
authorTitle: 'maintainer',
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([{title: 'maintainer'}]);
expect(
Expand All @@ -54,8 +58,27 @@ describe('getBlogPostAuthors', () => {
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'}]);
expect(
getBlogPostAuthors({
frontMatter: {
Expand All @@ -68,6 +91,7 @@ describe('getBlogPostAuthors', () => {
authorURL: 'https://github.com/slorber2',
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([
{
Expand All @@ -86,8 +110,69 @@ describe('getBlogPostAuthors', () => {
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([
{
key: 'slorber',
name: 'Sébastien Lorber',
imageURL: '/baseUrl/img/slorber.png',
},
]);
});

it('can read authors string[]', () => {
Expand All @@ -100,6 +185,7 @@ describe('getBlogPostAuthors', () => {
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 @@ describe('getBlogPostAuthors', () => {
authors: {name: 'Sébastien Lorber', title: 'maintainer'},
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([{name: 'Sébastien Lorber', title: 'maintainer'}]);
});
Expand All @@ -128,6 +215,7 @@ describe('getBlogPostAuthors', () => {
],
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([
{name: 'Sébastien Lorber', title: 'maintainer'},
Expand All @@ -153,6 +241,7 @@ describe('getBlogPostAuthors', () => {
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 @@ describe('getBlogPostAuthors', () => {
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 @@ describe('getBlogPostAuthors', () => {
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 @@ describe('getBlogPostAuthors', () => {
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 @@ describe('getBlogPostAuthors', () => {
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 @@ describe('getBlogPostAuthors', () => {
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 @@ describe('getBlogPostAuthors', () => {
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 @@ describe('getBlogPostAuthors', () => {
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
48 changes: 40 additions & 8 deletions packages/docusaurus-plugin-content-blog/src/authors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

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

function normalizeImageUrl({
imageURL,
baseUrl,
}: {
imageURL: string | undefined;
baseUrl: string;
}) {
return imageURL?.startsWith('/')
? normalizeUrl([baseUrl, imageURL])
: imageURL;
}

// 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;
const imageURL = normalizeImageUrl({
imageURL: frontMatter.author_image_url ?? frontMatter.authorImageURL,
baseUrl,
});

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

function fixAuthorImageBaseURL(
authors: Author[],
{baseUrl}: {baseUrl: string},
) {
return authors.map((author) => ({
...author,
imageURL: normalizeImageUrl({imageURL: author.imageURL, baseUrl}),
}));
}

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

const updatedAuthors = fixAuthorImageBaseURL(authors, params);

if (authorLegacy) {
// Technically, we could allow mixing legacy/authors front matter, but do we
// really want to?
if (authors.length > 0) {
if (updatedAuthors.length > 0) {
throw new Error(
`To declare blog post authors, use the 'authors' front matter in priority.
Don't mix 'authors' with other existing 'author_*' front matter. Choose one or the other, not both at the same time.`,
Expand All @@ -164,5 +196,5 @@ Don't mix 'authors' with other existing 'author_*' front matter. Choose one or t
return [authorLegacy];
}

return authors;
return updatedAuthors;
}
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