fix(content-docs): make category index text translatable (#7233)

This commit is contained in:
Joshua Chen 2022-04-27 23:40:15 +08:00 committed by GitHub
parent 996901b53b
commit 10a5ebecec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 121 additions and 104 deletions

View file

@ -18,6 +18,11 @@ exports[`toGlobalDataVersion generates the right docs, sidebars, and metadata 1`
"path": "/current/generated",
"sidebar": "tutorial",
},
{
"id": "/current/generated-2",
"path": "/current/generated-2",
"sidebar": "another",
},
],
"draftIds": [
"some-draft-id",
@ -31,7 +36,7 @@ exports[`toGlobalDataVersion generates the right docs, sidebars, and metadata 1`
"another": {
"link": {
"label": "Generated",
"path": "/current/generated",
"path": "/current/generated-2",
},
},
"links": {},

View file

@ -6,9 +6,79 @@
*/
import {toGlobalDataVersion} from '../globalData';
import {createSidebarsUtils} from '../sidebars/utils';
import {getCategoryGeneratedIndexMetadataList} from '../categoryGeneratedIndex';
import type {Sidebars} from '../sidebars/types';
describe('toGlobalDataVersion', () => {
it('generates the right docs, sidebars, and metadata', () => {
const docs = [
{
unversionedId: 'main',
permalink: '/current/main',
sidebar: 'tutorial',
frontMatter: {},
},
{
unversionedId: 'doc',
permalink: '/current/doc',
sidebar: 'tutorial',
frontMatter: {},
},
];
const sidebars: Sidebars = {
tutorial: [
{
type: 'doc',
id: 'main',
},
{
type: 'category',
label: 'Generated',
link: {
type: 'generated-index',
permalink: '/current/generated',
slug: '/current/generated',
},
items: [
{
type: 'doc',
id: 'doc',
},
],
},
],
links: [
{
type: 'link',
href: 'foo',
label: 'Foo',
},
{
type: 'link',
href: 'bar',
label: 'Bar',
},
],
another: [
{
type: 'category',
label: 'Generated',
link: {
type: 'generated-index',
permalink: '/current/generated-2',
slug: '/current/generated-2',
},
items: [
{
type: 'doc',
id: 'doc',
},
],
},
],
};
const sidebarsUtils = createSidebarsUtils(sidebars);
expect(
toGlobalDataVersion({
versionName: 'current',
@ -16,18 +86,7 @@ describe('toGlobalDataVersion', () => {
isLast: true,
path: '/current',
mainDocId: 'main',
docs: [
{
unversionedId: 'main',
permalink: '/current/main',
sidebar: 'tutorial',
},
{
unversionedId: 'doc',
permalink: '/current/doc',
sidebar: 'tutorial',
},
],
docs,
drafts: [
{
unversionedId: 'some-draft-id',
@ -35,64 +94,12 @@ describe('toGlobalDataVersion', () => {
sidebar: undefined,
},
],
sidebars: {
another: [
{
type: 'category',
label: 'Generated',
link: {
type: 'generated-index',
permalink: '/current/generated',
},
items: [
{
type: 'doc',
id: 'doc',
},
],
},
],
tutorial: [
{
type: 'doc',
id: 'main',
},
{
type: 'category',
label: 'Generated',
link: {
type: 'generated-index',
permalink: '/current/generated',
},
items: [
{
type: 'doc',
id: 'doc',
},
],
},
],
links: [
{
type: 'link',
href: 'foo',
label: 'Foo',
},
{
type: 'link',
href: 'bar',
label: 'Bar',
},
],
},
categoryGeneratedIndices: [
{
title: 'Generated',
slug: '/current/generated',
permalink: '/current/generated',
sidebar: 'tutorial',
},
],
sidebars,
categoryGeneratedIndices: getCategoryGeneratedIndexMetadataList({
docs,
sidebarsUtils,
}),
sidebarsUtils,
banner: 'unreleased',
badge: true,
className: 'current-cls',

View file

@ -7,8 +7,8 @@
import _ from 'lodash';
import type {Sidebars} from './sidebars/types';
import {createSidebarsUtils} from './sidebars/utils';
import type {LoadedVersion} from './types';
import {getMainDocId} from './docs';
import type {FullVersion} from './types';
import type {
CategoryGeneratedIndexMetadata,
DocMetadata,
@ -39,11 +39,10 @@ function toGlobalDataGeneratedIndex(
function toGlobalSidebars(
sidebars: Sidebars,
version: LoadedVersion,
version: FullVersion,
): {[sidebarId: string]: GlobalSidebar} {
const {getFirstLink} = createSidebarsUtils(sidebars);
return _.mapValues(sidebars, (sidebar, sidebarId) => {
const firstLink = getFirstLink(sidebarId);
const firstLink = version.sidebarsUtils.getFirstLink(sidebarId);
if (!firstLink) {
return {};
}
@ -62,13 +61,13 @@ function toGlobalSidebars(
});
}
export function toGlobalDataVersion(version: LoadedVersion): GlobalVersion {
export function toGlobalDataVersion(version: FullVersion): GlobalVersion {
return {
name: version.versionName,
label: version.label,
isLast: version.isLast,
path: version.path,
mainDocId: version.mainDocId,
mainDocId: getMainDocId(version),
docs: version.docs
.map(toGlobalDataDoc)
.concat(version.categoryGeneratedIndices.map(toGlobalDataGeneratedIndex)),

View file

@ -23,12 +23,7 @@ import type {LoadContext, Plugin} from '@docusaurus/types';
import {loadSidebars, resolveSidebarPathOption} from './sidebars';
import {CategoryMetadataFilenamePattern} from './sidebars/generator';
import type {DocEnv} from './docs';
import {
readVersionDocs,
processDocMetadata,
addDocNavigation,
getMainDocId,
} from './docs';
import {readVersionDocs, processDocMetadata, addDocNavigation} from './docs';
import {readVersionsMetadata} from './versions';
import type {
LoadedContent,
@ -37,12 +32,14 @@ import type {
DocFile,
DocsMarkdownOption,
VersionTag,
FullVersion,
} from './types';
import type {RuleSetRule} from 'webpack';
import {cliDocsVersionCommand} from './cli';
import {VERSIONS_JSON_FILE} from './constants';
import {toGlobalDataVersion} from './globalData';
import {toTagDocListProp} from './props';
import {getCategoryGeneratedIndexMetadataList} from './categoryGeneratedIndex';
import {
translateLoadedContent,
getLoadedContentTranslationFiles,
@ -58,7 +55,6 @@ import type {
DocFrontMatter,
} from '@docusaurus/plugin-content-docs';
import {createSidebarsUtils} from './sidebars/utils';
import {getCategoryGeneratedIndexMetadataList} from './categoryGeneratedIndex';
import _ from 'lodash';
export default async function pluginContentDocs(
@ -188,11 +184,6 @@ export default async function pluginContentDocs(
),
drafts,
sidebars,
mainDocId: getMainDocId({docs, sidebarsUtils}),
categoryGeneratedIndices: getCategoryGeneratedIndexMetadataList({
docs,
sidebarsUtils,
}),
};
}
@ -223,8 +214,19 @@ export default async function pluginContentDocs(
breadcrumbs,
} = options;
const {addRoute, createData, setGlobalData} = actions;
const versions: FullVersion[] = loadedVersions.map((version) => {
const sidebarsUtils = createSidebarsUtils(version.sidebars);
return {
...version,
sidebarsUtils,
categoryGeneratedIndices: getCategoryGeneratedIndexMetadataList({
docs: version.docs,
sidebarsUtils,
}),
};
});
async function createVersionTagsRoutes(version: LoadedVersion) {
async function createVersionTagsRoutes(version: FullVersion) {
const versionTags = getVersionTags(version.docs);
// TODO tags should be a sub route of the version route
@ -280,9 +282,9 @@ export default async function pluginContentDocs(
}
await Promise.all(
loadedVersions.map((loadedVersion) =>
versions.map((version) =>
createVersionRoutes({
loadedVersion,
version,
docItemComponent,
docLayoutComponent,
docCategoryGeneratedIndexComponent,
@ -294,11 +296,11 @@ export default async function pluginContentDocs(
);
// TODO tags should be a sub route of the version route
await Promise.all(loadedVersions.map(createVersionTagsRoutes));
await Promise.all(versions.map(createVersionTagsRoutes));
setGlobalData({
path: normalizeUrl([baseUrl, options.routeBasePath]),
versions: loadedVersions.map(toGlobalDataVersion),
versions: versions.map(toGlobalDataVersion),
breadcrumbs,
});
},

View file

@ -7,7 +7,7 @@
import type {PluginContentLoadedActions, RouteConfig} from '@docusaurus/types';
import {docuHash, createSlugger} from '@docusaurus/utils';
import type {LoadedVersion} from './types';
import type {FullVersion} from './types';
import type {
CategoryGeneratedIndexMetadata,
DocMetadata,
@ -21,7 +21,7 @@ export async function createCategoryGeneratedIndexRoutes({
docCategoryGeneratedIndexComponent,
aliasedSource,
}: {
version: LoadedVersion;
version: FullVersion;
actions: PluginContentLoadedActions;
docCategoryGeneratedIndexComponent: string;
aliasedSource: (str: string) => string;
@ -99,7 +99,7 @@ export async function createDocRoutes({
}
export async function createVersionRoutes({
loadedVersion,
version,
actions,
docItemComponent,
docLayoutComponent,
@ -107,7 +107,7 @@ export async function createVersionRoutes({
pluginId,
aliasedSource,
}: {
loadedVersion: LoadedVersion;
version: FullVersion;
actions: PluginContentLoadedActions;
docLayoutComponent: string;
docItemComponent: string;
@ -115,7 +115,7 @@ export async function createVersionRoutes({
pluginId: string;
aliasedSource: (str: string) => string;
}): Promise<void> {
async function doCreateVersionRoutes(version: LoadedVersion): Promise<void> {
async function doCreateVersionRoutes(): Promise<void> {
const versionMetadata = toVersionMetadataProp(pluginId, version);
const versionMetadataPropPath = await actions.createData(
`${docuHash(`version-${version.versionName}-metadata-prop`)}.json`,
@ -151,9 +151,9 @@ export async function createVersionRoutes({
}
try {
return await doCreateVersionRoutes(loadedVersion);
return await doCreateVersionRoutes();
} catch (err) {
logger.error`Can't create version routes for version name=${loadedVersion.versionName}`;
logger.error`Can't create version routes for version name=${version.versionName}`;
throw err;
}
}

View file

@ -16,6 +16,7 @@ import type {
CategoryGeneratedIndexMetadata,
} from '@docusaurus/plugin-content-docs';
import type {Tag} from '@docusaurus/types';
import type {SidebarsUtils} from './sidebars/utils';
export type DocFile = {
contentPath: string; // /!\ may be localized
@ -38,17 +39,20 @@ export type VersionTags = {
};
export type LoadedVersion = VersionMetadata & {
mainDocId: string;
docs: DocMetadata[];
drafts: DocMetadata[];
sidebars: Sidebars;
categoryGeneratedIndices: CategoryGeneratedIndexMetadata[];
};
export type LoadedContent = {
loadedVersions: LoadedVersion[];
};
export type FullVersion = LoadedVersion & {
sidebarsUtils: SidebarsUtils;
categoryGeneratedIndices: CategoryGeneratedIndexMetadata[];
};
export type DocBrokenMarkdownLink = BrokenMarkdownLink<VersionMetadata>;
export type DocsMarkdownOption = {