@docusaurus/plugin-content-docs 2.0.0-beta.12faed89d → 2.0.0-beta.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/categoryGeneratedIndex.d.ts +12 -0
- package/lib/categoryGeneratedIndex.js +37 -0
- package/lib/cli.d.ts +2 -2
- package/lib/cli.js +14 -35
- package/lib/client/docsClientUtils.d.ts +0 -3
- package/lib/client/docsClientUtils.js +19 -22
- package/lib/docFrontMatter.d.ts +1 -1
- package/lib/docFrontMatter.js +7 -3
- package/lib/docs.d.ts +25 -3
- package/lib/docs.js +126 -41
- package/lib/globalData.d.ts +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +101 -132
- package/lib/lastUpdate.js +12 -12
- package/lib/markdown/index.d.ts +3 -6
- package/lib/markdown/index.js +3 -3
- package/lib/markdown/linkify.js +2 -2
- package/lib/numberPrefix.d.ts +1 -1
- package/lib/options.d.ts +3 -3
- package/lib/options.js +48 -11
- package/lib/props.d.ts +7 -2
- package/lib/props.js +60 -8
- package/lib/routes.d.ts +27 -0
- package/lib/routes.js +105 -0
- package/lib/{sidebarItemsGenerator.d.ts → sidebars/generator.d.ts} +5 -2
- package/lib/sidebars/generator.js +216 -0
- package/lib/sidebars/index.d.ts +15 -0
- package/lib/sidebars/index.js +73 -0
- package/lib/sidebars/normalization.d.ts +14 -0
- package/lib/sidebars/normalization.js +77 -0
- package/lib/sidebars/processor.d.ts +18 -0
- package/lib/sidebars/processor.js +85 -0
- package/lib/sidebars/types.d.ts +127 -0
- package/{src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars2.js → lib/sidebars/types.js} +2 -10
- package/lib/sidebars/utils.d.ts +35 -0
- package/lib/sidebars/utils.js +228 -0
- package/lib/sidebars/validation.d.ts +10 -0
- package/lib/sidebars/validation.js +138 -0
- package/lib/slug.d.ts +4 -3
- package/lib/slug.js +27 -15
- package/{src/__tests__/__fixtures__/site-with-doc-label/docusaurus.config.js → lib/tags.d.ts} +2 -8
- package/lib/tags.js +20 -0
- package/lib/theme/hooks/useDocs.js +21 -21
- package/lib/translations.d.ts +2 -2
- package/lib/translations.js +71 -29
- package/lib/types.d.ts +52 -63
- package/lib/versions.d.ts +3 -3
- package/lib/versions.js +41 -22
- package/package.json +20 -20
- package/src/categoryGeneratedIndex.ts +57 -0
- package/src/cli.ts +10 -42
- package/src/client/docsClientUtils.ts +14 -26
- package/{types.d.ts → src/deps.d.ts} +0 -0
- package/src/docFrontMatter.ts +9 -4
- package/src/docs.ts +164 -36
- package/src/globalData.ts +6 -1
- package/src/index.ts +127 -175
- package/src/lastUpdate.ts +14 -16
- package/src/markdown/index.ts +8 -12
- package/src/numberPrefix.ts +5 -3
- package/src/options.ts +56 -15
- package/src/plugin-content-docs.d.ts +173 -40
- package/src/props.ts +90 -15
- package/src/routes.ts +169 -0
- package/src/sidebars/generator.ts +302 -0
- package/src/sidebars/index.ts +94 -0
- package/src/sidebars/normalization.ts +112 -0
- package/src/sidebars/processor.ts +154 -0
- package/src/sidebars/types.ts +211 -0
- package/src/sidebars/utils.ts +329 -0
- package/src/sidebars/validation.ts +168 -0
- package/src/slug.ts +32 -17
- package/src/tags.ts +19 -0
- package/src/translations.ts +103 -47
- package/src/types.ts +64 -107
- package/src/versions.ts +59 -25
- package/lib/.tsbuildinfo +0 -1
- package/lib/sidebarItemsGenerator.js +0 -211
- package/lib/sidebars.d.ts +0 -43
- package/lib/sidebars.js +0 -320
- package/src/__tests__/__fixtures__/bad-id-site/docs/invalid-id.md +0 -5
- package/src/__tests__/__fixtures__/bad-slug-on-doc-home-site/docs/docWithSlug.md +0 -5
- package/src/__tests__/__fixtures__/empty-site/docusaurus.config.js +0 -16
- package/src/__tests__/__fixtures__/empty-site/sidebars.json +0 -1
- package/src/__tests__/__fixtures__/sidebars/sidebars-category-shorthand.js +0 -34
- package/src/__tests__/__fixtures__/sidebars/sidebars-category-wrong-items.json +0 -11
- package/src/__tests__/__fixtures__/sidebars/sidebars-category-wrong-label.json +0 -11
- package/src/__tests__/__fixtures__/sidebars/sidebars-category.js +0 -44
- package/src/__tests__/__fixtures__/sidebars/sidebars-collapsed-first-level.json +0 -20
- package/src/__tests__/__fixtures__/sidebars/sidebars-collapsed.json +0 -21
- package/src/__tests__/__fixtures__/sidebars/sidebars-doc-id-not-string.json +0 -10
- package/src/__tests__/__fixtures__/sidebars/sidebars-first-level-not-category.js +0 -20
- package/src/__tests__/__fixtures__/sidebars/sidebars-link-wrong-href.json +0 -11
- package/src/__tests__/__fixtures__/sidebars/sidebars-link-wrong-label.json +0 -11
- package/src/__tests__/__fixtures__/sidebars/sidebars-link.json +0 -11
- package/src/__tests__/__fixtures__/sidebars/sidebars-unknown-type.json +0 -14
- package/src/__tests__/__fixtures__/sidebars/sidebars-wrong-field.json +0 -20
- package/src/__tests__/__fixtures__/sidebars/sidebars.json +0 -20
- package/src/__tests__/__fixtures__/simple-site/docs/foo/bar.md +0 -69
- package/src/__tests__/__fixtures__/simple-site/docs/foo/baz.md +0 -70
- package/src/__tests__/__fixtures__/simple-site/docs/headingAsTitle.md +0 -1
- package/src/__tests__/__fixtures__/simple-site/docs/hello.md +0 -53
- package/src/__tests__/__fixtures__/simple-site/docs/ipsum.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/lorem.md +0 -6
- package/src/__tests__/__fixtures__/simple-site/docs/rootAbsoluteSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/rootRelativeSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/rootResolvedSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/rootTryToEscapeSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/slugs/absoluteSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/slugs/relativeSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/slugs/resolvedSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/slugs/tryToEscapeSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docusaurus.config.js +0 -14
- package/src/__tests__/__fixtures__/simple-site/sidebars.json +0 -23
- package/src/__tests__/__fixtures__/simple-site/wrong-sidebars.json +0 -7
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/0-getting-started.md +0 -3
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/1-installation.md +0 -3
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/00_api-overview.md +0 -3
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/01_Core APIs/0 --- Client API.md +0 -1
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/01_Core APIs/1 --- Server API.md +0 -1
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/0. Plugin API.md +0 -1
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/1. Theme API.md +0 -1
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/_category_.yml +0 -1
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/03_api-end.md +0 -3
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/_category_.json +0 -3
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/0-guide2.5.md +0 -8
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/02-guide2.md +0 -7
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/_category_.json +0 -3
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/a-guide4.md +0 -7
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/b-guide5.md +0 -7
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/guide3.md +0 -8
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/z-guide1.md +0 -8
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docusaurus.config.js +0 -14
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars.js +0 -23
- package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-1.md +0 -7
- package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-2.md +0 -8
- package/src/__tests__/__fixtures__/site-with-doc-label/sidebars.json +0 -14
- package/src/__tests__/__fixtures__/versioned-site/community/team.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/community_sidebars.json +0 -3
- package/src/__tests__/__fixtures__/versioned-site/community_versioned_docs/version-1.0.0/team.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/community_versioned_sidebars/version-1.0.0-sidebars.json +0 -3
- package/src/__tests__/__fixtures__/versioned-site/community_versions.json +0 -1
- package/src/__tests__/__fixtures__/versioned-site/docs/foo/bar.md +0 -4
- package/src/__tests__/__fixtures__/versioned-site/docs/hello.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/docs/slugs/absoluteSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/docs/slugs/relativeSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/docs/slugs/resolvedSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/docs/slugs/tryToEscapeSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/docusaurus.config.js +0 -18
- package/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs-community/current/team.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/sidebars.json +0 -10
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/foo/bar.md +0 -4
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/foo/baz.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/hello.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/foo/bar.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/hello.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootAbsoluteSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootRelativeSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootResolvedSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootTryToEscapeSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/absoluteSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/relativeSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/resolvedSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/tryToEscapeSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.0-sidebars.json +0 -11
- package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.1-sidebars.json +0 -10
- package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-withSlugs-sidebars.json +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versions.json +0 -5
- package/src/__tests__/__snapshots__/cli.test.ts.snap +0 -90
- package/src/__tests__/__snapshots__/index.test.ts.snap +0 -1902
- package/src/__tests__/__snapshots__/sidebars.test.ts.snap +0 -218
- package/src/__tests__/__snapshots__/translations.test.ts.snap +0 -484
- package/src/__tests__/cli.test.ts +0 -333
- package/src/__tests__/docFrontMatter.test.ts +0 -244
- package/src/__tests__/docs.test.ts +0 -878
- package/src/__tests__/index.test.ts +0 -1868
- package/src/__tests__/lastUpdate.test.ts +0 -69
- package/src/__tests__/numberPrefix.test.ts +0 -199
- package/src/__tests__/options.test.ts +0 -231
- package/src/__tests__/sidebarItemsGenerator.test.ts +0 -336
- package/src/__tests__/sidebars.test.ts +0 -639
- package/src/__tests__/slug.test.ts +0 -109
- package/src/__tests__/translations.test.ts +0 -158
- package/src/__tests__/versions.test.ts +0 -741
- package/src/client/__tests__/docsClientUtils.test.ts +0 -371
- package/src/markdown/__tests__/__fixtures__/docs/doc-localized.md +0 -1
- package/src/markdown/__tests__/__fixtures__/docs/doc1.md +0 -13
- package/src/markdown/__tests__/__fixtures__/docs/doc2.md +0 -12
- package/src/markdown/__tests__/__fixtures__/docs/doc4.md +0 -19
- package/src/markdown/__tests__/__fixtures__/docs/doc5.md +0 -6
- package/src/markdown/__tests__/__fixtures__/docs/subdir/doc3.md +0 -3
- package/src/markdown/__tests__/__fixtures__/versioned_docs/version-1.0.0/doc2.md +0 -7
- package/src/markdown/__tests__/__fixtures__/versioned_docs/version-1.0.0/subdir/doc1.md +0 -3
- package/src/markdown/__tests__/__snapshots__/linkify.test.ts.snap +0 -82
- package/src/markdown/__tests__/linkify.test.ts +0 -190
- package/src/sidebarItemsGenerator.ts +0 -307
- package/src/sidebars.ts +0 -522
- package/tsconfig.json +0 -9
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type {Optional} from 'utility-types';
|
|
9
|
+
import type {
|
|
10
|
+
DocMetadataBase,
|
|
11
|
+
VersionMetadata,
|
|
12
|
+
NumberPrefixParser,
|
|
13
|
+
SidebarOptions,
|
|
14
|
+
} from '../types';
|
|
15
|
+
import {Required} from 'utility-types';
|
|
16
|
+
|
|
17
|
+
// Makes all properties visible when hovering over the type
|
|
18
|
+
type Expand<T extends Record<string, unknown>> = {[P in keyof T]: T[P]};
|
|
19
|
+
|
|
20
|
+
export type SidebarItemBase = {
|
|
21
|
+
className?: string;
|
|
22
|
+
customProps?: Record<string, unknown>;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type SidebarItemDoc = SidebarItemBase & {
|
|
26
|
+
type: 'doc' | 'ref';
|
|
27
|
+
label?: string;
|
|
28
|
+
id: string;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export type SidebarItemLink = SidebarItemBase & {
|
|
32
|
+
type: 'link';
|
|
33
|
+
href: string;
|
|
34
|
+
label: string;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type SidebarItemAutogenerated = SidebarItemBase & {
|
|
38
|
+
type: 'autogenerated';
|
|
39
|
+
dirName: string;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
type SidebarItemCategoryBase = SidebarItemBase & {
|
|
43
|
+
type: 'category';
|
|
44
|
+
label: string;
|
|
45
|
+
collapsed: boolean;
|
|
46
|
+
collapsible: boolean;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export type SidebarItemCategoryLinkDoc = {type: 'doc'; id: string};
|
|
50
|
+
|
|
51
|
+
export type SidebarItemCategoryLinkGeneratedIndexConfig = {
|
|
52
|
+
type: 'generated-index';
|
|
53
|
+
slug?: string;
|
|
54
|
+
title?: string;
|
|
55
|
+
description?: string;
|
|
56
|
+
};
|
|
57
|
+
export type SidebarItemCategoryLinkGeneratedIndex = {
|
|
58
|
+
type: 'generated-index';
|
|
59
|
+
slug: string;
|
|
60
|
+
permalink: string;
|
|
61
|
+
title?: string;
|
|
62
|
+
description?: string;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export type SidebarItemCategoryLinkConfig =
|
|
66
|
+
| SidebarItemCategoryLinkDoc
|
|
67
|
+
| SidebarItemCategoryLinkGeneratedIndexConfig;
|
|
68
|
+
|
|
69
|
+
export type SidebarItemCategoryLink =
|
|
70
|
+
| SidebarItemCategoryLinkDoc
|
|
71
|
+
| SidebarItemCategoryLinkGeneratedIndex;
|
|
72
|
+
|
|
73
|
+
// The user-given configuration in sidebars.js, before normalization
|
|
74
|
+
export type SidebarItemCategoryConfig = Expand<
|
|
75
|
+
Optional<SidebarItemCategoryBase, 'collapsed' | 'collapsible'> & {
|
|
76
|
+
items: SidebarItemConfig[];
|
|
77
|
+
link?: SidebarItemCategoryLinkConfig;
|
|
78
|
+
}
|
|
79
|
+
>;
|
|
80
|
+
|
|
81
|
+
export type SidebarCategoriesShorthand = {
|
|
82
|
+
[sidebarCategory: string]: SidebarItemConfig[];
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export type SidebarItemConfig =
|
|
86
|
+
| SidebarItemDoc
|
|
87
|
+
| SidebarItemLink
|
|
88
|
+
| SidebarItemAutogenerated
|
|
89
|
+
| SidebarItemCategoryConfig
|
|
90
|
+
| string
|
|
91
|
+
| SidebarCategoriesShorthand;
|
|
92
|
+
|
|
93
|
+
export type SidebarConfig = SidebarCategoriesShorthand | SidebarItemConfig[];
|
|
94
|
+
export type SidebarsConfig = {
|
|
95
|
+
[sidebarId: string]: SidebarConfig;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// Normalized but still has 'autogenerated', which will be handled in processing
|
|
99
|
+
export type NormalizedSidebarItemCategory = Expand<
|
|
100
|
+
SidebarItemCategoryBase & {
|
|
101
|
+
items: NormalizedSidebarItem[];
|
|
102
|
+
link?: SidebarItemCategoryLink;
|
|
103
|
+
}
|
|
104
|
+
>;
|
|
105
|
+
|
|
106
|
+
export type NormalizedSidebarItem =
|
|
107
|
+
| SidebarItemDoc
|
|
108
|
+
| SidebarItemLink
|
|
109
|
+
| NormalizedSidebarItemCategory
|
|
110
|
+
| SidebarItemAutogenerated;
|
|
111
|
+
|
|
112
|
+
export type NormalizedSidebar = NormalizedSidebarItem[];
|
|
113
|
+
export type NormalizedSidebars = {
|
|
114
|
+
[sidebarId: string]: NormalizedSidebar;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export type SidebarItemCategory = Expand<
|
|
118
|
+
SidebarItemCategoryBase & {
|
|
119
|
+
items: SidebarItem[];
|
|
120
|
+
link?: SidebarItemCategoryLink;
|
|
121
|
+
}
|
|
122
|
+
>;
|
|
123
|
+
|
|
124
|
+
export type SidebarItemCategoryWithLink = Required<SidebarItemCategory, 'link'>;
|
|
125
|
+
|
|
126
|
+
export type SidebarItemCategoryWithGeneratedIndex =
|
|
127
|
+
SidebarItemCategoryWithLink & {link: SidebarItemCategoryLinkGeneratedIndex};
|
|
128
|
+
|
|
129
|
+
export type SidebarItem =
|
|
130
|
+
| SidebarItemDoc
|
|
131
|
+
| SidebarItemLink
|
|
132
|
+
| SidebarItemCategory;
|
|
133
|
+
|
|
134
|
+
// A sidebar item that is part of the previous/next ordered navigation
|
|
135
|
+
export type SidebarNavigationItem =
|
|
136
|
+
| SidebarItemDoc
|
|
137
|
+
| SidebarItemCategoryWithLink;
|
|
138
|
+
|
|
139
|
+
export type Sidebar = SidebarItem[];
|
|
140
|
+
export type SidebarItemType = SidebarItem['type'];
|
|
141
|
+
export type Sidebars = {
|
|
142
|
+
[sidebarId: string]: Sidebar;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// Doc links have been resolved to URLs, ready to be passed to the theme
|
|
146
|
+
export type PropSidebarItemCategory = Expand<
|
|
147
|
+
SidebarItemCategoryBase & {
|
|
148
|
+
items: PropSidebarItem[];
|
|
149
|
+
href?: string;
|
|
150
|
+
}
|
|
151
|
+
>;
|
|
152
|
+
|
|
153
|
+
// we may want to use a union type in props instead of this generic link?
|
|
154
|
+
export type PropSidebarItemLink = SidebarItemLink & {
|
|
155
|
+
docId?: string;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
export type PropSidebarItem = PropSidebarItemLink | PropSidebarItemCategory;
|
|
159
|
+
export type PropSidebar = PropSidebarItem[];
|
|
160
|
+
export type PropSidebars = {
|
|
161
|
+
[sidebarId: string]: PropSidebar;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
export type PropVersionDoc = {
|
|
165
|
+
id: string;
|
|
166
|
+
title: string;
|
|
167
|
+
description?: string;
|
|
168
|
+
sidebar?: string;
|
|
169
|
+
};
|
|
170
|
+
export type PropVersionDocs = {
|
|
171
|
+
[docId: string]: PropVersionDoc;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
// Reduce API surface for options.sidebarItemsGenerator
|
|
175
|
+
// The user-provided generator fn should receive only a subset of metadata
|
|
176
|
+
// A change to any of these metadata can be considered as a breaking change
|
|
177
|
+
export type SidebarItemsGeneratorDoc = Pick<
|
|
178
|
+
DocMetadataBase,
|
|
179
|
+
| 'id'
|
|
180
|
+
| 'unversionedId'
|
|
181
|
+
| 'frontMatter'
|
|
182
|
+
| 'source'
|
|
183
|
+
| 'sourceDirName'
|
|
184
|
+
| 'sidebarPosition'
|
|
185
|
+
>;
|
|
186
|
+
export type SidebarItemsGeneratorVersion = Pick<
|
|
187
|
+
VersionMetadata,
|
|
188
|
+
'versionName' | 'contentPath'
|
|
189
|
+
>;
|
|
190
|
+
|
|
191
|
+
export type SidebarItemsGeneratorArgs = {
|
|
192
|
+
item: SidebarItemAutogenerated;
|
|
193
|
+
version: SidebarItemsGeneratorVersion;
|
|
194
|
+
docs: SidebarItemsGeneratorDoc[];
|
|
195
|
+
numberPrefixParser: NumberPrefixParser;
|
|
196
|
+
options: SidebarOptions;
|
|
197
|
+
};
|
|
198
|
+
export type SidebarItemsGenerator = (
|
|
199
|
+
generatorArgs: SidebarItemsGeneratorArgs,
|
|
200
|
+
) => // TODO TS issue: the generator can generate un-normalized items!
|
|
201
|
+
Promise<SidebarItem[]>;
|
|
202
|
+
// Promise<SidebarItemConfig[]>;
|
|
203
|
+
|
|
204
|
+
// Also inject the default generator to conveniently wrap/enhance/sort the default sidebar gen logic
|
|
205
|
+
// see https://github.com/facebook/docusaurus/issues/4640#issuecomment-822292320
|
|
206
|
+
export type SidebarItemsGeneratorOptionArgs = {
|
|
207
|
+
defaultSidebarItemsGenerator: SidebarItemsGenerator;
|
|
208
|
+
} & SidebarItemsGeneratorArgs;
|
|
209
|
+
export type SidebarItemsGeneratorOption = (
|
|
210
|
+
generatorArgs: SidebarItemsGeneratorOptionArgs,
|
|
211
|
+
) => Promise<SidebarItem[]>;
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type {
|
|
9
|
+
Sidebars,
|
|
10
|
+
Sidebar,
|
|
11
|
+
SidebarItem,
|
|
12
|
+
SidebarItemCategory,
|
|
13
|
+
SidebarItemLink,
|
|
14
|
+
SidebarItemDoc,
|
|
15
|
+
SidebarItemType,
|
|
16
|
+
SidebarCategoriesShorthand,
|
|
17
|
+
SidebarItemConfig,
|
|
18
|
+
} from './types';
|
|
19
|
+
|
|
20
|
+
import {mapValues, difference, uniq} from 'lodash';
|
|
21
|
+
import {getElementsAround, toMessageRelativeFilePath} from '@docusaurus/utils';
|
|
22
|
+
import {DocMetadataBase, DocNavLink} from '../types';
|
|
23
|
+
import {
|
|
24
|
+
SidebarItemCategoryWithGeneratedIndex,
|
|
25
|
+
SidebarItemCategoryWithLink,
|
|
26
|
+
SidebarNavigationItem,
|
|
27
|
+
} from './types';
|
|
28
|
+
|
|
29
|
+
export function isCategoriesShorthand(
|
|
30
|
+
item: SidebarItemConfig,
|
|
31
|
+
): item is SidebarCategoriesShorthand {
|
|
32
|
+
return typeof item !== 'string' && !item.type;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function transformSidebarItems(
|
|
36
|
+
sidebar: Sidebar,
|
|
37
|
+
updateFn: (item: SidebarItem) => SidebarItem,
|
|
38
|
+
): Sidebar {
|
|
39
|
+
function transformRecursive(item: SidebarItem): SidebarItem {
|
|
40
|
+
if (item.type === 'category') {
|
|
41
|
+
return updateFn({
|
|
42
|
+
...item,
|
|
43
|
+
items: item.items.map(transformRecursive),
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
return updateFn(item);
|
|
47
|
+
}
|
|
48
|
+
return sidebar.map(transformRecursive);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Flatten sidebar items into a single flat array (containing categories/docs on the same level)
|
|
52
|
+
// /!\ order matters (useful for next/prev nav), top categories appear before their child elements
|
|
53
|
+
function flattenSidebarItems(items: SidebarItem[]): SidebarItem[] {
|
|
54
|
+
function flattenRecursive(item: SidebarItem): SidebarItem[] {
|
|
55
|
+
return item.type === 'category'
|
|
56
|
+
? [item, ...item.items.flatMap(flattenRecursive)]
|
|
57
|
+
: [item];
|
|
58
|
+
}
|
|
59
|
+
return items.flatMap(flattenRecursive);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function collectSidebarItemsOfType<
|
|
63
|
+
Type extends SidebarItemType,
|
|
64
|
+
Item extends SidebarItem & {type: SidebarItemType},
|
|
65
|
+
>(type: Type, sidebar: Sidebar): Item[] {
|
|
66
|
+
return flattenSidebarItems(sidebar).filter(
|
|
67
|
+
(item) => item.type === type,
|
|
68
|
+
) as Item[];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function collectSidebarDocItems(sidebar: Sidebar): SidebarItemDoc[] {
|
|
72
|
+
return collectSidebarItemsOfType('doc', sidebar);
|
|
73
|
+
}
|
|
74
|
+
export function collectSidebarCategories(
|
|
75
|
+
sidebar: Sidebar,
|
|
76
|
+
): SidebarItemCategory[] {
|
|
77
|
+
return collectSidebarItemsOfType('category', sidebar);
|
|
78
|
+
}
|
|
79
|
+
export function collectSidebarLinks(sidebar: Sidebar): SidebarItemLink[] {
|
|
80
|
+
return collectSidebarItemsOfType('link', sidebar);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// /!\ docId order matters for navigation!
|
|
84
|
+
export function collectSidebarDocIds(sidebar: Sidebar): string[] {
|
|
85
|
+
return flattenSidebarItems(sidebar).flatMap((item) => {
|
|
86
|
+
if (item.type === 'category') {
|
|
87
|
+
return item.link?.type === 'doc' ? [item.link.id] : [];
|
|
88
|
+
}
|
|
89
|
+
if (item.type === 'doc') {
|
|
90
|
+
return [item.id];
|
|
91
|
+
}
|
|
92
|
+
return [];
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function collectSidebarNavigation(
|
|
97
|
+
sidebar: Sidebar,
|
|
98
|
+
): SidebarNavigationItem[] {
|
|
99
|
+
return flattenSidebarItems(sidebar).flatMap((item) => {
|
|
100
|
+
if (item.type === 'category' && item.link) {
|
|
101
|
+
return [item as SidebarNavigationItem];
|
|
102
|
+
}
|
|
103
|
+
if (item.type === 'doc') {
|
|
104
|
+
return [item];
|
|
105
|
+
}
|
|
106
|
+
return [];
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function collectSidebarsDocIds(
|
|
111
|
+
sidebars: Sidebars,
|
|
112
|
+
): Record<string, string[]> {
|
|
113
|
+
return mapValues(sidebars, collectSidebarDocIds);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function collectSidebarsNavigations(
|
|
117
|
+
sidebars: Sidebars,
|
|
118
|
+
): Record<string, SidebarNavigationItem[]> {
|
|
119
|
+
return mapValues(sidebars, collectSidebarNavigation);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export type SidebarNavigation = {
|
|
123
|
+
sidebarName: string | undefined;
|
|
124
|
+
previous: SidebarNavigationItem | undefined;
|
|
125
|
+
next: SidebarNavigationItem | undefined;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// A convenient and performant way to query the sidebars content
|
|
129
|
+
export type SidebarsUtils = {
|
|
130
|
+
sidebars: Sidebars;
|
|
131
|
+
getFirstDocIdOfFirstSidebar: () => string | undefined;
|
|
132
|
+
getSidebarNameByDocId: (docId: string) => string | undefined;
|
|
133
|
+
getDocNavigation: (
|
|
134
|
+
unversionedId: string,
|
|
135
|
+
versionedId: string,
|
|
136
|
+
) => SidebarNavigation;
|
|
137
|
+
getCategoryGeneratedIndexList: () => SidebarItemCategoryWithGeneratedIndex[];
|
|
138
|
+
getCategoryGeneratedIndexNavigation: (
|
|
139
|
+
categoryGeneratedIndexPermalink: string,
|
|
140
|
+
) => SidebarNavigation;
|
|
141
|
+
|
|
142
|
+
checkSidebarsDocIds: (validDocIds: string[], sidebarFilePath: string) => void;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
export function createSidebarsUtils(sidebars: Sidebars): SidebarsUtils {
|
|
146
|
+
const sidebarNameToDocIds = collectSidebarsDocIds(sidebars);
|
|
147
|
+
const sidebarNameToNavigationItems = collectSidebarsNavigations(sidebars);
|
|
148
|
+
|
|
149
|
+
// Reverse mapping
|
|
150
|
+
const docIdToSidebarName = Object.fromEntries(
|
|
151
|
+
Object.entries(sidebarNameToDocIds).flatMap(([sidebarName, docIds]) =>
|
|
152
|
+
docIds.map((docId) => [docId, sidebarName]),
|
|
153
|
+
),
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
function getFirstDocIdOfFirstSidebar(): string | undefined {
|
|
157
|
+
return Object.values(sidebarNameToDocIds)[0]?.[0];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function getSidebarNameByDocId(docId: string): string | undefined {
|
|
161
|
+
return docIdToSidebarName[docId];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function emptySidebarNavigation(): SidebarNavigation {
|
|
165
|
+
return {
|
|
166
|
+
sidebarName: undefined,
|
|
167
|
+
previous: undefined,
|
|
168
|
+
next: undefined,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function getDocNavigation(
|
|
173
|
+
unversionedId: string,
|
|
174
|
+
versionedId: string,
|
|
175
|
+
): SidebarNavigation {
|
|
176
|
+
// TODO legacy id retro-compatibility!
|
|
177
|
+
let docId = unversionedId;
|
|
178
|
+
let sidebarName = getSidebarNameByDocId(docId);
|
|
179
|
+
if (!sidebarName) {
|
|
180
|
+
docId = versionedId;
|
|
181
|
+
sidebarName = getSidebarNameByDocId(docId);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (sidebarName) {
|
|
185
|
+
const navigationItems = sidebarNameToNavigationItems[sidebarName];
|
|
186
|
+
const currentItemIndex = navigationItems.findIndex((item) => {
|
|
187
|
+
if (item.type === 'doc') {
|
|
188
|
+
return item.id === docId;
|
|
189
|
+
}
|
|
190
|
+
if (item.type === 'category' && item.link.type === 'doc') {
|
|
191
|
+
return item.link.id === docId;
|
|
192
|
+
}
|
|
193
|
+
return false;
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
const {previous, next} = getElementsAround(
|
|
197
|
+
navigationItems,
|
|
198
|
+
currentItemIndex,
|
|
199
|
+
);
|
|
200
|
+
return {sidebarName, previous, next};
|
|
201
|
+
} else {
|
|
202
|
+
return emptySidebarNavigation();
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function getCategoryGeneratedIndexList(): SidebarItemCategoryWithGeneratedIndex[] {
|
|
207
|
+
return Object.values(sidebarNameToNavigationItems)
|
|
208
|
+
.flat()
|
|
209
|
+
.flatMap((item) => {
|
|
210
|
+
if (item.type === 'category' && item.link.type === 'generated-index') {
|
|
211
|
+
return [item as SidebarItemCategoryWithGeneratedIndex];
|
|
212
|
+
}
|
|
213
|
+
return [];
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// We identity the category generated index by its permalink (should be unique)
|
|
218
|
+
// More reliable than using object identity
|
|
219
|
+
function getCategoryGeneratedIndexNavigation(
|
|
220
|
+
categoryGeneratedIndexPermalink: string,
|
|
221
|
+
): SidebarNavigation {
|
|
222
|
+
function isCurrentCategoryGeneratedIndexItem(
|
|
223
|
+
item: SidebarNavigationItem,
|
|
224
|
+
): boolean {
|
|
225
|
+
return (
|
|
226
|
+
item.type === 'category' &&
|
|
227
|
+
item.link?.type === 'generated-index' &&
|
|
228
|
+
item.link.permalink === categoryGeneratedIndexPermalink
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const sidebarName = Object.entries(sidebarNameToNavigationItems).find(
|
|
233
|
+
([, navigationItems]) =>
|
|
234
|
+
navigationItems.find(isCurrentCategoryGeneratedIndexItem),
|
|
235
|
+
)?.[0];
|
|
236
|
+
|
|
237
|
+
if (sidebarName) {
|
|
238
|
+
const navigationItems = sidebarNameToNavigationItems[sidebarName];
|
|
239
|
+
const currentItemIndex = navigationItems.findIndex(
|
|
240
|
+
isCurrentCategoryGeneratedIndexItem,
|
|
241
|
+
);
|
|
242
|
+
const {previous, next} = getElementsAround(
|
|
243
|
+
navigationItems,
|
|
244
|
+
currentItemIndex,
|
|
245
|
+
);
|
|
246
|
+
return {sidebarName, previous, next};
|
|
247
|
+
} else {
|
|
248
|
+
return emptySidebarNavigation();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function checkSidebarsDocIds(validDocIds: string[], sidebarFilePath: string) {
|
|
253
|
+
const allSidebarDocIds = Object.values(sidebarNameToDocIds).flat();
|
|
254
|
+
const invalidSidebarDocIds = difference(allSidebarDocIds, validDocIds);
|
|
255
|
+
if (invalidSidebarDocIds.length > 0) {
|
|
256
|
+
throw new Error(
|
|
257
|
+
`Invalid sidebar file at "${toMessageRelativeFilePath(
|
|
258
|
+
sidebarFilePath,
|
|
259
|
+
)}".
|
|
260
|
+
These sidebar document ids do not exist:
|
|
261
|
+
- ${invalidSidebarDocIds.sort().join('\n- ')}
|
|
262
|
+
|
|
263
|
+
Available document ids are:
|
|
264
|
+
- ${uniq(validDocIds).sort().join('\n- ')}`,
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return {
|
|
270
|
+
sidebars,
|
|
271
|
+
getFirstDocIdOfFirstSidebar,
|
|
272
|
+
getSidebarNameByDocId,
|
|
273
|
+
getDocNavigation,
|
|
274
|
+
getCategoryGeneratedIndexList,
|
|
275
|
+
getCategoryGeneratedIndexNavigation,
|
|
276
|
+
checkSidebarsDocIds,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export function toDocNavigationLink(doc: DocMetadataBase): DocNavLink {
|
|
281
|
+
const {
|
|
282
|
+
title,
|
|
283
|
+
permalink,
|
|
284
|
+
frontMatter: {
|
|
285
|
+
pagination_label: paginationLabel,
|
|
286
|
+
sidebar_label: sidebarLabel,
|
|
287
|
+
},
|
|
288
|
+
} = doc;
|
|
289
|
+
return {title: paginationLabel ?? sidebarLabel ?? title, permalink};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export function toNavigationLink(
|
|
293
|
+
navigationItem: SidebarNavigationItem | undefined,
|
|
294
|
+
docsById: Record<string, DocMetadataBase>,
|
|
295
|
+
): DocNavLink | undefined {
|
|
296
|
+
function getDocById(docId: string) {
|
|
297
|
+
const doc = docsById[docId];
|
|
298
|
+
if (!doc) {
|
|
299
|
+
throw new Error(
|
|
300
|
+
`Can't create navigation link: no doc found with id=${docId}`,
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
return doc;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function handleCategory(category: SidebarItemCategoryWithLink): DocNavLink {
|
|
307
|
+
if (category.link.type === 'doc') {
|
|
308
|
+
return toDocNavigationLink(getDocById(category.link.id));
|
|
309
|
+
} else if (category.link.type === 'generated-index') {
|
|
310
|
+
return {
|
|
311
|
+
title: category.label,
|
|
312
|
+
permalink: category.link.permalink,
|
|
313
|
+
};
|
|
314
|
+
} else {
|
|
315
|
+
throw new Error('unexpected category link type');
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
if (!navigationItem) {
|
|
319
|
+
return undefined;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (navigationItem.type === 'doc') {
|
|
323
|
+
return toDocNavigationLink(getDocById(navigationItem.id));
|
|
324
|
+
} else if (navigationItem.type === 'category') {
|
|
325
|
+
return handleCategory(navigationItem);
|
|
326
|
+
} else {
|
|
327
|
+
throw new Error('unexpected navigation item');
|
|
328
|
+
}
|
|
329
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {Joi, URISchema} from '@docusaurus/utils-validation';
|
|
9
|
+
import type {
|
|
10
|
+
SidebarItemConfig,
|
|
11
|
+
SidebarCategoriesShorthand,
|
|
12
|
+
SidebarItemBase,
|
|
13
|
+
SidebarItemAutogenerated,
|
|
14
|
+
SidebarItemDoc,
|
|
15
|
+
SidebarItemLink,
|
|
16
|
+
SidebarItemCategoryConfig,
|
|
17
|
+
SidebarItemCategoryLink,
|
|
18
|
+
SidebarsConfig,
|
|
19
|
+
SidebarItemCategoryLinkDoc,
|
|
20
|
+
SidebarItemCategoryLinkGeneratedIndex,
|
|
21
|
+
} from './types';
|
|
22
|
+
import {isCategoriesShorthand} from './utils';
|
|
23
|
+
import {CategoryMetadataFile} from './generator';
|
|
24
|
+
|
|
25
|
+
const sidebarItemBaseSchema = Joi.object<SidebarItemBase>({
|
|
26
|
+
className: Joi.string(),
|
|
27
|
+
customProps: Joi.object().unknown(),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const sidebarItemAutogeneratedSchema =
|
|
31
|
+
sidebarItemBaseSchema.append<SidebarItemAutogenerated>({
|
|
32
|
+
type: 'autogenerated',
|
|
33
|
+
dirName: Joi.string()
|
|
34
|
+
.required()
|
|
35
|
+
.pattern(/^[^/](.*[^/])?$/)
|
|
36
|
+
.message(
|
|
37
|
+
'"dirName" must be a dir path relative to the docs folder root, and should not start or end with slash',
|
|
38
|
+
),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const sidebarItemDocSchema = sidebarItemBaseSchema.append<SidebarItemDoc>({
|
|
42
|
+
type: Joi.string().valid('doc', 'ref').required(),
|
|
43
|
+
id: Joi.string().required(),
|
|
44
|
+
label: Joi.string(),
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const sidebarItemLinkSchema = sidebarItemBaseSchema.append<SidebarItemLink>({
|
|
48
|
+
type: 'link',
|
|
49
|
+
href: URISchema.required(),
|
|
50
|
+
label: Joi.string()
|
|
51
|
+
.required()
|
|
52
|
+
.messages({'any.unknown': '"label" must be a string'}),
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const sidebarItemCategoryLinkSchema = Joi.object<SidebarItemCategoryLink>()
|
|
56
|
+
.when('.type', {
|
|
57
|
+
switch: [
|
|
58
|
+
{
|
|
59
|
+
is: 'doc',
|
|
60
|
+
then: Joi.object<SidebarItemCategoryLinkDoc>({
|
|
61
|
+
type: 'doc',
|
|
62
|
+
id: Joi.string().required(),
|
|
63
|
+
}),
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
is: 'generated-index',
|
|
67
|
+
then: Joi.object<SidebarItemCategoryLinkGeneratedIndex>({
|
|
68
|
+
type: 'generated-index',
|
|
69
|
+
slug: Joi.string().optional(),
|
|
70
|
+
// permalink: Joi.string().optional(), // No, this one is not in the user config, only in the normalized version
|
|
71
|
+
title: Joi.string().optional(),
|
|
72
|
+
description: Joi.string().optional(),
|
|
73
|
+
}),
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
is: Joi.string().required(),
|
|
77
|
+
then: Joi.forbidden().messages({
|
|
78
|
+
'any.unknown': 'Unknown sidebar category link type "{.type}".',
|
|
79
|
+
}),
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
})
|
|
83
|
+
.id('sidebarCategoryLinkSchema');
|
|
84
|
+
|
|
85
|
+
const sidebarItemCategorySchema =
|
|
86
|
+
sidebarItemBaseSchema.append<SidebarItemCategoryConfig>({
|
|
87
|
+
type: 'category',
|
|
88
|
+
label: Joi.string()
|
|
89
|
+
.required()
|
|
90
|
+
.messages({'any.unknown': '"label" must be a string'}),
|
|
91
|
+
// TODO: Joi doesn't allow mutual recursion. See https://github.com/sideway/joi/issues/2611
|
|
92
|
+
items: Joi.array()
|
|
93
|
+
.required()
|
|
94
|
+
.messages({'any.unknown': '"items" must be an array'}), // .items(Joi.link('#sidebarItemSchema')),
|
|
95
|
+
link: sidebarItemCategoryLinkSchema,
|
|
96
|
+
collapsed: Joi.boolean().messages({
|
|
97
|
+
'any.unknown': '"collapsed" must be a boolean',
|
|
98
|
+
}),
|
|
99
|
+
collapsible: Joi.boolean().messages({
|
|
100
|
+
'any.unknown': '"collapsible" must be a boolean',
|
|
101
|
+
}),
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const sidebarItemSchema: Joi.Schema<SidebarItemConfig> = Joi.object()
|
|
105
|
+
.when('.type', {
|
|
106
|
+
switch: [
|
|
107
|
+
{is: 'link', then: sidebarItemLinkSchema},
|
|
108
|
+
{
|
|
109
|
+
is: Joi.string().valid('doc', 'ref').required(),
|
|
110
|
+
then: sidebarItemDocSchema,
|
|
111
|
+
},
|
|
112
|
+
{is: 'autogenerated', then: sidebarItemAutogeneratedSchema},
|
|
113
|
+
{is: 'category', then: sidebarItemCategorySchema},
|
|
114
|
+
{
|
|
115
|
+
is: Joi.any().required(),
|
|
116
|
+
then: Joi.forbidden().messages({
|
|
117
|
+
'any.unknown': 'Unknown sidebar item type "{.type}".',
|
|
118
|
+
}),
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
})
|
|
122
|
+
.id('sidebarItemSchema');
|
|
123
|
+
|
|
124
|
+
function validateSidebarItem(item: unknown): asserts item is SidebarItemConfig {
|
|
125
|
+
if (typeof item === 'string') {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
// TODO: remove once with proper Joi support
|
|
129
|
+
// Because we can't use Joi to validate nested items (see above), we do it manually
|
|
130
|
+
if (isCategoriesShorthand(item as SidebarItemConfig)) {
|
|
131
|
+
Object.values(item as SidebarCategoriesShorthand).forEach((category) =>
|
|
132
|
+
category.forEach(validateSidebarItem),
|
|
133
|
+
);
|
|
134
|
+
} else {
|
|
135
|
+
Joi.assert(item, sidebarItemSchema);
|
|
136
|
+
|
|
137
|
+
if ((item as SidebarItemCategoryConfig).type === 'category') {
|
|
138
|
+
(item as SidebarItemCategoryConfig).items.forEach(validateSidebarItem);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function validateSidebars(
|
|
144
|
+
sidebars: unknown,
|
|
145
|
+
): asserts sidebars is SidebarsConfig {
|
|
146
|
+
Object.values(sidebars as SidebarsConfig).forEach((sidebar) => {
|
|
147
|
+
if (Array.isArray(sidebar)) {
|
|
148
|
+
sidebar.forEach(validateSidebarItem);
|
|
149
|
+
} else {
|
|
150
|
+
validateSidebarItem(sidebar);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const categoryMetadataFileSchema = Joi.object<CategoryMetadataFile>({
|
|
156
|
+
label: Joi.string(),
|
|
157
|
+
position: Joi.number(),
|
|
158
|
+
collapsed: Joi.boolean(),
|
|
159
|
+
collapsible: Joi.boolean(),
|
|
160
|
+
className: Joi.string(),
|
|
161
|
+
link: sidebarItemCategoryLinkSchema,
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
export function validateCategoryMetadataFile(
|
|
165
|
+
unsafeContent: unknown,
|
|
166
|
+
): CategoryMetadataFile {
|
|
167
|
+
return Joi.attempt(unsafeContent, categoryMetadataFileSchema);
|
|
168
|
+
}
|