@docusaurus/plugin-content-docs 2.0.0-beta.1decd6f80 → 2.0.0-beta.20
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 +3 -2
- package/lib/cli.js +65 -69
- package/lib/client/docsClientUtils.d.ts +9 -28
- package/lib/client/docsClientUtils.js +34 -42
- package/lib/client/index.d.ts +22 -0
- package/lib/client/index.js +72 -0
- package/lib/constants.d.ts +4 -0
- package/lib/constants.js +4 -1
- package/lib/docs.d.ts +33 -4
- package/lib/docs.js +173 -62
- package/{src/__tests__/__fixtures__/site-with-doc-label/docusaurus.config.js → lib/frontMatter.d.ts} +4 -8
- package/lib/{docFrontMatter.js → frontMatter.js} +16 -6
- package/lib/globalData.d.ts +3 -3
- package/lib/globalData.js +35 -6
- package/lib/index.d.ts +3 -3
- package/lib/index.js +135 -140
- package/lib/lastUpdate.d.ts +4 -6
- package/lib/lastUpdate.js +22 -26
- package/lib/markdown/index.d.ts +3 -6
- package/lib/markdown/index.js +3 -3
- package/lib/markdown/linkify.d.ts +1 -1
- package/lib/markdown/linkify.js +7 -4
- package/lib/numberPrefix.d.ts +1 -1
- package/lib/numberPrefix.js +16 -21
- package/lib/options.d.ts +3 -5
- package/lib/options.js +55 -19
- package/lib/props.d.ts +7 -2
- package/lib/props.js +72 -15
- package/lib/routes.d.ts +29 -0
- package/lib/routes.js +96 -0
- package/lib/server-export.d.ts +9 -0
- package/lib/server-export.js +22 -0
- package/lib/{sidebarItemsGenerator.d.ts → sidebars/generator.d.ts} +1 -6
- package/lib/sidebars/generator.js +209 -0
- package/lib/sidebars/index.d.ts +13 -0
- package/lib/sidebars/index.js +92 -0
- package/lib/sidebars/normalization.d.ts +13 -0
- package/lib/sidebars/normalization.js +55 -0
- package/lib/sidebars/postProcessor.d.ts +8 -0
- package/lib/sidebars/postProcessor.js +65 -0
- package/lib/sidebars/processor.d.ts +10 -0
- package/lib/sidebars/processor.js +90 -0
- package/lib/sidebars/types.d.ts +178 -0
- package/{src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars2.js → lib/sidebars/types.js} +2 -10
- package/lib/sidebars/utils.d.ts +54 -0
- package/lib/sidebars/utils.js +255 -0
- package/lib/sidebars/validation.d.ts +11 -0
- package/lib/sidebars/validation.js +138 -0
- package/lib/slug.d.ts +5 -4
- package/lib/slug.js +29 -19
- package/{src/__tests__/__fixtures__/sidebars/sidebars-first-level-not-category.js → lib/tags.d.ts} +3 -14
- package/lib/tags.js +21 -0
- package/lib/translations.d.ts +3 -3
- package/lib/translations.js +84 -94
- package/lib/types.d.ts +13 -171
- package/lib/versions/files.d.ts +44 -0
- package/lib/versions/files.js +142 -0
- package/lib/versions/index.d.ts +36 -0
- package/lib/versions/index.js +155 -0
- package/lib/versions/validation.d.ts +17 -0
- package/lib/versions/validation.js +71 -0
- package/package.json +32 -26
- package/src/categoryGeneratedIndex.ts +60 -0
- package/src/cli.ts +90 -109
- package/src/client/docsClientUtils.ts +47 -84
- package/src/client/index.ts +103 -0
- package/src/constants.ts +4 -2
- package/{types.d.ts → src/deps.d.ts} +1 -1
- package/src/docs.ts +244 -63
- package/src/frontMatter.ts +51 -0
- package/src/globalData.ts +57 -7
- package/src/index.ts +186 -190
- package/src/lastUpdate.ts +26 -33
- package/src/markdown/index.ts +10 -16
- package/src/markdown/linkify.ts +8 -5
- package/src/numberPrefix.ts +19 -26
- package/src/options.ts +60 -32
- package/src/plugin-content-docs.d.ts +632 -75
- package/src/props.ts +109 -22
- package/src/routes.ts +159 -0
- package/src/server-export.ts +22 -0
- package/src/sidebars/README.md +9 -0
- package/src/sidebars/generator.ts +292 -0
- package/src/sidebars/index.ts +118 -0
- package/src/sidebars/normalization.ts +85 -0
- package/src/sidebars/postProcessor.ts +89 -0
- package/src/sidebars/processor.ts +139 -0
- package/src/sidebars/types.ts +275 -0
- package/src/sidebars/utils.ts +391 -0
- package/src/sidebars/validation.ts +174 -0
- package/src/slug.ts +42 -23
- package/src/tags.ts +20 -0
- package/src/translations.ts +124 -117
- package/src/types.ts +18 -231
- package/src/versions/files.ts +220 -0
- package/src/versions/index.ts +247 -0
- package/src/versions/validation.ts +113 -0
- package/lib/.tsbuildinfo +0 -4673
- package/lib/docFrontMatter.d.ts +0 -21
- package/lib/sidebarItemsGenerator.js +0 -211
- package/lib/sidebars.d.ts +0 -42
- package/lib/sidebars.js +0 -309
- package/lib/theme/hooks/useDocs.d.ts +0 -20
- package/lib/theme/hooks/useDocs.js +0 -72
- package/lib/versions.d.ts +0 -16
- package/lib/versions.js +0 -287
- 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-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 -67
- package/src/__tests__/__fixtures__/simple-site/docs/headingAsTitle.md +0 -1
- package/src/__tests__/__fixtures__/simple-site/docs/hello.md +0 -52
- 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 -1907
- package/src/__tests__/__snapshots__/sidebars.test.ts.snap +0 -218
- package/src/__tests__/__snapshots__/translations.test.ts.snap +0 -487
- package/src/__tests__/cli.test.ts +0 -333
- package/src/__tests__/docFrontMatter.test.ts +0 -204
- package/src/__tests__/docs.test.ts +0 -875
- package/src/__tests__/index.test.ts +0 -1831
- package/src/__tests__/lastUpdate.test.ts +0 -68
- package/src/__tests__/numberPrefix.test.ts +0 -199
- package/src/__tests__/options.test.ts +0 -232
- package/src/__tests__/sidebarItemsGenerator.test.ts +0 -336
- package/src/__tests__/sidebars.test.ts +0 -638
- package/src/__tests__/slug.test.ts +0 -109
- package/src/__tests__/translations.test.ts +0 -159
- package/src/__tests__/versions.test.ts +0 -718
- package/src/client/__tests__/docsClientUtils.test.ts +0 -372
- package/src/docFrontMatter.ts +0 -53
- 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 -489
- package/src/theme/hooks/useDocs.ts +0 -99
- package/src/versions.ts +0 -511
- package/tsconfig.json +0 -9
package/src/cli.ts
CHANGED
|
@@ -7,20 +7,21 @@
|
|
|
7
7
|
|
|
8
8
|
import {
|
|
9
9
|
getVersionsFilePath,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
getVersionDocsDirPath,
|
|
11
|
+
getVersionSidebarsPath,
|
|
12
|
+
getDocsDirPathLocalized,
|
|
13
|
+
} from './versions/files';
|
|
14
|
+
import {validateVersionName} from './versions/validation';
|
|
13
15
|
import fs from 'fs-extra';
|
|
14
16
|
import path from 'path';
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
import {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
function createVersionedSidebarFile({
|
|
17
|
+
import type {PluginOptions} from '@docusaurus/plugin-content-docs';
|
|
18
|
+
import {loadSidebarsFileUnsafe} from './sidebars';
|
|
19
|
+
import {CURRENT_VERSION_NAME} from './constants';
|
|
20
|
+
import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
|
|
21
|
+
import logger from '@docusaurus/logger';
|
|
22
|
+
import type {LoadContext} from '@docusaurus/types';
|
|
23
|
+
|
|
24
|
+
async function createVersionedSidebarFile({
|
|
24
25
|
siteDir,
|
|
25
26
|
pluginId,
|
|
26
27
|
sidebarPath,
|
|
@@ -32,133 +33,113 @@ function createVersionedSidebarFile({
|
|
|
32
33
|
version: string;
|
|
33
34
|
}) {
|
|
34
35
|
// Load current sidebar and create a new versioned sidebars file (if needed).
|
|
35
|
-
|
|
36
|
+
// Note: we don't need the sidebars file to be normalized: it's ok to let
|
|
37
|
+
// plugin option changes to impact older, versioned sidebars
|
|
38
|
+
// We don't validate here, assuming the user has already built the version
|
|
39
|
+
const sidebars = await loadSidebarsFileUnsafe(sidebarPath);
|
|
36
40
|
|
|
37
|
-
// Do not create a useless versioned sidebars file if sidebars file is empty
|
|
38
|
-
|
|
39
|
-
|
|
41
|
+
// Do not create a useless versioned sidebars file if sidebars file is empty
|
|
42
|
+
// or sidebars are disabled/false)
|
|
43
|
+
const shouldCreateVersionedSidebarFile = Object.keys(sidebars).length > 0;
|
|
40
44
|
|
|
41
45
|
if (shouldCreateVersionedSidebarFile) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const normalizeItem = (
|
|
46
|
-
item: UnprocessedSidebarItem,
|
|
47
|
-
): UnprocessedSidebarItem => {
|
|
48
|
-
switch (item.type) {
|
|
49
|
-
case 'category':
|
|
50
|
-
return {...item, items: item.items.map(normalizeItem)};
|
|
51
|
-
case 'ref':
|
|
52
|
-
case 'doc':
|
|
53
|
-
return {
|
|
54
|
-
type: item.type,
|
|
55
|
-
id: `version-${version}/${item.id}`,
|
|
56
|
-
};
|
|
57
|
-
default:
|
|
58
|
-
return item;
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
const versionedSidebar: UnprocessedSidebars = Object.entries(
|
|
63
|
-
loadedSidebars,
|
|
64
|
-
).reduce((acc: UnprocessedSidebars, [sidebarId, sidebarItems]) => {
|
|
65
|
-
const newVersionedSidebarId = `version-${version}/${sidebarId}`;
|
|
66
|
-
acc[newVersionedSidebarId] = sidebarItems.map(normalizeItem);
|
|
67
|
-
return acc;
|
|
68
|
-
}, {});
|
|
69
|
-
|
|
70
|
-
const versionedSidebarsDir = getVersionedSidebarsDirPath(siteDir, pluginId);
|
|
71
|
-
const newSidebarFile = path.join(
|
|
72
|
-
versionedSidebarsDir,
|
|
73
|
-
`version-${version}-sidebars.json`,
|
|
74
|
-
);
|
|
75
|
-
fs.ensureDirSync(path.dirname(newSidebarFile));
|
|
76
|
-
fs.writeFileSync(
|
|
77
|
-
newSidebarFile,
|
|
78
|
-
`${JSON.stringify(versionedSidebar, null, 2)}\n`,
|
|
46
|
+
await fs.outputFile(
|
|
47
|
+
getVersionSidebarsPath(siteDir, pluginId, version),
|
|
48
|
+
`${JSON.stringify(sidebars, null, 2)}\n`,
|
|
79
49
|
'utf8',
|
|
80
50
|
);
|
|
81
51
|
}
|
|
82
52
|
}
|
|
83
53
|
|
|
84
54
|
// Tests depend on non-default export for mocking.
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
siteDir:
|
|
89
|
-
|
|
90
|
-
options: PathOptions,
|
|
91
|
-
): void {
|
|
55
|
+
export async function cliDocsVersionCommand(
|
|
56
|
+
version: string,
|
|
57
|
+
{id: pluginId, path: docsPath, sidebarPath}: PluginOptions,
|
|
58
|
+
{siteDir, i18n}: LoadContext,
|
|
59
|
+
): Promise<void> {
|
|
92
60
|
// It wouldn't be very user-friendly to show a [default] log prefix,
|
|
93
61
|
// so we use [docs] instead of [default]
|
|
94
62
|
const pluginIdLogPrefix =
|
|
95
|
-
pluginId === DEFAULT_PLUGIN_ID ? '[docs]
|
|
96
|
-
|
|
97
|
-
if (!version) {
|
|
98
|
-
throw new Error(
|
|
99
|
-
`${pluginIdLogPrefix}No version tag specified!. Pass the version you wish to create as an argument. Ex: 1.0.0`,
|
|
100
|
-
);
|
|
101
|
-
}
|
|
63
|
+
pluginId === DEFAULT_PLUGIN_ID ? '[docs]' : `[${pluginId}]`;
|
|
102
64
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
if (version.length > 32) {
|
|
110
|
-
throw new Error(
|
|
111
|
-
`${pluginIdLogPrefix}Invalid version tag specified! Length must <= 32 characters. Try something like: 1.0.0`,
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// Since we are going to create `version-${version}` folder, we need to make
|
|
116
|
-
// sure it's a valid pathname.
|
|
117
|
-
// eslint-disable-next-line no-control-regex
|
|
118
|
-
if (/[<>:"|?*\x00-\x1F]/g.test(version)) {
|
|
119
|
-
throw new Error(
|
|
120
|
-
`${pluginIdLogPrefix}Invalid version tag specified! Please ensure its a valid pathname too. Try something like: 1.0.0`,
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (/^\.\.?$/.test(version)) {
|
|
125
|
-
throw new Error(
|
|
126
|
-
`${pluginIdLogPrefix}Invalid version tag specified! Do not name your version "." or "..". Try something like: 1.0.0`,
|
|
127
|
-
);
|
|
65
|
+
try {
|
|
66
|
+
validateVersionName(version);
|
|
67
|
+
} catch (e) {
|
|
68
|
+
logger.info`${pluginIdLogPrefix}: Invalid version name provided. Try something like: 1.0.0`;
|
|
69
|
+
throw e;
|
|
128
70
|
}
|
|
129
71
|
|
|
130
72
|
// Load existing versions.
|
|
131
73
|
let versions = [];
|
|
132
74
|
const versionsJSONFile = getVersionsFilePath(siteDir, pluginId);
|
|
133
|
-
if (fs.
|
|
134
|
-
versions =
|
|
75
|
+
if (await fs.pathExists(versionsJSONFile)) {
|
|
76
|
+
versions = await fs.readJSON(versionsJSONFile);
|
|
135
77
|
}
|
|
136
78
|
|
|
137
79
|
// Check if version already exists.
|
|
138
80
|
if (versions.includes(version)) {
|
|
139
81
|
throw new Error(
|
|
140
|
-
`${pluginIdLogPrefix}
|
|
82
|
+
`${pluginIdLogPrefix}: this version already exists! Use a version tag that does not already exist.`,
|
|
141
83
|
);
|
|
142
84
|
}
|
|
143
85
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
// Copy docs files.
|
|
147
|
-
const docsDir = path.join(siteDir, docsPath);
|
|
148
|
-
if (fs.existsSync(docsDir) && fs.readdirSync(docsDir).length > 0) {
|
|
149
|
-
const versionedDir = getVersionedDocsDirPath(siteDir, pluginId);
|
|
150
|
-
const newVersionDir = path.join(versionedDir, `version-${version}`);
|
|
151
|
-
fs.copySync(docsDir, newVersionDir);
|
|
152
|
-
} else {
|
|
153
|
-
throw new Error(`${pluginIdLogPrefix}There is no docs to version !`);
|
|
86
|
+
if (i18n.locales.length > 1) {
|
|
87
|
+
logger.info`Versioned docs will be created for the following locales: name=${i18n.locales}`;
|
|
154
88
|
}
|
|
155
89
|
|
|
156
|
-
|
|
90
|
+
await Promise.all(
|
|
91
|
+
i18n.locales.map(async (locale) => {
|
|
92
|
+
// Copy docs files.
|
|
93
|
+
const docsDir =
|
|
94
|
+
locale === i18n.defaultLocale
|
|
95
|
+
? path.resolve(siteDir, docsPath)
|
|
96
|
+
: getDocsDirPathLocalized({
|
|
97
|
+
siteDir,
|
|
98
|
+
locale,
|
|
99
|
+
pluginId,
|
|
100
|
+
versionName: CURRENT_VERSION_NAME,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
if (
|
|
104
|
+
!(await fs.pathExists(docsDir)) ||
|
|
105
|
+
(await fs.readdir(docsDir)).length === 0
|
|
106
|
+
) {
|
|
107
|
+
if (locale === i18n.defaultLocale) {
|
|
108
|
+
throw new Error(
|
|
109
|
+
logger.interpolate`${pluginIdLogPrefix}: no docs found in path=${docsDir}.`,
|
|
110
|
+
);
|
|
111
|
+
} else {
|
|
112
|
+
logger.warn`${pluginIdLogPrefix}: no docs found in path=${docsDir}. Skipping.`;
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const newVersionDir =
|
|
118
|
+
locale === i18n.defaultLocale
|
|
119
|
+
? getVersionDocsDirPath(siteDir, pluginId, version)
|
|
120
|
+
: getDocsDirPathLocalized({
|
|
121
|
+
siteDir,
|
|
122
|
+
locale,
|
|
123
|
+
pluginId,
|
|
124
|
+
versionName: version,
|
|
125
|
+
});
|
|
126
|
+
await fs.copy(docsDir, newVersionDir);
|
|
127
|
+
}),
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
await createVersionedSidebarFile({
|
|
131
|
+
siteDir,
|
|
132
|
+
pluginId,
|
|
133
|
+
version,
|
|
134
|
+
sidebarPath,
|
|
135
|
+
});
|
|
157
136
|
|
|
158
137
|
// Update versions.json file.
|
|
159
138
|
versions.unshift(version);
|
|
160
|
-
fs.
|
|
161
|
-
|
|
139
|
+
await fs.outputFile(
|
|
140
|
+
versionsJSONFile,
|
|
141
|
+
`${JSON.stringify(versions, null, 2)}\n`,
|
|
142
|
+
);
|
|
162
143
|
|
|
163
|
-
|
|
144
|
+
logger.success`name=${pluginIdLogPrefix}: version name=${version} created!`;
|
|
164
145
|
}
|
|
@@ -7,49 +7,37 @@
|
|
|
7
7
|
|
|
8
8
|
import {matchPath} from '@docusaurus/router';
|
|
9
9
|
|
|
10
|
-
import {
|
|
10
|
+
import type {
|
|
11
|
+
GlobalPluginData,
|
|
12
|
+
GlobalVersion,
|
|
13
|
+
GlobalDoc,
|
|
14
|
+
ActivePlugin,
|
|
15
|
+
ActiveDocContext,
|
|
16
|
+
DocVersionSuggestions,
|
|
17
|
+
} from '@docusaurus/plugin-content-docs/client';
|
|
18
|
+
import type {UseDataOptions} from '@docusaurus/types';
|
|
11
19
|
|
|
12
20
|
// This code is not part of the api surface, not in ./theme on purpose
|
|
13
21
|
|
|
14
|
-
// Short/convenient type aliases
|
|
15
|
-
type Version = GlobalVersion;
|
|
16
|
-
type Doc = GlobalDoc;
|
|
17
|
-
|
|
18
|
-
export type ActivePlugin = {
|
|
19
|
-
pluginId: string;
|
|
20
|
-
pluginData: GlobalPluginData;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export type GetActivePluginOptions = {failfast?: boolean};
|
|
24
|
-
|
|
25
22
|
// get the data of the plugin that is currently "active"
|
|
26
23
|
// ie the docs of that plugin are currently browsed
|
|
27
24
|
// it is useful to support multiple docs plugin instances
|
|
28
25
|
export function getActivePlugin(
|
|
29
|
-
|
|
30
|
-
pathname: string,
|
|
31
|
-
options: {failfast: true}, // use fail-fast option if you know for sure one plugin instance is active
|
|
32
|
-
): ActivePlugin;
|
|
33
|
-
export function getActivePlugin(
|
|
34
|
-
allPluginDatas: Record<string, GlobalPluginData>,
|
|
35
|
-
pathname: string,
|
|
36
|
-
options?: GetActivePluginOptions,
|
|
37
|
-
): ActivePlugin | undefined;
|
|
38
|
-
|
|
39
|
-
export function getActivePlugin(
|
|
40
|
-
allPluginDatas: Record<string, GlobalPluginData>,
|
|
26
|
+
allPluginData: {[pluginId: string]: GlobalPluginData},
|
|
41
27
|
pathname: string,
|
|
42
|
-
options:
|
|
28
|
+
options: UseDataOptions = {},
|
|
43
29
|
): ActivePlugin | undefined {
|
|
44
|
-
const activeEntry = Object.entries(
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
30
|
+
const activeEntry = Object.entries(allPluginData)
|
|
31
|
+
// Route sorting: '/android/foo' should match '/android' instead of '/'
|
|
32
|
+
.sort((a, b) => b[1].path.localeCompare(a[1].path))
|
|
33
|
+
.find(
|
|
34
|
+
([, pluginData]) =>
|
|
35
|
+
!!matchPath(pathname, {
|
|
36
|
+
path: pluginData.path,
|
|
37
|
+
exact: false,
|
|
38
|
+
strict: false,
|
|
39
|
+
}),
|
|
40
|
+
);
|
|
53
41
|
|
|
54
42
|
const activePlugin: ActivePlugin | undefined = activeEntry
|
|
55
43
|
? {pluginId: activeEntry[0], pluginData: activeEntry[1]}
|
|
@@ -57,8 +45,8 @@ export function getActivePlugin(
|
|
|
57
45
|
|
|
58
46
|
if (!activePlugin && options.failfast) {
|
|
59
47
|
throw new Error(
|
|
60
|
-
`Can't find active docs plugin for
|
|
61
|
-
|
|
48
|
+
`Can't find active docs plugin for "${pathname}" pathname, while it was expected to be found. Maybe you tried to use a docs feature that can only be used on a docs-related page? Existing docs plugin paths are: ${Object.values(
|
|
49
|
+
allPluginData,
|
|
62
50
|
)
|
|
63
51
|
.map((plugin) => plugin.path)
|
|
64
52
|
.join(', ')}`,
|
|
@@ -68,42 +56,34 @@ export function getActivePlugin(
|
|
|
68
56
|
return activePlugin;
|
|
69
57
|
}
|
|
70
58
|
|
|
71
|
-
export
|
|
72
|
-
|
|
73
|
-
activeDoc?: Doc;
|
|
74
|
-
alternateDocVersions: Record<string, Doc>;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
export const getLatestVersion = (data: GlobalPluginData): Version => {
|
|
78
|
-
return data.versions.find((version) => version.isLast)!;
|
|
79
|
-
};
|
|
59
|
+
export const getLatestVersion = (data: GlobalPluginData): GlobalVersion =>
|
|
60
|
+
data.versions.find((version) => version.isLast)!;
|
|
80
61
|
|
|
81
|
-
|
|
82
|
-
// because there's no version currently considered as active
|
|
83
|
-
export const getActiveVersion = (
|
|
62
|
+
export function getActiveVersion(
|
|
84
63
|
data: GlobalPluginData,
|
|
85
64
|
pathname: string,
|
|
86
|
-
):
|
|
65
|
+
): GlobalVersion | undefined {
|
|
87
66
|
const lastVersion = getLatestVersion(data);
|
|
88
67
|
// Last version is a route like /docs/*,
|
|
89
|
-
// we need to
|
|
68
|
+
// we need to match it last or it would match /docs/version-1.0/* as well
|
|
90
69
|
const orderedVersionsMetadata = [
|
|
91
70
|
...data.versions.filter((version) => version !== lastVersion),
|
|
92
71
|
lastVersion,
|
|
93
72
|
];
|
|
94
|
-
return orderedVersionsMetadata.find(
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
73
|
+
return orderedVersionsMetadata.find(
|
|
74
|
+
(version) =>
|
|
75
|
+
!!matchPath(pathname, {
|
|
76
|
+
path: version.path,
|
|
77
|
+
exact: false,
|
|
78
|
+
strict: false,
|
|
79
|
+
}),
|
|
80
|
+
);
|
|
81
|
+
}
|
|
102
82
|
|
|
103
|
-
export
|
|
83
|
+
export function getActiveDocContext(
|
|
104
84
|
data: GlobalPluginData,
|
|
105
85
|
pathname: string,
|
|
106
|
-
): ActiveDocContext
|
|
86
|
+
): ActiveDocContext {
|
|
107
87
|
const activeVersion = getActiveVersion(data, pathname);
|
|
108
88
|
const activeDoc = activeVersion?.docs.find(
|
|
109
89
|
(doc) =>
|
|
@@ -137,32 +117,15 @@ export const getActiveDocContext = (
|
|
|
137
117
|
activeDoc,
|
|
138
118
|
alternateDocVersions: alternateVersionDocs,
|
|
139
119
|
};
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export type DocVersionSuggestions = {
|
|
143
|
-
// suggest the same doc, in latest version (if exist)
|
|
144
|
-
latestDocSuggestion?: GlobalDoc;
|
|
145
|
-
// suggest the latest version
|
|
146
|
-
latestVersionSuggestion?: GlobalVersion;
|
|
147
|
-
};
|
|
120
|
+
}
|
|
148
121
|
|
|
149
|
-
export
|
|
122
|
+
export function getDocVersionSuggestions(
|
|
150
123
|
data: GlobalPluginData,
|
|
151
124
|
pathname: string,
|
|
152
|
-
): DocVersionSuggestions
|
|
125
|
+
): DocVersionSuggestions {
|
|
153
126
|
const latestVersion = getLatestVersion(data);
|
|
154
127
|
const activeDocContext = getActiveDocContext(data, pathname);
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
const latestDocSuggestion: GlobalDoc | undefined = isNotOnLatestVersion
|
|
160
|
-
? activeDocContext?.alternateDocVersions[latestVersion.name]
|
|
161
|
-
: undefined;
|
|
162
|
-
|
|
163
|
-
const latestVersionSuggestion = isNotOnLatestVersion
|
|
164
|
-
? latestVersion
|
|
165
|
-
: undefined;
|
|
166
|
-
|
|
167
|
-
return {latestDocSuggestion, latestVersionSuggestion};
|
|
168
|
-
};
|
|
128
|
+
const latestDocSuggestion: GlobalDoc | undefined =
|
|
129
|
+
activeDocContext?.alternateDocVersions[latestVersion.name];
|
|
130
|
+
return {latestDocSuggestion, latestVersionSuggestion: latestVersion};
|
|
131
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
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 {useLocation} from '@docusaurus/router';
|
|
9
|
+
import {
|
|
10
|
+
useAllPluginInstancesData,
|
|
11
|
+
usePluginData,
|
|
12
|
+
} from '@docusaurus/useGlobalData';
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
getActivePlugin,
|
|
16
|
+
getLatestVersion,
|
|
17
|
+
getActiveVersion,
|
|
18
|
+
getActiveDocContext,
|
|
19
|
+
getDocVersionSuggestions,
|
|
20
|
+
} from './docsClientUtils';
|
|
21
|
+
import type {
|
|
22
|
+
GlobalPluginData,
|
|
23
|
+
GlobalVersion,
|
|
24
|
+
ActivePlugin,
|
|
25
|
+
ActiveDocContext,
|
|
26
|
+
DocVersionSuggestions,
|
|
27
|
+
} from '@docusaurus/plugin-content-docs/client';
|
|
28
|
+
import type {UseDataOptions} from '@docusaurus/types';
|
|
29
|
+
|
|
30
|
+
// Important to use a constant object to avoid React useEffect executions etc.
|
|
31
|
+
// see https://github.com/facebook/docusaurus/issues/5089
|
|
32
|
+
const StableEmptyObject = {};
|
|
33
|
+
|
|
34
|
+
// In blog-only mode, docs hooks are still used by the theme. We need a fail-
|
|
35
|
+
// safe fallback when the docs plugin is not in use
|
|
36
|
+
export const useAllDocsData = (): {[pluginId: string]: GlobalPluginData} =>
|
|
37
|
+
useAllPluginInstancesData('docusaurus-plugin-content-docs') ??
|
|
38
|
+
StableEmptyObject;
|
|
39
|
+
|
|
40
|
+
export const useDocsData = (pluginId: string | undefined): GlobalPluginData =>
|
|
41
|
+
usePluginData('docusaurus-plugin-content-docs', pluginId, {
|
|
42
|
+
failfast: true,
|
|
43
|
+
}) as GlobalPluginData;
|
|
44
|
+
|
|
45
|
+
// TODO this feature should be provided by docusaurus core
|
|
46
|
+
export function useActivePlugin(
|
|
47
|
+
options: UseDataOptions = {},
|
|
48
|
+
): ActivePlugin | undefined {
|
|
49
|
+
const data = useAllDocsData();
|
|
50
|
+
const {pathname} = useLocation();
|
|
51
|
+
return getActivePlugin(data, pathname, options);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function useActivePluginAndVersion(
|
|
55
|
+
options: UseDataOptions = {},
|
|
56
|
+
):
|
|
57
|
+
| {activePlugin: ActivePlugin; activeVersion: GlobalVersion | undefined}
|
|
58
|
+
| undefined {
|
|
59
|
+
const activePlugin = useActivePlugin(options);
|
|
60
|
+
const {pathname} = useLocation();
|
|
61
|
+
if (!activePlugin) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
const activeVersion = getActiveVersion(activePlugin.pluginData, pathname);
|
|
65
|
+
return {
|
|
66
|
+
activePlugin,
|
|
67
|
+
activeVersion,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function useVersions(pluginId: string | undefined): GlobalVersion[] {
|
|
72
|
+
const data = useDocsData(pluginId);
|
|
73
|
+
return data.versions;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function useLatestVersion(pluginId: string | undefined): GlobalVersion {
|
|
77
|
+
const data = useDocsData(pluginId);
|
|
78
|
+
return getLatestVersion(data);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function useActiveVersion(
|
|
82
|
+
pluginId: string | undefined,
|
|
83
|
+
): GlobalVersion | undefined {
|
|
84
|
+
const data = useDocsData(pluginId);
|
|
85
|
+
const {pathname} = useLocation();
|
|
86
|
+
return getActiveVersion(data, pathname);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function useActiveDocContext(
|
|
90
|
+
pluginId: string | undefined,
|
|
91
|
+
): ActiveDocContext {
|
|
92
|
+
const data = useDocsData(pluginId);
|
|
93
|
+
const {pathname} = useLocation();
|
|
94
|
+
return getActiveDocContext(data, pathname);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function useDocVersionSuggestions(
|
|
98
|
+
pluginId: string | undefined,
|
|
99
|
+
): DocVersionSuggestions {
|
|
100
|
+
const data = useDocsData(pluginId);
|
|
101
|
+
const {pathname} = useLocation();
|
|
102
|
+
return getDocVersionSuggestions(data, pathname);
|
|
103
|
+
}
|
package/src/constants.ts
CHANGED
|
@@ -5,9 +5,11 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
/** The name of the version that's actively worked on (e.g. `website/docs`) */
|
|
9
9
|
export const CURRENT_VERSION_NAME = 'current';
|
|
10
|
-
|
|
10
|
+
/** All doc versions are stored here by version names */
|
|
11
11
|
export const VERSIONED_DOCS_DIR = 'versioned_docs';
|
|
12
|
+
/** All doc versioned sidebars are stored here by version names */
|
|
12
13
|
export const VERSIONED_SIDEBARS_DIR = 'versioned_sidebars';
|
|
14
|
+
/** The version names. Should 1-1 map to the content of versioned docs dir. */
|
|
13
15
|
export const VERSIONS_JSON_FILE = 'versions.json';
|