@docusaurus/plugin-content-docs 2.0.0-beta.1ec2c95e3 → 2.0.0-beta.21
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 +58 -72
- package/lib/client/docsClientUtils.d.ts +9 -28
- package/lib/client/docsClientUtils.js +34 -43
- package/lib/client/index.d.ts +22 -0
- package/lib/client/index.js +59 -0
- package/lib/constants.d.ts +4 -0
- package/lib/constants.js +4 -1
- package/lib/docs.d.ts +32 -3
- package/lib/docs.js +164 -63
- package/{src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docusaurus.config.js → lib/frontMatter.d.ts} +4 -8
- package/lib/{docFrontMatter.js → frontMatter.js} +12 -3
- package/lib/globalData.d.ts +3 -3
- package/lib/globalData.js +35 -6
- package/lib/index.d.ts +3 -3
- package/lib/index.js +123 -149
- 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 +6 -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 +54 -13
- package/lib/props.d.ts +7 -2
- package/lib/props.js +70 -14
- 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 +11 -0
- package/lib/sidebars/postProcessor.js +81 -0
- package/lib/sidebars/processor.d.ts +10 -0
- package/lib/sidebars/processor.js +79 -0
- package/lib/sidebars/types.d.ts +178 -0
- package/lib/{docFrontMatter.d.ts → sidebars/types.js} +2 -2
- 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 +142 -0
- package/lib/slug.d.ts +5 -4
- package/lib/slug.js +28 -18
- 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 +83 -93
- package/lib/types.d.ts +13 -184
- package/lib/versions/files.d.ts +51 -0
- package/lib/versions/files.js +143 -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 +34 -29
- package/src/categoryGeneratedIndex.ts +60 -0
- package/src/cli.ts +85 -114
- package/src/client/docsClientUtils.ts +44 -71
- package/src/client/index.ts +106 -0
- package/src/constants.ts +4 -2
- package/{types.d.ts → src/deps.d.ts} +1 -1
- package/src/docs.ts +228 -65
- package/src/{docFrontMatter.ts → frontMatter.ts} +20 -10
- package/src/globalData.ts +57 -7
- package/src/index.ts +179 -216
- package/src/lastUpdate.ts +26 -37
- package/src/markdown/index.ts +10 -16
- package/src/markdown/linkify.ts +7 -4
- package/src/numberPrefix.ts +19 -26
- package/src/options.ts +59 -18
- package/src/plugin-content-docs.d.ts +642 -89
- package/src/props.ts +103 -21
- package/src/routes.ts +159 -0
- package/src/server-export.ts +22 -0
- package/src/sidebars/README.md +10 -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 +112 -0
- package/src/sidebars/processor.ts +123 -0
- package/src/sidebars/types.ts +275 -0
- package/src/sidebars/utils.ts +390 -0
- package/src/sidebars/validation.ts +178 -0
- package/src/slug.ts +41 -22
- package/src/tags.ts +20 -0
- package/src/translations.ts +129 -123
- package/src/types.ts +18 -249
- package/src/versions/files.ts +220 -0
- package/src/versions/index.ts +247 -0
- package/src/versions/validation.ts +115 -0
- 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/lib/theme/hooks/useDocs.d.ts +0 -20
- package/lib/theme/hooks/useDocs.js +0 -75
- package/lib/versions.d.ts +0 -16
- package/lib/versions.js +0 -319
- 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 -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/partialAutogeneratedSidebars.js +0 -23
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars2.js +0 -16
- 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 -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/src/theme/hooks/useDocs.ts +0 -103
- package/src/versions.ts +0 -572
- package/tsconfig.json +0 -9
|
@@ -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]}
|
|
@@ -58,7 +46,7 @@ export function getActivePlugin(
|
|
|
58
46
|
if (!activePlugin && options.failfast) {
|
|
59
47
|
throw new Error(
|
|
60
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(
|
|
61
|
-
|
|
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,22 +117,15 @@ export const getActiveDocContext = (
|
|
|
137
117
|
activeDoc,
|
|
138
118
|
alternateDocVersions: alternateVersionDocs,
|
|
139
119
|
};
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export type DocVersionSuggestions = {
|
|
143
|
-
// suggest the latest version
|
|
144
|
-
latestVersionSuggestion: GlobalVersion;
|
|
145
|
-
// suggest the same doc, in latest version (if exist)
|
|
146
|
-
latestDocSuggestion?: GlobalDoc;
|
|
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
128
|
const latestDocSuggestion: GlobalDoc | undefined =
|
|
156
|
-
activeDocContext
|
|
129
|
+
activeDocContext.alternateDocVersions[latestVersion.name];
|
|
157
130
|
return {latestDocSuggestion, latestVersionSuggestion: latestVersion};
|
|
158
|
-
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
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') as
|
|
38
|
+
| {
|
|
39
|
+
[pluginId: string]: GlobalPluginData;
|
|
40
|
+
}
|
|
41
|
+
| undefined) ?? StableEmptyObject;
|
|
42
|
+
|
|
43
|
+
export const useDocsData = (pluginId: string | undefined): GlobalPluginData =>
|
|
44
|
+
usePluginData('docusaurus-plugin-content-docs', pluginId, {
|
|
45
|
+
failfast: true,
|
|
46
|
+
}) as GlobalPluginData;
|
|
47
|
+
|
|
48
|
+
// TODO this feature should be provided by docusaurus core
|
|
49
|
+
export function useActivePlugin(
|
|
50
|
+
options: UseDataOptions = {},
|
|
51
|
+
): ActivePlugin | undefined {
|
|
52
|
+
const data = useAllDocsData();
|
|
53
|
+
const {pathname} = useLocation();
|
|
54
|
+
return getActivePlugin(data, pathname, options);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function useActivePluginAndVersion(
|
|
58
|
+
options: UseDataOptions = {},
|
|
59
|
+
):
|
|
60
|
+
| {activePlugin: ActivePlugin; activeVersion: GlobalVersion | undefined}
|
|
61
|
+
| undefined {
|
|
62
|
+
const activePlugin = useActivePlugin(options);
|
|
63
|
+
const {pathname} = useLocation();
|
|
64
|
+
if (!activePlugin) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
const activeVersion = getActiveVersion(activePlugin.pluginData, pathname);
|
|
68
|
+
return {
|
|
69
|
+
activePlugin,
|
|
70
|
+
activeVersion,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function useVersions(pluginId: string | undefined): GlobalVersion[] {
|
|
75
|
+
const data = useDocsData(pluginId);
|
|
76
|
+
return data.versions;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function useLatestVersion(pluginId: string | undefined): GlobalVersion {
|
|
80
|
+
const data = useDocsData(pluginId);
|
|
81
|
+
return getLatestVersion(data);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function useActiveVersion(
|
|
85
|
+
pluginId: string | undefined,
|
|
86
|
+
): GlobalVersion | undefined {
|
|
87
|
+
const data = useDocsData(pluginId);
|
|
88
|
+
const {pathname} = useLocation();
|
|
89
|
+
return getActiveVersion(data, pathname);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function useActiveDocContext(
|
|
93
|
+
pluginId: string | undefined,
|
|
94
|
+
): ActiveDocContext {
|
|
95
|
+
const data = useDocsData(pluginId);
|
|
96
|
+
const {pathname} = useLocation();
|
|
97
|
+
return getActiveDocContext(data, pathname);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function useDocVersionSuggestions(
|
|
101
|
+
pluginId: string | undefined,
|
|
102
|
+
): DocVersionSuggestions {
|
|
103
|
+
const data = useDocsData(pluginId);
|
|
104
|
+
const {pathname} = useLocation();
|
|
105
|
+
return getDocVersionSuggestions(data, pathname);
|
|
106
|
+
}
|
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';
|