@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.
Files changed (214) hide show
  1. package/lib/categoryGeneratedIndex.d.ts +12 -0
  2. package/lib/categoryGeneratedIndex.js +35 -0
  3. package/lib/cli.d.ts +2 -2
  4. package/lib/cli.js +37 -51
  5. package/lib/client/docsClientUtils.d.ts +6 -26
  6. package/lib/client/docsClientUtils.js +28 -34
  7. package/lib/{theme/hooks/useDocs.d.ts → client/index.d.ts} +4 -3
  8. package/lib/{theme/hooks/useDocs.js → client/index.js} +28 -25
  9. package/lib/docs.d.ts +31 -4
  10. package/lib/docs.js +160 -54
  11. package/{src/__tests__/__fixtures__/simple-site/docusaurus.config.js → lib/frontMatter.d.ts} +4 -8
  12. package/lib/{docFrontMatter.js → frontMatter.js} +13 -6
  13. package/lib/globalData.d.ts +2 -2
  14. package/lib/globalData.js +32 -3
  15. package/lib/index.d.ts +4 -3
  16. package/lib/index.js +123 -136
  17. package/lib/lastUpdate.d.ts +4 -6
  18. package/lib/lastUpdate.js +22 -26
  19. package/lib/markdown/index.d.ts +3 -6
  20. package/lib/markdown/index.js +3 -3
  21. package/lib/markdown/linkify.d.ts +1 -1
  22. package/lib/markdown/linkify.js +7 -3
  23. package/lib/numberPrefix.d.ts +1 -1
  24. package/lib/numberPrefix.js +16 -21
  25. package/lib/options.d.ts +3 -5
  26. package/lib/options.js +55 -19
  27. package/lib/props.d.ts +7 -2
  28. package/lib/props.js +70 -14
  29. package/lib/routes.d.ts +28 -0
  30. package/lib/routes.js +110 -0
  31. package/lib/server-export.d.ts +8 -0
  32. package/lib/server-export.js +23 -0
  33. package/lib/{sidebarItemsGenerator.d.ts → sidebars/generator.d.ts} +1 -6
  34. package/lib/sidebars/generator.js +209 -0
  35. package/lib/sidebars/index.d.ts +13 -0
  36. package/lib/sidebars/index.js +94 -0
  37. package/lib/sidebars/normalization.d.ts +13 -0
  38. package/lib/sidebars/normalization.js +55 -0
  39. package/lib/sidebars/postProcessor.d.ts +8 -0
  40. package/lib/sidebars/postProcessor.js +65 -0
  41. package/lib/sidebars/processor.d.ts +10 -0
  42. package/lib/sidebars/processor.js +79 -0
  43. package/lib/sidebars/types.d.ts +174 -0
  44. package/{src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars2.js → lib/sidebars/types.js} +2 -10
  45. package/lib/sidebars/utils.d.ts +54 -0
  46. package/lib/sidebars/utils.js +255 -0
  47. package/lib/sidebars/validation.d.ts +11 -0
  48. package/lib/sidebars/validation.js +138 -0
  49. package/lib/slug.d.ts +6 -4
  50. package/lib/slug.js +29 -19
  51. package/{src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docusaurus.config.js → lib/tags.d.ts} +2 -8
  52. package/lib/tags.js +21 -0
  53. package/lib/translations.d.ts +2 -2
  54. package/lib/translations.js +89 -49
  55. package/lib/types.d.ts +60 -130
  56. package/lib/versions.d.ts +29 -4
  57. package/lib/versions.js +134 -97
  58. package/package.json +30 -26
  59. package/src/categoryGeneratedIndex.ts +55 -0
  60. package/src/cli.ts +47 -63
  61. package/src/client/docsClientUtils.ts +38 -73
  62. package/src/{theme/hooks/useDocs.ts → client/index.ts} +16 -11
  63. package/{types.d.ts → src/deps.d.ts} +1 -1
  64. package/src/docs.ts +212 -46
  65. package/src/{docFrontMatter.ts → frontMatter.ts} +21 -26
  66. package/src/globalData.ts +53 -3
  67. package/src/index.ts +168 -178
  68. package/src/lastUpdate.ts +26 -33
  69. package/src/markdown/index.ts +10 -16
  70. package/src/markdown/linkify.ts +6 -2
  71. package/src/numberPrefix.ts +19 -26
  72. package/src/options.ts +60 -32
  73. package/src/plugin-content-docs.d.ts +263 -40
  74. package/src/props.ts +105 -21
  75. package/src/routes.ts +185 -0
  76. package/src/server-export.ts +24 -0
  77. package/src/sidebars/README.md +9 -0
  78. package/src/sidebars/generator.ts +292 -0
  79. package/src/sidebars/index.ts +120 -0
  80. package/src/sidebars/normalization.ts +85 -0
  81. package/src/sidebars/postProcessor.ts +89 -0
  82. package/src/sidebars/processor.ts +120 -0
  83. package/src/sidebars/types.ts +274 -0
  84. package/src/sidebars/utils.ts +388 -0
  85. package/src/sidebars/validation.ts +174 -0
  86. package/src/slug.ts +40 -23
  87. package/src/tags.ts +19 -0
  88. package/src/translations.ts +124 -66
  89. package/src/types.ts +67 -187
  90. package/src/versions.ts +205 -110
  91. package/lib/.tsbuildinfo +0 -4717
  92. package/lib/docFrontMatter.d.ts +0 -21
  93. package/lib/sidebarItemsGenerator.js +0 -211
  94. package/lib/sidebars.d.ts +0 -42
  95. package/lib/sidebars.js +0 -309
  96. package/src/__tests__/__fixtures__/bad-id-site/docs/invalid-id.md +0 -5
  97. package/src/__tests__/__fixtures__/bad-slug-on-doc-home-site/docs/docWithSlug.md +0 -5
  98. package/src/__tests__/__fixtures__/empty-site/docusaurus.config.js +0 -16
  99. package/src/__tests__/__fixtures__/empty-site/sidebars.json +0 -1
  100. package/src/__tests__/__fixtures__/sidebars/sidebars-category-shorthand.js +0 -34
  101. package/src/__tests__/__fixtures__/sidebars/sidebars-category-wrong-items.json +0 -11
  102. package/src/__tests__/__fixtures__/sidebars/sidebars-category-wrong-label.json +0 -11
  103. package/src/__tests__/__fixtures__/sidebars/sidebars-category.js +0 -44
  104. package/src/__tests__/__fixtures__/sidebars/sidebars-collapsed-first-level.json +0 -20
  105. package/src/__tests__/__fixtures__/sidebars/sidebars-collapsed.json +0 -21
  106. package/src/__tests__/__fixtures__/sidebars/sidebars-doc-id-not-string.json +0 -10
  107. package/src/__tests__/__fixtures__/sidebars/sidebars-first-level-not-category.js +0 -20
  108. package/src/__tests__/__fixtures__/sidebars/sidebars-link-wrong-href.json +0 -11
  109. package/src/__tests__/__fixtures__/sidebars/sidebars-link-wrong-label.json +0 -11
  110. package/src/__tests__/__fixtures__/sidebars/sidebars-link.json +0 -11
  111. package/src/__tests__/__fixtures__/sidebars/sidebars-unknown-type.json +0 -14
  112. package/src/__tests__/__fixtures__/sidebars/sidebars-wrong-field.json +0 -20
  113. package/src/__tests__/__fixtures__/sidebars/sidebars.json +0 -20
  114. package/src/__tests__/__fixtures__/simple-site/docs/foo/bar.md +0 -69
  115. package/src/__tests__/__fixtures__/simple-site/docs/foo/baz.md +0 -67
  116. package/src/__tests__/__fixtures__/simple-site/docs/headingAsTitle.md +0 -1
  117. package/src/__tests__/__fixtures__/simple-site/docs/hello.md +0 -52
  118. package/src/__tests__/__fixtures__/simple-site/docs/ipsum.md +0 -5
  119. package/src/__tests__/__fixtures__/simple-site/docs/lorem.md +0 -6
  120. package/src/__tests__/__fixtures__/simple-site/docs/rootAbsoluteSlug.md +0 -5
  121. package/src/__tests__/__fixtures__/simple-site/docs/rootRelativeSlug.md +0 -5
  122. package/src/__tests__/__fixtures__/simple-site/docs/rootResolvedSlug.md +0 -5
  123. package/src/__tests__/__fixtures__/simple-site/docs/rootTryToEscapeSlug.md +0 -5
  124. package/src/__tests__/__fixtures__/simple-site/docs/slugs/absoluteSlug.md +0 -5
  125. package/src/__tests__/__fixtures__/simple-site/docs/slugs/relativeSlug.md +0 -5
  126. package/src/__tests__/__fixtures__/simple-site/docs/slugs/resolvedSlug.md +0 -5
  127. package/src/__tests__/__fixtures__/simple-site/docs/slugs/tryToEscapeSlug.md +0 -5
  128. package/src/__tests__/__fixtures__/simple-site/sidebars.json +0 -23
  129. package/src/__tests__/__fixtures__/simple-site/wrong-sidebars.json +0 -7
  130. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/0-getting-started.md +0 -3
  131. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/1-installation.md +0 -3
  132. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/00_api-overview.md +0 -3
  133. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/01_Core APIs/0 --- Client API.md +0 -1
  134. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/01_Core APIs/1 --- Server API.md +0 -1
  135. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/0. Plugin API.md +0 -1
  136. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/1. Theme API.md +0 -1
  137. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/_category_.yml +0 -1
  138. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/03_api-end.md +0 -3
  139. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/_category_.json +0 -3
  140. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/0-guide2.5.md +0 -8
  141. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/02-guide2.md +0 -7
  142. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/_category_.json +0 -3
  143. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/a-guide4.md +0 -7
  144. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/b-guide5.md +0 -7
  145. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/guide3.md +0 -8
  146. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/z-guide1.md +0 -8
  147. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars.js +0 -23
  148. package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-1.md +0 -7
  149. package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-2.md +0 -8
  150. package/src/__tests__/__fixtures__/site-with-doc-label/docusaurus.config.js +0 -14
  151. package/src/__tests__/__fixtures__/site-with-doc-label/sidebars.json +0 -14
  152. package/src/__tests__/__fixtures__/versioned-site/community/team.md +0 -1
  153. package/src/__tests__/__fixtures__/versioned-site/community_sidebars.json +0 -3
  154. package/src/__tests__/__fixtures__/versioned-site/community_versioned_docs/version-1.0.0/team.md +0 -1
  155. package/src/__tests__/__fixtures__/versioned-site/community_versioned_sidebars/version-1.0.0-sidebars.json +0 -3
  156. package/src/__tests__/__fixtures__/versioned-site/community_versions.json +0 -1
  157. package/src/__tests__/__fixtures__/versioned-site/docs/foo/bar.md +0 -4
  158. package/src/__tests__/__fixtures__/versioned-site/docs/hello.md +0 -1
  159. package/src/__tests__/__fixtures__/versioned-site/docs/slugs/absoluteSlug.md +0 -5
  160. package/src/__tests__/__fixtures__/versioned-site/docs/slugs/relativeSlug.md +0 -5
  161. package/src/__tests__/__fixtures__/versioned-site/docs/slugs/resolvedSlug.md +0 -5
  162. package/src/__tests__/__fixtures__/versioned-site/docs/slugs/tryToEscapeSlug.md +0 -5
  163. package/src/__tests__/__fixtures__/versioned-site/docusaurus.config.js +0 -18
  164. package/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md +0 -1
  165. package/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs-community/current/team.md +0 -5
  166. package/src/__tests__/__fixtures__/versioned-site/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md +0 -1
  167. package/src/__tests__/__fixtures__/versioned-site/sidebars.json +0 -10
  168. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/foo/bar.md +0 -4
  169. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/foo/baz.md +0 -1
  170. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/hello.md +0 -1
  171. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/foo/bar.md +0 -1
  172. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/hello.md +0 -1
  173. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootAbsoluteSlug.md +0 -5
  174. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootRelativeSlug.md +0 -5
  175. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootResolvedSlug.md +0 -5
  176. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootTryToEscapeSlug.md +0 -5
  177. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/absoluteSlug.md +0 -5
  178. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/relativeSlug.md +0 -5
  179. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/resolvedSlug.md +0 -5
  180. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/tryToEscapeSlug.md +0 -5
  181. package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.0-sidebars.json +0 -11
  182. package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.1-sidebars.json +0 -10
  183. package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-withSlugs-sidebars.json +0 -5
  184. package/src/__tests__/__fixtures__/versioned-site/versions.json +0 -5
  185. package/src/__tests__/__snapshots__/cli.test.ts.snap +0 -90
  186. package/src/__tests__/__snapshots__/index.test.ts.snap +0 -1907
  187. package/src/__tests__/__snapshots__/sidebars.test.ts.snap +0 -218
  188. package/src/__tests__/__snapshots__/translations.test.ts.snap +0 -487
  189. package/src/__tests__/cli.test.ts +0 -333
  190. package/src/__tests__/docFrontMatter.test.ts +0 -204
  191. package/src/__tests__/docs.test.ts +0 -875
  192. package/src/__tests__/index.test.ts +0 -1831
  193. package/src/__tests__/lastUpdate.test.ts +0 -68
  194. package/src/__tests__/numberPrefix.test.ts +0 -199
  195. package/src/__tests__/options.test.ts +0 -232
  196. package/src/__tests__/sidebarItemsGenerator.test.ts +0 -336
  197. package/src/__tests__/sidebars.test.ts +0 -638
  198. package/src/__tests__/slug.test.ts +0 -109
  199. package/src/__tests__/translations.test.ts +0 -159
  200. package/src/__tests__/versions.test.ts +0 -718
  201. package/src/client/__tests__/docsClientUtils.test.ts +0 -372
  202. package/src/markdown/__tests__/__fixtures__/docs/doc-localized.md +0 -1
  203. package/src/markdown/__tests__/__fixtures__/docs/doc1.md +0 -13
  204. package/src/markdown/__tests__/__fixtures__/docs/doc2.md +0 -12
  205. package/src/markdown/__tests__/__fixtures__/docs/doc4.md +0 -19
  206. package/src/markdown/__tests__/__fixtures__/docs/doc5.md +0 -6
  207. package/src/markdown/__tests__/__fixtures__/docs/subdir/doc3.md +0 -3
  208. package/src/markdown/__tests__/__fixtures__/versioned_docs/version-1.0.0/doc2.md +0 -7
  209. package/src/markdown/__tests__/__fixtures__/versioned_docs/version-1.0.0/subdir/doc1.md +0 -3
  210. package/src/markdown/__tests__/__snapshots__/linkify.test.ts.snap +0 -82
  211. package/src/markdown/__tests__/linkify.test.ts +0 -190
  212. package/src/sidebarItemsGenerator.ts +0 -307
  213. package/src/sidebars.ts +0 -489
  214. package/tsconfig.json +0 -9
