@docusaurus/plugin-content-docs 2.0.0-beta.12faed89d → 2.0.0-beta.13
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/.tsbuildinfo +1 -1
- package/lib/categoryGeneratedIndex.d.ts +12 -0
- package/lib/categoryGeneratedIndex.js +37 -0
- package/lib/cli.d.ts +2 -2
- package/lib/cli.js +12 -34
- 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 +125 -41
- package/lib/globalData.d.ts +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +100 -131
- package/lib/lastUpdate.js +8 -9
- 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/lib/sidebars/types.js +8 -0
- 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/lib/tags.d.ts +8 -0
- 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/__tests__/__fixtures__/simple-site/docs/_partials/somePartial.md +3 -0
- package/src/__tests__/__fixtures__/simple-site/docs/_partials/subfolder/somePartial.md +3 -0
- package/src/__tests__/__fixtures__/simple-site/docs/_somePartial.md +3 -0
- package/src/__tests__/__fixtures__/simple-site/docs/foo/baz.md +5 -0
- package/src/__tests__/__fixtures__/simple-site/docs/hello.md +2 -0
- package/src/__tests__/__fixtures__/simple-site/docs/rootAbsoluteSlug.md +2 -0
- package/src/__tests__/__fixtures__/simple-site/docs/rootRelativeSlug.md +2 -0
- package/src/__tests__/__fixtures__/simple-site/docs/rootResolvedSlug.md +2 -0
- package/src/__tests__/__fixtures__/simple-site/docs/rootTryToEscapeSlug.md +2 -0
- package/src/__tests__/__fixtures__/simple-site/sidebars.json +15 -1
- package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-1.md +1 -0
- package/src/__tests__/__fixtures__/versioned-site/docs/foo/bar.md +6 -0
- package/src/__tests__/__fixtures__/versioned-site/docs/hello.md +3 -0
- package/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md +3 -0
- package/src/__tests__/__fixtures__/versioned-site/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md +3 -0
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/hello.md +3 -0
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_partials/somePartial.md +3 -0
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_partials/subfolder/somePartial.md +3 -0
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_somePartial.md +3 -0
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/hello.md +3 -0
- package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.1-sidebars.json +2 -2
- package/src/__tests__/__snapshots__/cli.test.ts.snap +48 -73
- package/src/__tests__/__snapshots__/docs.test.ts.snap +140 -0
- package/src/__tests__/__snapshots__/index.test.ts.snap +745 -97
- package/src/__tests__/__snapshots__/translations.test.ts.snap +45 -15
- package/src/__tests__/cli.test.ts +15 -11
- package/src/__tests__/docFrontMatter.test.ts +160 -45
- package/src/__tests__/docs.test.ts +311 -150
- package/src/__tests__/index.test.ts +108 -66
- package/src/__tests__/lastUpdate.test.ts +1 -1
- package/src/__tests__/options.test.ts +48 -3
- package/src/__tests__/props.test.ts +62 -0
- package/src/__tests__/slug.test.ts +127 -20
- package/src/__tests__/translations.test.ts +7 -1
- package/src/__tests__/versions.test.ts +73 -70
- package/src/categoryGeneratedIndex.ts +57 -0
- package/src/cli.ts +8 -41
- 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 +158 -32
- package/src/globalData.ts +6 -1
- package/src/index.ts +125 -169
- package/src/lastUpdate.ts +10 -13
- package/src/markdown/index.ts +8 -12
- package/src/numberPrefix.ts +5 -3
- package/src/options.ts +59 -14
- package/src/plugin-content-docs.d.ts +173 -40
- package/src/props.ts +90 -15
- package/src/routes.ts +173 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-shorthand.js +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-wrong-items.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-wrong-label.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category.js +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-collapsed-first-level.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-collapsed.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-doc-id-not-string.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-first-level-not-category.js +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link-wrong-href.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link-wrong-label.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-unknown-type.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-wrong-field.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars.json +0 -0
- package/src/{__tests__/__snapshots__/sidebars.test.ts.snap → sidebars/__tests__/__snapshots__/index.test.ts.snap} +36 -6
- package/src/{__tests__/sidebarItemsGenerator.test.ts → sidebars/__tests__/generator.test.ts} +143 -18
- package/src/sidebars/__tests__/index.test.ts +204 -0
- package/src/sidebars/__tests__/processor.test.ts +237 -0
- package/src/sidebars/__tests__/utils.test.ts +695 -0
- package/src/sidebars/__tests__/validation.test.ts +105 -0
- package/src/sidebars/generator.ts +310 -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/sidebarItemsGenerator.js +0 -211
- package/lib/sidebars.d.ts +0 -43
- package/lib/sidebars.js +0 -320
- package/src/__tests__/sidebars.test.ts +0 -639
- package/src/sidebarItemsGenerator.ts +0 -307
- package/src/sidebars.ts +0 -522
|
@@ -0,0 +1,154 @@
|
|
|
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
|
+
NumberPrefixParser,
|
|
10
|
+
DocMetadataBase,
|
|
11
|
+
VersionMetadata,
|
|
12
|
+
SidebarOptions,
|
|
13
|
+
} from '../types';
|
|
14
|
+
import type {
|
|
15
|
+
Sidebars,
|
|
16
|
+
Sidebar,
|
|
17
|
+
SidebarItem,
|
|
18
|
+
NormalizedSidebarItem,
|
|
19
|
+
NormalizedSidebar,
|
|
20
|
+
NormalizedSidebars,
|
|
21
|
+
SidebarItemsGeneratorOption,
|
|
22
|
+
SidebarItemsGeneratorDoc,
|
|
23
|
+
SidebarItemsGeneratorVersion,
|
|
24
|
+
NormalizedSidebarItemCategory,
|
|
25
|
+
SidebarItemCategory,
|
|
26
|
+
SidebarItemAutogenerated,
|
|
27
|
+
} from './types';
|
|
28
|
+
import {transformSidebarItems} from './utils';
|
|
29
|
+
import {DefaultSidebarItemsGenerator} from './generator';
|
|
30
|
+
import {mapValues, memoize, pick} from 'lodash';
|
|
31
|
+
import combinePromises from 'combine-promises';
|
|
32
|
+
import {normalizeItem} from './normalization';
|
|
33
|
+
import {Slugger} from '@docusaurus/utils';
|
|
34
|
+
|
|
35
|
+
export type SidebarProcessorParams = {
|
|
36
|
+
sidebarItemsGenerator: SidebarItemsGeneratorOption;
|
|
37
|
+
numberPrefixParser: NumberPrefixParser;
|
|
38
|
+
docs: DocMetadataBase[];
|
|
39
|
+
version: VersionMetadata;
|
|
40
|
+
categoryLabelSlugger: Slugger;
|
|
41
|
+
sidebarOptions: SidebarOptions;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
function toSidebarItemsGeneratorDoc(
|
|
45
|
+
doc: DocMetadataBase,
|
|
46
|
+
): SidebarItemsGeneratorDoc {
|
|
47
|
+
return pick(doc, [
|
|
48
|
+
'id',
|
|
49
|
+
'unversionedId',
|
|
50
|
+
'frontMatter',
|
|
51
|
+
'source',
|
|
52
|
+
'sourceDirName',
|
|
53
|
+
'sidebarPosition',
|
|
54
|
+
]);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function toSidebarItemsGeneratorVersion(
|
|
58
|
+
version: VersionMetadata,
|
|
59
|
+
): SidebarItemsGeneratorVersion {
|
|
60
|
+
return pick(version, ['versionName', 'contentPath']);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Handle the generation of autogenerated sidebar items and other post-processing checks
|
|
64
|
+
async function processSidebar(
|
|
65
|
+
unprocessedSidebar: NormalizedSidebar,
|
|
66
|
+
params: SidebarProcessorParams,
|
|
67
|
+
): Promise<Sidebar> {
|
|
68
|
+
const {
|
|
69
|
+
sidebarItemsGenerator,
|
|
70
|
+
numberPrefixParser,
|
|
71
|
+
docs,
|
|
72
|
+
version,
|
|
73
|
+
sidebarOptions,
|
|
74
|
+
} = params;
|
|
75
|
+
|
|
76
|
+
// Just a minor lazy transformation optimization
|
|
77
|
+
const getSidebarItemsGeneratorDocsAndVersion = memoize(() => ({
|
|
78
|
+
docs: docs.map(toSidebarItemsGeneratorDoc),
|
|
79
|
+
version: toSidebarItemsGeneratorVersion(version),
|
|
80
|
+
}));
|
|
81
|
+
|
|
82
|
+
async function processCategoryItem(
|
|
83
|
+
item: NormalizedSidebarItemCategory,
|
|
84
|
+
): Promise<SidebarItemCategory> {
|
|
85
|
+
return {
|
|
86
|
+
...item,
|
|
87
|
+
items: (await Promise.all(item.items.map(processItem))).flat(),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function processAutoGeneratedItem(
|
|
92
|
+
item: SidebarItemAutogenerated,
|
|
93
|
+
): Promise<SidebarItem[]> {
|
|
94
|
+
// TODO the returned type can't be trusted in practice (generator can be user-provided)
|
|
95
|
+
const generatedItems = await sidebarItemsGenerator({
|
|
96
|
+
item,
|
|
97
|
+
numberPrefixParser,
|
|
98
|
+
defaultSidebarItemsGenerator: DefaultSidebarItemsGenerator,
|
|
99
|
+
...getSidebarItemsGeneratorDocsAndVersion(),
|
|
100
|
+
options: sidebarOptions,
|
|
101
|
+
});
|
|
102
|
+
// TODO validate generated items: user can generate bad items
|
|
103
|
+
|
|
104
|
+
const generatedItemsNormalized = generatedItems.flatMap((generatedItem) =>
|
|
105
|
+
normalizeItem(generatedItem, {...params, ...sidebarOptions}),
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
// Process again... weird but sidebar item generated might generate some auto-generated items?
|
|
109
|
+
return processItems(generatedItemsNormalized);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async function processItem(
|
|
113
|
+
item: NormalizedSidebarItem,
|
|
114
|
+
): Promise<SidebarItem[]> {
|
|
115
|
+
if (item.type === 'category') {
|
|
116
|
+
return [await processCategoryItem(item)];
|
|
117
|
+
}
|
|
118
|
+
if (item.type === 'autogenerated') {
|
|
119
|
+
return processAutoGeneratedItem(item);
|
|
120
|
+
}
|
|
121
|
+
return [item];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async function processItems(
|
|
125
|
+
items: NormalizedSidebarItem[],
|
|
126
|
+
): Promise<SidebarItem[]> {
|
|
127
|
+
return (await Promise.all(items.map(processItem))).flat();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const processedSidebar = await processItems(unprocessedSidebar);
|
|
131
|
+
|
|
132
|
+
const fixSidebarItemInconsistencies = (item: SidebarItem): SidebarItem => {
|
|
133
|
+
// A non-collapsible category can't be collapsed!
|
|
134
|
+
if (item.type === 'category' && !item.collapsible && item.collapsed) {
|
|
135
|
+
return {
|
|
136
|
+
...item,
|
|
137
|
+
collapsed: false,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
return item;
|
|
141
|
+
};
|
|
142
|
+
return transformSidebarItems(processedSidebar, fixSidebarItemInconsistencies);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export async function processSidebars(
|
|
146
|
+
unprocessedSidebars: NormalizedSidebars,
|
|
147
|
+
params: SidebarProcessorParams,
|
|
148
|
+
): Promise<Sidebars> {
|
|
149
|
+
return combinePromises(
|
|
150
|
+
mapValues(unprocessedSidebars, (unprocessedSidebar) =>
|
|
151
|
+
processSidebar(unprocessedSidebar, params),
|
|
152
|
+
),
|
|
153
|
+
);
|
|
154
|
+
}
|
|
@@ -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
|
+
}
|