Skip to content

Commit 2aef92c

Browse files
OzakIOneslorber
andauthored
fix(blog): normalize inline authors socials (#10424)
Co-authored-by: OzakIOne <OzakIOne@users.noreply.github.com> Co-authored-by: Sébastien Lorber <slorber@users.noreply.github.com>
1 parent 200b38b commit 2aef92c

File tree

5 files changed

+272
-11
lines changed

5 files changed

+272
-11
lines changed

packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts

+229-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88
import {fromPartial, type PartialDeep} from '@total-typescript/shoehorn';
99
import {getBlogPostAuthors, groupBlogPostsByAuthorKey} from '../authors';
10-
import type {AuthorsMap, BlogPost} from '@docusaurus/plugin-content-blog';
10+
import type {
11+
AuthorAttributes,
12+
AuthorsMap,
13+
BlogPost,
14+
} from '@docusaurus/plugin-content-blog';
1115

1216
function post(partial: PartialDeep<BlogPost>): BlogPost {
1317
return fromPartial(partial);
@@ -268,11 +272,176 @@ describe('getBlogPostAuthors', () => {
268272
]);
269273
});
270274

271-
it('can read authors Author', () => {
275+
it('read different values from socials', () => {
276+
function testSocials(socials: AuthorAttributes['socials'] | undefined) {
277+
return getBlogPostAuthors({
278+
frontMatter: {
279+
authors: {
280+
name: 'Sébastien Lorber',
281+
title: 'maintainer',
282+
socials,
283+
},
284+
},
285+
authorsMap: undefined,
286+
baseUrl: '/',
287+
});
288+
}
289+
290+
// @ts-expect-error test
291+
expect(() => testSocials(null)).not.toThrow();
292+
// @ts-expect-error test
293+
expect(testSocials(null)).toEqual([
294+
{
295+
name: 'Sébastien Lorber',
296+
title: 'maintainer',
297+
imageURL: undefined,
298+
socials: {},
299+
key: null,
300+
page: null,
301+
},
302+
]);
303+
expect(() => () => testSocials(undefined)).not.toThrow();
304+
// @ts-expect-error test
305+
expect(() => testSocials({twitter: undefined}))
306+
.toThrowErrorMatchingInlineSnapshot(`
307+
"Author socials should be usernames/userIds/handles, or fully qualified HTTP(s) absolute URLs.
308+
Social platform 'twitter' has illegal value 'undefined'"
309+
`);
310+
expect(
311+
// @ts-expect-error test
312+
() => testSocials({twitter: null}),
313+
).toThrowErrorMatchingInlineSnapshot(`
314+
"Author socials should be usernames/userIds/handles, or fully qualified HTTP(s) absolute URLs.
315+
Social platform 'twitter' has illegal value 'null'"
316+
`);
317+
});
318+
319+
it('can read empty socials', () => {
320+
expect(
321+
getBlogPostAuthors({
322+
frontMatter: {
323+
authors: {
324+
name: 'Sébastien Lorber',
325+
title: 'maintainer',
326+
socials: {},
327+
},
328+
},
329+
authorsMap: undefined,
330+
baseUrl: '/',
331+
}),
332+
).toEqual([
333+
{
334+
name: 'Sébastien Lorber',
335+
title: 'maintainer',
336+
imageURL: undefined,
337+
socials: {},
338+
key: null,
339+
page: null,
340+
},
341+
]);
342+
});
343+
344+
it('can normalize full socials from Author', () => {
345+
expect(
346+
getBlogPostAuthors({
347+
frontMatter: {
348+
authors: {
349+
name: 'Sébastien Lorber',
350+
title: 'maintainer',
351+
socials: {
352+
github: 'https://github.com/slorber',
353+
linkedin: 'https://www.linkedin.com/in/sebastienlorber/',
354+
stackoverflow: 'https://stackoverflow.com/users/82609',
355+
twitter: 'https://twitter.com/sebastienlorber',
356+
x: 'https://x.com/sebastienlorber',
357+
},
358+
},
359+
},
360+
authorsMap: undefined,
361+
baseUrl: '/',
362+
}),
363+
).toEqual([
364+
{
365+
name: 'Sébastien Lorber',
366+
title: 'maintainer',
367+
imageURL: undefined,
368+
key: null,
369+
socials: {
370+
github: 'https://github.com/slorber',
371+
linkedin: 'https://www.linkedin.com/in/sebastienlorber/',
372+
stackoverflow: 'https://stackoverflow.com/users/82609',
373+
twitter: 'https://twitter.com/sebastienlorber',
374+
x: 'https://x.com/sebastienlorber',
375+
},
376+
page: null,
377+
},
378+
]);
379+
});
380+
381+
it('can normalize handle socials from Author', () => {
382+
expect(
383+
getBlogPostAuthors({
384+
frontMatter: {
385+
authors: {
386+
name: 'Sébastien Lorber',
387+
title: 'maintainer',
388+
socials: {
389+
github: 'slorber',
390+
x: 'sebastienlorber',
391+
linkedin: 'sebastienlorber',
392+
stackoverflow: '82609',
393+
twitter: 'sebastienlorber',
394+
},
395+
},
396+
},
397+
authorsMap: undefined,
398+
baseUrl: '/',
399+
}),
400+
).toEqual([
401+
{
402+
name: 'Sébastien Lorber',
403+
title: 'maintainer',
404+
imageURL: undefined,
405+
key: null,
406+
socials: {
407+
github: 'https://github.com/slorber',
408+
linkedin: 'https://www.linkedin.com/in/sebastienlorber/',
409+
stackoverflow: 'https://stackoverflow.com/users/82609',
410+
twitter: 'https://twitter.com/sebastienlorber',
411+
x: 'https://x.com/sebastienlorber',
412+
},
413+
page: null,
414+
},
415+
]);
416+
});
417+
418+
it('can normalize socials from Author[]', () => {
272419
expect(
273420
getBlogPostAuthors({
274421
frontMatter: {
275-
authors: {name: 'Sébastien Lorber', title: 'maintainer'},
422+
authors: [
423+
{
424+
name: 'Sébastien Lorber',
425+
title: 'maintainer',
426+
socials: {
427+
github: 'slorber',
428+
x: 'sebastienlorber',
429+
linkedin: 'sebastienlorber',
430+
stackoverflow: '82609',
431+
twitter: 'sebastienlorber',
432+
},
433+
},
434+
{
435+
name: 'Seb',
436+
socials: {
437+
github: 'https://github.com/slorber',
438+
linkedin: 'https://www.linkedin.com/in/sebastienlorber/',
439+
stackoverflow: 'https://stackoverflow.com/users/82609',
440+
twitter: 'https://twitter.com/sebastienlorber',
441+
x: 'https://x.com/sebastienlorber',
442+
},
443+
},
444+
],
276445
},
277446
authorsMap: undefined,
278447
baseUrl: '/',
@@ -283,6 +452,26 @@ describe('getBlogPostAuthors', () => {
283452
title: 'maintainer',
284453
imageURL: undefined,
285454
key: null,
455+
socials: {
456+
github: 'https://github.com/slorber',
457+
linkedin: 'https://www.linkedin.com/in/sebastienlorber/',
458+
stackoverflow: 'https://stackoverflow.com/users/82609',
459+
twitter: 'https://twitter.com/sebastienlorber',
460+
x: 'https://x.com/sebastienlorber',
461+
},
462+
page: null,
463+
},
464+
{
465+
name: 'Seb',
466+
imageURL: undefined,
467+
key: null,
468+
socials: {
469+
github: 'https://github.com/slorber',
470+
linkedin: 'https://www.linkedin.com/in/sebastienlorber/',
471+
stackoverflow: 'https://stackoverflow.com/users/82609',
472+
twitter: 'https://twitter.com/sebastienlorber',
473+
x: 'https://x.com/sebastienlorber',
474+
},
286475
page: null,
287476
},
288477
]);
@@ -293,8 +482,13 @@ describe('getBlogPostAuthors', () => {
293482
getBlogPostAuthors({
294483
frontMatter: {
295484
authors: [
296-
{name: 'Sébastien Lorber', title: 'maintainer'},
297-
{name: 'Yangshun Tay'},
485+
{
486+
name: 'Sébastien Lorber',
487+
title: 'maintainer',
488+
},
489+
{
490+
name: 'Yangshun Tay',
491+
},
298492
],
299493
},
300494
authorsMap: undefined,
@@ -306,9 +500,16 @@ describe('getBlogPostAuthors', () => {
306500
title: 'maintainer',
307501
imageURL: undefined,
308502
key: null,
503+
socials: {},
504+
page: null,
505+
},
506+
{
507+
name: 'Yangshun Tay',
508+
imageURL: undefined,
509+
socials: {},
510+
key: null,
309511
page: null,
310512
},
311-
{name: 'Yangshun Tay', imageURL: undefined, key: null, page: null},
312513
]);
313514
});
314515

@@ -323,7 +524,12 @@ describe('getBlogPostAuthors', () => {
323524
title: 'Yangshun title local override',
324525
extra: 42,
325526
},
326-
{name: 'Alexey'},
527+
{
528+
name: 'Alexey',
529+
socials: {
530+
github: 'lex111',
531+
},
532+
},
327533
],
328534
},
329535
authorsMap: {
@@ -355,10 +561,19 @@ describe('getBlogPostAuthors', () => {
355561
name: 'Yangshun Tay',
356562
title: 'Yangshun title local override',
357563
extra: 42,
564+
socials: {},
358565
imageURL: undefined,
359566
page: null,
360567
},
361-
{name: 'Alexey', imageURL: undefined, key: null, page: null},
568+
{
569+
name: 'Alexey',
570+
imageURL: undefined,
571+
key: null,
572+
page: null,
573+
socials: {
574+
github: 'https://github.com/lex111',
575+
},
576+
},
362577
]);
363578
});
364579

@@ -627,6 +842,7 @@ describe('getBlogPostAuthors', () => {
627842
imageURL: './ozaki.png',
628843
key: null,
629844
page: null,
845+
socials: {},
630846
},
631847
]);
632848
expect(() => withoutBaseUrlTest).not.toThrow();
@@ -635,6 +851,7 @@ describe('getBlogPostAuthors', () => {
635851
imageURL: './ozaki.png',
636852
key: null,
637853
page: null,
854+
socials: {},
638855
},
639856
]);
640857
});
@@ -654,6 +871,7 @@ describe('getBlogPostAuthors', () => {
654871
imageURL: '/ozaki.png',
655872
key: null,
656873
page: null,
874+
socials: {},
657875
},
658876
]);
659877
});
@@ -673,6 +891,7 @@ describe('getBlogPostAuthors', () => {
673891
imageURL: '/img/ozaki.png',
674892
key: null,
675893
page: null,
894+
socials: {},
676895
},
677896
]);
678897
});
@@ -692,6 +911,7 @@ describe('getBlogPostAuthors', () => {
692911
imageURL: '/img/ozaki.png',
693912
key: null,
694913
page: null,
914+
socials: {},
695915
},
696916
]);
697917
});
@@ -711,6 +931,7 @@ describe('getBlogPostAuthors', () => {
711931
imageURL: '/img/img/ozaki.png',
712932
key: null,
713933
page: null,
934+
socials: {},
714935
},
715936
]);
716937
});

