mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-25 20:48:50 +02:00
* docs i18n initial poc * docs i18n initial poc * docs i18n initial poc * docs i18n initial poc * crowdin-v2 attempt * fix source * use crowdin env variable * try to install crowdin on netlify * try to install crowdin on netlify * try to use crowdin jar directly * try to curl the crowdin jar * add java version cmd * try to run crowdin jar in netlify * fix translatedDocsDirPath * fix loadContext issue due to site baseUrl not being modified in generted config file * real validateLocalesFile * add locale option to deploy command * better LocalizationFile type * create util getPluginI18nPath * better core localization context loading code * More explicit VersionMetadata type for localized docs folders * Ability to translate blog posts with Crowdin! * blog: refactor markdown loader + report broken links + try to get linkify working better * upgrade crowdin config to upload all docs folder files except source code related files * try to support translated pages * make markdown pages translation work * add write-translations cli command template * fix site not reloaded with correct options * refactor a bit the read/write of @generated/i18n.json file * Add <Translate> + translate() API + use it on the docusaurus homepage * watch locale translation dir * early POC of adding babel parsing for translation extraction * fs.stat => pathExists * add install:fast script * TSC: noUnusedLocals false as it's already checked by eslint * POC of extracting translations from source code * minor typo * fix extracted key to code * initial docs extracted translations * stable plugin translations POC * add crowdin commands * quickfix for i18n deployment * POC of themeConfig translation * add ability to have localized site without path prefix * sidebar typo * refactor translation system to output multiple translation files * translate properly the docs plugin * improve theme classic translation * rework translation extractor to handle new Chrome I18n JSON format (include id/description) * writeTranslations: allow to pass locales cli arg * fix ThemeConfig TS issues * fix localizePath errors * temporary add write-translations to netlify deploy preview * complete example of french translated folder * update fr folder * remove all translations from repo * minor translation refactors * fix all docs-related tests * fix blog feed tests * fix last blog tests * refactor i18n context a bit, extract codeTranslations in an extra generated file * improve @generated/i18n type * fix some i18n todos * minor refactor * fix logo typing issue after merge * move i18n.json to siteConfig instead * try to fix windows CI build * fix config test * attempt to fix windows non-posix path * increase v1 minify css jest timeout due to flaky test * proper support for localizePath on windows * remove non-functional install:fast * docs, fix docsDirPathLocalized * fix Docs i18n / md linkify issues * ensure theme-classic swizzling will use "nextjs" sources (transpiled less aggressively, to make them human readable) * fix some snapshots * improve themeConfig translation code * refactor a bit getPluginI18nPath * readTranslationFileContent => ensure files are valid, fail fast * fix versions tests * add extractSourceCodeAstTranslations comments/resource links * ignore eslint: packages/docusaurus-theme-classic/lib-next/ * fix windows CI with cross-env * crowdin ignore .DS_Store * improve writeTranslations + add exhaustive tests for translations.ts * remove typo * Wire currentLocale to algolia search * improve i18n locale error * Add tests for translationsExtractor.ts * better code translation extraction regarding statically evaluable code * fix typo * fix typo * improve theme-classic transpilation * refactor + add i18n tests * typo * test new utils * add missing snapshots * fix snapshot * blog onBrokenMarkdownLink * add sidebars tests * theme-classic index should now use ES modules * tests for theme-classic translations * useless comment * add more translation tests * simplify/cleanup writeTranslations * try to fix Netlify fr deployment * blog: test translated md is used during feed generation * blog: better i18n tests regarding editUrl + md translation application * more i18n tests for docs plugin * more i18n tests for docs plugin * Add tests for pages i18n * polish docusaurus build i18n logs
334 lines
8.9 KiB
TypeScript
334 lines
8.9 KiB
TypeScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
import flatMap from 'lodash.flatmap';
|
|
import fs from 'fs-extra';
|
|
import importFresh from 'import-fresh';
|
|
import {
|
|
Sidebars,
|
|
SidebarItem,
|
|
SidebarItemLink,
|
|
SidebarItemDoc,
|
|
Sidebar,
|
|
SidebarItemCategory,
|
|
SidebarItemType,
|
|
} from './types';
|
|
import {mapValues, flatten, difference} from 'lodash';
|
|
import {getElementsAround} from '@docusaurus/utils';
|
|
|
|
type SidebarItemCategoryJSON = {
|
|
type: 'category';
|
|
label: string;
|
|
items: SidebarItemJSON[];
|
|
collapsed?: boolean;
|
|
};
|
|
|
|
type SidebarItemJSON =
|
|
| string
|
|
| SidebarCategoryShorthandJSON
|
|
| SidebarItemDoc
|
|
| SidebarItemLink
|
|
| SidebarItemCategoryJSON
|
|
| {
|
|
type: string;
|
|
[key: string]: unknown;
|
|
};
|
|
|
|
type SidebarCategoryShorthandJSON = {
|
|
[sidebarCategory: string]: SidebarItemJSON[];
|
|
};
|
|
|
|
type SidebarJSON = SidebarCategoryShorthandJSON | SidebarItemJSON[];
|
|
|
|
// Sidebar given by user that is not normalized yet. e.g: sidebars.json
|
|
type SidebarsJSON = {
|
|
[sidebarId: string]: SidebarJSON;
|
|
};
|
|
|
|
function isCategoryShorthand(
|
|
item: SidebarItemJSON,
|
|
): item is SidebarCategoryShorthandJSON {
|
|
return typeof item !== 'string' && !item.type;
|
|
}
|
|
|
|
// categories are collapsed by default, unless user set collapsed = false
|
|
const defaultCategoryCollapsedValue = true;
|
|
|
|
/**
|
|
* Convert {category1: [item1,item2]} shorthand syntax to long-form syntax
|
|
*/
|
|
function normalizeCategoryShorthand(
|
|
sidebar: SidebarCategoryShorthandJSON,
|
|
): SidebarItemCategoryJSON[] {
|
|
return Object.entries(sidebar).map(([label, items]) => ({
|
|
type: 'category',
|
|
collapsed: defaultCategoryCollapsedValue,
|
|
label,
|
|
items,
|
|
}));
|
|
}
|
|
|
|
/**
|
|
* Check that item contains only allowed keys.
|
|
*/
|
|
function assertItem<K extends string>(
|
|
item: any,
|
|
keys: K[],
|
|
): asserts item is Record<K, any> {
|
|
const unknownKeys = Object.keys(item).filter(
|
|
// @ts-expect-error: key is always string
|
|
(key) => !keys.includes(key as string) && key !== 'type',
|
|
);
|
|
|
|
if (unknownKeys.length) {
|
|
throw new Error(
|
|
`Unknown sidebar item keys: ${unknownKeys}. Item: ${JSON.stringify(
|
|
item,
|
|
)}`,
|
|
);
|
|
}
|
|
}
|
|
|
|
function assertIsCategory(
|
|
item: unknown,
|
|
): asserts item is SidebarItemCategoryJSON {
|
|
assertItem(item, ['items', 'label', 'collapsed']);
|
|
if (typeof item.label !== 'string') {
|
|
throw new Error(
|
|
`Error loading ${JSON.stringify(item)}. "label" must be a string.`,
|
|
);
|
|
}
|
|
if (!Array.isArray(item.items)) {
|
|
throw new Error(
|
|
`Error loading ${JSON.stringify(item)}. "items" must be an array.`,
|
|
);
|
|
}
|
|
// "collapsed" is an optional property
|
|
if (item.hasOwnProperty('collapsed') && typeof item.collapsed !== 'boolean') {
|
|
throw new Error(
|
|
`Error loading ${JSON.stringify(item)}. "collapsed" must be a boolean.`,
|
|
);
|
|
}
|
|
}
|
|
|
|
function assertIsDoc(item: unknown): asserts item is SidebarItemDoc {
|
|
assertItem(item, ['id']);
|
|
if (typeof item.id !== 'string') {
|
|
throw new Error(
|
|
`Error loading ${JSON.stringify(item)}. "id" must be a string.`,
|
|
);
|
|
}
|
|
}
|
|
|
|
function assertIsLink(item: unknown): asserts item is SidebarItemLink {
|
|
assertItem(item, ['href', 'label']);
|
|
if (typeof item.href !== 'string') {
|
|
throw new Error(
|
|
`Error loading ${JSON.stringify(item)}. "href" must be a string.`,
|
|
);
|
|
}
|
|
if (typeof item.label !== 'string') {
|
|
throw new Error(
|
|
`Error loading ${JSON.stringify(item)}. "label" must be a string.`,
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Normalizes recursively item and all its children. Ensures that at the end
|
|
* each item will be an object with the corresponding type.
|
|
*/
|
|
function normalizeItem(item: SidebarItemJSON): SidebarItem[] {
|
|
if (typeof item === 'string') {
|
|
return [
|
|
{
|
|
type: 'doc',
|
|
id: item,
|
|
},
|
|
];
|
|
}
|
|
if (isCategoryShorthand(item)) {
|
|
return flatMap(normalizeCategoryShorthand(item), normalizeItem);
|
|
}
|
|
switch (item.type) {
|
|
case 'category':
|
|
assertIsCategory(item);
|
|
return [
|
|
{
|
|
collapsed: defaultCategoryCollapsedValue,
|
|
...item,
|
|
items: flatMap(item.items, normalizeItem),
|
|
},
|
|
];
|
|
case 'link':
|
|
assertIsLink(item);
|
|
return [item];
|
|
case 'ref':
|
|
case 'doc':
|
|
assertIsDoc(item);
|
|
return [item];
|
|
default: {
|
|
const extraMigrationError =
|
|
item.type === 'subcategory'
|
|
? "Docusaurus v2: 'subcategory' has been renamed as 'category'"
|
|
: '';
|
|
throw new Error(
|
|
`Unknown sidebar item type [${
|
|
item.type
|
|
}]. Sidebar item=${JSON.stringify(item)} ${extraMigrationError}`,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
function normalizeSidebar(sidebar: SidebarJSON) {
|
|
const normalizedSidebar: SidebarItemJSON[] = Array.isArray(sidebar)
|
|
? sidebar
|
|
: normalizeCategoryShorthand(sidebar);
|
|
|
|
return flatMap(normalizedSidebar, normalizeItem);
|
|
}
|
|
|
|
function normalizeSidebars(sidebars: SidebarsJSON): Sidebars {
|
|
return mapValues(sidebars, normalizeSidebar);
|
|
}
|
|
|
|
// TODO refactor: make async
|
|
export function loadSidebars(sidebarFilePath: string): Sidebars {
|
|
if (!sidebarFilePath) {
|
|
throw new Error(`sidebarFilePath not provided: ${sidebarFilePath}`);
|
|
}
|
|
|
|
// sidebars file is optional, some users use docs without sidebars!
|
|
// See https://github.com/facebook/docusaurus/issues/3366
|
|
if (!fs.existsSync(sidebarFilePath)) {
|
|
// throw new Error(`No sidebar file exist at path: ${sidebarFilePath}`);
|
|
return {};
|
|
}
|
|
|
|
// We don't want sidebars to be cached because of hot reloading.
|
|
const sidebarJson = importFresh(sidebarFilePath) as SidebarsJSON;
|
|
return normalizeSidebars(sidebarJson);
|
|
}
|
|
|
|
function collectSidebarItemsOfType<
|
|
Type extends SidebarItemType,
|
|
Item extends SidebarItem & {type: SidebarItemType}
|
|
>(type: Type, sidebar: Sidebar): Item[] {
|
|
function collectRecursive(item: SidebarItem): Item[] {
|
|
const currentItemsCollected: Item[] =
|
|
item.type === type ? [item as Item] : [];
|
|
|
|
const childItemsCollected: Item[] =
|
|
item.type === 'category' ? flatten(item.items.map(collectRecursive)) : [];
|
|
|
|
return [...currentItemsCollected, ...childItemsCollected];
|
|
}
|
|
|
|
return flatten(sidebar.map(collectRecursive));
|
|
}
|
|
|
|
export function collectSidebarDocItems(sidebar: Sidebar): SidebarItemDoc[] {
|
|
return collectSidebarItemsOfType('doc', sidebar);
|
|
}
|
|
export function collectSidebarCategories(
|
|
sidebar: Sidebar,
|
|
): SidebarItemCategory[] {
|
|
return collectSidebarItemsOfType('category', sidebar);
|
|
}
|
|
export function collectSidebarLinks(sidebar: Sidebar): SidebarItemLink[] {
|
|
return collectSidebarItemsOfType('link', sidebar);
|
|
}
|
|
|
|
export function transformSidebarItems(
|
|
sidebar: Sidebar,
|
|
updateFn: (item: SidebarItem) => SidebarItem,
|
|
): Sidebar {
|
|
function transformRecursive(item: SidebarItem): SidebarItem {
|
|
if (item.type === 'category') {
|
|
return updateFn({
|
|
...item,
|
|
items: item.items.map(transformRecursive),
|
|
});
|
|
}
|
|
return updateFn(item);
|
|
}
|
|
return sidebar.map(transformRecursive);
|
|
}
|
|
|
|
export function collectSidebarsDocIds(
|
|
sidebars: Sidebars,
|
|
): Record<string, string[]> {
|
|
return mapValues(sidebars, (sidebar) => {
|
|
return collectSidebarDocItems(sidebar).map((docItem) => docItem.id);
|
|
});
|
|
}
|
|
|
|
export function createSidebarsUtils(sidebars: Sidebars) {
|
|
const sidebarNameToDocIds = collectSidebarsDocIds(sidebars);
|
|
|
|
function getFirstDocIdOfFirstSidebar(): string | undefined {
|
|
return Object.values(sidebarNameToDocIds)[0]?.[0];
|
|
}
|
|
|
|
function getSidebarNameByDocId(docId: string): string | undefined {
|
|
// TODO lookup speed can be optimized
|
|
const entry = Object.entries(
|
|
sidebarNameToDocIds,
|
|
).find(([_sidebarName, docIds]) => docIds.includes(docId));
|
|
|
|
return entry?.[0];
|
|
}
|
|
|
|
function getDocNavigation(
|
|
docId: string,
|
|
): {
|
|
sidebarName: string | undefined;
|
|
previousId: string | undefined;
|
|
nextId: string | undefined;
|
|
} {
|
|
const sidebarName = getSidebarNameByDocId(docId);
|
|
if (sidebarName) {
|
|
const docIds = sidebarNameToDocIds[sidebarName];
|
|
const currentIndex = docIds.indexOf(docId);
|
|
const {previous, next} = getElementsAround(docIds, currentIndex);
|
|
return {
|
|
sidebarName,
|
|
previousId: previous,
|
|
nextId: next,
|
|
};
|
|
} else {
|
|
return {
|
|
sidebarName: undefined,
|
|
previousId: undefined,
|
|
nextId: undefined,
|
|
};
|
|
}
|
|
}
|
|
|
|
function checkSidebarsDocIds(validDocIds: string[]) {
|
|
const allSidebarDocIds = flatten(Object.values(sidebarNameToDocIds));
|
|
const invalidSidebarDocIds = difference(allSidebarDocIds, validDocIds);
|
|
if (invalidSidebarDocIds.length > 0) {
|
|
throw new Error(
|
|
`Bad sidebars file.
|
|
These sidebar document ids do not exist:
|
|
- ${invalidSidebarDocIds.sort().join('\n- ')}\`,
|
|
|
|
Available document ids=
|
|
- ${validDocIds.sort().join('\n- ')}`,
|
|
);
|
|
}
|
|
}
|
|
|
|
return {
|
|
getFirstDocIdOfFirstSidebar,
|
|
getSidebarNameByDocId,
|
|
getDocNavigation,
|
|
checkSidebarsDocIds,
|
|
};
|
|
}
|