refactor(content-{blog,docs}): unify handling of tags (#7117)

This commit is contained in:
Joshua Chen 2022-04-07 21:58:21 +08:00 committed by GitHub
parent ca718ccac0
commit 1156be3f20
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 170 additions and 178 deletions

View file

@ -86,7 +86,6 @@ export {
translateTagsPageTitle,
listTagsByLetters,
type TagLetterEntry,
type TagsListItem,
} from './utils/tagsUtils';
export {useHistoryPopHandler} from './utils/historyUtils';

View file

@ -15,32 +15,32 @@ describe('listTagsByLetters', () => {
it('creates letters list', () => {
const tag1: Tag = {
name: 'tag1',
label: 'tag1',
permalink: '/tag1',
count: 1,
};
const tag2: Tag = {
name: 'Tag2',
label: 'Tag2',
permalink: '/tag2',
count: 11,
};
const tagZxy: Tag = {
name: 'zxy',
label: 'zxy',
permalink: '/zxy',
count: 987,
};
const tagAbc: Tag = {
name: 'Abc',
label: 'Abc',
permalink: '/abc',
count: 123,
};
const tagDef: Tag = {
name: 'def',
label: 'def',
permalink: '/def',
count: 1,
};
const tagAaa: Tag = {
name: 'aaa',
label: 'aaa',
permalink: '/aaa',
count: 10,
};

View file

@ -6,6 +6,7 @@
*/
import {translate} from '@docusaurus/Translate';
import type {TagsListItem} from '@docusaurus/types';
export const translateTagsPageTitle = (): string =>
translate({
@ -14,13 +15,7 @@ export const translateTagsPageTitle = (): string =>
description: 'The title of the tag list page',
});
export type TagsListItem = Readonly<{
name: string;
permalink: string;
count: number;
}>;
export type TagLetterEntry = Readonly<{letter: string; tags: TagsListItem[]}>;
export type TagLetterEntry = {letter: string; tags: TagsListItem[]};
function getTagLetter(tag: string): string {
return tag[0]!.toUpperCase();
@ -35,7 +30,7 @@ export function listTagsByLetters(
): TagLetterEntry[] {
const groups: {[initial: string]: TagsListItem[]} = {};
Object.values(tags).forEach((tag) => {
const initial = getTagLetter(tag.name);
const initial = getTagLetter(tag.label);
groups[initial] ??= [];
groups[initial]!.push(tag);
});
@ -47,7 +42,7 @@ export function listTagsByLetters(
.map(([letter, letterTags]) => {
// Sort tags inside a letter
const sortedTags = letterTags.sort((tag1, tag2) =>
tag1.name.localeCompare(tag2.name),
tag1.label.localeCompare(tag2.label),
);
return {letter, tags: sortedTags};
})