@docusaurus/plugin-content-docs 2.0.0-beta.15a2b59f9 → 2.0.0-beta.17
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 +38 -0
- package/lib/cli.d.ts +2 -2
- package/lib/cli.js +27 -44
- package/lib/client/docsClientUtils.d.ts +3 -25
- package/lib/client/docsClientUtils.js +25 -24
- package/lib/{theme/hooks/useDocs.d.ts → client/index.d.ts} +1 -2
- package/lib/{theme/hooks/useDocs.js → client/index.js} +26 -26
- package/lib/docFrontMatter.d.ts +1 -1
- package/lib/docFrontMatter.js +9 -3
- package/lib/docs.d.ts +39 -3
- package/lib/docs.js +157 -53
- package/lib/globalData.d.ts +5 -1
- package/lib/globalData.js +35 -2
- package/lib/index.d.ts +4 -3
- package/lib/index.js +110 -144
- package/lib/lastUpdate.js +15 -28
- 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 +2 -2
- package/lib/numberPrefix.d.ts +1 -1
- package/lib/numberPrefix.js +9 -9
- package/lib/options.d.ts +3 -3
- package/lib/options.js +50 -10
- package/lib/props.d.ts +7 -2
- package/lib/props.js +66 -11
- 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 +193 -0
- package/lib/sidebars/index.d.ts +13 -0
- package/lib/sidebars/index.js +88 -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 +70 -0
- package/lib/sidebars/processor.d.ts +8 -0
- package/lib/sidebars/processor.js +79 -0
- package/lib/sidebars/types.d.ts +166 -0
- package/{src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars2.js → lib/sidebars/types.js} +2 -10
- package/lib/sidebars/utils.d.ts +43 -0
- package/lib/sidebars/utils.js +259 -0
- package/lib/sidebars/validation.d.ts +9 -0
- package/lib/sidebars/validation.js +137 -0
- package/lib/slug.d.ts +6 -4
- package/lib/slug.js +28 -17
- package/{src/__tests__/__fixtures__/site-with-doc-label/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 +91 -49
- package/lib/types.d.ts +40 -126
- package/lib/versions.d.ts +29 -4
- package/lib/versions.js +92 -87
- package/package.json +30 -27
- package/src/categoryGeneratedIndex.ts +59 -0
- package/src/cli.ts +34 -54
- package/src/client/docsClientUtils.ts +32 -57
- package/src/{theme/hooks/useDocs.ts → client/index.ts} +11 -10
- package/{types.d.ts → src/deps.d.ts} +0 -0
- package/src/docFrontMatter.ts +12 -5
- package/src/docs.ts +213 -47
- package/src/globalData.ts +53 -2
- package/src/index.ts +151 -197
- package/src/lastUpdate.ts +14 -37
- package/src/markdown/index.ts +9 -13
- package/src/markdown/linkify.ts +1 -1
- package/src/numberPrefix.ts +11 -8
- package/src/options.ts +59 -15
- package/src/plugin-content-docs.d.ts +249 -54
- package/src/props.ts +97 -18
- 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 +269 -0
- package/src/sidebars/index.ts +113 -0
- package/src/sidebars/normalization.ts +85 -0
- package/src/sidebars/postProcessor.ts +94 -0
- package/src/sidebars/processor.ts +126 -0
- package/src/sidebars/types.ts +273 -0
- package/src/sidebars/utils.ts +392 -0
- package/src/sidebars/validation.ts +173 -0
- package/src/slug.ts +39 -21
- package/src/tags.ts +19 -0
- package/src/translations.ts +121 -62
- package/src/types.ts +46 -183
- package/src/versions.ts +136 -102
- package/lib/.tsbuildinfo +0 -1
- package/lib/sidebarItemsGenerator.js +0 -211
- package/lib/sidebars.d.ts +0 -43
- package/lib/sidebars.js +0 -320
- package/src/__tests__/__fixtures__/bad-id-site/docs/invalid-id.md +0 -5
- package/src/__tests__/__fixtures__/bad-slug-on-doc-home-site/docs/docWithSlug.md +0 -5
- package/src/__tests__/__fixtures__/empty-site/docusaurus.config.js +0 -16
- package/src/__tests__/__fixtures__/empty-site/sidebars.json +0 -1
- package/src/__tests__/__fixtures__/sidebars/sidebars-category-shorthand.js +0 -34
- package/src/__tests__/__fixtures__/sidebars/sidebars-category-wrong-items.json +0 -11
- package/src/__tests__/__fixtures__/sidebars/sidebars-category-wrong-label.json +0 -11
- package/src/__tests__/__fixtures__/sidebars/sidebars-category.js +0 -44
- package/src/__tests__/__fixtures__/sidebars/sidebars-collapsed-first-level.json +0 -20
- package/src/__tests__/__fixtures__/sidebars/sidebars-collapsed.json +0 -21
- package/src/__tests__/__fixtures__/sidebars/sidebars-doc-id-not-string.json +0 -10
- package/src/__tests__/__fixtures__/sidebars/sidebars-first-level-not-category.js +0 -20
- package/src/__tests__/__fixtures__/sidebars/sidebars-link-wrong-href.json +0 -11
- package/src/__tests__/__fixtures__/sidebars/sidebars-link-wrong-label.json +0 -11
- package/src/__tests__/__fixtures__/sidebars/sidebars-link.json +0 -11
- package/src/__tests__/__fixtures__/sidebars/sidebars-unknown-type.json +0 -14
- package/src/__tests__/__fixtures__/sidebars/sidebars-wrong-field.json +0 -20
- package/src/__tests__/__fixtures__/sidebars/sidebars.json +0 -20
- package/src/__tests__/__fixtures__/simple-site/docs/foo/bar.md +0 -69
- package/src/__tests__/__fixtures__/simple-site/docs/foo/baz.md +0 -70
- package/src/__tests__/__fixtures__/simple-site/docs/headingAsTitle.md +0 -1
- package/src/__tests__/__fixtures__/simple-site/docs/hello.md +0 -53
- package/src/__tests__/__fixtures__/simple-site/docs/ipsum.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/lorem.md +0 -6
- package/src/__tests__/__fixtures__/simple-site/docs/rootAbsoluteSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/rootRelativeSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/rootResolvedSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/rootTryToEscapeSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/slugs/absoluteSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/slugs/relativeSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/slugs/resolvedSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/slugs/tryToEscapeSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docusaurus.config.js +0 -14
- package/src/__tests__/__fixtures__/simple-site/sidebars.json +0 -23
- package/src/__tests__/__fixtures__/simple-site/wrong-sidebars.json +0 -7
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/0-getting-started.md +0 -3
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/1-installation.md +0 -3
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/00_api-overview.md +0 -3
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/01_Core APIs/0 --- Client API.md +0 -1
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/01_Core APIs/1 --- Server API.md +0 -1
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/0. Plugin API.md +0 -1
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/1. Theme API.md +0 -1
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/_category_.yml +0 -1
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/03_api-end.md +0 -3
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/_category_.json +0 -3
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/0-guide2.5.md +0 -8
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/02-guide2.md +0 -7
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/_category_.json +0 -3
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/a-guide4.md +0 -7
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/b-guide5.md +0 -7
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/guide3.md +0 -8
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/z-guide1.md +0 -8
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docusaurus.config.js +0 -14
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars.js +0 -23
- package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-1.md +0 -7
- package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-2.md +0 -8
- package/src/__tests__/__fixtures__/site-with-doc-label/sidebars.json +0 -14
- package/src/__tests__/__fixtures__/versioned-site/community/team.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/community_sidebars.json +0 -3
- package/src/__tests__/__fixtures__/versioned-site/community_versioned_docs/version-1.0.0/team.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/community_versioned_sidebars/version-1.0.0-sidebars.json +0 -3
- package/src/__tests__/__fixtures__/versioned-site/community_versions.json +0 -1
- package/src/__tests__/__fixtures__/versioned-site/docs/foo/bar.md +0 -4
- package/src/__tests__/__fixtures__/versioned-site/docs/hello.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/docs/slugs/absoluteSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/docs/slugs/relativeSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/docs/slugs/resolvedSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/docs/slugs/tryToEscapeSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/docusaurus.config.js +0 -18
- package/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs-community/current/team.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/sidebars.json +0 -10
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/foo/bar.md +0 -4
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/foo/baz.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/hello.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/foo/bar.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/hello.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootAbsoluteSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootRelativeSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootResolvedSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootTryToEscapeSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/absoluteSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/relativeSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/resolvedSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/tryToEscapeSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.0-sidebars.json +0 -11
- package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.1-sidebars.json +0 -10
- package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-withSlugs-sidebars.json +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versions.json +0 -5
- package/src/__tests__/__snapshots__/cli.test.ts.snap +0 -90
- package/src/__tests__/__snapshots__/index.test.ts.snap +0 -1916
- 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 -244
- package/src/__tests__/docs.test.ts +0 -878
- package/src/__tests__/index.test.ts +0 -1871
- package/src/__tests__/lastUpdate.test.ts +0 -69
- package/src/__tests__/numberPrefix.test.ts +0 -199
- package/src/__tests__/options.test.ts +0 -231
- package/src/__tests__/sidebarItemsGenerator.test.ts +0 -336
- package/src/__tests__/sidebars.test.ts +0 -639
- package/src/__tests__/slug.test.ts +0 -109
- package/src/__tests__/translations.test.ts +0 -159
- package/src/__tests__/versions.test.ts +0 -741
- package/src/client/__tests__/docsClientUtils.test.ts +0 -371
- package/src/markdown/__tests__/__fixtures__/docs/doc-localized.md +0 -1
- package/src/markdown/__tests__/__fixtures__/docs/doc1.md +0 -13
- package/src/markdown/__tests__/__fixtures__/docs/doc2.md +0 -12
- package/src/markdown/__tests__/__fixtures__/docs/doc4.md +0 -19
- package/src/markdown/__tests__/__fixtures__/docs/doc5.md +0 -6
- package/src/markdown/__tests__/__fixtures__/docs/subdir/doc3.md +0 -3
- package/src/markdown/__tests__/__fixtures__/versioned_docs/version-1.0.0/doc2.md +0 -7
- package/src/markdown/__tests__/__fixtures__/versioned_docs/version-1.0.0/subdir/doc1.md +0 -3
- package/src/markdown/__tests__/__snapshots__/linkify.test.ts.snap +0 -82
- package/src/markdown/__tests__/linkify.test.ts +0 -190
- package/src/sidebarItemsGenerator.ts +0 -307
- package/src/sidebars.ts +0 -522
- package/tsconfig.json +0 -9
package/lib/versions.js
CHANGED
|
@@ -6,23 +6,19 @@
|
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.getDocsDirPaths = exports.readVersionsMetadata = exports.getVersionsFilePath = exports.getVersionedSidebarsDirPath = exports.getVersionedDocsDirPath = void 0;
|
|
9
|
+
exports.getDocsDirPaths = exports.readVersionsMetadata = exports.filterVersions = exports.getVersionBadge = exports.getVersionBanner = exports.getDefaultVersionBanner = exports.readVersionNames = exports.readVersionsFile = exports.getVersionsFilePath = exports.getVersionedSidebarsDirPath = exports.getVersionedDocsDirPath = void 0;
|
|
10
10
|
const tslib_1 = require("tslib");
|
|
11
11
|
const path_1 = tslib_1.__importDefault(require("path"));
|
|
12
12
|
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
|
|
13
13
|
const constants_1 = require("./constants");
|
|
14
|
-
const constants_2 = require("@docusaurus/core/lib/constants");
|
|
15
14
|
const utils_1 = require("@docusaurus/utils");
|
|
16
|
-
const lodash_1 = require("lodash");
|
|
15
|
+
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
17
16
|
const sidebars_1 = require("./sidebars");
|
|
18
17
|
// retro-compatibility: no prefix for the default plugin id
|
|
19
18
|
function addPluginIdPrefix(fileOrDir, pluginId) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
else {
|
|
24
|
-
return `${pluginId}_${fileOrDir}`;
|
|
25
|
-
}
|
|
19
|
+
return pluginId === utils_1.DEFAULT_PLUGIN_ID
|
|
20
|
+
? fileOrDir
|
|
21
|
+
: `${pluginId}_${fileOrDir}`;
|
|
26
22
|
}
|
|
27
23
|
function getVersionedDocsDirPath(siteDir, pluginId) {
|
|
28
24
|
return path_1.default.join(siteDir, addPluginIdPrefix(constants_1.VERSIONED_DOCS_DIR, pluginId));
|
|
@@ -51,39 +47,37 @@ function ensureValidVersionArray(versionArray) {
|
|
|
51
47
|
}
|
|
52
48
|
versionArray.forEach(ensureValidVersionString);
|
|
53
49
|
}
|
|
54
|
-
|
|
55
|
-
function readVersionsFile(siteDir, pluginId) {
|
|
50
|
+
async function readVersionsFile(siteDir, pluginId) {
|
|
56
51
|
const versionsFilePath = getVersionsFilePath(siteDir, pluginId);
|
|
57
|
-
if (fs_extra_1.default.
|
|
58
|
-
const content = JSON.parse(fs_extra_1.default.
|
|
52
|
+
if (await fs_extra_1.default.pathExists(versionsFilePath)) {
|
|
53
|
+
const content = JSON.parse(await fs_extra_1.default.readFile(versionsFilePath, 'utf8'));
|
|
59
54
|
ensureValidVersionArray(content);
|
|
60
55
|
return content;
|
|
61
56
|
}
|
|
62
|
-
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
57
|
+
return null;
|
|
65
58
|
}
|
|
66
|
-
|
|
67
|
-
function readVersionNames(siteDir, options) {
|
|
68
|
-
const versionFileContent = readVersionsFile(siteDir, options.id);
|
|
59
|
+
exports.readVersionsFile = readVersionsFile;
|
|
60
|
+
async function readVersionNames(siteDir, options) {
|
|
61
|
+
const versionFileContent = await readVersionsFile(siteDir, options.id);
|
|
69
62
|
if (!versionFileContent && options.disableVersioning) {
|
|
70
|
-
throw new Error(`Docs: using "disableVersioning
|
|
63
|
+
throw new Error(`Docs: using "disableVersioning: ${options.disableVersioning}" option on a non-versioned site does not make sense.`);
|
|
71
64
|
}
|
|
72
|
-
const versions = options.disableVersioning ? [] : versionFileContent
|
|
73
|
-
// We add the current version at the beginning, unless
|
|
74
|
-
// - user don't want to
|
|
75
|
-
// - it's been explicitly added to versions.json
|
|
65
|
+
const versions = options.disableVersioning ? [] : versionFileContent ?? [];
|
|
66
|
+
// We add the current version at the beginning, unless:
|
|
67
|
+
// - user don't want to; or
|
|
68
|
+
// - it's already been explicitly added to versions.json
|
|
76
69
|
if (options.includeCurrentVersion &&
|
|
77
70
|
!versions.includes(constants_1.CURRENT_VERSION_NAME)) {
|
|
78
71
|
versions.unshift(constants_1.CURRENT_VERSION_NAME);
|
|
79
72
|
}
|
|
80
73
|
if (versions.length === 0) {
|
|
81
|
-
throw new Error(`It is not possible to use docs without any version. Please check the configuration of these options: "includeCurrentVersion
|
|
74
|
+
throw new Error(`It is not possible to use docs without any version. Please check the configuration of these options: "includeCurrentVersion: ${options.includeCurrentVersion}", "disableVersioning: ${options.disableVersioning}".`);
|
|
82
75
|
}
|
|
83
76
|
return versions;
|
|
84
77
|
}
|
|
78
|
+
exports.readVersionNames = readVersionNames;
|
|
85
79
|
function getDocsDirPathLocalized({ siteDir, locale, pluginId, versionName, }) {
|
|
86
|
-
return utils_1.getPluginI18nPath({
|
|
80
|
+
return (0, utils_1.getPluginI18nPath)({
|
|
87
81
|
siteDir,
|
|
88
82
|
locale,
|
|
89
83
|
pluginName: 'docusaurus-plugin-content-docs',
|
|
@@ -97,27 +91,23 @@ function getDocsDirPathLocalized({ siteDir, locale, pluginId, versionName, }) {
|
|
|
97
91
|
}
|
|
98
92
|
function getVersionMetadataPaths({ versionName, context, options, }) {
|
|
99
93
|
const isCurrentVersion = versionName === constants_1.CURRENT_VERSION_NAME;
|
|
100
|
-
const contentPath = isCurrentVersion
|
|
101
|
-
? path_1.default.resolve(context.siteDir, options.path)
|
|
102
|
-
: path_1.default.join(getVersionedDocsDirPath(context.siteDir, options.id), `version-${versionName}`);
|
|
103
94
|
const contentPathLocalized = getDocsDirPathLocalized({
|
|
104
95
|
siteDir: context.siteDir,
|
|
105
96
|
locale: context.i18n.currentLocale,
|
|
106
97
|
pluginId: options.id,
|
|
107
98
|
versionName,
|
|
108
99
|
});
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
100
|
+
if (isCurrentVersion) {
|
|
101
|
+
return {
|
|
102
|
+
contentPath: path_1.default.resolve(context.siteDir, options.path),
|
|
103
|
+
contentPathLocalized,
|
|
104
|
+
sidebarFilePath: (0, sidebars_1.resolveSidebarPathOption)(context.siteDir, options.sidebarPath),
|
|
105
|
+
};
|
|
116
106
|
}
|
|
117
107
|
return {
|
|
118
|
-
contentPath,
|
|
108
|
+
contentPath: path_1.default.join(getVersionedDocsDirPath(context.siteDir, options.id), `version-${versionName}`),
|
|
119
109
|
contentPathLocalized,
|
|
120
|
-
sidebarFilePath:
|
|
110
|
+
sidebarFilePath: path_1.default.join(getVersionedSidebarsDirPath(context.siteDir, options.id), `version-${versionName}-sidebars.json`),
|
|
121
111
|
};
|
|
122
112
|
}
|
|
123
113
|
function getVersionEditUrls({ contentPath, contentPathLocalized, context: { siteDir, i18n }, options: { id, path: currentVersionPath, editUrl, editCurrentVersion }, }) {
|
|
@@ -138,10 +128,10 @@ function getVersionEditUrls({ contentPath, contentPathLocalized, context: { site
|
|
|
138
128
|
pluginId: id,
|
|
139
129
|
})
|
|
140
130
|
: contentPathLocalized;
|
|
141
|
-
const versionPathSegment = utils_1.posixPath(path_1.default.relative(siteDir, path_1.default.resolve(siteDir, editDirPath)));
|
|
142
|
-
const versionPathSegmentLocalized = utils_1.posixPath(path_1.default.relative(siteDir, path_1.default.resolve(siteDir, editDirPathLocalized)));
|
|
143
|
-
const versionEditUrl = utils_1.normalizeUrl([editUrl, versionPathSegment]);
|
|
144
|
-
const versionEditUrlLocalized = utils_1.normalizeUrl([
|
|
131
|
+
const versionPathSegment = (0, utils_1.posixPath)(path_1.default.relative(siteDir, path_1.default.resolve(siteDir, editDirPath)));
|
|
132
|
+
const versionPathSegmentLocalized = (0, utils_1.posixPath)(path_1.default.relative(siteDir, path_1.default.resolve(siteDir, editDirPathLocalized)));
|
|
133
|
+
const versionEditUrl = (0, utils_1.normalizeUrl)([editUrl, versionPathSegment]);
|
|
134
|
+
const versionEditUrlLocalized = (0, utils_1.normalizeUrl)([
|
|
145
135
|
editUrl,
|
|
146
136
|
versionPathSegmentLocalized,
|
|
147
137
|
]);
|
|
@@ -153,33 +143,44 @@ function getVersionEditUrls({ contentPath, contentPathLocalized, context: { site
|
|
|
153
143
|
function getDefaultVersionBanner({ versionName, versionNames, lastVersionName, }) {
|
|
154
144
|
// Current version: good, no banner
|
|
155
145
|
if (versionName === lastVersionName) {
|
|
156
|
-
return
|
|
146
|
+
return null;
|
|
157
147
|
}
|
|
158
148
|
// Upcoming versions: unreleased banner
|
|
159
|
-
|
|
149
|
+
if (versionNames.indexOf(versionName) < versionNames.indexOf(lastVersionName)) {
|
|
160
150
|
return 'unreleased';
|
|
161
151
|
}
|
|
162
152
|
// Older versions: display unmaintained banner
|
|
163
|
-
|
|
164
|
-
return 'unmaintained';
|
|
165
|
-
}
|
|
153
|
+
return 'unmaintained';
|
|
166
154
|
}
|
|
155
|
+
exports.getDefaultVersionBanner = getDefaultVersionBanner;
|
|
167
156
|
function getVersionBanner({ versionName, versionNames, lastVersionName, options, }) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
157
|
+
const versionBannerOption = options.versions[versionName]?.banner;
|
|
158
|
+
if (versionBannerOption) {
|
|
159
|
+
return versionBannerOption === 'none' ? null : versionBannerOption;
|
|
160
|
+
}
|
|
161
|
+
return getDefaultVersionBanner({
|
|
171
162
|
versionName,
|
|
172
163
|
versionNames,
|
|
173
164
|
lastVersionName,
|
|
174
|
-
})
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
exports.getVersionBanner = getVersionBanner;
|
|
168
|
+
function getVersionBadge({ versionName, versionNames, options, }) {
|
|
169
|
+
const versionBadgeOption = options.versions[versionName]?.badge;
|
|
170
|
+
// If site is not versioned or only one version is included
|
|
171
|
+
// we don't show the version badge by default
|
|
172
|
+
// See https://github.com/facebook/docusaurus/issues/3362
|
|
173
|
+
const versionBadgeDefault = versionNames.length !== 1;
|
|
174
|
+
return versionBadgeOption ?? versionBadgeDefault;
|
|
175
|
+
}
|
|
176
|
+
exports.getVersionBadge = getVersionBadge;
|
|
177
|
+
function getVersionClassName({ versionName, options, }) {
|
|
178
|
+
const versionClassNameOption = options.versions[versionName]?.className;
|
|
179
|
+
const versionClassNameDefault = `docs-version-${versionName}`;
|
|
180
|
+
return versionClassNameOption ?? versionClassNameDefault;
|
|
175
181
|
}
|
|
176
182
|
function createVersionMetadata({ versionName, versionNames, lastVersionName, context, options, }) {
|
|
177
|
-
|
|
178
|
-
const { sidebarFilePath, contentPath, contentPathLocalized, } = getVersionMetadataPaths({
|
|
179
|
-
versionName,
|
|
180
|
-
context,
|
|
181
|
-
options,
|
|
182
|
-
});
|
|
183
|
+
const { sidebarFilePath, contentPath, contentPathLocalized } = getVersionMetadataPaths({ versionName, context, options });
|
|
183
184
|
const isLast = versionName === lastVersionName;
|
|
184
185
|
// retro-compatible values
|
|
185
186
|
const defaultVersionLabel = versionName === constants_1.CURRENT_VERSION_NAME ? 'Next' : versionName;
|
|
@@ -190,10 +191,10 @@ function createVersionMetadata({ versionName, versionNames, lastVersionName, con
|
|
|
190
191
|
return versionName === constants_1.CURRENT_VERSION_NAME ? 'next' : versionName;
|
|
191
192
|
}
|
|
192
193
|
const defaultVersionPathPart = getDefaultVersionPathPart();
|
|
193
|
-
const versionOptions =
|
|
194
|
-
const versionLabel =
|
|
195
|
-
const versionPathPart =
|
|
196
|
-
const versionPath = utils_1.normalizeUrl([
|
|
194
|
+
const versionOptions = options.versions[versionName] ?? {};
|
|
195
|
+
const versionLabel = versionOptions.label ?? defaultVersionLabel;
|
|
196
|
+
const versionPathPart = versionOptions.path ?? defaultVersionPathPart;
|
|
197
|
+
const versionPath = (0, utils_1.normalizeUrl)([
|
|
197
198
|
context.baseUrl,
|
|
198
199
|
options.routeBasePath,
|
|
199
200
|
versionPathPart,
|
|
@@ -206,18 +207,24 @@ function createVersionMetadata({ versionName, versionNames, lastVersionName, con
|
|
|
206
207
|
});
|
|
207
208
|
// Because /docs/:route` should always be after `/docs/versionName/:route`.
|
|
208
209
|
const routePriority = versionPathPart === '' ? -1 : undefined;
|
|
210
|
+
// the path that will be used to refer the docs tags
|
|
211
|
+
// example below will be using /docs/tags
|
|
212
|
+
const tagsPath = (0, utils_1.normalizeUrl)([versionPath, options.tagsBasePath]);
|
|
209
213
|
return {
|
|
210
214
|
versionName,
|
|
211
215
|
versionLabel,
|
|
212
216
|
versionPath,
|
|
213
|
-
|
|
214
|
-
|
|
217
|
+
tagsPath,
|
|
218
|
+
versionEditUrl: versionEditUrls?.versionEditUrl,
|
|
219
|
+
versionEditUrlLocalized: versionEditUrls?.versionEditUrlLocalized,
|
|
215
220
|
versionBanner: getVersionBanner({
|
|
216
221
|
versionName,
|
|
217
222
|
versionNames,
|
|
218
223
|
lastVersionName,
|
|
219
224
|
options,
|
|
220
225
|
}),
|
|
226
|
+
versionBadge: getVersionBadge({ versionName, versionNames, options }),
|
|
227
|
+
versionClassName: getVersionClassName({ versionName, options }),
|
|
221
228
|
isLast,
|
|
222
229
|
routePriority,
|
|
223
230
|
sidebarFilePath,
|
|
@@ -225,25 +232,26 @@ function createVersionMetadata({ versionName, versionNames, lastVersionName, con
|
|
|
225
232
|
contentPathLocalized,
|
|
226
233
|
};
|
|
227
234
|
}
|
|
228
|
-
function checkVersionMetadataPaths({ versionMetadata, context, }) {
|
|
235
|
+
async function checkVersionMetadataPaths({ versionMetadata, context, }) {
|
|
229
236
|
const { versionName, contentPath, sidebarFilePath } = versionMetadata;
|
|
230
237
|
const { siteDir } = context;
|
|
231
238
|
const isCurrentVersion = versionName === constants_1.CURRENT_VERSION_NAME;
|
|
232
|
-
if (!fs_extra_1.default.
|
|
239
|
+
if (!(await fs_extra_1.default.pathExists(contentPath))) {
|
|
233
240
|
throw new Error(`The docs folder does not exist for version "${versionName}". A docs folder is expected to be found at ${path_1.default.relative(siteDir, contentPath)}.`);
|
|
234
241
|
}
|
|
235
|
-
// If the current version defines a path to a sidebar file
|
|
236
|
-
// Note: for versioned sidebars, the file may not exist (as
|
|
242
|
+
// If the current version defines a path to a sidebar file that does not
|
|
243
|
+
// exist, we throw! Note: for versioned sidebars, the file may not exist (as
|
|
244
|
+
// we prefer to not create it rather than to create an empty file)
|
|
237
245
|
// See https://github.com/facebook/docusaurus/issues/3366
|
|
238
246
|
// See https://github.com/facebook/docusaurus/pull/4775
|
|
239
247
|
if (isCurrentVersion &&
|
|
240
248
|
typeof sidebarFilePath === 'string' &&
|
|
241
|
-
!fs_extra_1.default.
|
|
249
|
+
!(await fs_extra_1.default.pathExists(sidebarFilePath))) {
|
|
242
250
|
throw new Error(`The path to the sidebar file does not exist at "${path_1.default.relative(siteDir, sidebarFilePath)}".
|
|
243
251
|
Please set the docs "sidebarPath" field in your config file to:
|
|
244
252
|
- a sidebars path that exists
|
|
245
253
|
- false: to disable the sidebar
|
|
246
|
-
- undefined: for Docusaurus
|
|
254
|
+
- undefined: for Docusaurus to generate it automatically`);
|
|
247
255
|
}
|
|
248
256
|
}
|
|
249
257
|
// TODO for retrocompatibility with existing behavior
|
|
@@ -253,17 +261,15 @@ function getDefaultLastVersionName(versionNames) {
|
|
|
253
261
|
if (versionNames.length === 1) {
|
|
254
262
|
return versionNames[0];
|
|
255
263
|
}
|
|
256
|
-
|
|
257
|
-
return versionNames.filter((versionName) => versionName !== constants_1.CURRENT_VERSION_NAME)[0];
|
|
258
|
-
}
|
|
264
|
+
return versionNames.filter((versionName) => versionName !== constants_1.CURRENT_VERSION_NAME)[0];
|
|
259
265
|
}
|
|
260
266
|
function checkVersionsOptions(availableVersionNames, options) {
|
|
261
267
|
const availableVersionNamesMsg = `Available version names are: ${availableVersionNames.join(', ')}`;
|
|
262
268
|
if (options.lastVersion &&
|
|
263
269
|
!availableVersionNames.includes(options.lastVersion)) {
|
|
264
|
-
throw new Error(`Docs option lastVersion
|
|
270
|
+
throw new Error(`Docs option lastVersion: ${options.lastVersion} is invalid. ${availableVersionNamesMsg}`);
|
|
265
271
|
}
|
|
266
|
-
const unknownVersionConfigNames = lodash_1.difference(Object.keys(options.versions), availableVersionNames);
|
|
272
|
+
const unknownVersionConfigNames = lodash_1.default.difference(Object.keys(options.versions), availableVersionNames);
|
|
267
273
|
if (unknownVersionConfigNames.length > 0) {
|
|
268
274
|
throw new Error(`Invalid docs option "versions": unknown versions (${unknownVersionConfigNames.join(',')}) found. ${availableVersionNamesMsg}`);
|
|
269
275
|
}
|
|
@@ -271,7 +277,7 @@ function checkVersionsOptions(availableVersionNames, options) {
|
|
|
271
277
|
if (options.onlyIncludeVersions.length === 0) {
|
|
272
278
|
throw new Error(`Invalid docs option "onlyIncludeVersions": an empty array is not allowed, at least one version is needed.`);
|
|
273
279
|
}
|
|
274
|
-
const unknownOnlyIncludeVersionNames = lodash_1.difference(options.onlyIncludeVersions, availableVersionNames);
|
|
280
|
+
const unknownOnlyIncludeVersionNames = lodash_1.default.difference(options.onlyIncludeVersions, availableVersionNames);
|
|
275
281
|
if (unknownOnlyIncludeVersionNames.length > 0) {
|
|
276
282
|
throw new Error(`Invalid docs option "onlyIncludeVersions": unknown versions (${unknownOnlyIncludeVersionNames.join(',')}) found. ${availableVersionNamesMsg}`);
|
|
277
283
|
}
|
|
@@ -281,24 +287,23 @@ function checkVersionsOptions(availableVersionNames, options) {
|
|
|
281
287
|
}
|
|
282
288
|
}
|
|
283
289
|
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
290
|
+
/**
|
|
291
|
+
* Filter versions according to provided options.
|
|
292
|
+
* Note: we preserve the order in which versions are provided;
|
|
293
|
+
* the order of the onlyIncludeVersions array does not matter
|
|
294
|
+
*/
|
|
287
295
|
function filterVersions(versionNamesUnfiltered, options) {
|
|
288
296
|
if (options.onlyIncludeVersions) {
|
|
289
297
|
return versionNamesUnfiltered.filter((name) => (options.onlyIncludeVersions || []).includes(name));
|
|
290
298
|
}
|
|
291
|
-
|
|
292
|
-
return versionNamesUnfiltered;
|
|
293
|
-
}
|
|
299
|
+
return versionNamesUnfiltered;
|
|
294
300
|
}
|
|
295
|
-
|
|
296
|
-
function readVersionsMetadata({ context, options, }) {
|
|
297
|
-
|
|
298
|
-
const versionNamesUnfiltered = readVersionNames(context.siteDir, options);
|
|
301
|
+
exports.filterVersions = filterVersions;
|
|
302
|
+
async function readVersionsMetadata({ context, options, }) {
|
|
303
|
+
const versionNamesUnfiltered = await readVersionNames(context.siteDir, options);
|
|
299
304
|
checkVersionsOptions(versionNamesUnfiltered, options);
|
|
300
305
|
const versionNames = filterVersions(versionNamesUnfiltered, options);
|
|
301
|
-
const lastVersionName =
|
|
306
|
+
const lastVersionName = options.lastVersion ?? getDefaultLastVersionName(versionNames);
|
|
302
307
|
const versionsMetadata = versionNames.map((versionName) => createVersionMetadata({
|
|
303
308
|
versionName,
|
|
304
309
|
versionNames,
|
|
@@ -306,7 +311,7 @@ function readVersionsMetadata({ context, options, }) {
|
|
|
306
311
|
context,
|
|
307
312
|
options,
|
|
308
313
|
}));
|
|
309
|
-
versionsMetadata.
|
|
314
|
+
await Promise.all(versionsMetadata.map((versionMetadata) => checkVersionMetadataPaths({ versionMetadata, context })));
|
|
310
315
|
return versionsMetadata;
|
|
311
316
|
}
|
|
312
317
|
exports.readVersionsMetadata = readVersionsMetadata;
|
package/package.json
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/plugin-content-docs",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.17",
|
|
4
4
|
"description": "Docs plugin for Docusaurus.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
"./client": "./lib/client/index.js",
|
|
8
|
+
"./server": "./lib/server-export.js",
|
|
9
|
+
".": "./lib/index.js"
|
|
10
|
+
},
|
|
6
11
|
"types": "src/plugin-content-docs.d.ts",
|
|
7
12
|
"scripts": {
|
|
8
13
|
"build": "tsc",
|
|
@@ -17,41 +22,39 @@
|
|
|
17
22
|
"directory": "packages/docusaurus-plugin-content-docs"
|
|
18
23
|
},
|
|
19
24
|
"license": "MIT",
|
|
20
|
-
"devDependencies": {
|
|
21
|
-
"@docusaurus/module-type-aliases": "2.0.0-beta.15a2b59f9",
|
|
22
|
-
"@types/js-yaml": "^4.0.0",
|
|
23
|
-
"@types/picomatch": "^2.2.1",
|
|
24
|
-
"commander": "^5.1.0",
|
|
25
|
-
"picomatch": "^2.1.1"
|
|
26
|
-
},
|
|
27
25
|
"dependencies": {
|
|
28
|
-
"@docusaurus/core": "2.0.0-beta.
|
|
29
|
-
"@docusaurus/
|
|
30
|
-
"@docusaurus/
|
|
31
|
-
"@docusaurus/utils": "2.0.0-beta.
|
|
32
|
-
"@docusaurus/utils-validation": "2.0.0-beta.
|
|
33
|
-
"chalk": "^4.1.1",
|
|
26
|
+
"@docusaurus/core": "2.0.0-beta.17",
|
|
27
|
+
"@docusaurus/logger": "2.0.0-beta.17",
|
|
28
|
+
"@docusaurus/mdx-loader": "2.0.0-beta.17",
|
|
29
|
+
"@docusaurus/utils": "2.0.0-beta.17",
|
|
30
|
+
"@docusaurus/utils-validation": "2.0.0-beta.17",
|
|
34
31
|
"combine-promises": "^1.1.0",
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"import-fresh": "^3.2.2",
|
|
40
|
-
"js-yaml": "^4.0.0",
|
|
41
|
-
"loader-utils": "^1.2.3",
|
|
42
|
-
"lodash": "^4.17.20",
|
|
32
|
+
"fs-extra": "^10.0.1",
|
|
33
|
+
"import-fresh": "^3.3.0",
|
|
34
|
+
"js-yaml": "^4.1.0",
|
|
35
|
+
"lodash": "^4.17.21",
|
|
43
36
|
"remark-admonitions": "^1.2.1",
|
|
44
|
-
"
|
|
45
|
-
"tslib": "^2.2.0",
|
|
37
|
+
"tslib": "^2.3.1",
|
|
46
38
|
"utility-types": "^3.10.0",
|
|
47
|
-
"webpack": "^5.
|
|
39
|
+
"webpack": "^5.69.1"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@docusaurus/module-type-aliases": "2.0.0-beta.17",
|
|
43
|
+
"@docusaurus/types": "2.0.0-beta.17",
|
|
44
|
+
"@types/js-yaml": "^4.0.5",
|
|
45
|
+
"@types/picomatch": "^2.3.0",
|
|
46
|
+
"commander": "^5.1.0",
|
|
47
|
+
"escape-string-regexp": "^4.0.0",
|
|
48
|
+
"picomatch": "^2.3.1",
|
|
49
|
+
"shelljs": "^0.8.5",
|
|
50
|
+
"utility-types": "^3.10.0"
|
|
48
51
|
},
|
|
49
52
|
"peerDependencies": {
|
|
50
53
|
"react": "^16.8.4 || ^17.0.0",
|
|
51
54
|
"react-dom": "^16.8.4 || ^17.0.0"
|
|
52
55
|
},
|
|
53
56
|
"engines": {
|
|
54
|
-
"node": ">=
|
|
57
|
+
"node": ">=14"
|
|
55
58
|
},
|
|
56
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "0032c0b0480083227af2e1b4da2d3ee6ce992403"
|
|
57
60
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
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 {CategoryGeneratedIndexMetadata, DocMetadataBase} from './types';
|
|
9
|
+
import type {SidebarItemCategoryWithGeneratedIndex} from './sidebars/types';
|
|
10
|
+
import {type SidebarsUtils, toNavigationLink} from './sidebars/utils';
|
|
11
|
+
import {createDocsByIdIndex} from './docs';
|
|
12
|
+
|
|
13
|
+
function getCategoryGeneratedIndexMetadata({
|
|
14
|
+
category,
|
|
15
|
+
sidebarsUtils,
|
|
16
|
+
docsById,
|
|
17
|
+
}: {
|
|
18
|
+
category: SidebarItemCategoryWithGeneratedIndex;
|
|
19
|
+
sidebarsUtils: SidebarsUtils;
|
|
20
|
+
docsById: Record<string, DocMetadataBase>;
|
|
21
|
+
}): CategoryGeneratedIndexMetadata {
|
|
22
|
+
const {sidebarName, previous, next} =
|
|
23
|
+
sidebarsUtils.getCategoryGeneratedIndexNavigation(category.link.permalink);
|
|
24
|
+
if (!sidebarName) {
|
|
25
|
+
throw new Error('unexpected');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
title: category.link.title ?? category.label,
|
|
30
|
+
description: category.link.description,
|
|
31
|
+
image: category.link.image,
|
|
32
|
+
keywords: category.link.keywords,
|
|
33
|
+
slug: category.link.slug,
|
|
34
|
+
permalink: category.link.permalink,
|
|
35
|
+
sidebar: sidebarName,
|
|
36
|
+
previous: toNavigationLink(previous, docsById),
|
|
37
|
+
next: toNavigationLink(next, docsById),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function getCategoryGeneratedIndexMetadataList({
|
|
42
|
+
docs,
|
|
43
|
+
sidebarsUtils,
|
|
44
|
+
}: {
|
|
45
|
+
sidebarsUtils: SidebarsUtils;
|
|
46
|
+
docs: DocMetadataBase[];
|
|
47
|
+
}): CategoryGeneratedIndexMetadata[] {
|
|
48
|
+
const docsById = createDocsByIdIndex(docs);
|
|
49
|
+
|
|
50
|
+
const categoryGeneratedIndexItems =
|
|
51
|
+
sidebarsUtils.getCategoryGeneratedIndexList();
|
|
52
|
+
return categoryGeneratedIndexItems.map((category) =>
|
|
53
|
+
getCategoryGeneratedIndexMetadata({
|
|
54
|
+
category,
|
|
55
|
+
sidebarsUtils,
|
|
56
|
+
docsById,
|
|
57
|
+
}),
|
|
58
|
+
);
|
|
59
|
+
}
|
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,63 +32,37 @@ 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.
|
|
50
|
+
await fs.ensureDir(path.dirname(newSidebarFile));
|
|
51
|
+
await fs.writeFile(
|
|
77
52
|
newSidebarFile,
|
|
78
|
-
`${JSON.stringify(
|
|
53
|
+
`${JSON.stringify(sidebars, null, 2)}\n`,
|
|
79
54
|
'utf8',
|
|
80
55
|
);
|
|
81
56
|
}
|
|
82
57
|
}
|
|
83
58
|
|
|
84
59
|
// Tests depend on non-default export for mocking.
|
|
85
|
-
|
|
86
|
-
export function cliDocsVersionCommand(
|
|
60
|
+
export async function cliDocsVersionCommand(
|
|
87
61
|
version: string | null | undefined,
|
|
88
62
|
siteDir: string,
|
|
89
63
|
pluginId: string,
|
|
90
|
-
options: PathOptions,
|
|
91
|
-
): void {
|
|
64
|
+
options: PathOptions & SidebarOptions,
|
|
65
|
+
): Promise<void> {
|
|
92
66
|
// It wouldn't be very user-friendly to show a [default] log prefix,
|
|
93
67
|
// so we use [docs] instead of [default]
|
|
94
68
|
const pluginIdLogPrefix =
|
|
@@ -130,8 +104,8 @@ export function cliDocsVersionCommand(
|
|
|
130
104
|
// Load existing versions.
|
|
131
105
|
let versions = [];
|
|
132
106
|
const versionsJSONFile = getVersionsFilePath(siteDir, pluginId);
|
|
133
|
-
if (fs.
|
|
134
|
-
versions = JSON.parse(fs.
|
|
107
|
+
if (await fs.pathExists(versionsJSONFile)) {
|
|
108
|
+
versions = JSON.parse(await fs.readFile(versionsJSONFile, 'utf8'));
|
|
135
109
|
}
|
|
136
110
|
|
|
137
111
|
// Check if version already exists.
|
|
@@ -146,15 +120,18 @@ export function cliDocsVersionCommand(
|
|
|
146
120
|
// Copy docs files.
|
|
147
121
|
const docsDir = path.join(siteDir, docsPath);
|
|
148
122
|
|
|
149
|
-
if (
|
|
123
|
+
if (
|
|
124
|
+
(await fs.pathExists(docsDir)) &&
|
|
125
|
+
(await fs.readdir(docsDir)).length > 0
|
|
126
|
+
) {
|
|
150
127
|
const versionedDir = getVersionedDocsDirPath(siteDir, pluginId);
|
|
151
128
|
const newVersionDir = path.join(versionedDir, `version-${version}`);
|
|
152
|
-
fs.
|
|
129
|
+
await fs.copy(docsDir, newVersionDir);
|
|
153
130
|
} else {
|
|
154
131
|
throw new Error(`${pluginIdLogPrefix}: there is no docs to version!`);
|
|
155
132
|
}
|
|
156
133
|
|
|
157
|
-
createVersionedSidebarFile({
|
|
134
|
+
await createVersionedSidebarFile({
|
|
158
135
|
siteDir,
|
|
159
136
|
pluginId,
|
|
160
137
|
version,
|
|
@@ -163,8 +140,11 @@ export function cliDocsVersionCommand(
|
|
|
163
140
|
|
|
164
141
|
// Update versions.json file.
|
|
165
142
|
versions.unshift(version);
|
|
166
|
-
fs.
|
|
167
|
-
fs.
|
|
143
|
+
await fs.ensureDir(path.dirname(versionsJSONFile));
|
|
144
|
+
await fs.writeFile(
|
|
145
|
+
versionsJSONFile,
|
|
146
|
+
`${JSON.stringify(versions, null, 2)}\n`,
|
|
147
|
+
);
|
|
168
148
|
|
|
169
|
-
|
|
149
|
+
logger.success`name=${pluginIdLogPrefix}: version name=${version} created!`;
|
|
170
150
|
}
|