mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-23 19:48:54 +02:00
feat(content-docs): sidebar category linking to document or auto-generated index page (#5830)
Co-authored-by: Joshua Chen <sidachen2003@gmail.com> Co-authored-by: Armano <armano2@users.noreply.github.com> Co-authored-by: Alexey Pyltsyn <lex61rus@gmail.com>
This commit is contained in:
parent
95f911efef
commit
cfae5d0933
105 changed files with 3904 additions and 816 deletions
|
@ -15,39 +15,52 @@ import {
|
|||
DefaultNumberPrefixParser,
|
||||
stripPathNumberPrefixes,
|
||||
} from './numberPrefix';
|
||||
import type {NumberPrefixParser} from './types';
|
||||
import type {DocMetadataBase, NumberPrefixParser} from './types';
|
||||
import {isConventionalDocIndex} from './docs';
|
||||
|
||||
export default function getSlug({
|
||||
baseID,
|
||||
frontmatterSlug,
|
||||
dirName,
|
||||
source,
|
||||
sourceDirName,
|
||||
stripDirNumberPrefixes = true,
|
||||
numberPrefixParser = DefaultNumberPrefixParser,
|
||||
}: {
|
||||
baseID: string;
|
||||
frontmatterSlug?: string;
|
||||
dirName: string;
|
||||
source: DocMetadataBase['slug'];
|
||||
sourceDirName: DocMetadataBase['sourceDirName'];
|
||||
stripDirNumberPrefixes?: boolean;
|
||||
numberPrefixParser?: NumberPrefixParser;
|
||||
}): string {
|
||||
const baseSlug = frontmatterSlug || baseID;
|
||||
let slug: string;
|
||||
if (baseSlug.startsWith('/')) {
|
||||
slug = baseSlug;
|
||||
} else {
|
||||
function getDirNameSlug(): string {
|
||||
const dirNameStripped = stripDirNumberPrefixes
|
||||
? stripPathNumberPrefixes(dirName, numberPrefixParser)
|
||||
: dirName;
|
||||
? stripPathNumberPrefixes(sourceDirName, numberPrefixParser)
|
||||
: sourceDirName;
|
||||
const resolveDirname =
|
||||
dirName === '.'
|
||||
sourceDirName === '.'
|
||||
? '/'
|
||||
: addLeadingSlash(addTrailingSlash(dirNameStripped));
|
||||
slug = resolvePathname(baseSlug, resolveDirname);
|
||||
return resolveDirname;
|
||||
}
|
||||
|
||||
if (!isValidPathname(slug)) {
|
||||
throw new Error(
|
||||
`We couldn't compute a valid slug for document with id "${baseID}" in "${dirName}" directory.
|
||||
function computeSlug(): string {
|
||||
if (frontmatterSlug?.startsWith('/')) {
|
||||
return frontmatterSlug;
|
||||
} else {
|
||||
const dirNameSlug = getDirNameSlug();
|
||||
if (!frontmatterSlug && isConventionalDocIndex({source, sourceDirName})) {
|
||||
return dirNameSlug;
|
||||
}
|
||||
const baseSlug = frontmatterSlug || baseID;
|
||||
return resolvePathname(baseSlug, getDirNameSlug());
|
||||
}
|
||||
}
|
||||
|
||||
function ensureValidSlug(slug: string): string {
|
||||
if (!isValidPathname(slug)) {
|
||||
throw new Error(
|
||||
`We couldn't compute a valid slug for document with id "${baseID}" in "${sourceDirName}" directory.
|
||||
The slug we computed looks invalid: ${slug}.
|
||||
Maybe your slug frontmatter is incorrect or you use weird chars in the file path?
|
||||
By using the slug frontmatter, you should be able to fix this error, by using the slug of your choice:
|
||||
|
@ -57,8 +70,10 @@ Example =>
|
|||
slug: /my/customDocPath
|
||||
---
|
||||
`,
|
||||
);
|
||||
);
|
||||
}
|
||||
return slug;
|
||||
}
|
||||
|
||||
return slug;
|
||||
return ensureValidSlug(computeSlug());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue