@docusaurus/plugin-content-docs 2.0.0-beta.12faed89d → 2.0.0-beta.13

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 (138) hide show
  1. package/lib/.tsbuildinfo +1 -1
  2. package/lib/categoryGeneratedIndex.d.ts +12 -0
  3. package/lib/categoryGeneratedIndex.js +37 -0
  4. package/lib/cli.d.ts +2 -2
  5. package/lib/cli.js +12 -34
  6. package/lib/client/docsClientUtils.d.ts +0 -3
  7. package/lib/client/docsClientUtils.js +19 -22
  8. package/lib/docFrontMatter.d.ts +1 -1
  9. package/lib/docFrontMatter.js +7 -3
  10. package/lib/docs.d.ts +25 -3
  11. package/lib/docs.js +125 -41
  12. package/lib/globalData.d.ts +1 -1
  13. package/lib/index.d.ts +1 -1
  14. package/lib/index.js +100 -131
  15. package/lib/lastUpdate.js +8 -9
  16. package/lib/markdown/index.d.ts +3 -6
  17. package/lib/markdown/index.js +3 -3
  18. package/lib/markdown/linkify.js +2 -2
  19. package/lib/numberPrefix.d.ts +1 -1
  20. package/lib/options.d.ts +3 -3
  21. package/lib/options.js +48 -11
  22. package/lib/props.d.ts +7 -2
  23. package/lib/props.js +60 -8
  24. package/lib/routes.d.ts +27 -0
  25. package/lib/routes.js +105 -0
  26. package/lib/{sidebarItemsGenerator.d.ts → sidebars/generator.d.ts} +5 -2
  27. package/lib/sidebars/generator.js +216 -0
  28. package/lib/sidebars/index.d.ts +15 -0
  29. package/lib/sidebars/index.js +73 -0
  30. package/lib/sidebars/normalization.d.ts +14 -0
  31. package/lib/sidebars/normalization.js +77 -0
  32. package/lib/sidebars/processor.d.ts +18 -0
  33. package/lib/sidebars/processor.js +85 -0
  34. package/lib/sidebars/types.d.ts +127 -0
  35. package/lib/sidebars/types.js +8 -0
  36. package/lib/sidebars/utils.d.ts +35 -0
  37. package/lib/sidebars/utils.js +228 -0
  38. package/lib/sidebars/validation.d.ts +10 -0
  39. package/lib/sidebars/validation.js +138 -0
  40. package/lib/slug.d.ts +4 -3
  41. package/lib/slug.js +27 -15
  42. package/lib/tags.d.ts +8 -0
  43. package/lib/tags.js +20 -0
  44. package/lib/theme/hooks/useDocs.js +21 -21
  45. package/lib/translations.d.ts +2 -2
  46. package/lib/translations.js +71 -29
  47. package/lib/types.d.ts +52 -63
  48. package/lib/versions.d.ts +3 -3
  49. package/lib/versions.js +41 -22
  50. package/package.json +20 -20
  51. package/src/__tests__/__fixtures__/simple-site/docs/_partials/somePartial.md +3 -0
  52. package/src/__tests__/__fixtures__/simple-site/docs/_partials/subfolder/somePartial.md +3 -0
  53. package/src/__tests__/__fixtures__/simple-site/docs/_somePartial.md +3 -0
  54. package/src/__tests__/__fixtures__/simple-site/docs/foo/baz.md +5 -0
  55. package/src/__tests__/__fixtures__/simple-site/docs/hello.md +2 -0
  56. package/src/__tests__/__fixtures__/simple-site/docs/rootAbsoluteSlug.md +2 -0
  57. package/src/__tests__/__fixtures__/simple-site/docs/rootRelativeSlug.md +2 -0
  58. package/src/__tests__/__fixtures__/simple-site/docs/rootResolvedSlug.md +2 -0
  59. package/src/__tests__/__fixtures__/simple-site/docs/rootTryToEscapeSlug.md +2 -0
  60. package/src/__tests__/__fixtures__/simple-site/sidebars.json +15 -1
  61. package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-1.md +1 -0
  62. package/src/__tests__/__fixtures__/versioned-site/docs/foo/bar.md +6 -0
  63. package/src/__tests__/__fixtures__/versioned-site/docs/hello.md +3 -0
  64. package/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md +3 -0
  65. package/src/__tests__/__fixtures__/versioned-site/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md +3 -0
  66. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/hello.md +3 -0
  67. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_partials/somePartial.md +3 -0
  68. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_partials/subfolder/somePartial.md +3 -0
  69. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_somePartial.md +3 -0
  70. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/hello.md +3 -0
  71. package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.1-sidebars.json +2 -2
  72. package/src/__tests__/__snapshots__/cli.test.ts.snap +48 -73
  73. package/src/__tests__/__snapshots__/docs.test.ts.snap +140 -0
  74. package/src/__tests__/__snapshots__/index.test.ts.snap +745 -97
  75. package/src/__tests__/__snapshots__/translations.test.ts.snap +45 -15
  76. package/src/__tests__/cli.test.ts +15 -11
  77. package/src/__tests__/docFrontMatter.test.ts +160 -45
  78. package/src/__tests__/docs.test.ts +311 -150
  79. package/src/__tests__/index.test.ts +108 -66
  80. package/src/__tests__/lastUpdate.test.ts +1 -1
  81. package/src/__tests__/options.test.ts +48 -3
  82. package/src/__tests__/props.test.ts +62 -0
  83. package/src/__tests__/slug.test.ts +127 -20
  84. package/src/__tests__/translations.test.ts +7 -1
  85. package/src/__tests__/versions.test.ts +73 -70
  86. package/src/categoryGeneratedIndex.ts +57 -0
  87. package/src/cli.ts +8 -41
  88. package/src/client/docsClientUtils.ts +14 -26
  89. package/{types.d.ts → src/deps.d.ts} +0 -0
  90. package/src/docFrontMatter.ts +9 -4
  91. package/src/docs.ts +158 -32
  92. package/src/globalData.ts +6 -1
  93. package/src/index.ts +125 -169
  94. package/src/lastUpdate.ts +10 -13
  95. package/src/markdown/index.ts +8 -12
  96. package/src/numberPrefix.ts +5 -3
  97. package/src/options.ts +59 -14
  98. package/src/plugin-content-docs.d.ts +173 -40
  99. package/src/props.ts +90 -15
  100. package/src/routes.ts +173 -0
  101. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-shorthand.js +0 -0
  102. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-wrong-items.json +0 -0
  103. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-wrong-label.json +0 -0
  104. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category.js +0 -0
  105. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-collapsed-first-level.json +0 -0
  106. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-collapsed.json +0 -0
  107. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-doc-id-not-string.json +0 -0
  108. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-first-level-not-category.js +0 -0
  109. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link-wrong-href.json +0 -0
  110. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link-wrong-label.json +0 -0
  111. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link.json +0 -0
  112. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-unknown-type.json +0 -0
  113. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-wrong-field.json +0 -0
  114. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars.json +0 -0
  115. package/src/{__tests__/__snapshots__/sidebars.test.ts.snap → sidebars/__tests__/__snapshots__/index.test.ts.snap} +36 -6
  116. package/src/{__tests__/sidebarItemsGenerator.test.ts → sidebars/__tests__/generator.test.ts} +143 -18
  117. package/src/sidebars/__tests__/index.test.ts +204 -0
  118. package/src/sidebars/__tests__/processor.test.ts +237 -0
  119. package/src/sidebars/__tests__/utils.test.ts +695 -0
  120. package/src/sidebars/__tests__/validation.test.ts +105 -0
  121. package/src/sidebars/generator.ts +310 -0
  122. package/src/sidebars/index.ts +94 -0
  123. package/src/sidebars/normalization.ts +112 -0
  124. package/src/sidebars/processor.ts +154 -0
  125. package/src/sidebars/types.ts +211 -0
  126. package/src/sidebars/utils.ts +329 -0
  127. package/src/sidebars/validation.ts +168 -0
  128. package/src/slug.ts +32 -17
  129. package/src/tags.ts +19 -0
  130. package/src/translations.ts +103 -47
  131. package/src/types.ts +64 -107
  132. package/src/versions.ts +59 -25
  133. package/lib/sidebarItemsGenerator.js +0 -211
  134. package/lib/sidebars.d.ts +0 -43
  135. package/lib/sidebars.js +0 -320
  136. package/src/__tests__/sidebars.test.ts +0 -639
  137. package/src/sidebarItemsGenerator.ts +0 -307
  138. package/src/sidebars.ts +0 -522
