fix(content-docs): allow translating doc labels in sidebars.js (#7634)

This commit is contained in:
Joshua Chen 2022-06-16 22:11:21 +08:00 committed by GitHub
parent 5fe33bef06
commit 20e8e90762
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 159 additions and 6 deletions

View file

@ -1,5 +1,52 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`normalization adds a translatable marker for labels defined in sidebars.js 1`] = `
{
"sidebar": [
{
"id": "google",
"label": "Google",
"translatable": true,
"type": "doc",
},
{
"items": [
{
"id": "doc1",
"type": "doc",
},
{
"id": "doc2",
"type": "doc",
},
],
"label": "Category 1",
"type": "category",
},
{
"items": [
{
"id": "doc3",
"type": "doc",
},
{
"id": "doc4",
"type": "doc",
},
{
"id": "msft",
"label": "Microsoft",
"translatable": true,
"type": "ref",
},
],
"label": "Category 2",
"type": "category",
},
],
}
`;
exports[`normalization normalizes shorthands 1`] = `
{
"sidebar": [

View file

@ -91,4 +91,30 @@ describe('normalization', () => {
`"Invalid sidebar items collection \`"item"\` in sidebar sidebar: it must either be an array of sidebar items or a shorthand notation (which doesn't contain a \`type\` property). See https://docusaurus.io/docs/sidebar/items for all valid syntaxes."`,
);
});
it('adds a translatable marker for labels defined in sidebars.js', () => {
expect(
normalizeSidebars({
sidebar: [
{
type: 'doc',
id: 'google',
label: 'Google',
},
{
'Category 1': ['doc1', 'doc2'],
'Category 2': [
'doc3',
'doc4',
{
type: 'ref',
id: 'msft',
label: 'Microsoft',
},
],
},
],
}),
).toMatchSnapshot();
});
});

View file

@ -44,6 +44,12 @@ export function normalizeItem(
// This will never throw anyways
return normalizeSidebar(item, 'sidebar items slice');
}
if (
(item.type === 'doc' || item.type === 'ref') &&
typeof item.label === 'string'
) {
return [{...item, translatable: true}];
}
if (item.type === 'category') {
const normalizedCategory: NormalizedSidebarItemCategory = {
...item,

View file

@ -27,6 +27,11 @@ export type SidebarItemDoc = SidebarItemBase & {
type: 'doc' | 'ref';
label?: string;
id: string;
/**
* This is an internal marker. Items with labels defined in the config needs
* to be translated with JSON
*/
translatable?: true;
};
export type SidebarItemHtml = SidebarItemBase & {
@ -94,7 +99,7 @@ export type SidebarCategoriesShorthand = {
};
export type SidebarItemConfig =
| SidebarItemDoc
| Omit<SidebarItemDoc, 'translatable'>
| SidebarItemHtml
| SidebarItemLink
| SidebarItemAutogenerated

View file

@ -81,6 +81,9 @@ export function collectSidebarCategories(
export function collectSidebarLinks(sidebar: Sidebar): SidebarItemLink[] {
return collectSidebarItemsOfType('link', sidebar);
}
export function collectSidebarRefs(sidebar: Sidebar): SidebarItemDoc[] {
return collectSidebarItemsOfType('ref', sidebar);
}
// /!\ docId order matters for navigation!
export function collectSidebarDocIds(sidebar: Sidebar): string[] {

View file

@ -47,6 +47,7 @@ const sidebarItemDocSchema = sidebarItemBaseSchema.append<SidebarItemDoc>({
type: Joi.string().valid('doc', 'ref').required(),
id: Joi.string().required(),
label: Joi.string(),
translatable: Joi.boolean(),
});
const sidebarItemHtmlSchema = sidebarItemBaseSchema.append<SidebarItemHtml>({