Skip to content

Commit

Permalink
fix: apply baseUrl to relative image
Browse files Browse the repository at this point in the history
  • Loading branch information
OzakIOne committed Aug 23, 2024
1 parent 349a584 commit c9d04f7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.

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

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,21 @@ describe('getAuthorsMap', () => {
contentPaths,
authorsMapPath: 'authors_does_not_exist.yml',
authorsBaseRoutePath: '/authors',
baseUrl: '/',
}),
).resolves.toBeUndefined();
});

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

describe('validateAuthorsMapInput', () => {
Expand Down
11 changes: 10 additions & 1 deletion packages/docusaurus-plugin-content-blog/src/authorsMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ export function checkAuthorsMapPermalinkCollisions(
function normalizeAuthor({
authorsBaseRoutePath,
authorKey,
baseUrl,
author,
}: {
authorsBaseRoutePath: string;
authorKey: string;
baseUrl: string;
author: AuthorInput;
}): Author & {key: string} {
function getAuthorPage(): AuthorPage | null {
Expand All @@ -109,24 +111,30 @@ function normalizeAuthor({
permalink: normalizeUrl([authorsBaseRoutePath, slug]),
};
}
console.log('baseUrl:', baseUrl);

return {
...author,
key: authorKey,
page: getAuthorPage(),
imageURL: author.imageURL?.startsWith('/')
? normalizeUrl([baseUrl, author.imageURL])
: author.imageURL,
socials: author.socials ? normalizeSocials(author.socials) : undefined,
};
}

function normalizeAuthorsMap({
authorsBaseRoutePath,
authorsMapInput,
baseUrl,
}: {
authorsBaseRoutePath: string;
authorsMapInput: AuthorsMapInput;
baseUrl: string;
}): AuthorsMap {
return _.mapValues(authorsMapInput, (author, authorKey) => {
return normalizeAuthor({authorsBaseRoutePath, authorKey, author});
return normalizeAuthor({authorsBaseRoutePath, authorKey, author, baseUrl});
});
}

Expand All @@ -153,6 +161,7 @@ export async function getAuthorsMap(params: {
authorsMapPath: string;
authorsBaseRoutePath: string;
contentPaths: BlogContentPaths;
baseUrl: string;
}): Promise<AuthorsMap | undefined> {
const authorsMapInput = await getAuthorsMapInput(params);
if (!authorsMapInput) {
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-content-blog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export default async function pluginContentBlog(
routeBasePath,
authorsBasePath,
]),
baseUrl,
});
checkAuthorsMapPermalinkCollisions(authorsMap);

Expand Down

0 comments on commit c9d04f7

Please sign in to comment.