mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-16 02:27:21 +02:00
feat(v2): absolute slugs and slug resolution system (#3084)
* rework slug to allow absolute slugs and slug resolution * add slug metadata tests * refactor docs metadata test + fix slug bugs * fix tests * fix docs tests failing due to randomness + update snapshot * add test for addLeadingSlash
This commit is contained in:
parent
6730590c1e
commit
f4434b2e42
39 changed files with 791 additions and 255 deletions
41
packages/docusaurus-plugin-content-docs/src/slug.ts
Normal file
41
packages/docusaurus-plugin-content-docs/src/slug.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
/**
|
||||
* 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 {
|
||||
addLeadingSlash,
|
||||
addTrailingSlash,
|
||||
isValidPathname,
|
||||
resolvePathname,
|
||||
} from '@docusaurus/utils';
|
||||
|
||||
export default function getSlug({
|
||||
baseID,
|
||||
frontmatterSlug,
|
||||
dirName,
|
||||
}: {
|
||||
baseID: string;
|
||||
frontmatterSlug?: string;
|
||||
dirName: string;
|
||||
}) {
|
||||
const baseSlug: string = frontmatterSlug || baseID;
|
||||
let slug: string;
|
||||
if (baseSlug.startsWith('/')) {
|
||||
slug = baseSlug;
|
||||
} else {
|
||||
const resolveDirname =
|
||||
dirName === '.' ? '/' : addLeadingSlash(addTrailingSlash(dirName));
|
||||
slug = resolvePathname(baseSlug, resolveDirname);
|
||||
}
|
||||
|
||||
if (!isValidPathname(slug)) {
|
||||
throw new Error(
|
||||
`Unable to resolve valid document slug. Maybe your slug frontmatter is incorrect? Doc id=${baseID} / dirName=${dirName} / frontmatterSlug=${frontmatterSlug} => bad result slug=${slug}`,
|
||||
);
|
||||
}
|
||||
|
||||
return slug;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue