mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-17 08:37:57 +02:00
fix(content-blog): add baseUrl for author.image_url (#9581)
This commit is contained in:
parent
7650829e91
commit
4cc56133f7
3 changed files with 137 additions and 9 deletions
|
@ -19,6 +19,7 @@ describe('getBlogPostAuthors', () => {
|
||||||
getBlogPostAuthors({
|
getBlogPostAuthors({
|
||||||
frontMatter: {},
|
frontMatter: {},
|
||||||
authorsMap: undefined,
|
authorsMap: undefined,
|
||||||
|
baseUrl: '/',
|
||||||
}),
|
}),
|
||||||
).toEqual([]);
|
).toEqual([]);
|
||||||
expect(
|
expect(
|
||||||
|
@ -27,6 +28,7 @@ describe('getBlogPostAuthors', () => {
|
||||||
authors: [],
|
authors: [],
|
||||||
},
|
},
|
||||||
authorsMap: undefined,
|
authorsMap: undefined,
|
||||||
|
baseUrl: '/',
|
||||||
}),
|
}),
|
||||||
).toEqual([]);
|
).toEqual([]);
|
||||||
});
|
});
|
||||||
|
@ -38,6 +40,7 @@ describe('getBlogPostAuthors', () => {
|
||||||
author: 'Sébastien Lorber',
|
author: 'Sébastien Lorber',
|
||||||
},
|
},
|
||||||
authorsMap: undefined,
|
authorsMap: undefined,
|
||||||
|
baseUrl: '/',
|
||||||
}),
|
}),
|
||||||
).toEqual([{name: 'Sébastien Lorber'}]);
|
).toEqual([{name: 'Sébastien Lorber'}]);
|
||||||
expect(
|
expect(
|
||||||
|
@ -46,6 +49,7 @@ describe('getBlogPostAuthors', () => {
|
||||||
authorTitle: 'maintainer',
|
authorTitle: 'maintainer',
|
||||||
},
|
},
|
||||||
authorsMap: undefined,
|
authorsMap: undefined,
|
||||||
|
baseUrl: '/',
|
||||||
}),
|
}),
|
||||||
).toEqual([{title: 'maintainer'}]);
|
).toEqual([{title: 'maintainer'}]);
|
||||||
expect(
|
expect(
|
||||||
|
@ -54,8 +58,27 @@ describe('getBlogPostAuthors', () => {
|
||||||
authorImageURL: 'https://github.com/slorber.png',
|
authorImageURL: 'https://github.com/slorber.png',
|
||||||
},
|
},
|
||||||
authorsMap: undefined,
|
authorsMap: undefined,
|
||||||
|
baseUrl: '/',
|
||||||
}),
|
}),
|
||||||
).toEqual([{imageURL: 'https://github.com/slorber.png'}]);
|
).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(
|
expect(
|
||||||
getBlogPostAuthors({
|
getBlogPostAuthors({
|
||||||
frontMatter: {
|
frontMatter: {
|
||||||
|
@ -68,6 +91,7 @@ describe('getBlogPostAuthors', () => {
|
||||||
authorURL: 'https://github.com/slorber2',
|
authorURL: 'https://github.com/slorber2',
|
||||||
},
|
},
|
||||||
authorsMap: undefined,
|
authorsMap: undefined,
|
||||||
|
baseUrl: '/',
|
||||||
}),
|
}),
|
||||||
).toEqual([
|
).toEqual([
|
||||||
{
|
{
|
||||||
|
@ -86,8 +110,69 @@ describe('getBlogPostAuthors', () => {
|
||||||
authors: 'slorber',
|
authors: 'slorber',
|
||||||
},
|
},
|
||||||
authorsMap: {slorber: {name: 'Sébastien Lorber'}},
|
authorsMap: {slorber: {name: 'Sébastien Lorber'}},
|
||||||
|
baseUrl: '/',
|
||||||
}),
|
}),
|
||||||
).toEqual([{key: 'slorber', name: 'Sébastien Lorber'}]);
|
).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[]', () => {
|
it('can read authors string[]', () => {
|
||||||
|
@ -100,6 +185,7 @@ describe('getBlogPostAuthors', () => {
|
||||||
slorber: {name: 'Sébastien Lorber', title: 'maintainer'},
|
slorber: {name: 'Sébastien Lorber', title: 'maintainer'},
|
||||||
yangshun: {name: 'Yangshun Tay'},
|
yangshun: {name: 'Yangshun Tay'},
|
||||||
},
|
},
|
||||||
|
baseUrl: '/',
|
||||||
}),
|
}),
|
||||||
).toEqual([
|
).toEqual([
|
||||||
{key: 'slorber', name: 'Sébastien Lorber', title: 'maintainer'},
|
{key: 'slorber', name: 'Sébastien Lorber', title: 'maintainer'},
|
||||||
|
@ -114,6 +200,7 @@ describe('getBlogPostAuthors', () => {
|
||||||
authors: {name: 'Sébastien Lorber', title: 'maintainer'},
|
authors: {name: 'Sébastien Lorber', title: 'maintainer'},
|
||||||
},
|
},
|
||||||
authorsMap: undefined,
|
authorsMap: undefined,
|
||||||
|
baseUrl: '/',
|
||||||
}),
|
}),
|
||||||
).toEqual([{name: 'Sébastien Lorber', title: 'maintainer'}]);
|
).toEqual([{name: 'Sébastien Lorber', title: 'maintainer'}]);
|
||||||
});
|
});
|
||||||
|
@ -128,6 +215,7 @@ describe('getBlogPostAuthors', () => {
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
authorsMap: undefined,
|
authorsMap: undefined,
|
||||||
|
baseUrl: '/',
|
||||||
}),
|
}),
|
||||||
).toEqual([
|
).toEqual([
|
||||||
{name: 'Sébastien Lorber', title: 'maintainer'},
|
{name: 'Sébastien Lorber', title: 'maintainer'},
|
||||||
|
@ -153,6 +241,7 @@ describe('getBlogPostAuthors', () => {
|
||||||
slorber: {name: 'Sébastien Lorber', title: 'maintainer'},
|
slorber: {name: 'Sébastien Lorber', title: 'maintainer'},
|
||||||
yangshun: {name: 'Yangshun Tay', title: 'Yangshun title original'},
|
yangshun: {name: 'Yangshun Tay', title: 'Yangshun title original'},
|
||||||
},
|
},
|
||||||
|
baseUrl: '/',
|
||||||
}),
|
}),
|
||||||
).toEqual([
|
).toEqual([
|
||||||
{key: 'slorber', name: 'Sébastien Lorber', title: 'maintainer'},
|
{key: 'slorber', name: 'Sébastien Lorber', title: 'maintainer'},
|
||||||
|
@ -173,6 +262,7 @@ describe('getBlogPostAuthors', () => {
|
||||||
authors: 'slorber',
|
authors: 'slorber',
|
||||||
},
|
},
|
||||||
authorsMap: undefined,
|
authorsMap: undefined,
|
||||||
|
baseUrl: '/',
|
||||||
}),
|
}),
|
||||||
).toThrowErrorMatchingInlineSnapshot(`
|
).toThrowErrorMatchingInlineSnapshot(`
|
||||||
"Can't reference blog post authors by a key (such as 'slorber') because no authors map file could be loaded.
|
"Can't reference blog post authors by a key (such as 'slorber') because no authors map file could be loaded.
|
||||||
|
@ -187,6 +277,7 @@ describe('getBlogPostAuthors', () => {
|
||||||
authors: 'slorber',
|
authors: 'slorber',
|
||||||
},
|
},
|
||||||
authorsMap: {},
|
authorsMap: {},
|
||||||
|
baseUrl: '/',
|
||||||
}),
|
}),
|
||||||
).toThrowErrorMatchingInlineSnapshot(`
|
).toThrowErrorMatchingInlineSnapshot(`
|
||||||
"Can't reference blog post authors by a key (such as 'slorber') because no authors map file could be loaded.
|
"Can't reference blog post authors by a key (such as 'slorber') because no authors map file could be loaded.
|
||||||
|
@ -205,6 +296,7 @@ describe('getBlogPostAuthors', () => {
|
||||||
yangshun: {name: 'Yangshun Tay'},
|
yangshun: {name: 'Yangshun Tay'},
|
||||||
jmarcey: {name: 'Joel Marcey'},
|
jmarcey: {name: 'Joel Marcey'},
|
||||||
},
|
},
|
||||||
|
baseUrl: '/',
|
||||||
}),
|
}),
|
||||||
).toThrowErrorMatchingInlineSnapshot(`
|
).toThrowErrorMatchingInlineSnapshot(`
|
||||||
"Blog author with key "slorber" not found in the authors map file.
|
"Blog author with key "slorber" not found in the authors map file.
|
||||||
|
@ -225,6 +317,7 @@ describe('getBlogPostAuthors', () => {
|
||||||
yangshun: {name: 'Yangshun Tay'},
|
yangshun: {name: 'Yangshun Tay'},
|
||||||
jmarcey: {name: 'Joel Marcey'},
|
jmarcey: {name: 'Joel Marcey'},
|
||||||
},
|
},
|
||||||
|
baseUrl: '/',
|
||||||
}),
|
}),
|
||||||
).toThrowErrorMatchingInlineSnapshot(`
|
).toThrowErrorMatchingInlineSnapshot(`
|
||||||
"Blog author with key "slorber" not found in the authors map file.
|
"Blog author with key "slorber" not found in the authors map file.
|
||||||
|
@ -245,6 +338,7 @@ describe('getBlogPostAuthors', () => {
|
||||||
yangshun: {name: 'Yangshun Tay'},
|
yangshun: {name: 'Yangshun Tay'},
|
||||||
jmarcey: {name: 'Joel Marcey'},
|
jmarcey: {name: 'Joel Marcey'},
|
||||||
},
|
},
|
||||||
|
baseUrl: '/',
|
||||||
}),
|
}),
|
||||||
).toThrowErrorMatchingInlineSnapshot(`
|
).toThrowErrorMatchingInlineSnapshot(`
|
||||||
"Blog author with key "slorber" not found in the authors map file.
|
"Blog author with key "slorber" not found in the authors map file.
|
||||||
|
@ -262,6 +356,7 @@ describe('getBlogPostAuthors', () => {
|
||||||
author: 'Yangshun Tay',
|
author: 'Yangshun Tay',
|
||||||
},
|
},
|
||||||
authorsMap: undefined,
|
authorsMap: undefined,
|
||||||
|
baseUrl: '/',
|
||||||
}),
|
}),
|
||||||
).toThrowErrorMatchingInlineSnapshot(`
|
).toThrowErrorMatchingInlineSnapshot(`
|
||||||
"To declare blog post authors, use the 'authors' front matter in priority.
|
"To declare blog post authors, use the 'authors' front matter in priority.
|
||||||
|
@ -275,6 +370,7 @@ describe('getBlogPostAuthors', () => {
|
||||||
author_title: 'legacy title',
|
author_title: 'legacy title',
|
||||||
},
|
},
|
||||||
authorsMap: {slorber: {name: 'Sébastien Lorber'}},
|
authorsMap: {slorber: {name: 'Sébastien Lorber'}},
|
||||||
|
baseUrl: '/',
|
||||||
}),
|
}),
|
||||||
).toThrowErrorMatchingInlineSnapshot(`
|
).toThrowErrorMatchingInlineSnapshot(`
|
||||||
"To declare blog post authors, use the 'authors' front matter in priority.
|
"To declare blog post authors, use the 'authors' front matter in priority.
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* 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 {Joi, URISchema} from '@docusaurus/utils-validation';
|
||||||
import type {BlogContentPaths} from './types';
|
import type {BlogContentPaths} from './types';
|
||||||
import type {
|
import type {
|
||||||
|
@ -68,17 +68,37 @@ export async function getAuthorsMap(params: {
|
||||||
type AuthorsParam = {
|
type AuthorsParam = {
|
||||||
frontMatter: BlogPostFrontMatter;
|
frontMatter: BlogPostFrontMatter;
|
||||||
authorsMap: AuthorsMap | undefined;
|
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
|
// Legacy v1/early-v2 front matter fields
|
||||||
// We may want to deprecate those in favor of using only frontMatter.authors
|
// We may want to deprecate those in favor of using only frontMatter.authors
|
||||||
function getFrontMatterAuthorLegacy(
|
function getFrontMatterAuthorLegacy({
|
||||||
frontMatter: BlogPostFrontMatter,
|
baseUrl,
|
||||||
): Author | undefined {
|
frontMatter,
|
||||||
|
}: {
|
||||||
|
baseUrl: string;
|
||||||
|
frontMatter: BlogPostFrontMatter;
|
||||||
|
}): Author | undefined {
|
||||||
const name = frontMatter.author;
|
const name = frontMatter.author;
|
||||||
const title = frontMatter.author_title ?? frontMatter.authorTitle;
|
const title = frontMatter.author_title ?? frontMatter.authorTitle;
|
||||||
const url = frontMatter.author_url ?? frontMatter.authorURL;
|
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) {
|
if (name || title || url || imageURL) {
|
||||||
return {
|
return {
|
||||||
|
@ -148,14 +168,26 @@ ${Object.keys(authorsMap)
|
||||||
return frontMatterAuthors.map(toAuthor);
|
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[] {
|
export function getBlogPostAuthors(params: AuthorsParam): Author[] {
|
||||||
const authorLegacy = getFrontMatterAuthorLegacy(params.frontMatter);
|
const authorLegacy = getFrontMatterAuthorLegacy(params);
|
||||||
const authors = getFrontMatterAuthors(params);
|
const authors = getFrontMatterAuthors(params);
|
||||||
|
|
||||||
|
const updatedAuthors = fixAuthorImageBaseURL(authors, params);
|
||||||
|
|
||||||
if (authorLegacy) {
|
if (authorLegacy) {
|
||||||
// Technically, we could allow mixing legacy/authors front matter, but do we
|
// Technically, we could allow mixing legacy/authors front matter, but do we
|
||||||
// really want to?
|
// really want to?
|
||||||
if (authors.length > 0) {
|
if (updatedAuthors.length > 0) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`To declare blog post authors, use the 'authors' front matter in priority.
|
`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.`,
|
Don't mix 'authors' with other existing 'author_*' front matter. Choose one or the other, not both at the same time.`,
|
||||||
|
@ -164,5 +196,5 @@ Don't mix 'authors' with other existing 'author_*' front matter. Choose one or t
|
||||||
return [authorLegacy];
|
return [authorLegacy];
|
||||||
}
|
}
|
||||||
|
|
||||||
return authors;
|
return updatedAuthors;
|
||||||
}
|
}
|
||||||
|
|
|
@ -319,7 +319,7 @@ async function processBlogSourceFile(
|
||||||
routeBasePath,
|
routeBasePath,
|
||||||
tagsRouteBasePath,
|
tagsRouteBasePath,
|
||||||
]);
|
]);
|
||||||
const authors = getBlogPostAuthors({authorsMap, frontMatter});
|
const authors = getBlogPostAuthors({authorsMap, frontMatter, baseUrl});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: slug,
|
id: slug,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue