refactor(content-docs): read category metadata files before autogenerating (#6586)

* refactor(content-docs): read category metadata files before autogenerating

* fix tests

* fix Windows...

* warn user when behavior is undetermined

* oops

* fix typo
This commit is contained in:
Joshua Chen 2022-02-03 16:16:19 +08:00 committed by GitHub
parent b03431f139
commit 1ca07f8466
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 120 additions and 128 deletions

View file

@ -9,12 +9,16 @@ import fs from 'fs-extra';
import importFresh from 'import-fresh';
import type {SidebarsConfig, Sidebars, NormalizedSidebars} from './types';
import type {NormalizeSidebarsParams} from '../types';
import {validateSidebars} from './validation';
import {validateSidebars, validateCategoryMetadataFile} from './validation';
import {normalizeSidebars} from './normalization';
import {processSidebars, type SidebarProcessorParams} from './processor';
import path from 'path';
import {createSlugger} from '@docusaurus/utils';
import {createSlugger, Globby} from '@docusaurus/utils';
import logger from '@docusaurus/logger';
import type {PluginOptions} from '@docusaurus/plugin-content-docs';
import Yaml from 'js-yaml';
import {groupBy, mapValues} from 'lodash';
import combinePromises from 'combine-promises';
export const DefaultSidebars: SidebarsConfig = {
defaultSidebar: [
@ -38,6 +42,33 @@ export function resolveSidebarPathOption(
: sidebarPathOption;
}
async function readCategoriesMetadata(contentPath: string) {
const categoryFiles = await Globby('**/_category_.{json,yml,yaml}', {
cwd: contentPath,
});
const categoryToFile = groupBy(categoryFiles, path.dirname);
return combinePromises(
mapValues(categoryToFile, async (files, folder) => {
const [filePath] = files;
if (files.length > 1) {
logger.warn`There are more than one category metadata files for path=${folder}: ${files.join(
', ',
)}. The behavior is undetermined.`;
}
const content = await fs.readFile(
path.join(contentPath, filePath),
'utf-8',
);
try {
return validateCategoryMetadataFile(Yaml.load(content));
} catch (e) {
logger.error`The docs sidebar category metadata file path=${filePath} looks invalid!`;
throw e;
}
}),
);
}
async function loadSidebarsFileUnsafe(
sidebarFilePath: string | false | undefined,
): Promise<SidebarsConfig> {
@ -80,7 +111,7 @@ export async function loadNormalizedSidebars(
// Note: sidebarFilePath must be absolute, use resolveSidebarPathOption
export async function loadSidebars(
sidebarFilePath: string | false | undefined,
options: SidebarProcessorParams,
options: Omit<SidebarProcessorParams, 'categoriesMetadata'>,
): Promise<Sidebars> {
const normalizeSidebarsParams: NormalizeSidebarsParams = {
...options.sidebarOptions,
@ -91,5 +122,8 @@ export async function loadSidebars(
sidebarFilePath,
normalizeSidebarsParams,
);
return processSidebars(normalizedSidebars, options);
const categoriesMetadata = await readCategoriesMetadata(
options.version.contentPath,
);
return processSidebars(normalizedSidebars, {...options, categoriesMetadata});
}