@docusaurus/plugin-content-docs 2.0.0-beta.15d451942 → 2.0.0-beta.18
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 +35 -0
- package/lib/cli.d.ts +2 -2
- package/lib/cli.js +37 -51
- package/lib/client/docsClientUtils.d.ts +6 -26
- package/lib/client/docsClientUtils.js +28 -34
- package/lib/{theme/hooks/useDocs.d.ts → client/index.d.ts} +4 -3
- package/lib/{theme/hooks/useDocs.js → client/index.js} +28 -25
- package/lib/docs.d.ts +31 -4
- package/lib/docs.js +160 -54
- package/{src/__tests__/__fixtures__/simple-site/docusaurus.config.js → lib/frontMatter.d.ts} +4 -8
- package/lib/{docFrontMatter.js → frontMatter.js} +13 -6
- package/lib/globalData.d.ts +2 -2
- package/lib/globalData.js +32 -3
- package/lib/index.d.ts +4 -3
- package/lib/index.js +123 -136
- 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 -3
- 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 +70 -14
- package/lib/routes.d.ts +28 -0
- package/lib/routes.js +110 -0
- package/lib/server-export.d.ts +8 -0
- package/lib/server-export.js +23 -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 +94 -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 +79 -0
- package/lib/sidebars/types.d.ts +174 -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 +6 -4
- package/lib/slug.js +29 -19
- package/{src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docusaurus.config.js → lib/tags.d.ts} +2 -8
- package/lib/tags.js +21 -0
- package/lib/translations.d.ts +2 -2
- package/lib/translations.js +89 -49
- package/lib/types.d.ts +60 -130
- package/lib/versions.d.ts +29 -4
- package/lib/versions.js +134 -97
- package/package.json +30 -26
- package/src/categoryGeneratedIndex.ts +55 -0
- package/src/cli.ts +47 -63
- package/src/client/docsClientUtils.ts +38 -73
- package/src/{theme/hooks/useDocs.ts → client/index.ts} +16 -11
- package/{types.d.ts → src/deps.d.ts} +1 -1
- package/src/docs.ts +212 -46
- package/src/{docFrontMatter.ts → frontMatter.ts} +21 -26
- package/src/globalData.ts +53 -3
- package/src/index.ts +168 -178
- package/src/lastUpdate.ts +26 -33
- package/src/markdown/index.ts +10 -16
- package/src/markdown/linkify.ts +6 -2
- package/src/numberPrefix.ts +19 -26
- package/src/options.ts +60 -32
- package/src/plugin-content-docs.d.ts +263 -40
- package/src/props.ts +105 -21
- package/src/routes.ts +185 -0
- package/src/server-export.ts +24 -0
- package/src/sidebars/README.md +9 -0
- package/src/sidebars/generator.ts +292 -0
- package/src/sidebars/index.ts +120 -0
- package/src/sidebars/normalization.ts +85 -0
- package/src/sidebars/postProcessor.ts +89 -0
- package/src/sidebars/processor.ts +120 -0
- package/src/sidebars/types.ts +274 -0
- package/src/sidebars/utils.ts +388 -0
- package/src/sidebars/validation.ts +174 -0
- package/src/slug.ts +40 -23
- package/src/tags.ts +19 -0
- package/src/translations.ts +124 -66
- package/src/types.ts +67 -187
- package/src/versions.ts +205 -110
- package/lib/.tsbuildinfo +0 -4717
- 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/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 -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/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/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/docusaurus.config.js +0 -14
- 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/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/tsconfig.json +0 -9
package/src/cli.ts
CHANGED
|
@@ -12,15 +12,15 @@ import {
|
|
|
12
12
|
} from './versions';
|
|
13
13
|
import fs from 'fs-extra';
|
|
14
14
|
import path from 'path';
|
|
15
|
-
import {
|
|
15
|
+
import type {
|
|
16
16
|
PathOptions,
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
} from './
|
|
20
|
-
import {
|
|
21
|
-
import
|
|
17
|
+
SidebarOptions,
|
|
18
|
+
} from '@docusaurus/plugin-content-docs';
|
|
19
|
+
import {loadSidebarsFileUnsafe, resolveSidebarPathOption} from './sidebars';
|
|
20
|
+
import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
|
|
21
|
+
import logger from '@docusaurus/logger';
|
|
22
22
|
|
|
23
|
-
function createVersionedSidebarFile({
|
|
23
|
+
async function createVersionedSidebarFile({
|
|
24
24
|
siteDir,
|
|
25
25
|
pluginId,
|
|
26
26
|
sidebarPath,
|
|
@@ -32,112 +32,85 @@ function createVersionedSidebarFile({
|
|
|
32
32
|
version: string;
|
|
33
33
|
}) {
|
|
34
34
|
// Load current sidebar and create a new versioned sidebars file (if needed).
|
|
35
|
-
|
|
35
|
+
// Note: we don't need the sidebars file to be normalized: it's ok to let
|
|
36
|
+
// plugin option changes to impact older, versioned sidebars
|
|
37
|
+
// We don't validate here, assuming the user has already built the version
|
|
38
|
+
const sidebars = await loadSidebarsFileUnsafe(sidebarPath);
|
|
36
39
|
|
|
37
|
-
// Do not create a useless versioned sidebars file if sidebars file is empty
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
// Do not create a useless versioned sidebars file if sidebars file is empty
|
|
41
|
+
// or sidebars are disabled/false)
|
|
42
|
+
const shouldCreateVersionedSidebarFile = Object.keys(sidebars).length > 0;
|
|
40
43
|
|
|
41
44
|
if (shouldCreateVersionedSidebarFile) {
|
|
42
|
-
// TODO @slorber: this "version prefix" in versioned sidebars looks like a bad idea to me
|
|
43
|
-
// TODO try to get rid of it
|
|
44
|
-
// Transform id in original sidebar to versioned id.
|
|
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
45
|
const versionedSidebarsDir = getVersionedSidebarsDirPath(siteDir, pluginId);
|
|
71
46
|
const newSidebarFile = path.join(
|
|
72
47
|
versionedSidebarsDir,
|
|
73
48
|
`version-${version}-sidebars.json`,
|
|
74
49
|
);
|
|
75
|
-
fs.
|
|
76
|
-
fs.writeFileSync(
|
|
50
|
+
await fs.outputFile(
|
|
77
51
|
newSidebarFile,
|
|
78
|
-
`${JSON.stringify(
|
|
52
|
+
`${JSON.stringify(sidebars, null, 2)}\n`,
|
|
79
53
|
'utf8',
|
|
80
54
|
);
|
|
81
55
|
}
|
|
82
56
|
}
|
|
83
57
|
|
|
84
58
|
// Tests depend on non-default export for mocking.
|
|
85
|
-
|
|
86
|
-
export function cliDocsVersionCommand(
|
|
59
|
+
export async function cliDocsVersionCommand(
|
|
87
60
|
version: string | null | undefined,
|
|
88
61
|
siteDir: string,
|
|
89
62
|
pluginId: string,
|
|
90
|
-
options: PathOptions,
|
|
91
|
-
): void {
|
|
63
|
+
options: PathOptions & SidebarOptions,
|
|
64
|
+
): Promise<void> {
|
|
92
65
|
// It wouldn't be very user-friendly to show a [default] log prefix,
|
|
93
66
|
// so we use [docs] instead of [default]
|
|
94
67
|
const pluginIdLogPrefix =
|
|
95
|
-
pluginId === DEFAULT_PLUGIN_ID ? '[docs]
|
|
68
|
+
pluginId === DEFAULT_PLUGIN_ID ? '[docs]' : `[${pluginId}]`;
|
|
96
69
|
|
|
97
70
|
if (!version) {
|
|
98
71
|
throw new Error(
|
|
99
|
-
`${pluginIdLogPrefix}
|
|
72
|
+
`${pluginIdLogPrefix}: no version tag specified! Pass the version you wish to create as an argument, for example: 1.0.0.`,
|
|
100
73
|
);
|
|
101
74
|
}
|
|
102
75
|
|
|
103
76
|
if (version.includes('/') || version.includes('\\')) {
|
|
104
77
|
throw new Error(
|
|
105
|
-
`${pluginIdLogPrefix}
|
|
78
|
+
`${pluginIdLogPrefix}: invalid version tag specified! Do not include slash (/) or backslash (\\). Try something like: 1.0.0.`,
|
|
106
79
|
);
|
|
107
80
|
}
|
|
108
81
|
|
|
109
82
|
if (version.length > 32) {
|
|
110
83
|
throw new Error(
|
|
111
|
-
`${pluginIdLogPrefix}
|
|
84
|
+
`${pluginIdLogPrefix}: invalid version tag specified! Length cannot exceed 32 characters. Try something like: 1.0.0.`,
|
|
112
85
|
);
|
|
113
86
|
}
|
|
114
87
|
|
|
115
88
|
// Since we are going to create `version-${version}` folder, we need to make
|
|
116
89
|
// sure it's a valid pathname.
|
|
117
90
|
// eslint-disable-next-line no-control-regex
|
|
118
|
-
if (/[<>:"|?*\x00-\x1F]
|
|
91
|
+
if (/[<>:"|?*\x00-\x1F]/.test(version)) {
|
|
119
92
|
throw new Error(
|
|
120
|
-
`${pluginIdLogPrefix}
|
|
93
|
+
`${pluginIdLogPrefix}: invalid version tag specified! Please ensure its a valid pathname too. Try something like: 1.0.0.`,
|
|
121
94
|
);
|
|
122
95
|
}
|
|
123
96
|
|
|
124
97
|
if (/^\.\.?$/.test(version)) {
|
|
125
98
|
throw new Error(
|
|
126
|
-
`${pluginIdLogPrefix}
|
|
99
|
+
`${pluginIdLogPrefix}: invalid version tag specified! Do not name your version "." or "..". Try something like: 1.0.0.`,
|
|
127
100
|
);
|
|
128
101
|
}
|
|
129
102
|
|
|
130
103
|
// Load existing versions.
|
|
131
104
|
let versions = [];
|
|
132
105
|
const versionsJSONFile = getVersionsFilePath(siteDir, pluginId);
|
|
133
|
-
if (fs.
|
|
134
|
-
versions = JSON.parse(fs.
|
|
106
|
+
if (await fs.pathExists(versionsJSONFile)) {
|
|
107
|
+
versions = JSON.parse(await fs.readFile(versionsJSONFile, 'utf8'));
|
|
135
108
|
}
|
|
136
109
|
|
|
137
110
|
// Check if version already exists.
|
|
138
111
|
if (versions.includes(version)) {
|
|
139
112
|
throw new Error(
|
|
140
|
-
`${pluginIdLogPrefix}
|
|
113
|
+
`${pluginIdLogPrefix}: this version already exists! Use a version tag that does not already exist.`,
|
|
141
114
|
);
|
|
142
115
|
}
|
|
143
116
|
|
|
@@ -145,20 +118,31 @@ export function cliDocsVersionCommand(
|
|
|
145
118
|
|
|
146
119
|
// Copy docs files.
|
|
147
120
|
const docsDir = path.join(siteDir, docsPath);
|
|
148
|
-
|
|
121
|
+
|
|
122
|
+
if (
|
|
123
|
+
(await fs.pathExists(docsDir)) &&
|
|
124
|
+
(await fs.readdir(docsDir)).length > 0
|
|
125
|
+
) {
|
|
149
126
|
const versionedDir = getVersionedDocsDirPath(siteDir, pluginId);
|
|
150
127
|
const newVersionDir = path.join(versionedDir, `version-${version}`);
|
|
151
|
-
fs.
|
|
128
|
+
await fs.copy(docsDir, newVersionDir);
|
|
152
129
|
} else {
|
|
153
|
-
throw new Error(`${pluginIdLogPrefix}
|
|
130
|
+
throw new Error(`${pluginIdLogPrefix}: there is no docs to version!`);
|
|
154
131
|
}
|
|
155
132
|
|
|
156
|
-
createVersionedSidebarFile({
|
|
133
|
+
await createVersionedSidebarFile({
|
|
134
|
+
siteDir,
|
|
135
|
+
pluginId,
|
|
136
|
+
version,
|
|
137
|
+
sidebarPath: resolveSidebarPathOption(siteDir, sidebarPath),
|
|
138
|
+
});
|
|
157
139
|
|
|
158
140
|
// Update versions.json file.
|
|
159
141
|
versions.unshift(version);
|
|
160
|
-
fs.
|
|
161
|
-
|
|
142
|
+
await fs.outputFile(
|
|
143
|
+
versionsJSONFile,
|
|
144
|
+
`${JSON.stringify(versions, null, 2)}\n`,
|
|
145
|
+
);
|
|
162
146
|
|
|
163
|
-
|
|
147
|
+
logger.success`name=${pluginIdLogPrefix}: version name=${version} created!`;
|
|
164
148
|
}
|
|
@@ -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
|
+
GetActivePluginOptions,
|
|
15
|
+
ActivePlugin,
|
|
16
|
+
ActiveDocContext,
|
|
17
|
+
DocVersionSuggestions,
|
|
18
|
+
} from '@docusaurus/plugin-content-docs/client';
|
|
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
28
|
options: GetActivePluginOptions = {},
|
|
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,36 +56,30 @@ 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
62
|
// Note: return undefined on doc-unrelated pages,
|
|
82
63
|
// because there's no version currently considered as active
|
|
83
64
|
export const getActiveVersion = (
|
|
84
65
|
data: GlobalPluginData,
|
|
85
66
|
pathname: string,
|
|
86
|
-
):
|
|
67
|
+
): GlobalVersion | undefined => {
|
|
87
68
|
const lastVersion = getLatestVersion(data);
|
|
88
69
|
// Last version is a route like /docs/*,
|
|
89
|
-
// we need to
|
|
70
|
+
// we need to match it last or it would match /docs/version-1.0/* as well
|
|
90
71
|
const orderedVersionsMetadata = [
|
|
91
72
|
...data.versions.filter((version) => version !== lastVersion),
|
|
92
73
|
lastVersion,
|
|
93
74
|
];
|
|
94
|
-
return orderedVersionsMetadata.find(
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
75
|
+
return orderedVersionsMetadata.find(
|
|
76
|
+
(version) =>
|
|
77
|
+
!!matchPath(pathname, {
|
|
78
|
+
path: version.path,
|
|
79
|
+
exact: false,
|
|
80
|
+
strict: false,
|
|
81
|
+
}),
|
|
82
|
+
);
|
|
101
83
|
};
|
|
102
84
|
|
|
103
85
|
export const getActiveDocContext = (
|
|
@@ -139,30 +121,13 @@ export const getActiveDocContext = (
|
|
|
139
121
|
};
|
|
140
122
|
};
|
|
141
123
|
|
|
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
|
-
};
|
|
148
|
-
|
|
149
124
|
export const getDocVersionSuggestions = (
|
|
150
125
|
data: GlobalPluginData,
|
|
151
126
|
pathname: string,
|
|
152
127
|
): DocVersionSuggestions => {
|
|
153
128
|
const latestVersion = getLatestVersion(data);
|
|
154
129
|
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};
|
|
130
|
+
const latestDocSuggestion: GlobalDoc | undefined =
|
|
131
|
+
activeDocContext?.alternateDocVersions[latestVersion.name];
|
|
132
|
+
return {latestDocSuggestion, latestVersionSuggestion: latestVersion};
|
|
168
133
|
};
|
|
@@ -6,33 +6,38 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import {useLocation} from '@docusaurus/router';
|
|
9
|
-
import useGlobalData, {
|
|
10
|
-
// useAllPluginInstancesData,
|
|
11
|
-
usePluginData,
|
|
12
|
-
} from '@docusaurus/useGlobalData';
|
|
9
|
+
import useGlobalData, {usePluginData} from '@docusaurus/useGlobalData';
|
|
13
10
|
|
|
14
|
-
import {GlobalPluginData, GlobalVersion} from '../../types';
|
|
15
11
|
import {
|
|
16
12
|
getActivePlugin,
|
|
17
13
|
getLatestVersion,
|
|
18
14
|
getActiveVersion,
|
|
19
15
|
getActiveDocContext,
|
|
20
16
|
getDocVersionSuggestions,
|
|
17
|
+
} from './docsClientUtils';
|
|
18
|
+
import type {
|
|
19
|
+
GlobalPluginData,
|
|
20
|
+
GlobalVersion,
|
|
21
21
|
ActivePlugin,
|
|
22
22
|
ActiveDocContext,
|
|
23
23
|
DocVersionSuggestions,
|
|
24
24
|
GetActivePluginOptions,
|
|
25
|
-
} from '
|
|
25
|
+
} from '@docusaurus/plugin-content-docs/client';
|
|
26
26
|
|
|
27
|
-
//
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
// Important to use a constant object to avoid React useEffect executions etc.
|
|
28
|
+
// see https://github.com/facebook/docusaurus/issues/5089
|
|
29
|
+
const StableEmptyObject = {};
|
|
30
|
+
|
|
31
|
+
// Not using useAllPluginInstancesData() because in blog-only mode, docs hooks
|
|
32
|
+
// are still used by the theme. We need a fail-safe fallback when the docs
|
|
33
|
+
// plugin is not in use
|
|
34
|
+
export const useAllDocsData = (): {[pluginId: string]: GlobalPluginData} =>
|
|
35
|
+
useGlobalData()['docusaurus-plugin-content-docs'] ?? StableEmptyObject;
|
|
32
36
|
|
|
33
37
|
export const useDocsData = (pluginId: string | undefined): GlobalPluginData =>
|
|
34
38
|
usePluginData('docusaurus-plugin-content-docs', pluginId) as GlobalPluginData;
|
|
35
39
|
|
|
40
|
+
// TODO this feature should be provided by docusaurus core
|
|
36
41
|
export const useActivePlugin = (
|
|
37
42
|
options: GetActivePluginOptions = {},
|
|
38
43
|
): ActivePlugin | undefined => {
|