@@ -1,1831 +0,0 @@
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
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
9
-
10
- import path from 'path';
11
- import {isMatch} from 'picomatch';
12
- import commander from 'commander';
13
- import {kebabCase, orderBy} from 'lodash';
14
-
15
- import fs from 'fs-extra';
16
- import pluginContentDocs from '../index';
17
- import {loadContext} from '@docusaurus/core/src/server/index';
18
- import {applyConfigureWebpack} from '@docusaurus/core/src/webpack/utils';
19
- import {RouteConfig} from '@docusaurus/types';
20
- import {posixPath} from '@docusaurus/utils';
21
- import {sortConfig} from '@docusaurus/core/src/server/plugins';
22
- import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants';
23
-
24
- import * as cliDocs from '../cli';
25
- import {OptionsSchema} from '../options';
26
- import {normalizePluginOptions} from '@docusaurus/utils-validation';
27
- import {
28
- DocMetadata,
29
- LoadedVersion,
30
- SidebarItem,
31
- SidebarItemsGeneratorOption,
32
- SidebarItemsGeneratorOptionArgs,
33
- } from '../types';
34
- import {toSidebarsProp} from '../props';
35
-
36
- import {validate} from 'webpack';
37
- import {DefaultSidebarItemsGenerator} from '../sidebarItemsGenerator';
38
- import {DisabledSidebars} from '../sidebars';
39
-
40
- function findDocById(version: LoadedVersion, unversionedId: string) {
41
- return version.docs.find((item) => item.unversionedId === unversionedId);
42
- }
43
- function getDocById(version: LoadedVersion, unversionedId: string) {
44
- const doc = findDocById(version, unversionedId);
45
- if (!doc) {
46
- throw new Error(
47
- `No doc found with id=${unversionedId} in version ${version.versionName}.
48
- Available ids=\n- ${version.docs.map((d) => d.unversionedId).join('\n- ')}`,
49
- );
50
- }
51
- return doc;
52
- }
53
-
54
- const defaultDocMetadata: Partial<DocMetadata> = {
55
- next: undefined,
56
- previous: undefined,
57
- editUrl: undefined,
58
- lastUpdatedAt: undefined,
59
- lastUpdatedBy: undefined,
60
- sidebar_label: undefined,
61
- formattedLastUpdatedAt: undefined,
62
- };
63
-
64
- const createFakeActions = (contentDir: string) => {
65
- const routeConfigs: RouteConfig[] = [];
66
- const dataContainer: Record<string, unknown> = {};
67
- const globalDataContainer: {pluginName?: {pluginId: unknown}} = {};
68
-
69
- const actions = {
70
- addRoute: (config: RouteConfig) => {
71
- routeConfigs.push(config);
72
- },
73
- createData: async (name: string, content: unknown) => {
74
- dataContainer[name] = content;
75
- return path.join(contentDir, name);
76
- },
77
- setGlobalData: (data: unknown) => {
78
- globalDataContainer.pluginName = {pluginId: data};
79
- },
80
- };
81
-
82
- // query by prefix, because files have a hash at the end
83
- // so it's not convenient to query by full filename
84
- const getCreatedDataByPrefix = (prefix: string) => {
85
- const entry = Object.entries(dataContainer).find(([key]) =>
86
- key.startsWith(prefix),
87
- );
88
- if (!entry) {
89
- throw new Error(`No created entry found for prefix=[${prefix}]
90
- Entries created:
91
- - ${Object.keys(dataContainer).join('\n- ')}
92
- `);
93
- }
94
- return JSON.parse(entry[1] as string);
95
- };
96
-
97
- // Extra fns useful for tests!
98
- const utils = {
99
- getGlobalData: () => globalDataContainer,
100
- getRouteConfigs: () => routeConfigs,
101
-
102
- checkVersionMetadataPropCreated: (version: LoadedVersion) => {
103
- const versionMetadataProp = getCreatedDataByPrefix(
104
- `version-${kebabCase(version.versionName)}-metadata-prop`,
105
- );
106
- expect(versionMetadataProp.docsSidebars).toEqual(toSidebarsProp(version));
107
- expect(versionMetadataProp.permalinkToSidebar).toEqual(
108
- version.permalinkToSidebar,
109
- );
110
- },
111
-
112
- expectSnapshot: () => {
113
- // Sort the route config like in src/server/plugins/index.ts for consistent snapshot ordering
114
- sortConfig(routeConfigs);
115
- expect(routeConfigs).not.toEqual([]);
116
- expect(routeConfigs).toMatchSnapshot('route config');
117
- expect(dataContainer).toMatchSnapshot('data');
118
- expect(globalDataContainer).toMatchSnapshot('global data');
119
- },
120
- };
121
-
122
- return {
123
- actions,
124
- utils,
125
- };
126
- };
127
-
128
- describe('sidebar', () => {
129
- test('site with wrong sidebar content', async () => {
130
- const siteDir = path.join(__dirname, '__fixtures__', 'simple-site');
131
- const context = await loadContext(siteDir);
132
- const sidebarPath = path.join(siteDir, 'wrong-sidebars.json');
133
- const plugin = pluginContentDocs(
134
- context,
135
- normalizePluginOptions(OptionsSchema, {
136
- sidebarPath,
137
- }),
138
- );
139
- await expect(plugin.loadContent!()).rejects.toThrowErrorMatchingSnapshot();
140
- });
141
-
142
- test('site with wrong sidebar file path', async () => {
143
- const siteDir = path.join(__dirname, '__fixtures__', 'site-with-doc-label');
144
- const context = await loadContext(siteDir);
145
-
146
- await expect(async () => {
147
- const plugin = pluginContentDocs(
148
- context,
149
- normalizePluginOptions(OptionsSchema, {
150
- sidebarPath: 'wrong-path-sidebar.json',
151
- }),
152
- );
153
- await plugin.loadContent!();
154
- }).rejects.toThrowErrorMatchingInlineSnapshot(`
155
- "The path to the sidebar file does not exist at [wrong-path-sidebar.json].
156
- Please set the docs [sidebarPath] field in your config file to:
157
- - a sidebars path that exists
158
- - false: to disable the sidebar
159
- - undefined: for Docusaurus generates it automatically"
160
- `);
161
- });
162
-
163
- test('site with undefined sidebar', async () => {
164
- const siteDir = path.join(__dirname, '__fixtures__', 'site-with-doc-label');
165
- const context = await loadContext(siteDir);
166
- const plugin = pluginContentDocs(
167
- context,
168
- normalizePluginOptions(OptionsSchema, {
169
- sidebarPath: undefined,
170
- }),
171
- );
172
- const result = await plugin.loadContent!();
173
-
174
- expect(result.loadedVersions).toHaveLength(1);
175
- expect(result.loadedVersions[0].sidebars).toMatchInlineSnapshot(`
176
- Object {
177
- "defaultSidebar": Array [
178
- Object {
179
- "id": "hello-1",
180
- "type": "doc",
181
- },
182
- Object {
183
- "id": "hello-2",
184
- "label": "Hello 2 From Doc",
185
- "type": "doc",
186
- },
187
- ],
188
- }
189
- `);
190
- });
191
-
192
- test('site with disabled sidebar', async () => {
193
- const siteDir = path.join(__dirname, '__fixtures__', 'site-with-doc-label');
194
- const context = await loadContext(siteDir);
195
- const plugin = pluginContentDocs(
196
- context,
197
- normalizePluginOptions(OptionsSchema, {
198
- sidebarPath: false,
199
- }),
200
- );
201
- const result = await plugin.loadContent!();
202
-
203
- expect(result.loadedVersions).toHaveLength(1);
204
- expect(result.loadedVersions[0].sidebars).toEqual(DisabledSidebars);
205
- });
206
- });
207
-
208
- describe('empty/no docs website', () => {
209
- const siteDir = path.join(__dirname, '__fixtures__', 'empty-site');
210
-
211
- test('no files in docs folder', async () => {
212
- const context = await loadContext(siteDir);
213
- await fs.ensureDir(path.join(siteDir, 'docs'));
214
- const plugin = pluginContentDocs(
215
- context,
216
- normalizePluginOptions(OptionsSchema, {}),
217
- );
218
- await expect(
219
- plugin.loadContent!(),
220
- ).rejects.toThrowErrorMatchingInlineSnapshot(
221
- `"Docs version current has no docs! At least one doc should exist at path=[docs]"`,
222
- );
223
- });
224
-
225
- test('docs folder does not exist', async () => {
226
- const context = await loadContext(siteDir);
227
- expect(() =>
228
- pluginContentDocs(
229
- context,
230
- normalizePluginOptions(OptionsSchema, {
231
- path: `path/doesnt/exist`,
232
- }),
233
- ),
234
- ).toThrowError(
235
- `The docs folder does not exist for version [current]. A docs folder is expected to be found at ${
236
- process.platform === 'win32'
237
- ? 'path\\doesnt\\exist'
238
- : 'path/doesnt/exist'
239
- }`,
240
- );
241
- });
242
- });
243
-
244
- describe('simple website', () => {
245
- async function loadSite() {
246
- const siteDir = path.join(__dirname, '__fixtures__', 'simple-site');
247
- const context = await loadContext(siteDir);
248
- const sidebarPath = path.join(siteDir, 'sidebars.json');
249
- const plugin = pluginContentDocs(
250
- context,
251
- normalizePluginOptions(OptionsSchema, {
252
- path: 'docs',
253
- sidebarPath,
254
- homePageId: 'hello',
255
- }),
256
- );
257
- const pluginContentDir = path.join(context.generatedFilesDir, plugin.name);
258
-
259
- return {siteDir, context, sidebarPath, plugin, pluginContentDir};
260
- }
261
-
262
- test('extendCli - docsVersion', async () => {
263
- const {siteDir, sidebarPath, plugin} = await loadSite();
264
- const mock = jest
265
- .spyOn(cliDocs, 'cliDocsVersionCommand')
266
- .mockImplementation();
267
- const cli = new commander.Command();
268
- // @ts-expect-error: TODO annoying type incompatibility
269
- plugin.extendCli!(cli);
270
- cli.parse(['node', 'test', 'docs:version', '1.0.0']);
271
- expect(mock).toHaveBeenCalledTimes(1);
272
- expect(mock).toHaveBeenCalledWith('1.0.0', siteDir, DEFAULT_PLUGIN_ID, {
273
- path: 'docs',
274
- sidebarPath,
275
- });
276
- mock.mockRestore();
277
- });
278
-
279
- test('getPathToWatch', async () => {
280
- const {siteDir, plugin} = await loadSite();
281
-
282
- const pathToWatch = plugin.getPathsToWatch!();
283
- const matchPattern = pathToWatch.map((filepath) =>
284
- posixPath(path.relative(siteDir, filepath)),
285
- );
286
- expect(matchPattern).not.toEqual([]);
287
- expect(matchPattern).toMatchInlineSnapshot(`
288
- Array [
289
- "sidebars.json",
290
- "i18n/en/docusaurus-plugin-content-docs/current/**/*.{md,mdx}",
291
- "docs/**/*.{md,mdx}",
292
- "docs/**/_category_.{json,yml,yaml}",
293
- ]
294
- `);
295
- expect(isMatch('docs/hello.md', matchPattern)).toEqual(true);
296
- expect(isMatch('docs/hello.mdx', matchPattern)).toEqual(true);
297
- expect(isMatch('docs/foo/bar.md', matchPattern)).toEqual(true);
298
- expect(isMatch('docs/hello.js', matchPattern)).toEqual(false);
299
- expect(isMatch('docs/super.mdl', matchPattern)).toEqual(false);
300
- expect(isMatch('docs/mdx', matchPattern)).toEqual(false);
301
- expect(isMatch('docs/headingAsTitle.md', matchPattern)).toEqual(true);
302
- expect(isMatch('sidebars.json', matchPattern)).toEqual(true);
303
- expect(isMatch('versioned_docs/hello.md', matchPattern)).toEqual(false);
304
- expect(isMatch('hello.md', matchPattern)).toEqual(false);
305
- expect(isMatch('super/docs/hello.md', matchPattern)).toEqual(false);
306
- });
307
-
308
- test('configureWebpack', async () => {
309
- const {plugin} = await loadSite();
310
-
311
- const config = applyConfigureWebpack(
312
- plugin.configureWebpack,
313
- {
314
- entry: './src/index.js',
315
- output: {
316
- filename: 'main.js',
317
- path: path.resolve(__dirname, 'dist'),
318
- },
319
- },
320
- false,
321
- );
322
- const errors = validate(config);
323
- expect(errors).toBeUndefined();
324
- });
325
-
326
- test('content', async () => {
327
- const {siteDir, plugin, pluginContentDir} = await loadSite();
328
- const content = await plugin.loadContent!();
329
- expect(content.loadedVersions.length).toEqual(1);
330
- const [currentVersion] = content.loadedVersions;
331
-
332
- expect(findDocById(currentVersion, 'hello')).toEqual({
333
- ...defaultDocMetadata,
334
- version: 'current',
335
- id: 'hello',
336
- unversionedId: 'hello',
337
- sourceDirName: '.',
338
- isDocsHomePage: true,
339
- permalink: '/docs/',
340
- slug: '/',
341
- previous: {
342
- title: 'baz',
343
- permalink: '/docs/foo/bazSlug.html',
344
- },
345
- sidebar: 'docs',
346
- source: path.posix.join(
347
- '@site',
348
- posixPath(path.relative(siteDir, currentVersion.contentPath)),
349
- 'hello.md',
350
- ),
351
- title: 'Hello, World !',
352
- description: 'Hi, Endilie here :)',
353
- frontMatter: {
354
- id: 'hello',
355
- title: 'Hello, World !',
356
- },
357
- });
358
-
359
- expect(getDocById(currentVersion, 'foo/bar')).toEqual({
360
- ...defaultDocMetadata,
361
- version: 'current',
362
- id: 'foo/bar',
363
- unversionedId: 'foo/bar',
364
- sourceDirName: 'foo',
365
- isDocsHomePage: false,
366
- next: {
367
- title: 'baz',
368
- permalink: '/docs/foo/bazSlug.html',
369
- },
370
- permalink: '/docs/foo/bar',
371
- slug: '/foo/bar',
372
- sidebar: 'docs',
373
- source: path.posix.join(
374
- '@site',
375
- posixPath(path.relative(siteDir, currentVersion.contentPath)),
376
- 'foo',
377
- 'bar.md',
378
- ),
379
- title: 'Remarkable',
380
- description: 'This is custom description',
381
- frontMatter: {
382
- description: 'This is custom description',
383
- id: 'bar',
384
- title: 'Bar',
385
- },
386
- });
387
-
388
- expect(currentVersion.sidebars).toMatchSnapshot();
389
-
390
- const {actions, utils} = createFakeActions(pluginContentDir);
391
-
392
- await plugin.contentLoaded!({
393
- content,
394
- actions,
395
- allContent: {},
396
- });
397
-
398
- utils.checkVersionMetadataPropCreated(currentVersion);
399
-
400
- utils.expectSnapshot();
401
-
402
- expect(utils.getGlobalData()).toMatchSnapshot();
403
- });
404
- });
405
-
406
- describe('versioned website', () => {
407
- async function loadSite() {
408
- const siteDir = path.join(__dirname, '__fixtures__', 'versioned-site');
409
- const context = await loadContext(siteDir);
410
- const sidebarPath = path.join(siteDir, 'sidebars.json');
411
- const routeBasePath = 'docs';
412
- const plugin = pluginContentDocs(
413
- context,
414
- normalizePluginOptions(OptionsSchema, {
415
- routeBasePath,
416
- sidebarPath,
417
- homePageId: 'hello',
418
- }),
419
- );
420
- const pluginContentDir = path.join(context.generatedFilesDir, plugin.name);
421
- return {
422
- siteDir,
423
- context,
424
- routeBasePath,
425
- sidebarPath,
426
- plugin,
427
- pluginContentDir,
428
- };
429
- }
430
-
431
- test('extendCli - docsVersion', async () => {
432
- const {siteDir, routeBasePath, sidebarPath, plugin} = await loadSite();
433
- const mock = jest
434
- .spyOn(cliDocs, 'cliDocsVersionCommand')
435
- .mockImplementation();
436
- const cli = new commander.Command();
437
- // @ts-expect-error: TODO annoying type incompatibility
438
- plugin.extendCli!(cli);
439
- cli.parse(['node', 'test', 'docs:version', '2.0.0']);
440
- expect(mock).toHaveBeenCalledTimes(1);
441
- expect(mock).toHaveBeenCalledWith('2.0.0', siteDir, DEFAULT_PLUGIN_ID, {
442
- path: routeBasePath,
443
- sidebarPath,
444
- });
445
- mock.mockRestore();
446
- });
447
-
448
- test('getPathToWatch', async () => {
449
- const {siteDir, plugin} = await loadSite();
450
- const pathToWatch = plugin.getPathsToWatch!();
451
- const matchPattern = pathToWatch.map((filepath) =>
452
- posixPath(path.relative(siteDir, filepath)),
453
- );
454
- expect(matchPattern).not.toEqual([]);
455
- expect(matchPattern).toMatchInlineSnapshot(`
456
- Array [
457
- "sidebars.json",
458
- "i18n/en/docusaurus-plugin-content-docs/current/**/*.{md,mdx}",
459
- "docs/**/*.{md,mdx}",
460
- "docs/**/_category_.{json,yml,yaml}",
461
- "versioned_sidebars/version-1.0.1-sidebars.json",
462
- "i18n/en/docusaurus-plugin-content-docs/version-1.0.1/**/*.{md,mdx}",
463
- "versioned_docs/version-1.0.1/**/*.{md,mdx}",
464
- "versioned_docs/version-1.0.1/**/_category_.{json,yml,yaml}",
465
- "versioned_sidebars/version-1.0.0-sidebars.json",
466
- "i18n/en/docusaurus-plugin-content-docs/version-1.0.0/**/*.{md,mdx}",
467
- "versioned_docs/version-1.0.0/**/*.{md,mdx}",
468
- "versioned_docs/version-1.0.0/**/_category_.{json,yml,yaml}",
469
- "versioned_sidebars/version-withSlugs-sidebars.json",
470
- "i18n/en/docusaurus-plugin-content-docs/version-withSlugs/**/*.{md,mdx}",
471
- "versioned_docs/version-withSlugs/**/*.{md,mdx}",
472
- "versioned_docs/version-withSlugs/**/_category_.{json,yml,yaml}",
473
- ]
474
- `);
475
- expect(isMatch('docs/hello.md', matchPattern)).toEqual(true);
476
- expect(isMatch('docs/hello.mdx', matchPattern)).toEqual(true);
477
- expect(isMatch('docs/foo/bar.md', matchPattern)).toEqual(true);
478
- expect(isMatch('sidebars.json', matchPattern)).toEqual(true);
479
- expect(
480
- isMatch('versioned_docs/version-1.0.0/hello.md', matchPattern),
481
- ).toEqual(true);
482
- expect(
483
- isMatch('versioned_docs/version-1.0.0/foo/bar.md', matchPattern),
484
- ).toEqual(true);
485
- expect(
486
- isMatch('versioned_sidebars/version-1.0.0-sidebars.json', matchPattern),
487
- ).toEqual(true);
488
-
489
- // Non existing version
490
- expect(
491
- isMatch('versioned_docs/version-2.0.0/foo/bar.md', matchPattern),
492
- ).toEqual(false);
493
- expect(
494
- isMatch('versioned_docs/version-2.0.0/hello.md', matchPattern),
495
- ).toEqual(false);
496
- expect(
497
- isMatch('versioned_sidebars/version-2.0.0-sidebars.json', matchPattern),
498
- ).toEqual(false);
499
-
500
- expect(isMatch('docs/hello.js', matchPattern)).toEqual(false);
501
- expect(isMatch('docs/super.mdl', matchPattern)).toEqual(false);
502
- expect(isMatch('docs/mdx', matchPattern)).toEqual(false);
503
- expect(isMatch('hello.md', matchPattern)).toEqual(false);
504
- expect(isMatch('super/docs/hello.md', matchPattern)).toEqual(false);
505
- });
506
-
507
- test('content', async () => {
508
- const {siteDir, plugin, pluginContentDir} = await loadSite();
509
- const content = await plugin.loadContent!();
510
- expect(content.loadedVersions.length).toEqual(4);
511
- const [
512
- currentVersion,
513
- version101,
514
- version100,
515
- versionWithSlugs,
516
- ] = content.loadedVersions;
517
-
518
- // foo/baz.md only exists in version -1.0.0
519
- expect(findDocById(currentVersion, 'foo/baz')).toBeUndefined();
520
- expect(findDocById(version101, 'foo/baz')).toBeUndefined();
521
- expect(findDocById(versionWithSlugs, 'foo/baz')).toBeUndefined();
522
-
523
- expect(getDocById(currentVersion, 'foo/bar')).toEqual({
524
- ...defaultDocMetadata,
525
- id: 'foo/bar',
526
- unversionedId: 'foo/bar',
527
- sourceDirName: 'foo',
528
- isDocsHomePage: false,
529
- permalink: '/docs/next/foo/barSlug',
530
- slug: '/foo/barSlug',
531
- source: path.posix.join(
532
- '@site',
533
- posixPath(path.relative(siteDir, currentVersion.contentPath)),
534
- 'foo',
535
- 'bar.md',
536
- ),
537
- title: 'bar',
538
- description: 'This is next version of bar.',
539
- frontMatter: {
540
- slug: 'barSlug',
541
- },
542
- version: 'current',
543
- sidebar: 'docs',
544
- next: {
545
- title: 'hello',
546
- permalink: '/docs/next/',
547
- },
548
- });
549
- expect(getDocById(currentVersion, 'hello')).toEqual({
550
- ...defaultDocMetadata,
551
- id: 'hello',
552
- unversionedId: 'hello',
553
- sourceDirName: '.',
554
- isDocsHomePage: true,
555
- permalink: '/docs/next/',
556
- slug: '/',
557
- source: path.posix.join(
558
- '@site',
559
- posixPath(path.relative(siteDir, currentVersion.contentPath)),
560
- 'hello.md',
561
- ),
562
- title: 'hello',
563
- description: 'Hello next !',
564
- frontMatter: {},
565
- version: 'current',
566
- sidebar: 'docs',
567
- previous: {
568
- title: 'bar',
569
- permalink: '/docs/next/foo/barSlug',
570
- },
571
- });
572
- expect(getDocById(version101, 'hello')).toEqual({
573
- ...defaultDocMetadata,
574
- id: 'version-1.0.1/hello',
575
- unversionedId: 'hello',
576
- sourceDirName: '.',
577
- isDocsHomePage: true,
578
- permalink: '/docs/',
579
- slug: '/',
580
- source: path.posix.join(
581
- '@site',
582
- posixPath(path.relative(siteDir, version101.contentPath)),
583
- 'hello.md',
584
- ),
585
- title: 'hello',
586
- description: 'Hello 1.0.1 !',
587
- frontMatter: {},
588
- version: '1.0.1',
589
- sidebar: 'version-1.0.1/docs',
590
- previous: {
591
- title: 'bar',
592
- permalink: '/docs/foo/bar',
593
- },
594
- });
595
- expect(getDocById(version100, 'foo/baz')).toEqual({
596
- ...defaultDocMetadata,
597
- id: 'version-1.0.0/foo/baz',
598
- unversionedId: 'foo/baz',
599
- sourceDirName: 'foo',
600
- isDocsHomePage: false,
601
- permalink: '/docs/1.0.0/foo/baz',
602
- slug: '/foo/baz',
603
- source: path.posix.join(
604
- '@site',
605
- posixPath(path.relative(siteDir, version100.contentPath)),
606
- 'foo',
607
- 'baz.md',
608
- ),
609
- title: 'baz',
610
- description:
611
- 'Baz 1.0.0 ! This will be deleted in next subsequent versions.',
612
- frontMatter: {},
613
- version: '1.0.0',
614
- sidebar: 'version-1.0.0/docs',
615
- next: {
616
- title: 'hello',
617
- permalink: '/docs/1.0.0/',
618
- },
619
- previous: {
620
- title: 'bar',
621
- permalink: '/docs/1.0.0/foo/barSlug',
622
- },
623
- });
624
-
625
- expect(currentVersion.sidebars).toMatchSnapshot('current version sidebars');
626
- expect(version101.sidebars).toMatchSnapshot('101 version sidebars');
627
- expect(version100.sidebars).toMatchSnapshot('100 version sidebars');
628
- expect(versionWithSlugs.sidebars).toMatchSnapshot(
629
- 'withSlugs version sidebars',
630
- );
631
-
632
- const {actions, utils} = createFakeActions(pluginContentDir);
633
- await plugin.contentLoaded!({
634
- content,
635
- actions,
636
- allContent: {},
637
- });
638
-
639
- utils.checkVersionMetadataPropCreated(currentVersion);
640
- utils.checkVersionMetadataPropCreated(version101);
641
- utils.checkVersionMetadataPropCreated(version100);
642
- utils.checkVersionMetadataPropCreated(versionWithSlugs);
643
-
644
- utils.expectSnapshot();
645
- });
646
- });
647
-
648
- describe('versioned website (community)', () => {
649
- async function loadSite() {
650
- const siteDir = path.join(__dirname, '__fixtures__', 'versioned-site');
651
- const context = await loadContext(siteDir);
652
- const sidebarPath = path.join(siteDir, 'community_sidebars.json');
653
- const routeBasePath = 'community';
654
- const pluginId = 'community';
655
- const plugin = pluginContentDocs(
656
- context,
657
- normalizePluginOptions(OptionsSchema, {
658
- id: 'community',
659
- path: 'community',
660
- routeBasePath,
661
- sidebarPath,
662
- }),
663
- );
664
- const pluginContentDir = path.join(context.generatedFilesDir, plugin.name);
665
- return {
666
- siteDir,
667
- context,
668
- routeBasePath,
669
- sidebarPath,
670
- pluginId,
671
- plugin,
672
- pluginContentDir,
673
- };
674
- }
675
-
676
- test('extendCli - docsVersion', async () => {
677
- const {
678
- siteDir,
679
- routeBasePath,
680
- sidebarPath,
681
- pluginId,
682
- plugin,
683
- } = await loadSite();
684
- const mock = jest
685
- .spyOn(cliDocs, 'cliDocsVersionCommand')
686
- .mockImplementation();
687
- const cli = new commander.Command();
688
- // @ts-expect-error: TODO annoying type incompatibility
689
- plugin.extendCli!(cli);
690
- cli.parse(['node', 'test', `docs:version:${pluginId}`, '2.0.0']);
691
- expect(mock).toHaveBeenCalledTimes(1);
692
- expect(mock).toHaveBeenCalledWith('2.0.0', siteDir, pluginId, {
693
- path: routeBasePath,
694
- sidebarPath,
695
- });
696
- mock.mockRestore();
697
- });
698
-
699
- test('getPathToWatch', async () => {
700
- const {siteDir, plugin} = await loadSite();
701
- const pathToWatch = plugin.getPathsToWatch!();
702
- const matchPattern = pathToWatch.map((filepath) =>
703
- posixPath(path.relative(siteDir, filepath)),
704
- );
705
- expect(matchPattern).not.toEqual([]);
706
- expect(matchPattern).toMatchInlineSnapshot(`
707
- Array [
708
- "community_sidebars.json",
709
- "i18n/en/docusaurus-plugin-content-docs-community/current/**/*.{md,mdx}",
710
- "community/**/*.{md,mdx}",
711
- "community/**/_category_.{json,yml,yaml}",
712
- "community_versioned_sidebars/version-1.0.0-sidebars.json",
713
- "i18n/en/docusaurus-plugin-content-docs-community/version-1.0.0/**/*.{md,mdx}",
714
- "community_versioned_docs/version-1.0.0/**/*.{md,mdx}",
715
- "community_versioned_docs/version-1.0.0/**/_category_.{json,yml,yaml}",
716
- ]
717
- `);
718
- expect(isMatch('community/team.md', matchPattern)).toEqual(true);
719
- expect(
720
- isMatch('community_versioned_docs/version-1.0.0/team.md', matchPattern),
721
- ).toEqual(true);
722
-
723
- // Non existing version
724
- expect(
725
- isMatch('community_versioned_docs/version-2.0.0/team.md', matchPattern),
726
- ).toEqual(false);
727
- expect(
728
- isMatch(
729
- 'community_versioned_sidebars/version-2.0.0-sidebars.json',
730
- matchPattern,
731
- ),
732
- ).toEqual(false);
733
-
734
- expect(isMatch('community/team.js', matchPattern)).toEqual(false);
735
- expect(
736
- isMatch('community_versioned_docs/version-1.0.0/team.js', matchPattern),
737
- ).toEqual(false);
738
- });
739
-
740
- test('content', async () => {
741
- const {siteDir, plugin, pluginContentDir} = await loadSite();
742
- const content = await plugin.loadContent!();
743
- expect(content.loadedVersions.length).toEqual(2);
744
- const [currentVersion, version100] = content.loadedVersions;
745
-
746
- expect(getDocById(currentVersion, 'team')).toEqual({
747
- ...defaultDocMetadata,
748
- id: 'team',
749
- unversionedId: 'team',
750
- sourceDirName: '.',
751
- isDocsHomePage: false,
752
- permalink: '/community/next/team',
753
- slug: '/team',
754
- source:
755
- '@site/i18n/en/docusaurus-plugin-content-docs-community/current/team.md',
756
- title: 'Team title translated',
757
- description: 'Team current version (translated)',
758
- version: 'current',
759
- sidebar: 'community',
760
- frontMatter: {title: 'Team title translated'},
761
- });
762
- expect(getDocById(version100, 'team')).toEqual({
763
- ...defaultDocMetadata,
764
- id: 'version-1.0.0/team',
765
- unversionedId: 'team',
766
- sourceDirName: '.',
767
- isDocsHomePage: false,
768
- permalink: '/community/team',
769
- slug: '/team',
770
- source: path.posix.join(
771
- '@site',
772
- posixPath(path.relative(siteDir, version100.contentPath)),
773
- 'team.md',
774
- ),
775
- title: 'team',
776
- description: 'Team 1.0.0',
777
- version: '1.0.0',
778
- sidebar: 'version-1.0.0/community',
779
- frontMatter: {},
780
- });
781
-
782
- expect(currentVersion.sidebars).toMatchSnapshot('current version sidebars');
783
- expect(version100.sidebars).toMatchSnapshot('100 version sidebars');
784
-
785
- const {actions, utils} = createFakeActions(pluginContentDir);
786
- await plugin.contentLoaded!({
787
- content,
788
- actions,
789
- allContent: {},
790
- });
791
-
792
- utils.checkVersionMetadataPropCreated(currentVersion);
793
- utils.checkVersionMetadataPropCreated(version100);
794
-
795
- utils.expectSnapshot();
796
- });
797
- });
798
-
799
- describe('site with doc label', () => {
800
- async function loadSite() {
801
- const siteDir = path.join(__dirname, '__fixtures__', 'site-with-doc-label');
802
- const context = await loadContext(siteDir);
803
- const sidebarPath = path.join(siteDir, 'sidebars.json');
804
- const plugin = pluginContentDocs(
805
- context,
806
- normalizePluginOptions(OptionsSchema, {
807
- path: 'docs',
808
- sidebarPath,
809
- homePageId: 'hello-1',
810
- }),
811
- );
812
-
813
- const content = (await plugin.loadContent?.())!;
814
-
815
- return {content};
816
- }
817
-
818
- test('label in sidebar.json is used', async () => {
819
- const {content} = await loadSite();
820
- const loadedVersion = content.loadedVersions[0];
821
- const sidebarProps = toSidebarsProp(loadedVersion);
822
-
823
- expect(sidebarProps.docs[0].label).toBe('Hello One');
824
- });
825
-
826
- test('sidebar_label in doc has higher precedence over label in sidebar.json', async () => {
827
- const {content} = await loadSite();
828
- const loadedVersion = content.loadedVersions[0];
829
- const sidebarProps = toSidebarsProp(loadedVersion);
830
-
831
- expect(sidebarProps.docs[1].label).toBe('Hello 2 From Doc');
832
- });
833
- });
834
-
835
- describe('site with full autogenerated sidebar', () => {
836
- async function loadSite() {
837
- const siteDir = path.join(
838
- __dirname,
839
- '__fixtures__',
840
- 'site-with-autogenerated-sidebar',
841
- );
842
- const context = await loadContext(siteDir);
843
- const plugin = pluginContentDocs(
844
- context,
845
- normalizePluginOptions(OptionsSchema, {
846
- path: 'docs',
847
- }),
848
- );
849
-
850
- const content = (await plugin.loadContent?.())!;
851
-
852
- return {content, siteDir};
853
- }
854
-
855
- test('sidebar is fully autogenerated', async () => {
856
- const {content} = await loadSite();
857
- const version = content.loadedVersions[0];
858
-
859
- expect(version.sidebars).toEqual({
860
- defaultSidebar: [
861
- {
862
- type: 'doc',
863
- id: 'getting-started',
864
- },
865
- {
866
- type: 'doc',
867
- id: 'installation',
868
- },
869
- {
870
- type: 'category',
871
- label: 'Guides',
872
- collapsed: true,
873
- items: [
874
- {
875
- type: 'doc',
876
- id: 'Guides/guide1',
877
- },
878
- {
879
- type: 'doc',
880
- id: 'Guides/guide2',
881
- },
882
- {
883
- type: 'doc',
884
- id: 'Guides/guide2.5',
885
- },
886
- {
887
- type: 'doc',
888
- id: 'Guides/guide3',
889
- },
890
- {
891
- type: 'doc',
892
- id: 'Guides/guide4',
893
- },
894
- {
895
- type: 'doc',
896
- id: 'Guides/guide5',
897
- },
898
- ],
899
- },
900
- {
901
- type: 'category',
902
- label: 'API (label from _category_.json)',
903
- collapsed: true,
904
- items: [
905
- {
906
- type: 'doc',
907
- id: 'API/api-overview',
908
- },
909
- {
910
- type: 'category',
911
- label: 'Core APIs',
912
- collapsed: true,
913
- items: [
914
- {
915
- type: 'doc',
916
-
917
- id: 'API/Core APIs/Client API',
918
- },
919
- {
920
- type: 'doc',
921
- id: 'API/Core APIs/Server API',
922
- },
923
- ],
924
- },
925
- {
926
- type: 'category',
927
- label: 'Extension APIs (label from _category_.yml)',
928
- collapsed: true,
929
- items: [
930
- {
931
- type: 'doc',
932
- id: 'API/Extension APIs/Plugin API',
933
- },
934
- {
935
- type: 'doc',
936
- id: 'API/Extension APIs/Theme API',
937
- },
938
- ],
939
- },
940
- {
941
- type: 'doc',
942
- id: 'API/api-end',
943
- },
944
- ],
945
- },
946
- ],
947
- });
948
- });
949
-
950
- test('docs in fully generated sidebar have correct metadatas', async () => {
951
- const {content, siteDir} = await loadSite();
952
- const version = content.loadedVersions[0];
953
-
954
- expect(getDocById(version, 'getting-started')).toEqual({
955
- ...defaultDocMetadata,
956
- id: 'getting-started',
957
- unversionedId: 'getting-started',
958
- sourceDirName: '.',
959
- isDocsHomePage: false,
960
- permalink: '/docs/getting-started',
961
- slug: '/getting-started',
962
- source: path.posix.join(
963
- '@site',
964
- posixPath(path.relative(siteDir, version.contentPath)),
965
- '0-getting-started.md',
966
- ),
967
- title: 'Getting Started',
968
- description: 'Getting started text',
969
- version: 'current',
970
- sidebar: 'defaultSidebar',
971
- frontMatter: {},
972
- sidebarPosition: 0,
973
- previous: undefined,
974
- next: {
975
- permalink: '/docs/installation',
976
- title: 'Installation',
977
- },
978
- });
979
-
980
- expect(getDocById(version, 'installation')).toEqual({
981
- ...defaultDocMetadata,
982
- id: 'installation',
983
- unversionedId: 'installation',
984
- sourceDirName: '.',
985
- isDocsHomePage: false,
986
- permalink: '/docs/installation',
987
- slug: '/installation',
988
- source: path.posix.join(
989
- '@site',
990
- posixPath(path.relative(siteDir, version.contentPath)),
991
- '1-installation.md',
992
- ),
993
- title: 'Installation',
994
- description: 'Installation text',
995
- version: 'current',
996
- sidebar: 'defaultSidebar',
997
- frontMatter: {},
998
- sidebarPosition: 1,
999
- previous: {
1000
- permalink: '/docs/getting-started',
1001
- title: 'Getting Started',
1002
- },
1003
- next: {
1004
- permalink: '/docs/Guides/guide1',
1005
- title: 'Guide 1',
1006
- },
1007
- });
1008
-
1009
- expect(getDocById(version, 'Guides/guide1')).toEqual({
1010
- ...defaultDocMetadata,
1011
- id: 'Guides/guide1',
1012
- unversionedId: 'Guides/guide1',
1013
- sourceDirName: 'Guides',
1014
- isDocsHomePage: false,
1015
- permalink: '/docs/Guides/guide1',
1016
- slug: '/Guides/guide1',
1017
- source: path.posix.join(
1018
- '@site',
1019
- posixPath(path.relative(siteDir, version.contentPath)),
1020
- 'Guides',
1021
- 'z-guide1.md',
1022
- ),
1023
- title: 'Guide 1',
1024
- description: 'Guide 1 text',
1025
- version: 'current',
1026
- sidebar: 'defaultSidebar',
1027
- frontMatter: {
1028
- id: 'guide1',
1029
- sidebar_position: 1,
1030
- },
1031
- sidebarPosition: 1,
1032
- previous: {
1033
- permalink: '/docs/installation',
1034
- title: 'Installation',
1035
- },
1036
- next: {
1037
- permalink: '/docs/Guides/guide2',
1038
- title: 'Guide 2',
1039
- },
1040
- });
1041
-
1042
- expect(getDocById(version, 'Guides/guide2')).toEqual({
1043
- ...defaultDocMetadata,
1044
- id: 'Guides/guide2',
1045
- unversionedId: 'Guides/guide2',
1046
- sourceDirName: 'Guides',
1047
- isDocsHomePage: false,
1048
- permalink: '/docs/Guides/guide2',
1049
- slug: '/Guides/guide2',
1050
- source: path.posix.join(
1051
- '@site',
1052
- posixPath(path.relative(siteDir, version.contentPath)),
1053
- 'Guides',
1054
- '02-guide2.md',
1055
- ),
1056
- title: 'Guide 2',
1057
- description: 'Guide 2 text',
1058
- version: 'current',
1059
- sidebar: 'defaultSidebar',
1060
- frontMatter: {
1061
- id: 'guide2',
1062
- },
1063
- sidebarPosition: 2,
1064
- previous: {
1065
- permalink: '/docs/Guides/guide1',
1066
- title: 'Guide 1',
1067
- },
1068
- next: {
1069
- permalink: '/docs/Guides/guide2.5',
1070
- title: 'Guide 2.5',
1071
- },
1072
- });
1073
-
1074
- expect(getDocById(version, 'Guides/guide2.5')).toEqual({
1075
- ...defaultDocMetadata,
1076
- id: 'Guides/guide2.5',
1077
- unversionedId: 'Guides/guide2.5',
1078
- sourceDirName: 'Guides',
1079
- isDocsHomePage: false,
1080
- permalink: '/docs/Guides/guide2.5',
1081
- slug: '/Guides/guide2.5',
1082
- source: path.posix.join(
1083
- '@site',
1084
- posixPath(path.relative(siteDir, version.contentPath)),
1085
- 'Guides',
1086
- '0-guide2.5.md',
1087
- ),
1088
- title: 'Guide 2.5',
1089
- description: 'Guide 2.5 text',
1090
- version: 'current',
1091
- sidebar: 'defaultSidebar',
1092
- frontMatter: {
1093
- id: 'guide2.5',
1094
- sidebar_position: 2.5,
1095
- },
1096
- sidebarPosition: 2.5,
1097
- previous: {
1098
- permalink: '/docs/Guides/guide2',
1099
- title: 'Guide 2',
1100
- },
1101
- next: {
1102
- permalink: '/docs/Guides/guide3',
1103
- title: 'Guide 3',
1104
- },
1105
- });
1106
-
1107
- expect(getDocById(version, 'Guides/guide3')).toEqual({
1108
- ...defaultDocMetadata,
1109
- id: 'Guides/guide3',
1110
- unversionedId: 'Guides/guide3',
1111
- sourceDirName: 'Guides',
1112
- isDocsHomePage: false,
1113
- permalink: '/docs/Guides/guide3',
1114
- slug: '/Guides/guide3',
1115
- source: path.posix.join(
1116
- '@site',
1117
- posixPath(path.relative(siteDir, version.contentPath)),
1118
- 'Guides',
1119
- 'guide3.md',
1120
- ),
1121
- title: 'Guide 3',
1122
- description: 'Guide 3 text',
1123
- version: 'current',
1124
- sidebar: 'defaultSidebar',
1125
- frontMatter: {
1126
- id: 'guide3',
1127
- sidebar_position: 3,
1128
- },
1129
- sidebarPosition: 3,
1130
- previous: {
1131
- permalink: '/docs/Guides/guide2.5',
1132
- title: 'Guide 2.5',
1133
- },
1134
- next: {
1135
- permalink: '/docs/Guides/guide4',
1136
- title: 'Guide 4',
1137
- },
1138
- });
1139
-
1140
- expect(getDocById(version, 'Guides/guide4')).toEqual({
1141
- ...defaultDocMetadata,
1142
- id: 'Guides/guide4',
1143
- unversionedId: 'Guides/guide4',
1144
- sourceDirName: 'Guides',
1145
- isDocsHomePage: false,
1146
- permalink: '/docs/Guides/guide4',
1147
- slug: '/Guides/guide4',
1148
- source: path.posix.join(
1149
- '@site',
1150
- posixPath(path.relative(siteDir, version.contentPath)),
1151
- 'Guides',
1152
- 'a-guide4.md',
1153
- ),
1154
- title: 'Guide 4',
1155
- description: 'Guide 4 text',
1156
- version: 'current',
1157
- sidebar: 'defaultSidebar',
1158
- frontMatter: {
1159
- id: 'guide4',
1160
- },
1161
- sidebarPosition: undefined,
1162
- previous: {
1163
- permalink: '/docs/Guides/guide3',
1164
- title: 'Guide 3',
1165
- },
1166
- next: {
1167
- permalink: '/docs/Guides/guide5',
1168
- title: 'Guide 5',
1169
- },
1170
- });
1171
-
1172
- expect(getDocById(version, 'Guides/guide5')).toEqual({
1173
- ...defaultDocMetadata,
1174
- id: 'Guides/guide5',
1175
- unversionedId: 'Guides/guide5',
1176
- sourceDirName: 'Guides',
1177
- isDocsHomePage: false,
1178
- permalink: '/docs/Guides/guide5',
1179
- slug: '/Guides/guide5',
1180
- source: path.posix.join(
1181
- '@site',
1182
- posixPath(path.relative(siteDir, version.contentPath)),
1183
- 'Guides',
1184
- 'b-guide5.md',
1185
- ),
1186
- title: 'Guide 5',
1187
- description: 'Guide 5 text',
1188
- version: 'current',
1189
- sidebar: 'defaultSidebar',
1190
- frontMatter: {
1191
- id: 'guide5',
1192
- },
1193
- sidebarPosition: undefined,
1194
- previous: {
1195
- permalink: '/docs/Guides/guide4',
1196
- title: 'Guide 4',
1197
- },
1198
- next: {
1199
- permalink: '/docs/API/api-overview',
1200
- title: 'API Overview',
1201
- },
1202
- });
1203
-
1204
- expect(getDocById(version, 'API/api-overview')).toEqual({
1205
- ...defaultDocMetadata,
1206
- id: 'API/api-overview',
1207
- unversionedId: 'API/api-overview',
1208
- sourceDirName: '3-API',
1209
- isDocsHomePage: false,
1210
- permalink: '/docs/API/api-overview',
1211
- slug: '/API/api-overview',
1212
- source: path.posix.join(
1213
- '@site',
1214
- posixPath(path.relative(siteDir, version.contentPath)),
1215
- '3-API',
1216
- '00_api-overview.md',
1217
- ),
1218
- title: 'API Overview',
1219
- description: 'API Overview text',
1220
- version: 'current',
1221
- sidebar: 'defaultSidebar',
1222
- frontMatter: {},
1223
- sidebarPosition: 0,
1224
- previous: {
1225
- permalink: '/docs/Guides/guide5',
1226
- title: 'Guide 5',
1227
- },
1228
- next: {
1229
- permalink: '/docs/API/Core APIs/Client API',
1230
- title: 'Client API',
1231
- },
1232
- });
1233
-
1234
- expect(getDocById(version, 'API/Core APIs/Client API')).toEqual({
1235
- ...defaultDocMetadata,
1236
- id: 'API/Core APIs/Client API',
1237
- unversionedId: 'API/Core APIs/Client API',
1238
- sourceDirName: '3-API/01_Core APIs',
1239
- isDocsHomePage: false,
1240
- permalink: '/docs/API/Core APIs/Client API',
1241
- slug: '/API/Core APIs/Client API',
1242
- source: path.posix.join(
1243
- '@site',
1244
- posixPath(path.relative(siteDir, version.contentPath)),
1245
- '3-API',
1246
- '01_Core APIs',
1247
- '0 --- Client API.md',
1248
- ),
1249
- title: 'Client API',
1250
- description: 'Client API text',
1251
- version: 'current',
1252
- sidebar: 'defaultSidebar',
1253
- frontMatter: {},
1254
- sidebarPosition: 0,
1255
- previous: {
1256
- permalink: '/docs/API/api-overview',
1257
- title: 'API Overview',
1258
- },
1259
- next: {
1260
- permalink: '/docs/API/Core APIs/Server API',
1261
- title: 'Server API',
1262
- },
1263
- });
1264
-
1265
- expect(getDocById(version, 'API/Core APIs/Server API')).toEqual({
1266
- ...defaultDocMetadata,
1267
- id: 'API/Core APIs/Server API',
1268
- unversionedId: 'API/Core APIs/Server API',
1269
- sourceDirName: '3-API/01_Core APIs',
1270
- isDocsHomePage: false,
1271
- permalink: '/docs/API/Core APIs/Server API',
1272
- slug: '/API/Core APIs/Server API',
1273
- source: path.posix.join(
1274
- '@site',
1275
- posixPath(path.relative(siteDir, version.contentPath)),
1276
- '3-API',
1277
- '01_Core APIs',
1278
- '1 --- Server API.md',
1279
- ),
1280
- title: 'Server API',
1281
- description: 'Server API text',
1282
- version: 'current',
1283
- sidebar: 'defaultSidebar',
1284
- frontMatter: {},
1285
- sidebarPosition: 1,
1286
- previous: {
1287
- permalink: '/docs/API/Core APIs/Client API',
1288
- title: 'Client API',
1289
- },
1290
- next: {
1291
- permalink: '/docs/API/Extension APIs/Plugin API',
1292
- title: 'Plugin API',
1293
- },
1294
- });
1295
-
1296
- expect(getDocById(version, 'API/Extension APIs/Plugin API')).toEqual({
1297
- ...defaultDocMetadata,
1298
- id: 'API/Extension APIs/Plugin API',
1299
- unversionedId: 'API/Extension APIs/Plugin API',
1300
- sourceDirName: '3-API/02_Extension APIs',
1301
- isDocsHomePage: false,
1302
- permalink: '/docs/API/Extension APIs/Plugin API',
1303
- slug: '/API/Extension APIs/Plugin API',
1304
- source: path.posix.join(
1305
- '@site',
1306
- posixPath(path.relative(siteDir, version.contentPath)),
1307
- '3-API',
1308
- '02_Extension APIs',
1309
- '0. Plugin API.md',
1310
- ),
1311
- title: 'Plugin API',
1312
- description: 'Plugin API text',
1313
- version: 'current',
1314
- sidebar: 'defaultSidebar',
1315
- frontMatter: {},
1316
- sidebarPosition: 0,
1317
- previous: {
1318
- permalink: '/docs/API/Core APIs/Server API',
1319
- title: 'Server API',
1320
- },
1321
- next: {
1322
- permalink: '/docs/API/Extension APIs/Theme API',
1323
- title: 'Theme API',
1324
- },
1325
- });
1326
-
1327
- expect(getDocById(version, 'API/Extension APIs/Theme API')).toEqual({
1328
- ...defaultDocMetadata,
1329
- id: 'API/Extension APIs/Theme API',
1330
- unversionedId: 'API/Extension APIs/Theme API',
1331
- sourceDirName: '3-API/02_Extension APIs',
1332
- isDocsHomePage: false,
1333
- permalink: '/docs/API/Extension APIs/Theme API',
1334
- slug: '/API/Extension APIs/Theme API',
1335
- source: path.posix.join(
1336
- '@site',
1337
- posixPath(path.relative(siteDir, version.contentPath)),
1338
- '3-API',
1339
- '02_Extension APIs',
1340
- '1. Theme API.md',
1341
- ),
1342
- title: 'Theme API',
1343
- description: 'Theme API text',
1344
- version: 'current',
1345
- sidebar: 'defaultSidebar',
1346
- frontMatter: {},
1347
- sidebarPosition: 1,
1348
- previous: {
1349
- permalink: '/docs/API/Extension APIs/Plugin API',
1350
- title: 'Plugin API',
1351
- },
1352
- next: {
1353
- permalink: '/docs/API/api-end',
1354
- title: 'API End',
1355
- },
1356
- });
1357
-
1358
- expect(getDocById(version, 'API/api-end')).toEqual({
1359
- ...defaultDocMetadata,
1360
- id: 'API/api-end',
1361
- unversionedId: 'API/api-end',
1362
- sourceDirName: '3-API',
1363
- isDocsHomePage: false,
1364
- permalink: '/docs/API/api-end',
1365
- slug: '/API/api-end',
1366
- source: path.posix.join(
1367
- '@site',
1368
- posixPath(path.relative(siteDir, version.contentPath)),
1369
- '3-API',
1370
- '03_api-end.md',
1371
- ),
1372
- title: 'API End',
1373
- description: 'API End text',
1374
- version: 'current',
1375
- sidebar: 'defaultSidebar',
1376
- frontMatter: {},
1377
- sidebarPosition: 3,
1378
- previous: {
1379
- permalink: '/docs/API/Extension APIs/Theme API',
1380
- title: 'Theme API',
1381
- },
1382
- next: undefined,
1383
- });
1384
- });
1385
- });
1386
-
1387
- describe('site with partial autogenerated sidebars', () => {
1388
- async function loadSite() {
1389
- const siteDir = path.join(
1390
- __dirname,
1391
- '__fixtures__',
1392
- 'site-with-autogenerated-sidebar',
1393
- );
1394
- const context = await loadContext(siteDir, {});
1395
- const plugin = pluginContentDocs(
1396
- context,
1397
- normalizePluginOptions(OptionsSchema, {
1398
- path: 'docs',
1399
- sidebarPath: path.join(
1400
- __dirname,
1401
- '__fixtures__',
1402
- 'site-with-autogenerated-sidebar',
1403
- 'partialAutogeneratedSidebars.js',
1404
- ),
1405
- }),
1406
- );
1407
-
1408
- const content = (await plugin.loadContent?.())!;
1409
-
1410
- return {content, siteDir};
1411
- }
1412
-
1413
- test('sidebar is partially autogenerated', async () => {
1414
- const {content} = await loadSite();
1415
- const version = content.loadedVersions[0];
1416
-
1417
- expect(version.sidebars).toEqual({
1418
- someSidebar: [
1419
- {
1420
- type: 'doc',
1421
- id: 'API/api-end',
1422
- },
1423
- {
1424
- type: 'category',
1425
- label: 'Some category',
1426
- collapsed: true,
1427
- items: [
1428
- {
1429
- type: 'doc',
1430
- id: 'API/api-overview',
1431
- },
1432
- {
1433
- type: 'doc',
1434
- id: 'API/Extension APIs/Plugin API',
1435
- },
1436
- {
1437
- type: 'doc',
1438
- id: 'API/Extension APIs/Theme API',
1439
- },
1440
- ],
1441
- },
1442
- ],
1443
- });
1444
- });
1445
-
1446
- test('docs in partially generated sidebar have correct metadatas', async () => {
1447
- const {content, siteDir} = await loadSite();
1448
- const version = content.loadedVersions[0];
1449
-
1450
- // Only looking at the docs of the autogen sidebar, others metadatas should not be affected
1451
-
1452
- expect(getDocById(version, 'API/api-end')).toEqual({
1453
- ...defaultDocMetadata,
1454
- id: 'API/api-end',
1455
- unversionedId: 'API/api-end',
1456
- sourceDirName: '3-API',
1457
- isDocsHomePage: false,
1458
- permalink: '/docs/API/api-end',
1459
- slug: '/API/api-end',
1460
- source: path.posix.join(
1461
- '@site',
1462
- posixPath(path.relative(siteDir, version.contentPath)),
1463
- '3-API',
1464
- '03_api-end.md',
1465
- ),
1466
- title: 'API End',
1467
- description: 'API End text',
1468
- version: 'current',
1469
- sidebar: 'someSidebar',
1470
- frontMatter: {},
1471
- sidebarPosition: 3, // ignored (not part of the autogenerated sidebar slice)
1472
- previous: undefined,
1473
- next: {
1474
- permalink: '/docs/API/api-overview',
1475
- title: 'API Overview',
1476
- },
1477
- });
1478
-
1479
- expect(getDocById(version, 'API/api-overview')).toEqual({
1480
- ...defaultDocMetadata,
1481
- id: 'API/api-overview',
1482
- unversionedId: 'API/api-overview',
1483
- sourceDirName: '3-API',
1484
- isDocsHomePage: false,
1485
- permalink: '/docs/API/api-overview',
1486
- slug: '/API/api-overview',
1487
- source: path.posix.join(
1488
- '@site',
1489
- posixPath(path.relative(siteDir, version.contentPath)),
1490
- '3-API',
1491
- '00_api-overview.md',
1492
- ),
1493
- title: 'API Overview',
1494
- description: 'API Overview text',
1495
- version: 'current',
1496
- sidebar: 'someSidebar',
1497
- frontMatter: {},
1498
- sidebarPosition: 0, // ignored (not part of the autogenerated sidebar slice)
1499
- previous: {
1500
- permalink: '/docs/API/api-end',
1501
- title: 'API End',
1502
- },
1503
- next: {
1504
- permalink: '/docs/API/Extension APIs/Plugin API',
1505
- title: 'Plugin API',
1506
- },
1507
- });
1508
-
1509
- expect(getDocById(version, 'API/Extension APIs/Plugin API')).toEqual({
1510
- ...defaultDocMetadata,
1511
- id: 'API/Extension APIs/Plugin API',
1512
- unversionedId: 'API/Extension APIs/Plugin API',
1513
- sourceDirName: '3-API/02_Extension APIs',
1514
- isDocsHomePage: false,
1515
- permalink: '/docs/API/Extension APIs/Plugin API',
1516
- slug: '/API/Extension APIs/Plugin API',
1517
- source: path.posix.join(
1518
- '@site',
1519
- posixPath(path.relative(siteDir, version.contentPath)),
1520
- '3-API',
1521
- '02_Extension APIs',
1522
- '0. Plugin API.md',
1523
- ),
1524
- title: 'Plugin API',
1525
- description: 'Plugin API text',
1526
- version: 'current',
1527
- sidebar: 'someSidebar',
1528
- frontMatter: {},
1529
- sidebarPosition: 0,
1530
- previous: {
1531
- permalink: '/docs/API/api-overview',
1532
- title: 'API Overview',
1533
- },
1534
- next: {
1535
- permalink: '/docs/API/Extension APIs/Theme API',
1536
- title: 'Theme API',
1537
- },
1538
- });
1539
-
1540
- expect(getDocById(version, 'API/Extension APIs/Theme API')).toEqual({
1541
- ...defaultDocMetadata,
1542
- id: 'API/Extension APIs/Theme API',
1543
- unversionedId: 'API/Extension APIs/Theme API',
1544
- sourceDirName: '3-API/02_Extension APIs',
1545
- isDocsHomePage: false,
1546
- permalink: '/docs/API/Extension APIs/Theme API',
1547
- slug: '/API/Extension APIs/Theme API',
1548
- source: path.posix.join(
1549
- '@site',
1550
- posixPath(path.relative(siteDir, version.contentPath)),
1551
- '3-API',
1552
- '02_Extension APIs',
1553
- '1. Theme API.md',
1554
- ),
1555
- title: 'Theme API',
1556
- description: 'Theme API text',
1557
- version: 'current',
1558
- sidebar: 'someSidebar',
1559
- frontMatter: {},
1560
- sidebarPosition: 1,
1561
- previous: {
1562
- permalink: '/docs/API/Extension APIs/Plugin API',
1563
- title: 'Plugin API',
1564
- },
1565
- next: undefined,
1566
- });
1567
- });
1568
- });
1569
-
1570
- describe('site with partial autogenerated sidebars 2 (fix #4638)', () => {
1571
- // Test added for edge case https://github.com/facebook/docusaurus/issues/4638
1572
-
1573
- async function loadSite() {
1574
- const siteDir = path.join(
1575
- __dirname,
1576
- '__fixtures__',
1577
- 'site-with-autogenerated-sidebar',
1578
- );
1579
- const context = await loadContext(siteDir, {});
1580
- const plugin = pluginContentDocs(
1581
- context,
1582
- normalizePluginOptions(OptionsSchema, {
1583
- path: 'docs',
1584
- sidebarPath: path.join(
1585
- __dirname,
1586
- '__fixtures__',
1587
- 'site-with-autogenerated-sidebar',
1588
- 'partialAutogeneratedSidebars2.js',
1589
- ),
1590
- }),
1591
- );
1592
-
1593
- const content = (await plugin.loadContent?.())!;
1594
-
1595
- return {content, siteDir};
1596
- }
1597
-
1598
- test('sidebar is partially autogenerated', async () => {
1599
- const {content} = await loadSite();
1600
- const version = content.loadedVersions[0];
1601
-
1602
- expect(version.sidebars).toEqual({
1603
- someSidebar: [
1604
- {
1605
- type: 'doc',
1606
- id: 'API/api-end',
1607
- },
1608
- {
1609
- type: 'doc',
1610
- id: 'API/api-overview',
1611
- },
1612
- {
1613
- type: 'category',
1614
- label: 'Core APIs',
1615
- collapsed: true,
1616
- items: [
1617
- {
1618
- type: 'doc',
1619
-
1620
- id: 'API/Core APIs/Client API',
1621
- },
1622
- {
1623
- type: 'doc',
1624
- id: 'API/Core APIs/Server API',
1625
- },
1626
- ],
1627
- },
1628
- {
1629
- type: 'category',
1630
- label: 'Extension APIs (label from _category_.yml)', // Fix #4638
1631
- collapsed: true,
1632
- items: [
1633
- {
1634
- type: 'doc',
1635
- id: 'API/Extension APIs/Plugin API',
1636
- },
1637
- {
1638
- type: 'doc',
1639
- id: 'API/Extension APIs/Theme API',
1640
- },
1641
- ],
1642
- },
1643
- {
1644
- type: 'doc',
1645
- id: 'API/api-end',
1646
- },
1647
- ],
1648
- });
1649
- });
1650
- });
1651
-
1652
- describe('site with custom sidebar items generator', () => {
1653
- async function loadSite(sidebarItemsGenerator: SidebarItemsGeneratorOption) {
1654
- const siteDir = path.join(
1655
- __dirname,
1656
- '__fixtures__',
1657
- 'site-with-autogenerated-sidebar',
1658
- );
1659
- const context = await loadContext(siteDir);
1660
- const plugin = pluginContentDocs(
1661
- context,
1662
- normalizePluginOptions(OptionsSchema, {
1663
- path: 'docs',
1664
- sidebarItemsGenerator,
1665
- }),
1666
- );
1667
- const content = (await plugin.loadContent?.())!;
1668
- return {content, siteDir};
1669
- }
1670
-
1671
- test('sidebarItemsGenerator is called with appropriate data', async () => {
1672
- const customSidebarItemsGeneratorMock = jest.fn(
1673
- async (_arg: SidebarItemsGeneratorOptionArgs) => [],
1674
- );
1675
- const {siteDir} = await loadSite(customSidebarItemsGeneratorMock);
1676
-
1677
- const generatorArg: SidebarItemsGeneratorOptionArgs =
1678
- customSidebarItemsGeneratorMock.mock.calls[0][0];
1679
-
1680
- // Make test pass even if docs are in different order and paths are absolutes
1681
- function makeDeterministic(
1682
- arg: SidebarItemsGeneratorOptionArgs,
1683
- ): SidebarItemsGeneratorOptionArgs {
1684
- return {
1685
- ...arg,
1686
- docs: orderBy(arg.docs, 'id'),
1687
- version: {
1688
- ...arg.version,
1689
- contentPath: path.relative(siteDir, arg.version.contentPath),
1690
- },
1691
- };
1692
- }
1693
-
1694
- expect(makeDeterministic(generatorArg)).toMatchSnapshot();
1695
- expect(generatorArg.defaultSidebarItemsGenerator).toEqual(
1696
- DefaultSidebarItemsGenerator,
1697
- );
1698
- });
1699
-
1700
- test('sidebar is autogenerated according to a custom sidebarItemsGenerator', async () => {
1701
- const customSidebarItemsGenerator: SidebarItemsGeneratorOption = async () => {
1702
- return [
1703
- {type: 'doc', id: 'API/api-overview'},
1704
- {type: 'doc', id: 'API/api-end'},
1705
- ];
1706
- };
1707
-
1708
- const {content} = await loadSite(customSidebarItemsGenerator);
1709
- const version = content.loadedVersions[0];
1710
-
1711
- expect(version.sidebars).toEqual({
1712
- defaultSidebar: [
1713
- {type: 'doc', id: 'API/api-overview'},
1714
- {type: 'doc', id: 'API/api-end'},
1715
- ],
1716
- });
1717
- });
1718
-
1719
- test('sidebarItemsGenerator can wrap/enhance/sort/reverse the default sidebar generator', async () => {
1720
- function reverseSidebarItems(items: SidebarItem[]): SidebarItem[] {
1721
- const result: SidebarItem[] = items.map((item) => {
1722
- if (item.type === 'category') {
1723
- return {...item, items: reverseSidebarItems(item.items)};
1724
- }
1725
- return item;
1726
- });
1727
- result.reverse();
1728
- return result;
1729
- }
1730
-
1731
- const reversedSidebarItemsGenerator: SidebarItemsGeneratorOption = async ({
1732
- defaultSidebarItemsGenerator,
1733
- ...args
1734
- }) => {
1735
- const sidebarItems = await defaultSidebarItemsGenerator(args);
1736
- return reverseSidebarItems(sidebarItems);
1737
- };
1738
-
1739
- const {content} = await loadSite(reversedSidebarItemsGenerator);
1740
- const version = content.loadedVersions[0];
1741
-
1742
- expect(version.sidebars).toEqual({
1743
- defaultSidebar: [
1744
- {
1745
- type: 'category',
1746
- label: 'API (label from _category_.json)',
1747
- collapsed: true,
1748
- items: [
1749
- {
1750
- type: 'doc',
1751
- id: 'API/api-end',
1752
- },
1753
- {
1754
- type: 'category',
1755
- label: 'Extension APIs (label from _category_.yml)',
1756
- collapsed: true,
1757
- items: [
1758
- {
1759
- type: 'doc',
1760
- id: 'API/Extension APIs/Theme API',
1761
- },
1762
- {
1763
- type: 'doc',
1764
- id: 'API/Extension APIs/Plugin API',
1765
- },
1766
- ],
1767
- },
1768
- {
1769
- type: 'category',
1770
- label: 'Core APIs',
1771
- collapsed: true,
1772
- items: [
1773
- {
1774
- type: 'doc',
1775
- id: 'API/Core APIs/Server API',
1776
- },
1777
- {
1778
- type: 'doc',
1779
- id: 'API/Core APIs/Client API',
1780
- },
1781
- ],
1782
- },
1783
- {
1784
- type: 'doc',
1785
- id: 'API/api-overview',
1786
- },
1787
- ],
1788
- },
1789
- {
1790
- type: 'category',
1791
- label: 'Guides',
1792
- collapsed: true,
1793
- items: [
1794
- {
1795
- type: 'doc',
1796
- id: 'Guides/guide5',
1797
- },
1798
- {
1799
- type: 'doc',
1800
- id: 'Guides/guide4',
1801
- },
1802
- {
1803
- type: 'doc',
1804
- id: 'Guides/guide3',
1805
- },
1806
- {
1807
- type: 'doc',
1808
- id: 'Guides/guide2.5',
1809
- },
1810
- {
1811
- type: 'doc',
1812
- id: 'Guides/guide2',
1813
- },
1814
- {
1815
- type: 'doc',
1816
- id: 'Guides/guide1',
1817
- },
1818
- ],
1819
- },
1820
- {
1821
- type: 'doc',
1822
- id: 'installation',
1823
- },
1824
- {
1825
- type: 'doc',
1826
- id: 'getting-started',
1827
- },
1828
- ],
1829
- });
1830
- });
1831
- });