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

View file

@ -6,52 +6,27 @@
*/ */
import {toGlobalDataVersion} from '../globalData'; import {toGlobalDataVersion} from '../globalData';
import {createSidebarsUtils} from '../sidebars/utils';
import {getCategoryGeneratedIndexMetadataList} from '../categoryGeneratedIndex';
import type {Sidebars} from '../sidebars/types';
describe('toGlobalDataVersion', () => { describe('toGlobalDataVersion', () => {
it('generates the right docs, sidebars, and metadata', () => { it('generates the right docs, sidebars, and metadata', () => {
expect( const docs = [
toGlobalDataVersion({
versionName: 'current',
label: 'Label',
isLast: true,
path: '/current',
mainDocId: 'main',
docs: [
{ {
unversionedId: 'main', unversionedId: 'main',
permalink: '/current/main', permalink: '/current/main',
sidebar: 'tutorial', sidebar: 'tutorial',
frontMatter: {},
}, },
{ {
unversionedId: 'doc', unversionedId: 'doc',
permalink: '/current/doc', permalink: '/current/doc',
sidebar: 'tutorial', sidebar: 'tutorial',
frontMatter: {},
}, },
], ];
drafts: [ const sidebars: Sidebars = {
{
unversionedId: 'some-draft-id',
permalink: '/current/draft',
sidebar: undefined,
},
],
sidebars: {
another: [
{
type: 'category',
label: 'Generated',
link: {
type: 'generated-index',
permalink: '/current/generated',
},
items: [
{
type: 'doc',
id: 'doc',
},
],
},
],
tutorial: [ tutorial: [
{ {
type: 'doc', type: 'doc',
@ -63,6 +38,7 @@ describe('toGlobalDataVersion', () => {
link: { link: {
type: 'generated-index', type: 'generated-index',
permalink: '/current/generated', permalink: '/current/generated',
slug: '/current/generated',
}, },
items: [ items: [
{ {
@ -84,15 +60,46 @@ describe('toGlobalDataVersion', () => {
label: 'Bar', label: 'Bar',
}, },
], ],
}, another: [
categoryGeneratedIndices: [
{ {
title: 'Generated', type: 'category',
slug: '/current/generated', label: 'Generated',
permalink: '/current/generated', link: {
sidebar: 'tutorial', type: 'generated-index',
permalink: '/current/generated-2',
slug: '/current/generated-2',
},
items: [
{
type: 'doc',
id: 'doc',
}, },
], ],
},
],
};
const sidebarsUtils = createSidebarsUtils(sidebars);
expect(
toGlobalDataVersion({
versionName: 'current',
label: 'Label',
isLast: true,
path: '/current',
mainDocId: 'main',
docs,
drafts: [
{
unversionedId: 'some-draft-id',
permalink: '/current/draft',
sidebar: undefined,
},
],
sidebars,
categoryGeneratedIndices: getCategoryGeneratedIndexMetadataList({
docs,
sidebarsUtils,
}),
sidebarsUtils,
banner: 'unreleased', banner: 'unreleased',
badge: true, badge: true,
className: 'current-cls', className: 'current-cls',

View file

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

View file

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

View file

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

View file

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