@docusaurus/plugin-content-docs 2.0.0-beta.0e652730d → 2.0.0-beta.10
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 +1 -4
- package/lib/client/docsClientUtils.js +21 -31
- package/lib/docFrontMatter.d.ts +1 -1
- package/lib/docFrontMatter.js +10 -6
- package/lib/docs.d.ts +25 -3
- package/lib/docs.js +125 -38
- package/lib/globalData.d.ts +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +104 -138
- package/lib/lastUpdate.js +9 -10
- 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 +49 -17
- package/lib/props.d.ts +7 -2
- package/lib/props.js +61 -9
- 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 +24 -21
- package/lib/translations.d.ts +2 -2
- package/lib/translations.js +71 -29
- package/lib/types.d.ts +52 -62
- package/lib/versions.d.ts +3 -3
- package/lib/versions.js +76 -24
- package/package.json +22 -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 +753 -112
- package/src/__tests__/__snapshots__/translations.test.ts.snap +45 -18
- package/src/__tests__/cli.test.ts +15 -11
- package/src/__tests__/docFrontMatter.test.ts +195 -40
- package/src/__tests__/docs.test.ts +311 -150
- package/src/__tests__/index.test.ts +112 -69
- package/src/__tests__/lastUpdate.test.ts +3 -2
- package/src/__tests__/options.test.ts +48 -4
- package/src/__tests__/props.test.ts +62 -0
- package/src/__tests__/slug.test.ts +127 -20
- package/src/__tests__/translations.test.ts +7 -2
- package/src/__tests__/versions.test.ts +93 -67
- package/src/categoryGeneratedIndex.ts +57 -0
- package/src/cli.ts +8 -41
- package/src/client/__tests__/docsClientUtils.test.ts +4 -5
- package/src/client/docsClientUtils.ts +19 -41
- package/{types.d.ts → src/deps.d.ts} +0 -0
- package/src/docFrontMatter.ts +13 -7
- package/src/docs.ts +158 -29
- package/src/globalData.ts +6 -1
- package/src/index.ts +134 -179
- package/src/lastUpdate.ts +10 -9
- package/src/markdown/index.ts +8 -12
- package/src/numberPrefix.ts +5 -3
- package/src/options.ts +59 -28
- package/src/plugin-content-docs.d.ts +179 -35
- package/src/props.ts +91 -16
- 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/theme/hooks/useDocs.ts +5 -1
- package/src/translations.ts +103 -47
- package/src/types.ts +67 -105
- package/src/versions.ts +117 -21
- package/lib/sidebarItemsGenerator.js +0 -211
- package/lib/sidebars.d.ts +0 -43
- package/lib/sidebars.js +0 -319
- package/src/__tests__/sidebars.test.ts +0 -639
- package/src/sidebarItemsGenerator.ts +0 -307
- package/src/sidebars.ts +0 -506
package/src/docFrontMatter.ts
CHANGED
|
@@ -5,16 +5,17 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
/* eslint-disable camelcase */
|
|
9
|
-
|
|
10
8
|
import {
|
|
11
9
|
JoiFrontMatter as Joi, // Custom instance for frontmatter
|
|
10
|
+
URISchema,
|
|
11
|
+
FrontMatterTagsSchema,
|
|
12
|
+
FrontMatterTOCHeadingLevels,
|
|
12
13
|
validateFrontMatter,
|
|
13
14
|
} from '@docusaurus/utils-validation';
|
|
14
|
-
import {DocFrontMatter} from './types';
|
|
15
|
+
import type {DocFrontMatter} from './types';
|
|
15
16
|
|
|
16
17
|
// NOTE: we don't add any default value on purpose here
|
|
17
|
-
// We don't want default values to magically appear in doc
|
|
18
|
+
// We don't want default values to magically appear in doc metadata and props
|
|
18
19
|
// While the user did not provide those values explicitly
|
|
19
20
|
// We use default values in code instead
|
|
20
21
|
const DocFrontMatterSchema = Joi.object<DocFrontMatter>({
|
|
@@ -23,14 +24,19 @@ const DocFrontMatterSchema = Joi.object<DocFrontMatter>({
|
|
|
23
24
|
hide_title: Joi.boolean(),
|
|
24
25
|
hide_table_of_contents: Joi.boolean(),
|
|
25
26
|
keywords: Joi.array().items(Joi.string().required()),
|
|
26
|
-
image:
|
|
27
|
+
image: URISchema,
|
|
27
28
|
description: Joi.string().allow(''), // see https://github.com/facebook/docusaurus/issues/4591#issuecomment-822372398
|
|
28
29
|
slug: Joi.string(),
|
|
29
30
|
sidebar_label: Joi.string(),
|
|
30
|
-
sidebar_position: Joi.number()
|
|
31
|
+
sidebar_position: Joi.number(),
|
|
32
|
+
sidebar_class_name: Joi.string(),
|
|
33
|
+
tags: FrontMatterTagsSchema,
|
|
31
34
|
pagination_label: Joi.string(),
|
|
32
|
-
custom_edit_url:
|
|
35
|
+
custom_edit_url: URISchema.allow('', null),
|
|
33
36
|
parse_number_prefixes: Joi.boolean(),
|
|
37
|
+
pagination_next: Joi.string().allow(null),
|
|
38
|
+
pagination_prev: Joi.string().allow(null),
|
|
39
|
+
...FrontMatterTOCHeadingLevels,
|
|
34
40
|
}).unknown();
|
|
35
41
|
|
|
36
42
|
export function validateDocFrontMatter(
|
package/src/docs.ts
CHANGED
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
|
|
8
8
|
import path from 'path';
|
|
9
9
|
import fs from 'fs-extra';
|
|
10
|
+
import chalk from 'chalk';
|
|
11
|
+
import {keyBy, last} from 'lodash';
|
|
10
12
|
import {
|
|
11
13
|
aliasedSitePath,
|
|
12
14
|
getEditUrl,
|
|
@@ -14,25 +16,33 @@ import {
|
|
|
14
16
|
normalizeUrl,
|
|
15
17
|
parseMarkdownString,
|
|
16
18
|
posixPath,
|
|
19
|
+
Globby,
|
|
20
|
+
normalizeFrontMatterTags,
|
|
17
21
|
} from '@docusaurus/utils';
|
|
18
|
-
import {LoadContext} from '@docusaurus/types';
|
|
22
|
+
import type {LoadContext} from '@docusaurus/types';
|
|
19
23
|
|
|
20
24
|
import {getFileLastUpdate} from './lastUpdate';
|
|
21
25
|
import {
|
|
22
26
|
DocFile,
|
|
23
27
|
DocMetadataBase,
|
|
28
|
+
DocMetadata,
|
|
29
|
+
DocNavLink,
|
|
24
30
|
LastUpdateData,
|
|
25
31
|
MetadataOptions,
|
|
26
32
|
PluginOptions,
|
|
27
33
|
VersionMetadata,
|
|
34
|
+
LoadedVersion,
|
|
28
35
|
} from './types';
|
|
29
36
|
import getSlug from './slug';
|
|
30
37
|
import {CURRENT_VERSION_NAME} from './constants';
|
|
31
|
-
import globby from 'globby';
|
|
32
38
|
import {getDocsDirPaths} from './versions';
|
|
33
39
|
import {stripPathNumberPrefixes} from './numberPrefix';
|
|
34
40
|
import {validateDocFrontMatter} from './docFrontMatter';
|
|
35
|
-
import
|
|
41
|
+
import {
|
|
42
|
+
SidebarsUtils,
|
|
43
|
+
toDocNavigationLink,
|
|
44
|
+
toNavigationLink,
|
|
45
|
+
} from './sidebars/utils';
|
|
36
46
|
|
|
37
47
|
type LastUpdateOptions = Pick<
|
|
38
48
|
PluginOptions,
|
|
@@ -92,11 +102,12 @@ export async function readVersionDocs(
|
|
|
92
102
|
versionMetadata: VersionMetadata,
|
|
93
103
|
options: Pick<
|
|
94
104
|
PluginOptions,
|
|
95
|
-
'include' | 'showLastUpdateAuthor' | 'showLastUpdateTime'
|
|
105
|
+
'include' | 'exclude' | 'showLastUpdateAuthor' | 'showLastUpdateTime'
|
|
96
106
|
>,
|
|
97
107
|
): Promise<DocFile[]> {
|
|
98
|
-
const sources = await
|
|
108
|
+
const sources = await Globby(options.include, {
|
|
99
109
|
cwd: versionMetadata.contentPath,
|
|
110
|
+
ignore: options.exclude,
|
|
100
111
|
});
|
|
101
112
|
return Promise.all(
|
|
102
113
|
sources.map((source) => readDocFile(versionMetadata, source, options)),
|
|
@@ -115,7 +126,6 @@ function doProcessDocMetadata({
|
|
|
115
126
|
options: MetadataOptions;
|
|
116
127
|
}): DocMetadataBase {
|
|
117
128
|
const {source, content, lastUpdate, contentPath, filePath} = docFile;
|
|
118
|
-
const {homePageId} = options;
|
|
119
129
|
const {siteDir, i18n} = context;
|
|
120
130
|
|
|
121
131
|
const {
|
|
@@ -129,8 +139,8 @@ function doProcessDocMetadata({
|
|
|
129
139
|
custom_edit_url: customEditURL,
|
|
130
140
|
|
|
131
141
|
// Strip number prefixes by default (01-MyFolder/01-MyDoc.md => MyFolder/MyDoc) by default,
|
|
132
|
-
// but allow to disable this behavior with
|
|
133
|
-
parse_number_prefixes = true,
|
|
142
|
+
// but allow to disable this behavior with frontmatter
|
|
143
|
+
parse_number_prefixes: parseNumberPrefixes = true,
|
|
134
144
|
} = frontMatter;
|
|
135
145
|
|
|
136
146
|
// ex: api/plugins/myDoc -> myDoc
|
|
@@ -144,7 +154,7 @@ function doProcessDocMetadata({
|
|
|
144
154
|
// ex: myDoc -> .
|
|
145
155
|
const sourceDirName = path.dirname(source);
|
|
146
156
|
|
|
147
|
-
const {filename: unprefixedFileName, numberPrefix} =
|
|
157
|
+
const {filename: unprefixedFileName, numberPrefix} = parseNumberPrefixes
|
|
148
158
|
? options.numberPrefixParser(sourceFileNameWithoutExtension)
|
|
149
159
|
: {filename: sourceFileNameWithoutExtension, numberPrefix: undefined};
|
|
150
160
|
|
|
@@ -172,7 +182,7 @@ function doProcessDocMetadata({
|
|
|
172
182
|
return undefined;
|
|
173
183
|
}
|
|
174
184
|
// Eventually remove the number prefixes from intermediate directories
|
|
175
|
-
return
|
|
185
|
+
return parseNumberPrefixes
|
|
176
186
|
? stripPathNumberPrefixes(sourceDirName, options.numberPrefixParser)
|
|
177
187
|
: sourceDirName;
|
|
178
188
|
}
|
|
@@ -185,23 +195,14 @@ function doProcessDocMetadata({
|
|
|
185
195
|
// legacy versioned id, requires a breaking change to modify this
|
|
186
196
|
const id = [versionIdPrefix, unversionedId].filter(Boolean).join('/');
|
|
187
197
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
const docSlug = isDocsHomePage
|
|
197
|
-
? '/'
|
|
198
|
-
: getSlug({
|
|
199
|
-
baseID,
|
|
200
|
-
dirName: sourceDirName,
|
|
201
|
-
frontmatterSlug: frontMatter.slug,
|
|
202
|
-
stripDirNumberPrefixes: parse_number_prefixes,
|
|
203
|
-
numberPrefixParser: options.numberPrefixParser,
|
|
204
|
-
});
|
|
198
|
+
const docSlug = getSlug({
|
|
199
|
+
baseID,
|
|
200
|
+
source,
|
|
201
|
+
sourceDirName,
|
|
202
|
+
frontmatterSlug: frontMatter.slug,
|
|
203
|
+
stripDirNumberPrefixes: parseNumberPrefixes,
|
|
204
|
+
numberPrefixParser: options.numberPrefixParser,
|
|
205
|
+
});
|
|
205
206
|
|
|
206
207
|
// Note: the title is used by default for page title, sidebar label, pagination buttons...
|
|
207
208
|
// frontMatter.title should be used in priority over contentTitle (because it can contain markdown/JSX syntax)
|
|
@@ -243,7 +244,6 @@ function doProcessDocMetadata({
|
|
|
243
244
|
return {
|
|
244
245
|
unversionedId,
|
|
245
246
|
id,
|
|
246
|
-
isDocsHomePage,
|
|
247
247
|
title,
|
|
248
248
|
description,
|
|
249
249
|
source: aliasedSitePath(filePath, siteDir),
|
|
@@ -251,6 +251,7 @@ function doProcessDocMetadata({
|
|
|
251
251
|
slug: docSlug,
|
|
252
252
|
permalink,
|
|
253
253
|
editUrl: customEditURL !== undefined ? customEditURL : getDocEditUrl(),
|
|
254
|
+
tags: normalizeFrontMatterTags(versionMetadata.tagsPath, frontMatter.tags),
|
|
254
255
|
version: versionMetadata.versionName,
|
|
255
256
|
lastUpdatedBy: lastUpdate.lastUpdatedBy,
|
|
256
257
|
lastUpdatedAt: lastUpdate.lastUpdatedAt,
|
|
@@ -275,9 +276,137 @@ export function processDocMetadata(args: {
|
|
|
275
276
|
} catch (e) {
|
|
276
277
|
console.error(
|
|
277
278
|
chalk.red(
|
|
278
|
-
`Can't process doc
|
|
279
|
+
`Can't process doc metadata for doc at path "${args.docFile.filePath}" in version "${args.versionMetadata.versionName}"`,
|
|
279
280
|
),
|
|
280
281
|
);
|
|
281
282
|
throw e;
|
|
282
283
|
}
|
|
283
284
|
}
|
|
285
|
+
|
|
286
|
+
export function addDocNavigation(
|
|
287
|
+
docsBase: DocMetadataBase[],
|
|
288
|
+
sidebarsUtils: SidebarsUtils,
|
|
289
|
+
sidebarFilePath: string,
|
|
290
|
+
): LoadedVersion['docs'] {
|
|
291
|
+
const docsById = createDocsByIdIndex(docsBase);
|
|
292
|
+
|
|
293
|
+
sidebarsUtils.checkSidebarsDocIds(
|
|
294
|
+
docsBase.flatMap(getDocIds),
|
|
295
|
+
sidebarFilePath,
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
// Add sidebar/next/previous to the docs
|
|
299
|
+
function addNavData(doc: DocMetadataBase): DocMetadata {
|
|
300
|
+
const navigation = sidebarsUtils.getDocNavigation(
|
|
301
|
+
doc.unversionedId,
|
|
302
|
+
doc.id,
|
|
303
|
+
);
|
|
304
|
+
|
|
305
|
+
const toNavigationLinkByDocId = (
|
|
306
|
+
docId: string | null | undefined,
|
|
307
|
+
type: 'prev' | 'next',
|
|
308
|
+
): DocNavLink | undefined => {
|
|
309
|
+
if (!docId) {
|
|
310
|
+
return undefined;
|
|
311
|
+
}
|
|
312
|
+
const navDoc = docsById[docId];
|
|
313
|
+
if (!navDoc) {
|
|
314
|
+
// This could only happen if user provided the ID through front matter
|
|
315
|
+
throw new Error(
|
|
316
|
+
`Error when loading ${doc.id} in ${doc.sourceDirName}: the pagination_${type} front matter points to a non-existent ID ${docId}.`,
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
return toDocNavigationLink(navDoc);
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
const previous: DocNavLink | undefined = doc.frontMatter.pagination_prev
|
|
323
|
+
? toNavigationLinkByDocId(doc.frontMatter.pagination_prev, 'prev')
|
|
324
|
+
: toNavigationLink(navigation.previous, docsById);
|
|
325
|
+
const next: DocNavLink | undefined = doc.frontMatter.pagination_next
|
|
326
|
+
? toNavigationLinkByDocId(doc.frontMatter.pagination_next, 'next')
|
|
327
|
+
: toNavigationLink(navigation.next, docsById);
|
|
328
|
+
|
|
329
|
+
return {...doc, sidebar: navigation.sidebarName, previous, next};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const docsWithNavigation = docsBase.map(addNavData);
|
|
333
|
+
// sort to ensure consistent output for tests
|
|
334
|
+
docsWithNavigation.sort((a, b) => a.id.localeCompare(b.id));
|
|
335
|
+
return docsWithNavigation;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* The "main doc" is the "version entry point"
|
|
340
|
+
* We browse this doc by clicking on a version:
|
|
341
|
+
* - the "home" doc (at '/docs/')
|
|
342
|
+
* - the first doc of the first sidebar
|
|
343
|
+
* - a random doc (if no docs are in any sidebar... edge case)
|
|
344
|
+
*/
|
|
345
|
+
export function getMainDocId({
|
|
346
|
+
docs,
|
|
347
|
+
sidebarsUtils,
|
|
348
|
+
}: {
|
|
349
|
+
docs: DocMetadataBase[];
|
|
350
|
+
sidebarsUtils: SidebarsUtils;
|
|
351
|
+
}): string {
|
|
352
|
+
function getMainDoc(): DocMetadata {
|
|
353
|
+
const versionHomeDoc = docs.find((doc) => doc.slug === '/');
|
|
354
|
+
const firstDocIdOfFirstSidebar =
|
|
355
|
+
sidebarsUtils.getFirstDocIdOfFirstSidebar();
|
|
356
|
+
if (versionHomeDoc) {
|
|
357
|
+
return versionHomeDoc;
|
|
358
|
+
} else if (firstDocIdOfFirstSidebar) {
|
|
359
|
+
return docs.find((doc) => doc.id === firstDocIdOfFirstSidebar)!;
|
|
360
|
+
} else {
|
|
361
|
+
return docs[0];
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
return getMainDoc().unversionedId;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function getLastPathSegment(str: string): string {
|
|
369
|
+
return last(str.split('/'))!;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// By convention, Docusaurus considers some docs are "indexes":
|
|
373
|
+
// - index.md
|
|
374
|
+
// - readme.md
|
|
375
|
+
// - <folder>/<folder>.md
|
|
376
|
+
//
|
|
377
|
+
// Those index docs produce a different behavior
|
|
378
|
+
// - Slugs do not end with a weird "/index" suffix
|
|
379
|
+
// - Auto-generated sidebar categories link to them as intro
|
|
380
|
+
export function isConventionalDocIndex(doc: {
|
|
381
|
+
source: DocMetadataBase['slug'];
|
|
382
|
+
sourceDirName: DocMetadataBase['sourceDirName'];
|
|
383
|
+
}): boolean {
|
|
384
|
+
// "@site/docs/folder/subFolder/subSubFolder/myDoc.md" => "myDoc"
|
|
385
|
+
const docName = path.parse(doc.source).name;
|
|
386
|
+
|
|
387
|
+
// "folder/subFolder/subSubFolder" => "subSubFolder"
|
|
388
|
+
const lastDirName = getLastPathSegment(doc.sourceDirName);
|
|
389
|
+
|
|
390
|
+
const eligibleDocIndexNames = ['index', 'readme', lastDirName.toLowerCase()];
|
|
391
|
+
|
|
392
|
+
return eligibleDocIndexNames.includes(docName.toLowerCase());
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// Return both doc ids
|
|
396
|
+
// TODO legacy retro-compatibility due to old versioned sidebars using versioned doc ids
|
|
397
|
+
// ("id" should be removed & "versionedId" should be renamed to "id")
|
|
398
|
+
export function getDocIds(doc: DocMetadataBase): [string, string] {
|
|
399
|
+
return [doc.unversionedId, doc.id];
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// docs are indexed by both versioned and unversioned ids at the same time
|
|
403
|
+
// TODO legacy retro-compatibility due to old versioned sidebars using versioned doc ids
|
|
404
|
+
// ("id" should be removed & "versionedId" should be renamed to "id")
|
|
405
|
+
export function createDocsByIdIndex<
|
|
406
|
+
Doc extends {id: string; unversionedId: string},
|
|
407
|
+
>(docs: Doc[]): Record<string, Doc> {
|
|
408
|
+
return {
|
|
409
|
+
...keyBy(docs, (doc) => doc.unversionedId),
|
|
410
|
+
...keyBy(docs, (doc) => doc.id),
|
|
411
|
+
};
|
|
412
|
+
}
|
package/src/globalData.ts
CHANGED
|
@@ -5,7 +5,12 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import {
|
|
8
|
+
import type {
|
|
9
|
+
DocMetadata,
|
|
10
|
+
GlobalDoc,
|
|
11
|
+
LoadedVersion,
|
|
12
|
+
GlobalVersion,
|
|
13
|
+
} from './types';
|
|
9
14
|
|
|
10
15
|
export function toGlobalDataDoc(doc: DocMetadata): GlobalDoc {
|
|
11
16
|
return {
|