From df86b3b4d9eea9b09dfd47a1b18299e4ecc2de88 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Mon, 26 Aug 2024 17:57:56 +0200 Subject: [PATCH] add tests --- .../__fixtures__/authorsMapFiles/authors.yml | 8 +- .../src/__tests__/authors.test.ts | 192 +++++++++++++++++- .../src/__tests__/authorsMap.test.ts | 69 ++++++- .../src/authors.ts | 10 +- .../src/authorsMap.ts | 5 +- 5 files changed, 263 insertions(+), 21 deletions(-) diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/authorsMapFiles/authors.yml b/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/authorsMapFiles/authors.yml index 26338ad9fe8f..0dffb1f16c8a 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/authorsMapFiles/authors.yml +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/authorsMapFiles/authors.yml @@ -1,4 +1,3 @@ - JMarcey: name: Joel Marcey title: Technical Lead & Developer Advocate at Facebook @@ -36,4 +35,9 @@ lex111: ozaki: name: ozaki title: ozaki - image_url: /img/ozaki.jpg + image_url: /ozaki.png + +ozakione: + name: ozakione + title: ozakione + image_url: /img/ozaki.png diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts b/packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts index 7072e8124caa..40b82502ca23 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts @@ -227,7 +227,7 @@ describe('getBlogPostAuthors', () => { { key: 'slorber', name: 'Sébastien Lorber', - imageURL: '/baseUrl/img/slorber.png', + imageURL: '/img/slorber.png', page: null, }, ]); @@ -522,3 +522,193 @@ describe('groupBlogPostsByAuthorKey', () => { }); }); }); + +describe('getBlogPostAuthors baseUrl', () => { + // Global author without baseUrl + it('getBlogPostAuthors do not modify global authors imageUrl without baseUrl', async () => { + expect( + getBlogPostAuthors({ + frontMatter: { + authors: ['ozaki'], + }, + authorsMap: { + ozaki: { + name: 'ozaki', + key: 'ozaki', + imageURL: 'ozaki.png', + page: null, + }, + }, + baseUrl: '/', + }), + ).toEqual([ + { + imageURL: 'ozaki.png', + key: 'ozaki', + name: '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: { + name: 'ozaki', + key: 'ozaki', + imageURL: 'ozaki.png', + page: null, + }, + }, + baseUrl: '/img/', + }), + ).toEqual([ + { + imageURL: 'ozaki.png', + key: 'ozaki', + name: 'ozaki', + page: null, + }, + ]); + }); + + // Inline author without baseUrl + it('getBlogPostAuthors can return imageURL without baseUrl for inline authors', async () => { + expect( + getBlogPostAuthors({ + frontMatter: { + authors: [{name: 'ozaki', imageURL: 'ozaki.png'}], + }, + authorsMap: undefined, + baseUrl: '/', + }), + ).toEqual([ + { + imageURL: 'ozaki.png', + key: null, + name: 'ozaki', + page: null, + }, + ]); + }); + + // Inline author with baseUrl + it('getBlogPostAuthors normalize imageURL with baseUrl for inline authors', async () => { + expect( + getBlogPostAuthors({ + frontMatter: { + authors: [{name: 'ozaki', imageURL: '/ozaki.png'}], + }, + authorsMap: undefined, + baseUrl: '/img/', + }), + ).toEqual([ + { + imageURL: '/img/ozaki.png', + key: null, + name: '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: { + name: 'ozaki', + key: 'ozaki', + imageURL: 'img/ozaki.png', + page: null, + }, + }, + baseUrl: '/', + }), + ).toEqual([ + { + imageURL: 'img/ozaki.png', + key: 'ozaki', + name: '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: { + name: 'ozaki', + key: 'ozaki', + imageURL: 'img/ozaki.png', + page: null, + }, + }, + baseUrl: '/img/', + }), + ).toEqual([ + { + imageURL: 'img/ozaki.png', + key: 'ozaki', + name: 'ozaki', + 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: [{name: 'ozaki', imageURL: 'img/ozaki.png'}], + }, + authorsMap: undefined, + baseUrl: '/', + }), + ).toEqual([ + { + imageURL: 'img/ozaki.png', + key: null, + name: 'ozaki', + 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: [{name: 'ozaki', imageURL: '/img/ozaki.png'}], + }, + authorsMap: undefined, + baseUrl: '/img/', + }), + ).toEqual([ + { + imageURL: '/img/img/ozaki.png', + key: null, + name: 'ozaki', + page: null, + }, + ]); + }); +}); diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/authorsMap.test.ts b/packages/docusaurus-plugin-content-blog/src/__tests__/authorsMap.test.ts index 4330dc5afcd1..8ce98865ca35 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/authorsMap.test.ts +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/authorsMap.test.ts @@ -106,17 +106,6 @@ describe('getAuthorsMap', () => { }), ).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', () => { @@ -319,3 +308,61 @@ describe('authors socials', () => { expect(validateAuthorsMap(authorsMap)).toEqual(authorsMap); }); }); + +describe('getAuthorsMap global author baseUrl', () => { + const fixturesDir = path.join(__dirname, '__fixtures__/authorsMapFiles'); + const contentPaths = { + contentPathLocalized: fixturesDir, + contentPath: fixturesDir, + }; + + 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'); + }); +}); diff --git a/packages/docusaurus-plugin-content-blog/src/authors.ts b/packages/docusaurus-plugin-content-blog/src/authors.ts index 242e6f52b8c6..89b0938d61ee 100644 --- a/packages/docusaurus-plugin-content-blog/src/authors.ts +++ b/packages/docusaurus-plugin-content-blog/src/authors.ts @@ -21,14 +21,14 @@ type AuthorsParam = { baseUrl: string; }; -function normalizeImageUrl({ +export function normalizeImageUrl({ imageURL, baseUrl, }: { imageURL: string | undefined; baseUrl: string; -}) { - return imageURL?.startsWith('/') && !imageURL.startsWith(baseUrl) +}): string | undefined { + return imageURL?.startsWith('/') ? normalizeUrl([baseUrl, imageURL]) : imageURL; } @@ -122,7 +122,9 @@ ${Object.keys(authorsMap) ...author, key: author.key ?? null, page: author.page ?? null, - imageURL: normalizeImageUrl({imageURL: author.imageURL, baseUrl}), + imageURL: author.key + ? author.imageURL + : normalizeImageUrl({imageURL: author.imageURL, baseUrl}), }; } } diff --git a/packages/docusaurus-plugin-content-blog/src/authorsMap.ts b/packages/docusaurus-plugin-content-blog/src/authorsMap.ts index 184d53720665..d195573ff42b 100644 --- a/packages/docusaurus-plugin-content-blog/src/authorsMap.ts +++ b/packages/docusaurus-plugin-content-blog/src/authorsMap.ts @@ -9,6 +9,7 @@ import _ from 'lodash'; import {readDataFile, normalizeUrl} from '@docusaurus/utils'; import {Joi, URISchema} from '@docusaurus/utils-validation'; import {AuthorSocialsSchema, normalizeSocials} from './authorsSocials'; +import {normalizeImageUrl} from './authors'; import type {BlogContentPaths} from './types'; import type { Author, @@ -116,9 +117,7 @@ function normalizeAuthor({ ...author, key: authorKey, page: getAuthorPage(), - imageURL: author.imageURL?.startsWith('/') - ? normalizeUrl([baseUrl, author.imageURL]) - : author.imageURL, + imageURL: normalizeImageUrl({imageURL: author.imageURL, baseUrl}), socials: author.socials ? normalizeSocials(author.socials) : undefined, }; }