@docusaurus/plugin-content-docs 0.0.0-4660 → 0.0.0-4665

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.
@@ -14,8 +14,6 @@ const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
14
14
  const path_1 = tslib_1.__importDefault(require("path"));
15
15
  const docs_1 = require("../docs");
16
16
  const BreadcrumbSeparator = '/';
17
- // To avoid possible name clashes with a folder of the same name as the ID
18
- const docIdPrefix = '$doc$/';
19
17
  // Just an alias to the make code more explicit
20
18
  function getLocalDocId(docId) {
21
19
  return lodash_1.default.last(docId.split('/'));
@@ -72,14 +70,16 @@ const DefaultSidebarItemsGenerator = async ({ numberPrefixParser, isCategoryInde
72
70
  const treeRoot = {};
73
71
  docs.forEach((doc) => {
74
72
  const breadcrumb = getRelativeBreadcrumb(doc);
75
- let currentDir = treeRoot; // We walk down the file's path to generate the fs structure
73
+ // We walk down the file's path to generate the fs structure
74
+ let currentDir = treeRoot;
76
75
  breadcrumb.forEach((dir) => {
77
76
  if (typeof currentDir[dir] === 'undefined') {
78
77
  currentDir[dir] = {}; // Create new folder.
79
78
  }
80
79
  currentDir = currentDir[dir]; // Go into the subdirectory.
81
80
  });
82
- currentDir[`${docIdPrefix}${doc.id}`] = null; // We've walked through the file path. Register the file in this directory.
81
+ // We've walked through the path. Register the file in this directory.
82
+ currentDir[path_1.default.basename(doc.source)] = doc.id;
83
83
  });
84
84
  return treeRoot;
85
85
  }
@@ -88,23 +88,24 @@ const DefaultSidebarItemsGenerator = async ({ numberPrefixParser, isCategoryInde
88
88
  * (From a record to an array of items, akin to normalizing shorthand)
89
89
  */
90
90
  function generateSidebar(fsModel) {
91
- function createDocItem(id) {
91
+ function createDocItem(id, fullPath, fileName) {
92
92
  const { sidebarPosition: position, frontMatter: { sidebar_label: label, sidebar_class_name: className }, } = getDoc(id);
93
93
  return {
94
94
  type: 'doc',
95
95
  id,
96
96
  position,
97
+ source: fileName,
97
98
  // We don't want these fields to magically appear in the generated
98
99
  // sidebar
99
100
  ...(label !== undefined && { label }),
100
101
  ...(className !== undefined && { className }),
101
102
  };
102
103
  }
103
- async function createCategoryItem(dir, fullPath, folderName) {
104
+ function createCategoryItem(dir, fullPath, folderName) {
104
105
  const categoryMetadata = categoriesMetadata[(0, utils_1.posixPath)(path_1.default.join(autogenDir, fullPath))];
105
106
  const className = categoryMetadata?.className;
106
107
  const { filename, numberPrefix } = numberPrefixParser(folderName);
107
- const allItems = await Promise.all(Object.entries(dir).map(([key, content]) => dirToItem(content, key, `${fullPath}/${key}`)));
108
+ const allItems = Object.entries(dir).map(([key, content]) => dirToItem(content, key, `${fullPath}/${key}`));
108
109
  // Try to match a doc inside the category folder,
109
110
  // using the "local id" (myDoc) or "qualified id" (dirName/myDoc)
110
111
  function findDocByLocalId(localId) {
@@ -147,19 +148,20 @@ const DefaultSidebarItemsGenerator = async ({ numberPrefixParser, isCategoryInde
147
148
  collapsible: categoryMetadata?.collapsible,
148
149
  collapsed: categoryMetadata?.collapsed,
149
150
  position: categoryMetadata?.position ?? numberPrefix,
151
+ source: folderName,
150
152
  ...(className !== undefined && { className }),
151
153
  items,
152
154
  ...(link && { link }),
153
155
  };
154
156
  }
155
- async function dirToItem(dir, // The directory item to be transformed.
156
- itemKey, // For docs, it's the doc ID; for categories, it's used to generate the next `relativePath`.
157
+ function dirToItem(dir, // The directory item to be transformed.
158
+ itemKey, // File/folder name; for categories, it's used to generate the next `relativePath`.
157
159
  fullPath) {
158
- return dir
160
+ return typeof dir === 'object'
159
161
  ? createCategoryItem(dir, fullPath, itemKey)
160
- : createDocItem(itemKey.substring(docIdPrefix.length));
162
+ : createDocItem(dir, fullPath, itemKey);
161
163
  }
162
- return Promise.all(Object.entries(fsModel).map(([key, content]) => dirToItem(content, key, key)));
164
+ return Object.entries(fsModel).map(([key, content]) => dirToItem(content, key, key));
163
165
  }
164
166
  /**
165
167
  * Step 4. Recursively sort the categories/docs + remove the "position"
@@ -175,13 +177,16 @@ const DefaultSidebarItemsGenerator = async ({ numberPrefixParser, isCategoryInde
175
177
  }
176
178
  return item;
177
179
  });
178
- const sortedSidebarItems = lodash_1.default.sortBy(processedSidebarItems, (item) => item.position);
179
- return sortedSidebarItems.map(({ position, ...item }) => item);
180
+ const sortedSidebarItems = lodash_1.default.sortBy(processedSidebarItems, [
181
+ 'position',
182
+ 'source',
183
+ ]);
184
+ return sortedSidebarItems.map(({ position, source, ...item }) => item);
180
185
  }
181
186
  // TODO: the whole code is designed for pipeline operator
182
187
  const docs = getAutogenDocs();
183
188
  const fsModel = treeify(docs);
184
- const sidebarWithPosition = await generateSidebar(fsModel);
189
+ const sidebarWithPosition = generateSidebar(fsModel);
185
190
  const sortedSidebar = sortItems(sidebarWithPosition);
186
191
  return sortedSidebar;
187
192
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/plugin-content-docs",
3
- "version": "0.0.0-4660",
3
+ "version": "0.0.0-4665",
4
4
  "description": "Docs plugin for Docusaurus.",
5
5
  "main": "lib/index.js",
6
6
  "exports": {
@@ -23,11 +23,11 @@
23
23
  },
24
24
  "license": "MIT",
25
25
  "dependencies": {
26
- "@docusaurus/core": "0.0.0-4660",
27
- "@docusaurus/logger": "0.0.0-4660",
28
- "@docusaurus/mdx-loader": "0.0.0-4660",
29
- "@docusaurus/utils": "0.0.0-4660",
30
- "@docusaurus/utils-validation": "0.0.0-4660",
26
+ "@docusaurus/core": "0.0.0-4665",
27
+ "@docusaurus/logger": "0.0.0-4665",
28
+ "@docusaurus/mdx-loader": "0.0.0-4665",
29
+ "@docusaurus/utils": "0.0.0-4665",
30
+ "@docusaurus/utils-validation": "0.0.0-4665",
31
31
  "combine-promises": "^1.1.0",
32
32
  "fs-extra": "^10.0.1",
33
33
  "import-fresh": "^3.3.0",
@@ -39,8 +39,8 @@
39
39
  "webpack": "^5.69.1"
40
40
  },
41
41
  "devDependencies": {
42
- "@docusaurus/module-type-aliases": "0.0.0-4660",
43
- "@docusaurus/types": "0.0.0-4660",
42
+ "@docusaurus/module-type-aliases": "0.0.0-4665",
43
+ "@docusaurus/types": "0.0.0-4665",
44
44
  "@types/js-yaml": "^4.0.5",
45
45
  "@types/picomatch": "^2.3.0",
46
46
  "commander": "^5.1.0",
@@ -56,5 +56,5 @@
56
56
  "engines": {
57
57
  "node": ">=14"
58
58
  },
59
- "gitHead": "78417b2a5827489b7e0d8b255843800151615a7b"
59
+ "gitHead": "88df79f3adcef89d18f58736a8293bfdb011d2d3"
60
60
  }
@@ -20,8 +20,6 @@ import path from 'path';
20
20
  import {createDocsByIdIndex, toCategoryIndexMatcherParam} from '../docs';
21
21
 
22
22
  const BreadcrumbSeparator = '/';
23
- // To avoid possible name clashes with a folder of the same name as the ID
24
- const docIdPrefix = '$doc$/';
25
23
 
26
24
  // Just an alias to the make code more explicit
27
25
  function getLocalDocId(docId: string): string {
@@ -31,16 +29,20 @@ function getLocalDocId(docId: string): string {
31
29
  export const CategoryMetadataFilenameBase = '_category_';
32
30
  export const CategoryMetadataFilenamePattern = '_category_.{json,yml,yaml}';
33
31
 
34
- type WithPosition<T> = T & {position?: number};
32
+ type WithPosition<T> = T & {
33
+ position?: number;
34
+ /** The source is the file/folder name */
35
+ source?: string;
36
+ };
35
37
 
36
38
  /**
37
39
  * A representation of the fs structure. For each object entry:
38
40
  * If it's a folder, the key is the directory name, and value is the directory
39
- * content; If it's a doc file, the key is the doc id prefixed with '$doc$/',
40
- * and value is null
41
+ * content; If it's a doc file, the key is the doc's source file name, and value
42
+ * is the doc ID
41
43
  */
42
44
  type Dir = {
43
- [item: string]: Dir | null;
45
+ [item: string]: Dir | string;
44
46
  };
45
47
 
46
48
  // Comment for this feature: https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
@@ -108,14 +110,16 @@ export const DefaultSidebarItemsGenerator: SidebarItemsGenerator = async ({
108
110
  const treeRoot: Dir = {};
109
111
  docs.forEach((doc) => {
110
112
  const breadcrumb = getRelativeBreadcrumb(doc);
111
- let currentDir = treeRoot; // We walk down the file's path to generate the fs structure
113
+ // We walk down the file's path to generate the fs structure
114
+ let currentDir = treeRoot;
112
115
  breadcrumb.forEach((dir) => {
113
116
  if (typeof currentDir[dir] === 'undefined') {
114
117
  currentDir[dir] = {}; // Create new folder.
115
118
  }
116
- currentDir = currentDir[dir]!; // Go into the subdirectory.
119
+ currentDir = currentDir[dir] as Dir; // Go into the subdirectory.
117
120
  });
118
- currentDir[`${docIdPrefix}${doc.id}`] = null; // We've walked through the file path. Register the file in this directory.
121
+ // We've walked through the path. Register the file in this directory.
122
+ currentDir[path.basename(doc.source)] = doc.id;
119
123
  });
120
124
  return treeRoot;
121
125
  }
@@ -126,8 +130,12 @@ export const DefaultSidebarItemsGenerator: SidebarItemsGenerator = async ({
126
130
  */
127
131
  function generateSidebar(
128
132
  fsModel: Dir,
129
- ): Promise<WithPosition<NormalizedSidebarItem>[]> {
130
- function createDocItem(id: string): WithPosition<SidebarItemDoc> {
133
+ ): WithPosition<NormalizedSidebarItem>[] {
134
+ function createDocItem(
135
+ id: string,
136
+ fullPath: string,
137
+ fileName: string,
138
+ ): WithPosition<SidebarItemDoc> {
131
139
  const {
132
140
  sidebarPosition: position,
133
141
  frontMatter: {sidebar_label: label, sidebar_class_name: className},
@@ -136,25 +144,24 @@ export const DefaultSidebarItemsGenerator: SidebarItemsGenerator = async ({
136
144
  type: 'doc',
137
145
  id,
138
146
  position,
147
+ source: fileName,
139
148
  // We don't want these fields to magically appear in the generated
140
149
  // sidebar
141
150
  ...(label !== undefined && {label}),
142
151
  ...(className !== undefined && {className}),
143
152
  };
144
153
  }
145
- async function createCategoryItem(
154
+ function createCategoryItem(
146
155
  dir: Dir,
147
156
  fullPath: string,
148
157
  folderName: string,
149
- ): Promise<WithPosition<NormalizedSidebarItemCategory>> {
158
+ ): WithPosition<NormalizedSidebarItemCategory> {
150
159
  const categoryMetadata =
151
160
  categoriesMetadata[posixPath(path.join(autogenDir, fullPath))];
152
161
  const className = categoryMetadata?.className;
153
162
  const {filename, numberPrefix} = numberPrefixParser(folderName);
154
- const allItems = await Promise.all(
155
- Object.entries(dir).map(([key, content]) =>
156
- dirToItem(content, key, `${fullPath}/${key}`),
157
- ),
163
+ const allItems = Object.entries(dir).map(([key, content]) =>
164
+ dirToItem(content, key, `${fullPath}/${key}`),
158
165
  );
159
166
 
160
167
  // Try to match a doc inside the category folder,
@@ -211,24 +218,23 @@ export const DefaultSidebarItemsGenerator: SidebarItemsGenerator = async ({
211
218
  collapsible: categoryMetadata?.collapsible,
212
219
  collapsed: categoryMetadata?.collapsed,
213
220
  position: categoryMetadata?.position ?? numberPrefix,
221
+ source: folderName,
214
222
  ...(className !== undefined && {className}),
215
223
  items,
216
224
  ...(link && {link}),
217
225
  };
218
226
  }
219
- async function dirToItem(
220
- dir: Dir | null, // The directory item to be transformed.
221
- itemKey: string, // For docs, it's the doc ID; for categories, it's used to generate the next `relativePath`.
227
+ function dirToItem(
228
+ dir: Dir | string, // The directory item to be transformed.
229
+ itemKey: string, // File/folder name; for categories, it's used to generate the next `relativePath`.
222
230
  fullPath: string, // `dir`'s full path relative to the autogen dir.
223
- ): Promise<WithPosition<NormalizedSidebarItem>> {
224
- return dir
231
+ ): WithPosition<NormalizedSidebarItem> {
232
+ return typeof dir === 'object'
225
233
  ? createCategoryItem(dir, fullPath, itemKey)
226
- : createDocItem(itemKey.substring(docIdPrefix.length));
234
+ : createDocItem(dir, fullPath, itemKey);
227
235
  }
228
- return Promise.all(
229
- Object.entries(fsModel).map(([key, content]) =>
230
- dirToItem(content, key, key),
231
- ),
236
+ return Object.entries(fsModel).map(([key, content]) =>
237
+ dirToItem(content, key, key),
232
238
  );
233
239
  }
234
240
 
@@ -248,16 +254,16 @@ export const DefaultSidebarItemsGenerator: SidebarItemsGenerator = async ({
248
254
  }
249
255
  return item;
250
256
  });
251
- const sortedSidebarItems = _.sortBy(
252
- processedSidebarItems,
253
- (item) => item.position,
254
- );
255
- return sortedSidebarItems.map(({position, ...item}) => item);
257
+ const sortedSidebarItems = _.sortBy(processedSidebarItems, [
258
+ 'position',
259
+ 'source',
260
+ ]);
261
+ return sortedSidebarItems.map(({position, source, ...item}) => item);
256
262
  }
257
263
  // TODO: the whole code is designed for pipeline operator
258
264
  const docs = getAutogenDocs();
259
265
  const fsModel = treeify(docs);
260
- const sidebarWithPosition = await generateSidebar(fsModel);
266
+ const sidebarWithPosition = generateSidebar(fsModel);
261
267
  const sortedSidebar = sortItems(sidebarWithPosition);
262
268
  return sortedSidebar;
263
269
  };