package/lib/props.js CHANGED
@@ -6,30 +6,51 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.toVersionMetadataProp = exports.toSidebarsProp = void 0;
9
+ exports.toTagDocListProp = exports.toVersionMetadataProp = exports.toSidebarsProp = void 0;
10
10
  const lodash_1 = require("lodash");
11
+ const docs_1 = require("./docs");
11
12
  function toSidebarsProp(loadedVersion) {
12
- const docsById = lodash_1.keyBy(loadedVersion.docs, (doc) => doc.id);
13
- const convertDocLink = (item) => {
14
- const docId = item.id;
13
+ const docsById = (0, docs_1.createDocsByIdIndex)(loadedVersion.docs);
14
+ function getDocById(docId) {
15
15
  const docMetadata = docsById[docId];
16
16
  if (!docMetadata) {
17
17
  throw new Error(`Invalid sidebars file. The document with id "${docId}" was used in the sidebar, but no document with this id could be found.
18
18
  Available document ids are:
19
19
  - ${Object.keys(docsById).sort().join('\n- ')}`);
20
20
  }
21
+ return docMetadata;
22
+ }
23
+ const convertDocLink = (item) => {
24
+ const docMetadata = getDocById(item.id);
21
25
  const { title, permalink, frontMatter: { sidebar_label: sidebarLabel }, } = docMetadata;
22
26
  return {
23
27
  type: 'link',
24
28
  label: sidebarLabel || item.label || title,
25
29
  href: permalink,
30
+ className: item.className,
26
31
  customProps: item.customProps,
32
+ docId: docMetadata.unversionedId,
27
33
  };
28
34
  };
29
- const normalizeItem = (item) => {
35
+ function getCategoryLinkHref(link) {
36
+ switch (link === null || link === void 0 ? void 0 : link.type) {
37
+ case 'doc':
38
+ return getDocById(link.id).permalink;
39
+ case 'generated-index':
40
+ return link.permalink;
41
+ default:
42
+ return undefined;
43
+ }
44
+ }
45
+ function convertCategory(item) {
46
+ const { link, ...rest } = item;
47
+ const href = getCategoryLinkHref(link);
48
+ return { ...rest, items: item.items.map(normalizeItem), ...(href && { href }) };
49
+ }
50
+ function normalizeItem(item) {
30
51
  switch (item.type) {
31
52
  case 'category':
32
- return { ...item, items: item.items.map(normalizeItem) };
53
+ return convertCategory(item);
33
54
  case 'ref':
34
55
  case 'doc':
35
56
  return convertDocLink(item);
@@ -37,21 +58,52 @@ Available document ids are:
37
58
  default:
38
59
  return item;
39
60
  }
40
- };
61
+ }
41
62
  // Transform the sidebar so that all sidebar item will be in the
42
63
  // form of 'link' or 'category' only.
43
64
  // This is what will be passed as props to the UI component.
44
- return lodash_1.mapValues(loadedVersion.sidebars, (items) => items.map(normalizeItem));
65
+ return (0, lodash_1.mapValues)(loadedVersion.sidebars, (items) => items.map(normalizeItem));
45
66
  }
46
67
  exports.toSidebarsProp = toSidebarsProp;
68
+ function toVersionDocsProp(loadedVersion) {
69
+ return (0, lodash_1.mapValues)((0, lodash_1.keyBy)(loadedVersion.docs, (doc) => doc.unversionedId), (doc) => ({
70
+ id: doc.unversionedId,
71
+ title: doc.title,
72
+ description: doc.description,
73
+ sidebar: doc.sidebar,
74
+ }));
75
+ }
47
76
  function toVersionMetadataProp(pluginId, loadedVersion) {
48
77
  return {
49
78
  pluginId,
50
79
  version: loadedVersion.versionName,
51
80
  label: loadedVersion.versionLabel,
52
81
  banner: loadedVersion.versionBanner,
82
+ badge: loadedVersion.versionBadge,
83
+ className: loadedVersion.versionClassName,
53
84
  isLast: loadedVersion.isLast,
54
85
  docsSidebars: toSidebarsProp(loadedVersion),
86
+ docs: toVersionDocsProp(loadedVersion),
55
87
  };
56
88
  }
57
89
  exports.toVersionMetadataProp = toVersionMetadataProp;
90
+ function toTagDocListProp({ allTagsPath, tag, docs, }) {
91
+ function toDocListProp() {
92
+ const list = (0, lodash_1.compact)(tag.docIds.map((id) => docs.find((doc) => doc.id === id)));
93
+ // Sort docs by title
94
+ list.sort((doc1, doc2) => doc1.title.localeCompare(doc2.title));
95
+ return list.map((doc) => ({
96
+ id: doc.id,
97
+ title: doc.title,
98
+ description: doc.description,
99
+ permalink: doc.permalink,
100
+ }));
101
+ }
102
+ return {
103
+ name: tag.name,
104
+ permalink: tag.permalink,
105
+ docs: toDocListProp(),
106
+ allTagsPath,
107
+ };
108
+ }
109
+ exports.toTagDocListProp = toTagDocListProp;
@@ -0,0 +1,27 @@
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
+ import { PluginContentLoadedActions, RouteConfig } from '@docusaurus/types';
8
+ import { DocMetadata, LoadedVersion } from './types';
9
+ export declare function createCategoryGeneratedIndexRoutes({ version, actions, docCategoryGeneratedIndexComponent, }: {
10
+ version: LoadedVersion;
11
+ actions: PluginContentLoadedActions;
12
+ docCategoryGeneratedIndexComponent: string;
13
+ }): Promise<RouteConfig[]>;
14
+ export declare function createDocRoutes({ docs, actions, docItemComponent, }: {
15
+ docs: DocMetadata[];
16
+ actions: PluginContentLoadedActions;
17
+ docItemComponent: string;
18
+ }): Promise<RouteConfig[]>;
19
+ export declare function createVersionRoutes({ loadedVersion, actions, docItemComponent, docLayoutComponent, docCategoryGeneratedIndexComponent, pluginId, aliasedSource, }: {
20
+ loadedVersion: LoadedVersion;
21
+ actions: PluginContentLoadedActions;
22
+ docLayoutComponent: string;
23
+ docItemComponent: string;
24
+ docCategoryGeneratedIndexComponent: string;
25
+ pluginId: string;
26
+ aliasedSource: (str: string) => string;
27
+ }): Promise<void>;
package/lib/routes.js ADDED
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.createVersionRoutes = exports.createDocRoutes = exports.createCategoryGeneratedIndexRoutes = void 0;
10
+ const tslib_1 = require("tslib");
11
+ const utils_1 = require("@docusaurus/utils");
12
+ const props_1 = require("./props");
13
+ const chalk_1 = (0, tslib_1.__importDefault)(require("chalk"));
14
+ async function createCategoryGeneratedIndexRoutes({ version, actions, docCategoryGeneratedIndexComponent, }) {
15
+ const slugs = (0, utils_1.createSlugger)();
16
+ async function createCategoryGeneratedIndexRoute(categoryGeneratedIndex) {
17
+ const { sidebar, title, description, slug, permalink, previous, next } = categoryGeneratedIndex;
18
+ const propFileName = slugs.slug(`${version.versionPath}-${categoryGeneratedIndex.sidebar}-category-${categoryGeneratedIndex.title}`);
19
+ const prop = {
20
+ title,
21
+ description,
22
+ slug,
23
+ permalink,
24
+ navigation: {
25
+ previous,
26
+ next,
27
+ },
28
+ };
29
+ const propData = await actions.createData(`${(0, utils_1.docuHash)(`category/${propFileName}`)}.json`, JSON.stringify(prop, null, 2));
30
+ return {
31
+ path: permalink,
32
+ component: docCategoryGeneratedIndexComponent,
33
+ exact: true,
34
+ modules: {
35
+ categoryGeneratedIndex: propData,
36
+ },
37
+ // Same as doc, this sidebar route attribute permits to associate this subpage to the given sidebar
38
+ ...(sidebar && { sidebar }),
39
+ };
40
+ }
41
+ return Promise.all(version.categoryGeneratedIndices.map(createCategoryGeneratedIndexRoute));
42
+ }
43
+ exports.createCategoryGeneratedIndexRoutes = createCategoryGeneratedIndexRoutes;
44
+ async function createDocRoutes({ docs, actions, docItemComponent, }) {
45
+ return Promise.all(docs.map(async (metadataItem) => {
46
+ await actions.createData(
47
+ // Note that this created data path must be in sync with
48
+ // metadataPath provided to mdx-loader.
49
+ `${(0, utils_1.docuHash)(metadataItem.source)}.json`, JSON.stringify(metadataItem, null, 2));
50
+ const docRoute = {
51
+ path: metadataItem.permalink,
52
+ component: docItemComponent,
53
+ exact: true,
54
+ modules: {
55
+ content: metadataItem.source,
56
+ },
57
+ // Because the parent (DocPage) comp need to access it easily
58
+ // This permits to render the sidebar once without unmount/remount when navigating (and preserve sidebar state)
59
+ ...(metadataItem.sidebar && {
60
+ sidebar: metadataItem.sidebar,
61
+ }),
62
+ };
63
+ return docRoute;
64
+ }));
65
+ }
66
+ exports.createDocRoutes = createDocRoutes;
67
+ async function createVersionRoutes({ loadedVersion, actions, docItemComponent, docLayoutComponent, docCategoryGeneratedIndexComponent, pluginId, aliasedSource, }) {
68
+ async function doCreateVersionRoutes(version) {
69
+ const versionMetadata = (0, props_1.toVersionMetadataProp)(pluginId, version);
70
+ const versionMetadataPropPath = await actions.createData(`${(0, utils_1.docuHash)(`version-${version.versionName}-metadata-prop`)}.json`, JSON.stringify(versionMetadata, null, 2));
71
+ async function createVersionSubRoutes() {
72
+ const [docRoutes, sidebarsRoutes] = await Promise.all([
73
+ createDocRoutes({ docs: version.docs, actions, docItemComponent }),
74
+ createCategoryGeneratedIndexRoutes({
75
+ version,
76
+ actions,
77
+ docCategoryGeneratedIndexComponent,
78
+ }),
79
+ ]);
80
+ const routes = [...docRoutes, ...sidebarsRoutes];
81
+ return routes.sort((a, b) => a.path.localeCompare(b.path));
82
+ }
83
+ actions.addRoute({
84
+ path: version.versionPath,
85
+ // allow matching /docs/* as well
86
+ exact: false,
87
+ // main docs component (DocPage)
88
+ component: docLayoutComponent,
89
+ // sub-routes for each doc
90
+ routes: await createVersionSubRoutes(),
91
+ modules: {
92
+ versionMetadata: aliasedSource(versionMetadataPropPath),
93
+ },
94
+ priority: version.routePriority,
95
+ });
96
+ }
97
+ try {
98
+ return await doCreateVersionRoutes(loadedVersion);
99
+ }
100
+ catch (e) {
101
+ console.error(chalk_1.default.red(`Can't create version routes for version "${loadedVersion.versionName}"`));
102
+ throw e;
103
+ }
104
+ }
105
+ exports.createVersionRoutes = createVersionRoutes;
@@ -4,12 +4,15 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import { SidebarItemsGenerator } from './types';
7
+ import type { SidebarItemsGenerator, SidebarItemCategoryLinkConfig } from './types';
8
8
  export declare const CategoryMetadataFilenameBase = "_category_";
9
9
  export declare const CategoryMetadataFilenamePattern = "_category_.{json,yml,yaml}";
10
- export declare type CategoryMetadatasFile = {
10
+ export declare type CategoryMetadataFile = {
11
11
  label?: string;
12
12
  position?: number;
13
13
  collapsed?: boolean;
14
+ collapsible?: boolean;
15
+ className?: string;
16
+ link?: SidebarItemCategoryLinkConfig;
14
17
  };
15
18
  export declare const DefaultSidebarItemsGenerator: SidebarItemsGenerator;
@@ -0,0 +1,216 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.DefaultSidebarItemsGenerator = exports.CategoryMetadataFilenamePattern = exports.CategoryMetadataFilenameBase = void 0;
10
+ const tslib_1 = require("tslib");
11
+ const lodash_1 = require("lodash");
12
+ const utils_1 = require("@docusaurus/utils");
13
+ const chalk_1 = (0, tslib_1.__importDefault)(require("chalk"));
14
+ const path_1 = (0, tslib_1.__importDefault)(require("path"));
15
+ const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
16
+ const js_yaml_1 = (0, tslib_1.__importDefault)(require("js-yaml"));
17
+ const validation_1 = require("./validation");
18
+ const docs_1 = require("../docs");
19
+ const BreadcrumbSeparator = '/';
20
+ // To avoid possible name clashes with a folder of the same name as the ID
21
+ const docIdPrefix = '$doc$/';
22
+ // Just an alias to the make code more explicit
23
+ function getLocalDocId(docId) {
24
+ return (0, lodash_1.last)(docId.split('/'));
25
+ }
26
+ exports.CategoryMetadataFilenameBase = '_category_';
27
+ exports.CategoryMetadataFilenamePattern = '_category_.{json,yml,yaml}';
28
+ // TODO I now believe we should read all the category metadata files ahead of time: we may need this metadata to customize docs metadata
29
+ // Example use-case being able to disable number prefix parsing at the folder level, or customize the default route path segment for an intermediate directory...
30
+ // TODO later if there is `CategoryFolder/with-category-name-doc.md`, we may want to read the metadata as yaml on it
31
+ // see https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
32
+ async function readCategoryMetadataFile(categoryDirPath) {
33
+ async function tryReadFile(filePath) {
34
+ const contentString = await fs_extra_1.default.readFile(filePath, { encoding: 'utf8' });
35
+ const unsafeContent = js_yaml_1.default.load(contentString);
36
+ try {
37
+ return (0, validation_1.validateCategoryMetadataFile)(unsafeContent);
38
+ }
39
+ catch (e) {
40
+ console.error(chalk_1.default.red(`The docs sidebar category metadata file looks invalid!\nPath: ${filePath}`));
41
+ throw e;
42
+ }
43
+ }
44
+ // eslint-disable-next-line no-restricted-syntax
45
+ for (const ext of ['.json', '.yml', '.yaml']) {
46
+ // Simpler to use only posix paths for mocking file metadata in tests
47
+ const filePath = (0, utils_1.posixPath)(path_1.default.join(categoryDirPath, `${exports.CategoryMetadataFilenameBase}${ext}`));
48
+ if (await fs_extra_1.default.pathExists(filePath)) {
49
+ return tryReadFile(filePath);
50
+ }
51
+ }
52
+ return null;
53
+ }
54
+ // Comment for this feature: https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
55
+ const DefaultSidebarItemsGenerator = async ({ numberPrefixParser, docs: allDocs, options, item: { dirName: autogenDir }, version, }) => {
56
+ const docsById = (0, docs_1.createDocsByIdIndex)(allDocs);
57
+ const findDoc = (docId) => docsById[docId];
58
+ const getDoc = (docId) => {
59
+ const doc = findDoc(docId);
60
+ if (!doc) {
61
+ throw new Error(`Can't find any doc with id=${docId}.\nAvailable doc ids:\n- ${Object.keys(docsById).join('\n- ')}`);
62
+ }
63
+ return doc;
64
+ };
65
+ /**
66
+ * Step 1. Extract the docs that are in the autogen dir.
67
+ */
68
+ function getAutogenDocs() {
69
+ function isInAutogeneratedDir(doc) {
70
+ return (
71
+ // Doc at the root of the autogenerated sidebar dir
72
+ doc.sourceDirName === autogenDir ||
73
+ // autogen dir is . and doc is in subfolder
74
+ autogenDir === '.' ||
75
+ // autogen dir is not . and doc is in subfolder
76
+ // "api/myDoc" startsWith "api/" (note "api2/myDoc" is not included)
77
+ doc.sourceDirName.startsWith((0, utils_1.addTrailingSlash)(autogenDir)));
78
+ }
79
+ const docs = allDocs.filter(isInAutogeneratedDir);
80
+ if (docs.length === 0) {
81
+ console.warn(chalk_1.default.yellow(`No docs found in dir ${autogenDir}: can't auto-generate a sidebar.`));
82
+ }
83
+ return docs;
84
+ }
85
+ /**
86
+ * Step 2. Turn the linear file list into a tree structure.
87
+ */
88
+ function treeify(docs) {
89
+ // Get the category breadcrumb of a doc (relative to the dir of the autogenerated sidebar item)
90
+ // autogenDir=a/b and docDir=a/b/c/d => returns [c, d]
91
+ // autogenDir=a/b and docDir=a/b => returns []
92
+ // TODO: try to use path.relative()
93
+ function getRelativeBreadcrumb(doc) {
94
+ return autogenDir === doc.sourceDirName
95
+ ? []
96
+ : doc.sourceDirName
97
+ .replace((0, utils_1.addTrailingSlash)(autogenDir), '')
98
+ .split(BreadcrumbSeparator);
99
+ }
100
+ const treeRoot = {};
101
+ docs.forEach((doc) => {
102
+ const breadcrumb = getRelativeBreadcrumb(doc);
103
+ let currentDir = treeRoot; // We walk down the file's path to generate the fs structure
104
+ // eslint-disable-next-line no-restricted-syntax
105
+ for (const dir of breadcrumb) {
106
+ if (typeof currentDir[dir] === 'undefined') {
107
+ currentDir[dir] = {}; // Create new folder.
108
+ }
109
+ currentDir = currentDir[dir]; // Go into the subdirectory.
110
+ }
111
+ currentDir[`${docIdPrefix}${doc.id}`] = null; // We've walked through the file path. Register the file in this directory.
112
+ });
113
+ return treeRoot;
114
+ }
115
+ /**
116
+ * Step 3. Recursively transform the tree-like file structure to sidebar items.
117
+ * (From a record to an array of items, akin to normalizing shorthand)
118
+ */
119
+ function generateSidebar(fsModel) {
120
+ function createDocItem(id) {
121
+ const { sidebarPosition: position, frontMatter: { sidebar_label: label, sidebar_class_name: className }, } = getDoc(id);
122
+ return {
123
+ type: 'doc',
124
+ id,
125
+ position,
126
+ // We don't want these fields to magically appear in the generated sidebar
127
+ ...(label !== undefined && { label }),
128
+ ...(className !== undefined && { className }),
129
+ };
130
+ }
131
+ async function createCategoryItem(dir, fullPath, folderName) {
132
+ var _a, _b, _c, _d;
133
+ const categoryPath = path_1.default.join(version.contentPath, autogenDir, fullPath);
134
+ const categoryMetadata = await readCategoryMetadataFile(categoryPath);
135
+ const className = categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.className;
136
+ const { filename, numberPrefix } = numberPrefixParser(folderName);
137
+ const allItems = await Promise.all(Object.entries(dir).map(([key, content]) => dirToItem(content, key, `${fullPath}/${key}`)));
138
+ // Try to match a doc inside the category folder,
139
+ // using the "local id" (myDoc) or "qualified id" (dirName/myDoc)
140
+ function findDocByLocalId(localId) {
141
+ return allItems.find((item) => item.type === 'doc' && getLocalDocId(item.id) === localId);
142
+ }
143
+ function findConventionalCategoryDocLink() {
144
+ return allItems.find((item) => item.type === 'doc' && (0, docs_1.isConventionalDocIndex)(getDoc(item.id)));
145
+ }
146
+ function getCategoryLinkedDocId() {
147
+ var _a, _b;
148
+ const link = categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.link;
149
+ if (link) {
150
+ if (link.type === 'doc') {
151
+ return ((_a = findDocByLocalId(link.id)) === null || _a === void 0 ? void 0 : _a.id) || getDoc(link.id).id;
152
+ }
153
+ else {
154
+ // We don't continue for other link types on purpose!
155
+ // IE if user decide to use type "generated-index", we should not pick a README.md file as the linked doc
156
+ return undefined;
157
+ }
158
+ }
159
+ // Apply default convention to pick index.md, README.md or <categoryName>.md as the category doc
160
+ return (_b = findConventionalCategoryDocLink()) === null || _b === void 0 ? void 0 : _b.id;
161
+ }
162
+ const categoryLinkedDocId = getCategoryLinkedDocId();
163
+ const link = categoryLinkedDocId
164
+ ? {
165
+ type: 'doc',
166
+ id: categoryLinkedDocId, // We "remap" a potentially "local id" to a "qualified id"
167
+ }
168
+ : // TODO typing issue
169
+ categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.link;
170
+ // If a doc is linked, remove it from the category subItems
171
+ const items = allItems.filter((item) => !(item.type === 'doc' && item.id === categoryLinkedDocId));
172
+ return {
173
+ type: 'category',
174
+ label: (_a = categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.label) !== null && _a !== void 0 ? _a : filename,
175
+ collapsible: (_b = categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.collapsible) !== null && _b !== void 0 ? _b : options.sidebarCollapsible,
176
+ collapsed: (_c = categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.collapsed) !== null && _c !== void 0 ? _c : options.sidebarCollapsed,
177
+ position: (_d = categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.position) !== null && _d !== void 0 ? _d : numberPrefix,
178
+ ...(className !== undefined && { className }),
179
+ items,
180
+ ...(link && { link }),
181
+ };
182
+ }
183
+ async function dirToItem(dir, // The directory item to be transformed.
184
+ itemKey, // For docs, it's the doc ID; for categories, it's used to generate the next `relativePath`.
185
+ fullPath) {
186
+ return dir
187
+ ? createCategoryItem(dir, fullPath, itemKey)
188
+ : createDocItem(itemKey.substring(docIdPrefix.length));
189
+ }
190
+ return Promise.all(Object.entries(fsModel).map(([key, content]) => dirToItem(content, key, key)));
191
+ }
192
+ /**
193
+ * Step 4. Recursively sort the categories/docs + remove the "position" attribute from final output.
194
+ * Note: the "position" is only used to sort "inside" a sidebar slice. It is not
195
+ * used to sort across multiple consecutive sidebar slices (ie a whole Category
196
+ * composed of multiple autogenerated items)
197
+ */
198
+ function sortItems(sidebarItems) {
199
+ const processedSidebarItems = sidebarItems.map((item) => {
200
+ if (item.type === 'category') {
201
+ return { ...item, items: sortItems(item.items) };
202
+ }
203
+ return item;
204
+ });
205
+ const sortedSidebarItems = (0, lodash_1.sortBy)(processedSidebarItems, (item) => item.position);
206
+ return sortedSidebarItems.map(({ position, ...item }) => item);
207
+ }
208
+ // TODO: the whole code is designed for pipeline operator
209
+ // return getAutogenDocs() |> treeify |> await generateSidebar(^) |> sortItems;
210
+ const docs = getAutogenDocs();
211
+ const fsModel = treeify(docs);
212
+ const sidebarWithPosition = await generateSidebar(fsModel);
213
+ const sortedSidebar = sortItems(sidebarWithPosition);
214
+ return sortedSidebar;
215
+ };
216
+ exports.DefaultSidebarItemsGenerator = DefaultSidebarItemsGenerator;
@@ -0,0 +1,15 @@
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
+ import type { SidebarsConfig, Sidebars, NormalizedSidebars } from './types';
8
+ import type { NormalizeSidebarsParams, PluginOptions } from '../types';
9
+ import { SidebarProcessorParams } from './processor';
10
+ export declare const DefaultSidebars: SidebarsConfig;
11
+ export declare const DisabledSidebars: SidebarsConfig;
12
+ export declare function resolveSidebarPathOption(siteDir: string, sidebarPathOption: PluginOptions['sidebarPath']): PluginOptions['sidebarPath'];
13
+ export declare function loadSidebarsFile(sidebarFilePath: string | false | undefined): SidebarsConfig;
14
+ export declare function loadNormalizedSidebars(sidebarFilePath: string | false | undefined, params: NormalizeSidebarsParams): NormalizedSidebars;
15
+ export declare function loadSidebars(sidebarFilePath: string | false | undefined, options: SidebarProcessorParams): Promise<Sidebars>;
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.loadSidebars = exports.loadNormalizedSidebars = exports.loadSidebarsFile = exports.resolveSidebarPathOption = exports.DisabledSidebars = exports.DefaultSidebars = void 0;
10
+ const tslib_1 = require("tslib");
11
+ const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
12
+ const import_fresh_1 = (0, tslib_1.__importDefault)(require("import-fresh"));
13
+ const validation_1 = require("./validation");
14
+ const normalization_1 = require("./normalization");
15
+ const processor_1 = require("./processor");
16
+ const path_1 = (0, tslib_1.__importDefault)(require("path"));
17
+ const utils_1 = require("@docusaurus/utils");
18
+ exports.DefaultSidebars = {
19
+ defaultSidebar: [
20
+ {
21
+ type: 'autogenerated',
22
+ dirName: '.',
23
+ },
24
+ ],
25
+ };
26
+ exports.DisabledSidebars = {};
27
+ // If a path is provided, make it absolute
28
+ // use this before loadSidebars()
29
+ function resolveSidebarPathOption(siteDir, sidebarPathOption) {
30
+ return sidebarPathOption
31
+ ? path_1.default.resolve(siteDir, sidebarPathOption)
32
+ : sidebarPathOption;
33
+ }
34
+ exports.resolveSidebarPathOption = resolveSidebarPathOption;
35
+ function loadSidebarsFileUnsafe(sidebarFilePath) {
36
+ // false => no sidebars
37
+ if (sidebarFilePath === false) {
38
+ return exports.DisabledSidebars;
39
+ }
40
+ // undefined => defaults to autogenerated sidebars
41
+ if (typeof sidebarFilePath === 'undefined') {
42
+ return exports.DefaultSidebars;
43
+ }
44
+ // Non-existent sidebars file: no sidebars
45
+ // Note: this edge case can happen on versioned docs, not current version
46
+ // We avoid creating empty versioned sidebars file with the CLI
47
+ if (!fs_extra_1.default.existsSync(sidebarFilePath)) {
48
+ return exports.DisabledSidebars;
49
+ }
50
+ // We don't want sidebars to be cached because of hot reloading.
51
+ return (0, import_fresh_1.default)(sidebarFilePath);
52
+ }
53
+ function loadSidebarsFile(sidebarFilePath) {
54
+ const sidebarsConfig = loadSidebarsFileUnsafe(sidebarFilePath);
55
+ (0, validation_1.validateSidebars)(sidebarsConfig);
56
+ return sidebarsConfig;
57
+ }
58
+ exports.loadSidebarsFile = loadSidebarsFile;
59
+ function loadNormalizedSidebars(sidebarFilePath, params) {
60
+ return (0, normalization_1.normalizeSidebars)(loadSidebarsFile(sidebarFilePath), params);
61
+ }
62
+ exports.loadNormalizedSidebars = loadNormalizedSidebars;
63
+ // Note: sidebarFilePath must be absolute, use resolveSidebarPathOption
64
+ async function loadSidebars(sidebarFilePath, options) {
65
+ const normalizeSidebarsParams = {
66
+ ...options.sidebarOptions,
67
+ version: options.version,
68
+ categoryLabelSlugger: (0, utils_1.createSlugger)(),
69
+ };
70
+ const normalizedSidebars = loadNormalizedSidebars(sidebarFilePath, normalizeSidebarsParams);
71
+ return (0, processor_1.processSidebars)(normalizedSidebars, options);
72
+ }
73
+ exports.loadSidebars = loadSidebars;
@@ -0,0 +1,14 @@
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
+ import type { NormalizeSidebarsParams } from '../types';
8
+ import type { NormalizedSidebarItem, NormalizedSidebars, SidebarItemConfig, SidebarsConfig } from './types';
9
+ /**
10
+ * Normalizes recursively item and all its children. Ensures that at the end
11
+ * each item will be an object with the corresponding type.
12
+ */
13
+ export declare function normalizeItem(item: SidebarItemConfig, options: NormalizeSidebarsParams): NormalizedSidebarItem[];
14
+ export declare function normalizeSidebars(sidebars: SidebarsConfig, params: NormalizeSidebarsParams): NormalizedSidebars;
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.normalizeSidebars = exports.normalizeItem = void 0;
10
+ const utils_1 = require("./utils");
11
+ const lodash_1 = require("lodash");
12
+ const utils_2 = require("@docusaurus/utils");
13
+ function normalizeCategoryLink(category, params) {
14
+ var _a, _b;
15
+ if (((_a = category.link) === null || _a === void 0 ? void 0 : _a.type) === 'generated-index') {
16
+ // default slug logic can be improved
17
+ const getDefaultSlug = () => `/category/${params.categoryLabelSlugger.slug(category.label)}`;
18
+ const slug = (_b = category.link.slug) !== null && _b !== void 0 ? _b : getDefaultSlug();
19
+ const permalink = (0, utils_2.normalizeUrl)([params.version.versionPath, slug]);
20
+ return {
21
+ ...category.link,
22
+ slug,
23
+ permalink,
24
+ };
25
+ }
26
+ return category.link;
27
+ }
28
+ function normalizeCategoriesShorthand(sidebar, options) {
29
+ return Object.entries(sidebar).map(([label, items]) => ({
30
+ type: 'category',
31
+ collapsed: options.sidebarCollapsed,
32
+ collapsible: options.sidebarCollapsible,
33
+ label,
34
+ items,
35
+ }));
36
+ }
37
+ /**
38
+ * Normalizes recursively item and all its children. Ensures that at the end
39
+ * each item will be an object with the corresponding type.
40
+ */
41
+ function normalizeItem(item, options) {
42
+ var _a, _b, _c;
43
+ if (typeof item === 'string') {
44
+ return [
45
+ {
46
+ type: 'doc',
47
+ id: item,
48
+ },
49
+ ];
50
+ }
51
+ if ((0, utils_1.isCategoriesShorthand)(item)) {
52
+ return normalizeCategoriesShorthand(item, options).flatMap((subItem) => normalizeItem(subItem, options));
53
+ }
54
+ if (item.type === 'category') {
55
+ const link = normalizeCategoryLink(item, options);
56
+ const normalizedCategory = {
57
+ ...item,
58
+ link,
59
+ items: ((_a = item.items) !== null && _a !== void 0 ? _a : []).flatMap((subItem) => normalizeItem(subItem, options)),
60
+ collapsible: (_b = item.collapsible) !== null && _b !== void 0 ? _b : options.sidebarCollapsible,
61
+ collapsed: (_c = item.collapsed) !== null && _c !== void 0 ? _c : options.sidebarCollapsed,
62
+ };
63
+ return [normalizedCategory];
64
+ }
65
+ return [item];
66
+ }
67
+ exports.normalizeItem = normalizeItem;
68
+ function normalizeSidebar(sidebar, options) {
69
+ const normalizedSidebar = Array.isArray(sidebar)
70
+ ? sidebar
71
+ : normalizeCategoriesShorthand(sidebar, options);
72
+ return normalizedSidebar.flatMap((subItem) => normalizeItem(subItem, options));
73
+ }
74
+ function normalizeSidebars(sidebars, params) {
75
+ return (0, lodash_1.mapValues)(sidebars, (items) => normalizeSidebar(items, params));
76
+ }
77
+ exports.normalizeSidebars = normalizeSidebars;