mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-25 05:57:53 +02:00
feat(v2): docs, make numberPrefixParser configurable, better defaults, minor breaking-changes (#4655)
* make number prefix parsing logic configurable * Make numberPrefixParser configurable + rename frontmatter + avoid parsing date/version patterns by default * add more tests * more test cases
This commit is contained in:
parent
d0d29f43cc
commit
c04e613ffe
14 changed files with 325 additions and 82 deletions
|
@ -15,7 +15,6 @@ import {
|
|||
import {sortBy, take, last, orderBy} from 'lodash';
|
||||
import {addTrailingSlash, posixPath} from '@docusaurus/utils';
|
||||
import {Joi} from '@docusaurus/utils-validation';
|
||||
import {extractNumberPrefix} from './numberPrefix';
|
||||
import chalk from 'chalk';
|
||||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
|
@ -41,20 +40,22 @@ type WithPosition = {position?: number};
|
|||
type SidebarItemWithPosition = SidebarItem & WithPosition;
|
||||
|
||||
const CategoryMetadatasFileSchema = Joi.object<CategoryMetadatasFile>({
|
||||
label: Joi.string().optional(),
|
||||
position: Joi.number().optional(),
|
||||
collapsed: Joi.boolean().optional(),
|
||||
label: Joi.string(),
|
||||
position: Joi.number(),
|
||||
collapsed: Joi.boolean(),
|
||||
});
|
||||
|
||||
// TODO I now believe we should read all the category metadata files ahead of time: we may need this metadata to customize docs metadata
|
||||
// Example use-case being able to disable number prefix parsing at the folder level, or customize the default route path segment for an intermediate directory...
|
||||
// TODO later if there is `CategoryFolder/index.md`, we may want to read the metadata as yaml on it
|
||||
// see https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
|
||||
async function readCategoryMetadatasFile(
|
||||
categoryDirPath: string,
|
||||
): Promise<CategoryMetadatasFile | null> {
|
||||
function assertCategoryMetadataFile(
|
||||
function validateCategoryMetadataFile(
|
||||
content: unknown,
|
||||
): asserts content is CategoryMetadatasFile {
|
||||
Joi.attempt(content, CategoryMetadatasFileSchema);
|
||||
): CategoryMetadatasFile {
|
||||
return Joi.attempt(content, CategoryMetadatasFileSchema);
|
||||
}
|
||||
|
||||
async function tryReadFile(
|
||||
|
@ -69,8 +70,7 @@ async function readCategoryMetadatasFile(
|
|||
const contentString = await fs.readFile(filePath, {encoding: 'utf8'});
|
||||
const unsafeContent: unknown = parse(contentString);
|
||||
try {
|
||||
assertCategoryMetadataFile(unsafeContent);
|
||||
return unsafeContent;
|
||||
return validateCategoryMetadataFile(unsafeContent);
|
||||
} catch (e) {
|
||||
console.error(
|
||||
chalk.red(
|
||||
|
@ -106,6 +106,7 @@ export const DefaultSidebarItemsGenerator: SidebarItemsGenerator = async functio
|
|||
item,
|
||||
docs: allDocs,
|
||||
version,
|
||||
numberPrefixParser,
|
||||
}): Promise<SidebarItem[]> {
|
||||
// Doc at the root of the autogenerated sidebar dir
|
||||
function isRootDoc(doc: SidebarItemsGeneratorDoc) {
|
||||
|
@ -194,7 +195,7 @@ export const DefaultSidebarItemsGenerator: SidebarItemsGenerator = async functio
|
|||
|
||||
const {tail} = parseBreadcrumb(breadcrumb);
|
||||
|
||||
const {filename, numberPrefix} = extractNumberPrefix(tail);
|
||||
const {filename, numberPrefix} = numberPrefixParser(tail);
|
||||
|
||||
const position = categoryMetadatas?.position ?? numberPrefix;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue