Skip to content

Commit

Permalink
fix(blog): apply baseUrl to relative image in blog authors (#10440)
Browse files Browse the repository at this point in the history
Co-authored-by: sebastien <lorber.sebastien@gmail.com>
  • Loading branch information
OzakIOne and slorber committed Aug 29, 2024
1 parent 9588028 commit 02ed7d9
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 10 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

232 changes: 230 additions & 2 deletions packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ describe('getBlogPostAuthors', () => {
authorsMap: {
slorber: {
name: 'Sébastien Lorber',
imageURL: '/img/slorber.png',
imageURL: '/baseUrl/img/slorber.png',
key: 'slorber',
page: null,
},
Expand Down Expand Up @@ -419,7 +419,6 @@ describe('getBlogPostAuthors', () => {
frontMatter: {
authors: ['yangshun', 'jmarcey', 'slorber'],
},

authorsMap: {
yangshun: {name: 'Yangshun Tay', key: 'yangshun', page: null},
jmarcey: {name: 'Joel Marcey', key: 'jmarcey', page: null},
Expand Down Expand Up @@ -486,6 +485,235 @@ describe('getBlogPostAuthors', () => {
Don't mix 'authors' with other existing 'author_*' front matter. Choose one or the other, not both at the same time."
`);
});

// Global author without baseUrl
it('getBlogPostAuthors do not modify global authors imageUrl without baseUrl', async () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: ['ozaki'],
},
authorsMap: {
ozaki: {
key: 'ozaki',
imageURL: '/ozaki.png',
page: null,
},
},
baseUrl: '/',
}),
).toEqual([
{
imageURL: '/ozaki.png',
key: 'ozaki',
page: null,
},
]);
});

// Global author with baseUrl
it('getBlogPostAuthors do not modify global authors imageUrl with baseUrl', async () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: ['ozaki'],
},
authorsMap: {
ozaki: {
key: 'ozaki',
imageURL: '/img/ozaki.png',
page: null,
},
},
baseUrl: '/img/',
}),
).toEqual([
{
imageURL: '/img/ozaki.png',
key: 'ozaki',
page: null,
},
]);
});

// Global author without baseUrl with a subfolder in img
it('getBlogPostAuthors do not modify globalAuthor imageUrl with subfolder without baseUrl', async () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: ['ozaki'],
},
authorsMap: {
ozaki: {
key: 'ozaki',
imageURL: '/img/ozaki.png',
page: null,
},
},
baseUrl: '/',
}),
).toEqual([
{
imageURL: '/img/ozaki.png',
key: 'ozaki',
page: null,
},
]);
});

// Global author with baseUrl with a subfolder in img
it('getBlogPostAuthors do not modify globalAuthor imageUrl with subfolder with baseUrl', async () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: ['ozaki'],
},
authorsMap: {
ozaki: {
key: 'ozaki',
imageURL: '/img/ozaki.png',
page: null,
},
},
baseUrl: '/img/',
}),
).toEqual([
{
imageURL: '/img/ozaki.png',
key: 'ozaki',
page: null,
},
]);
});

it('getBlogPostAuthors throws if global author imageURL does not have baseUrl', async () => {
expect(() =>
getBlogPostAuthors({
frontMatter: {
authors: ['ozaki'],
},
authorsMap: {
ozaki: {
key: 'ozaki',
imageURL: '/ozaki.png',
page: null,
},
},
baseUrl: '/baseUrl/',
}),
).toThrowErrorMatchingInlineSnapshot(
`"Docusaurus internal bug: global authors image /ozaki.png should start with the expected baseUrl=/baseUrl/"`,
);
});

it('getBlogPostAuthors do not throws if inline author imageURL is a link to a file', async () => {
const baseUrlTest = getBlogPostAuthors({
frontMatter: {
authors: [{imageURL: './ozaki.png'}],
},
authorsMap: undefined,
baseUrl: '/baseUrl/',
});
const withoutBaseUrlTest = getBlogPostAuthors({
frontMatter: {
authors: [{imageURL: './ozaki.png'}],
},
authorsMap: undefined,
baseUrl: '/',
});
expect(() => baseUrlTest).not.toThrow();
expect(baseUrlTest).toEqual([
{
imageURL: './ozaki.png',
key: null,
page: null,
},
]);
expect(() => withoutBaseUrlTest).not.toThrow();
expect(withoutBaseUrlTest).toEqual([
{
imageURL: './ozaki.png',
key: null,
page: null,
},
]);
});

// Inline author without baseUrl
it('getBlogPostAuthors can return imageURL without baseUrl for inline authors', async () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: [{imageURL: '/ozaki.png'}],
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([
{
imageURL: '/ozaki.png',
key: null,
page: null,
},
]);
});

// Inline author with baseUrl
it('getBlogPostAuthors normalize imageURL with baseUrl for inline authors', async () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: [{imageURL: '/ozaki.png'}],
},
authorsMap: undefined,
baseUrl: '/img/',
}),
).toEqual([
{
imageURL: '/img/ozaki.png',
key: null,
page: null,
},
]);
});

// Inline author without baseUrl with a subfolder in img
it('getBlogPostAuthors normalize imageURL from subfolder without baseUrl for inline authors', async () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: [{imageURL: '/img/ozaki.png'}],
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([
{
imageURL: '/img/ozaki.png',
key: null,
page: null,
},
]);
});

// Inline author with baseUrl with a subfolder in img
it('getBlogPostAuthors normalize imageURL from subfolder with baseUrl for inline authors', async () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: [{imageURL: '/img/ozaki.png'}],
},
authorsMap: undefined,
baseUrl: '/img/',
}),
).toEqual([
{
imageURL: '/img/img/ozaki.png',
key: null,
page: null,
},
]);
});
});

describe('groupBlogPostsByAuthorKey', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe('getAuthorsMap', () => {
contentPaths,
authorsMapPath: 'authors.yml',
authorsBaseRoutePath: '/authors',
baseUrl: '/',
}),
).resolves.toBeDefined();
});
Expand All @@ -90,6 +91,7 @@ describe('getAuthorsMap', () => {
contentPaths,
authorsMapPath: 'authors.json',
authorsBaseRoutePath: '/authors',
baseUrl: '/',
}),
).resolves.toBeDefined();
});
Expand All @@ -100,9 +102,60 @@ describe('getAuthorsMap', () => {
contentPaths,
authorsMapPath: 'authors_does_not_exist.yml',
authorsBaseRoutePath: '/authors',
baseUrl: '/',
}),
).resolves.toBeUndefined();
});

it('getAuthorsMap return imageURL with relative path', async () => {
const authorsMap = await getAuthorsMap({
contentPaths,
authorsMapPath: 'authors.yml',
authorsBaseRoutePath: '/authors',
baseUrl: '/',
});
expect(authorsMap?.ozaki?.imageURL).toBe('/ozaki.png');
});

it('getAuthorsMap normalize imageURL with baseUrl', async () => {
const authorsMap = await getAuthorsMap({
contentPaths,
authorsMapPath: 'authors.yml',
authorsBaseRoutePath: '/authors',
baseUrl: '/baseUrl/',
});
expect(authorsMap?.ozaki?.imageURL).toBe('/baseUrl/ozaki.png');
});

it('getAuthorsMap return imageURL with relative subdir path', async () => {
const authorsMap = await getAuthorsMap({
contentPaths,
authorsMapPath: 'authors.yml',
authorsBaseRoutePath: '/authors',
baseUrl: '/',
});
expect(authorsMap?.ozakione?.imageURL).toBe('/img/ozaki.png');
});

it('getAuthorsMap normalize imageURL with baseUrl and subdir same value', async () => {
const authorsMap = await getAuthorsMap({
contentPaths,
authorsMapPath: 'authors.yml',
authorsBaseRoutePath: '/authors',
baseUrl: '/img/',
});
expect(authorsMap?.ozakione?.imageURL).toBe('/img/img/ozaki.png');
});

it('getAuthorsMap normalize imageURL subdir with baseUrl', async () => {
const authorsMap = await getAuthorsMap({
contentPaths,
authorsMapPath: 'authors.yml',
authorsBaseRoutePath: '/authors',
baseUrl: '/blog/',
});
expect(authorsMap?.ozakione?.imageURL).toBe('/blog/img/ozaki.png');
});
});

describe('validateAuthorsMapInput', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ async function testGenerateFeeds(
contentPaths,
authorsMapPath: options.authorsMapPath,
authorsBaseRoutePath: '/authors',
baseUrl: '/',
});

const blogPosts = await generateBlogPosts(
Expand Down
Loading

0 comments on commit 02ed7d9

Please sign in to comment.