packages/docusaurus-plugin-content-blog/src/__tests__/authorsMap.test.ts

+30-2
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ describe('authors socials', () => {
308308
});
309309

310310
it('throw socials that are not strings', () => {
311-
const authorsMap: AuthorsMapInput = {
311+
const socialNumber: AuthorsMapInput = {
312312
ozaki: {
313313
name: 'ozaki',
314314
socials: {
@@ -318,11 +318,39 @@ describe('authors socials', () => {
318318
},
319319
};
320320

321+
const socialNull: AuthorsMapInput = {
322+
ozaki: {
323+
name: 'ozaki',
324+
socials: {
325+
// @ts-expect-error: for tests
326+
twitter: null,
327+
},
328+
},
329+
};
330+
331+
const socialNull2: AuthorsMapInput = {
332+
ozaki: {
333+
name: 'ozaki',
334+
// @ts-expect-error: for tests
335+
socials: null,
336+
},
337+
};
338+
321339
expect(() =>
322-
validateAuthorsMap(authorsMap),
340+
validateAuthorsMap(socialNumber),
323341
).toThrowErrorMatchingInlineSnapshot(
324342
`""ozaki.socials.twitter" must be a string"`,
325343
);
344+
expect(() =>
345+
validateAuthorsMap(socialNull),
346+
).toThrowErrorMatchingInlineSnapshot(
347+
`""ozaki.socials.twitter" must be a string"`,
348+
);
349+
expect(() =>
350+
validateAuthorsMap(socialNull2),
351+
).toThrowErrorMatchingInlineSnapshot(
352+
`""ozaki.socials" should be an author object containing properties like name, title, and imageURL."`,
353+
);
326354
});
327355

328356
it('throw socials that are objects', () => {

0 commit comments

Comments
 (0)