From 0bc33e12ce6762d04eb2b2078ddfb1cc0b1f5b5f Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sat, 23 Apr 2022 16:22:45 +0800 Subject: [PATCH] fix(content-docs): make category index text translatable --- .../__snapshots__/globalData.test.ts.snap | 7 +- .../src/__tests__/globalData.test.ts | 147 +++++++++--------- .../src/globalData.ts | 13 +- .../src/index.ts | 36 +++-- .../src/routes.ts | 14 +- .../src/types.ts | 8 +- 6 files changed, 121 insertions(+), 104 deletions(-) diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/globalData.test.ts.snap b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/globalData.test.ts.snap index 242bf1b74890..0bd0862b0f8d 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/globalData.test.ts.snap +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/globalData.test.ts.snap @@ -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": {}, diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/globalData.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/globalData.test.ts index d90dcb0e8784..8c05e812635f 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/globalData.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/globalData.test.ts @@ -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', diff --git a/packages/docusaurus-plugin-content-docs/src/globalData.ts b/packages/docusaurus-plugin-content-docs/src/globalData.ts index bc3b6dd86268..c688ab2c0098 100644 --- a/packages/docusaurus-plugin-content-docs/src/globalData.ts +++ b/packages/docusaurus-plugin-content-docs/src/globalData.ts @@ -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)), diff --git a/packages/docusaurus-plugin-content-docs/src/index.ts b/packages/docusaurus-plugin-content-docs/src/index.ts index eee9907eb6e6..3c0c0a9531df 100644 --- a/packages/docusaurus-plugin-content-docs/src/index.ts +++ b/packages/docusaurus-plugin-content-docs/src/index.ts @@ -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, }); }, diff --git a/packages/docusaurus-plugin-content-docs/src/routes.ts b/packages/docusaurus-plugin-content-docs/src/routes.ts index 739003737f32..57f5722ba1db 100644 --- a/packages/docusaurus-plugin-content-docs/src/routes.ts +++ b/packages/docusaurus-plugin-content-docs/src/routes.ts @@ -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 { - async function doCreateVersionRoutes(version: LoadedVersion): Promise { + async function doCreateVersionRoutes(): Promise { 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; } } diff --git a/packages/docusaurus-plugin-content-docs/src/types.ts b/packages/docusaurus-plugin-content-docs/src/types.ts index 713b8a9a35c7..b12fffaa9e29 100644 --- a/packages/docusaurus-plugin-content-docs/src/types.ts +++ b/packages/docusaurus-plugin-content-docs/src/types.ts @@ -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; export type DocsMarkdownOption = {