mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-05 04:12:53 +02:00
feat(content-docs): sidebar category linking to document or auto-generated index page (#5830)
Co-authored-by: Joshua Chen <sidachen2003@gmail.com> Co-authored-by: Armano <armano2@users.noreply.github.com> Co-authored-by: Alexey Pyltsyn <lex61rus@gmail.com>
This commit is contained in:
parent
95f911efef
commit
cfae5d0933
105 changed files with 3904 additions and 816 deletions
|
@ -6,7 +6,12 @@
|
|||
*/
|
||||
|
||||
import type {LoadedVersion, LoadedContent} from './types';
|
||||
import type {Sidebar, Sidebars} from './sidebars/types';
|
||||
import type {
|
||||
Sidebar,
|
||||
SidebarItemCategory,
|
||||
SidebarItemCategoryLink,
|
||||
Sidebars,
|
||||
} from './sidebars/types';
|
||||
|
||||
import {chain, mapValues, keyBy} from 'lodash';
|
||||
import {
|
||||
|
@ -21,6 +26,7 @@ import type {
|
|||
} from '@docusaurus/types';
|
||||
import {mergeTranslations} from '@docusaurus/utils';
|
||||
import {CURRENT_VERSION_NAME} from './constants';
|
||||
import {TranslationMessage} from '@docusaurus/types';
|
||||
|
||||
function getVersionFileName(versionName: string): string {
|
||||
if (versionName === CURRENT_VERSION_NAME) {
|
||||
|
@ -96,14 +102,48 @@ function getSidebarTranslationFileContent(
|
|||
sidebar: Sidebar,
|
||||
sidebarName: string,
|
||||
): TranslationFileContent {
|
||||
type TranslationMessageEntry = [string, TranslationMessage];
|
||||
|
||||
const categories = collectSidebarCategories(sidebar);
|
||||
const categoryContent: TranslationFileContent = chain(categories)
|
||||
.keyBy((category) => `sidebar.${sidebarName}.category.${category.label}`)
|
||||
.mapValues((category) => ({
|
||||
message: category.label,
|
||||
description: `The label for category ${category.label} in sidebar ${sidebarName}`,
|
||||
}))
|
||||
.value();
|
||||
|
||||
const categoryContent: TranslationFileContent = Object.fromEntries(
|
||||
categories.flatMap((category) => {
|
||||
const entries: TranslationMessageEntry[] = [];
|
||||
|
||||
entries.push([
|
||||
`sidebar.${sidebarName}.category.${category.label}`,
|
||||
{
|
||||
message: category.label,
|
||||
description: `The label for category ${category.label} in sidebar ${sidebarName}`,
|
||||
},
|
||||
]);
|
||||
|
||||
if (category.link) {
|
||||
if (category.link.type === 'generated-index') {
|
||||
if (category.link.title) {
|
||||
entries.push([
|
||||
`sidebar.${sidebarName}.category.${category.label}.link.generated-index.title`,
|
||||
{
|
||||
message: category.link.title,
|
||||
description: `The generated-index page title for category ${category.label} in sidebar ${sidebarName}`,
|
||||
},
|
||||
]);
|
||||
}
|
||||
if (category.link.description) {
|
||||
entries.push([
|
||||
`sidebar.${sidebarName}.category.${category.label}.link.generated-index.description`,
|
||||
{
|
||||
message: category.link.description,
|
||||
description: `The generated-index page description for category ${category.label} in sidebar ${sidebarName}`,
|
||||
},
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return entries;
|
||||
}),
|
||||
);
|
||||
|
||||
const links = collectSidebarLinks(sidebar);
|
||||
const linksContent: TranslationFileContent = chain(links)
|
||||
|
@ -126,13 +166,39 @@ function translateSidebar({
|
|||
sidebarName: string;
|
||||
sidebarsTranslations: TranslationFileContent;
|
||||
}): Sidebar {
|
||||
function transformSidebarCategoryLink(
|
||||
category: SidebarItemCategory,
|
||||
): SidebarItemCategoryLink | undefined {
|
||||
if (!category.link) {
|
||||
return undefined;
|
||||
}
|
||||
if (category.link.type === 'generated-index') {
|
||||
const title =
|
||||
sidebarsTranslations[
|
||||
`sidebar.${sidebarName}.category.${category.label}.link.generated-index.title`
|
||||
]?.message ?? category.link.title;
|
||||
const description =
|
||||
sidebarsTranslations[
|
||||
`sidebar.${sidebarName}.category.${category.label}.link.generated-index.description`
|
||||
]?.message ?? category.link.description;
|
||||
return {
|
||||
...category.link,
|
||||
title,
|
||||
description,
|
||||
};
|
||||
}
|
||||
return category.link;
|
||||
}
|
||||
|
||||
return transformSidebarItems(sidebar, (item) => {
|
||||
if (item.type === 'category') {
|
||||
const link = transformSidebarCategoryLink(item);
|
||||
return {
|
||||
...item,
|
||||
label:
|
||||
sidebarsTranslations[`sidebar.${sidebarName}.category.${item.label}`]
|
||||
?.message ?? item.label,
|
||||
...(link && {link}),
|
||||
};
|
||||
}
|
||||
if (item.type === 'link') {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue