Skip to content

Commit

Permalink
fix(content-docs): make category index text translatable
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed Apr 23, 2022
1 parent 242f2e7 commit 0bc33e1
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,93 +6,100 @@
*/

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',
label: 'Label',
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',
permalink: '/current/draft',
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',
Expand Down
13 changes: 6 additions & 7 deletions packages/docusaurus-plugin-content-docs/src/globalData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {};
}
Expand All @@ -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)),
Expand Down
36 changes: 19 additions & 17 deletions packages/docusaurus-plugin-content-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -188,11 +184,6 @@ export default async function pluginContentDocs(
),
drafts,
sidebars,
mainDocId: getMainDocId({docs, sidebarsUtils}),
categoryGeneratedIndices: getCategoryGeneratedIndexMetadataList({
docs,
sidebarsUtils,
}),
};
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -280,9 +282,9 @@ export default async function pluginContentDocs(
}

await Promise.all(
loadedVersions.map((loadedVersion) =>
versions.map((version) =>
createVersionRoutes({
loadedVersion,
version,
docItemComponent,
docLayoutComponent,
docCategoryGeneratedIndexComponent,
Expand All @@ -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,
});
},
Expand Down
14 changes: 7 additions & 7 deletions packages/docusaurus-plugin-content-docs/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,7 +21,7 @@ export async function createCategoryGeneratedIndexRoutes({
docCategoryGeneratedIndexComponent,
aliasedSource,
}: {
version: LoadedVersion;
version: FullVersion;
actions: PluginContentLoadedActions;
docCategoryGeneratedIndexComponent: string;
aliasedSource: (str: string) => string;
Expand Down Expand Up @@ -99,23 +99,23 @@ export async function createDocRoutes({
}

export async function createVersionRoutes({
loadedVersion,
version,
actions,
docItemComponent,
docLayoutComponent,
docCategoryGeneratedIndexComponent,
pluginId,
aliasedSource,
}: {
loadedVersion: LoadedVersion;
version: FullVersion;
actions: PluginContentLoadedActions;
docLayoutComponent: string;
docItemComponent: string;
docCategoryGeneratedIndexComponent: string;
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`,
Expand Down Expand Up @@ -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;
}
}
Loading

0 comments on commit 0bc33e1

Please sign in to comment.