@docusaurus/plugin-content-docs 0.0.0-4489 → 0.0.0-4490

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/docs.d.ts CHANGED
@@ -7,7 +7,7 @@
7
7
  import type { LoadContext } from '@docusaurus/types';
8
8
  import type { DocFile, DocMetadataBase, VersionMetadata, LoadedVersion } from './types';
9
9
  import type { SidebarsUtils } from './sidebars/utils';
10
- import type { MetadataOptions, PluginOptions } from '@docusaurus/plugin-content-docs';
10
+ import type { MetadataOptions, PluginOptions, CategoryIndexMatcher, CategoryIndexMatcherParam } from '@docusaurus/plugin-content-docs';
11
11
  declare type LastUpdateOptions = Pick<PluginOptions, 'showLastUpdateAuthor' | 'showLastUpdateTime'>;
12
12
  export declare function readDocFile(versionMetadata: Pick<VersionMetadata, 'contentPath' | 'contentPathLocalized'>, source: string, options: LastUpdateOptions): Promise<DocFile>;
13
13
  export declare function readVersionDocs(versionMetadata: VersionMetadata, options: Pick<PluginOptions, 'include' | 'exclude' | 'showLastUpdateAuthor' | 'showLastUpdateTime'>): Promise<DocFile[]>;
@@ -29,10 +29,22 @@ export declare function getMainDocId({ docs, sidebarsUtils, }: {
29
29
  docs: DocMetadataBase[];
30
30
  sidebarsUtils: SidebarsUtils;
31
31
  }): string;
32
- export declare function isConventionalDocIndex(doc: {
33
- source: DocMetadataBase['slug'];
34
- sourceDirName: DocMetadataBase['sourceDirName'];
35
- }): boolean;
32
+ export declare const isCategoryIndex: CategoryIndexMatcher;
33
+ export declare function toCategoryIndexMatcherParam({ source, sourceDirName, }: Pick<DocMetadataBase, 'source' | 'sourceDirName'>): CategoryIndexMatcherParam;
34
+ /**
35
+ * guides/sidebar/autogenerated.md -> 'autogenerated', '.md', ['sidebar', 'guides']
36
+ */
37
+ export declare function splitPath(str: string): {
38
+ /**
39
+ * The list of directories, from lowest level to highest.
40
+ * If there's no dir name, directories is ['.']
41
+ */
42
+ directories: string[];
43
+ /** The file name, without extension */
44
+ fileName: string;
45
+ /** The extension, with a leading dot */
46
+ extension: string;
47
+ };
36
48
  export declare function getDocIds(doc: DocMetadataBase): [string, string];
37
49
  export declare function createDocsByIdIndex<Doc extends {
38
50
  id: string;
package/lib/docs.js CHANGED
@@ -6,7 +6,7 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.createDocsByIdIndex = exports.getDocIds = exports.isConventionalDocIndex = exports.getMainDocId = exports.addDocNavigation = exports.processDocMetadata = exports.readVersionDocs = exports.readDocFile = void 0;
9
+ exports.createDocsByIdIndex = exports.getDocIds = exports.splitPath = exports.toCategoryIndexMatcherParam = exports.isCategoryIndex = exports.getMainDocId = exports.addDocNavigation = exports.processDocMetadata = exports.readVersionDocs = exports.readDocFile = void 0;
10
10
  const tslib_1 = require("tslib");
11
11
  const path_1 = (0, tslib_1.__importDefault)(require("path"));
12
12
  const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
@@ -232,26 +232,46 @@ function getMainDocId({ docs, sidebarsUtils, }) {
232
232
  return getMainDoc().unversionedId;
233
233
  }
234
234
  exports.getMainDocId = getMainDocId;
235
- function getLastPathSegment(str) {
236
- return (0, lodash_1.last)(str.split('/'));
237
- }
238
235
  // By convention, Docusaurus considers some docs are "indexes":
239
236
  // - index.md
240
237
  // - readme.md
241
238
  // - <folder>/<folder>.md
242
239
  //
240
+ // This function is the default implementation of this convention
241
+ //
243
242
  // Those index docs produce a different behavior
244
243
  // - Slugs do not end with a weird "/index" suffix
245
244
  // - Auto-generated sidebar categories link to them as intro
246
- function isConventionalDocIndex(doc) {
247
- // "@site/docs/folder/subFolder/subSubFolder/myDoc.md" => "myDoc"
248
- const docName = path_1.default.parse(doc.source).name;
249
- // "folder/subFolder/subSubFolder" => "subSubFolder"
250
- const lastDirName = getLastPathSegment(doc.sourceDirName);
251
- const eligibleDocIndexNames = ['index', 'readme', lastDirName.toLowerCase()];
252
- return eligibleDocIndexNames.includes(docName.toLowerCase());
245
+ const isCategoryIndex = ({ fileName, directories, }) => {
246
+ var _a;
247
+ const eligibleDocIndexNames = [
248
+ 'index',
249
+ 'readme',
250
+ (_a = directories[0]) === null || _a === void 0 ? void 0 : _a.toLowerCase(),
251
+ ];
252
+ return eligibleDocIndexNames.includes(fileName.toLowerCase());
253
+ };
254
+ exports.isCategoryIndex = isCategoryIndex;
255
+ function toCategoryIndexMatcherParam({ source, sourceDirName, }) {
256
+ // source + sourceDirName are always posix-style
257
+ return {
258
+ fileName: path_1.default.posix.parse(source).name,
259
+ extension: path_1.default.posix.parse(source).ext,
260
+ directories: sourceDirName.split(path_1.default.posix.sep).reverse(),
261
+ };
262
+ }
263
+ exports.toCategoryIndexMatcherParam = toCategoryIndexMatcherParam;
264
+ /**
265
+ * guides/sidebar/autogenerated.md -> 'autogenerated', '.md', ['sidebar', 'guides']
266
+ */
267
+ function splitPath(str) {
268
+ return {
269
+ fileName: path_1.default.parse(str).name,
270
+ extension: path_1.default.parse(str).ext,
271
+ directories: path_1.default.dirname(str).split(path_1.default.sep).reverse(),
272
+ };
253
273
  }
254
- exports.isConventionalDocIndex = isConventionalDocIndex;
274
+ exports.splitPath = splitPath;
255
275
  // Return both doc ids
256
276
  // TODO legacy retro-compatibility due to old versioned sidebars using versioned doc ids
257
277
  // ("id" should be removed & "versionedId" should be renamed to "id")
@@ -45,7 +45,7 @@ async function readCategoryMetadataFile(categoryDirPath) {
45
45
  return filePath ? tryReadFile(filePath) : null;
46
46
  }
47
47
  // Comment for this feature: https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
48
- const DefaultSidebarItemsGenerator = async ({ numberPrefixParser, docs: allDocs, options, item: { dirName: autogenDir }, version, }) => {
48
+ const DefaultSidebarItemsGenerator = async ({ numberPrefixParser, isCategoryIndex, docs: allDocs, options, item: { dirName: autogenDir }, version, }) => {
49
49
  const docsById = (0, docs_1.createDocsByIdIndex)(allDocs);
50
50
  const findDoc = (docId) => docsById[docId];
51
51
  const getDoc = (docId) => {
@@ -133,7 +133,13 @@ const DefaultSidebarItemsGenerator = async ({ numberPrefixParser, docs: allDocs,
133
133
  return allItems.find((item) => item.type === 'doc' && getLocalDocId(item.id) === localId);
134
134
  }
135
135
  function findConventionalCategoryDocLink() {
136
- return allItems.find((item) => item.type === 'doc' && (0, docs_1.isConventionalDocIndex)(getDoc(item.id)));
136
+ return allItems.find((item) => {
137
+ if (item.type !== 'doc') {
138
+ return false;
139
+ }
140
+ const doc = getDoc(item.id);
141
+ return isCategoryIndex((0, docs_1.toCategoryIndexMatcherParam)(doc));
142
+ });
137
143
  }
138
144
  function getCategoryLinkedDocId() {
139
145
  var _a, _b;
@@ -13,6 +13,7 @@ const generator_1 = require("./generator");
13
13
  const lodash_1 = require("lodash");
14
14
  const combine_promises_1 = (0, tslib_1.__importDefault)(require("combine-promises"));
15
15
  const normalization_1 = require("./normalization");
16
+ const docs_1 = require("../docs");
16
17
  function toSidebarItemsGeneratorDoc(doc) {
17
18
  return (0, lodash_1.pick)(doc, [
18
19
  'id',
@@ -46,6 +47,7 @@ async function processSidebar(unprocessedSidebar, params) {
46
47
  item,
47
48
  numberPrefixParser,
48
49
  defaultSidebarItemsGenerator: generator_1.DefaultSidebarItemsGenerator,
50
+ isCategoryIndex: docs_1.isCategoryIndex,
49
51
  ...getSidebarItemsGeneratorDocsAndVersion(),
50
52
  options: sidebarOptions,
51
53
  });
@@ -6,7 +6,7 @@
6
6
  */
7
7
  import type { Optional, Required } from 'utility-types';
8
8
  import type { DocMetadataBase, VersionMetadata } from '../types';
9
- import type { NumberPrefixParser, SidebarOptions } from '@docusaurus/plugin-content-docs';
9
+ import type { NumberPrefixParser, SidebarOptions, CategoryIndexMatcher } from '@docusaurus/plugin-content-docs';
10
10
  declare type Expand<T extends Record<string, unknown>> = {
11
11
  [P in keyof T]: T[P];
12
12
  };
@@ -121,6 +121,7 @@ export declare type SidebarItemsGeneratorArgs = {
121
121
  version: SidebarItemsGeneratorVersion;
122
122
  docs: SidebarItemsGeneratorDoc[];
123
123
  numberPrefixParser: NumberPrefixParser;
124
+ isCategoryIndex: CategoryIndexMatcher;
124
125
  options: SidebarOptions;
125
126
  };
126
127
  export declare type SidebarItemsGenerator = (generatorArgs: SidebarItemsGeneratorArgs) => Promise<SidebarItem[]>;
package/lib/slug.d.ts CHANGED
@@ -9,7 +9,7 @@ import type { NumberPrefixParser } from '@docusaurus/plugin-content-docs';
9
9
  export default function getSlug({ baseID, frontMatterSlug, source, sourceDirName, stripDirNumberPrefixes, numberPrefixParser, }: {
10
10
  baseID: string;
11
11
  frontMatterSlug?: string;
12
- source: DocMetadataBase['slug'];
12
+ source: DocMetadataBase['source'];
13
13
  sourceDirName: DocMetadataBase['sourceDirName'];
14
14
  stripDirNumberPrefixes?: boolean;
15
15
  numberPrefixParser?: NumberPrefixParser;
package/lib/slug.js CHANGED
@@ -25,7 +25,8 @@ function getSlug({ baseID, frontMatterSlug, source, sourceDirName, stripDirNumbe
25
25
  }
26
26
  else {
27
27
  const dirNameSlug = getDirNameSlug();
28
- if (!frontMatterSlug && (0, docs_1.isConventionalDocIndex)({ source, sourceDirName })) {
28
+ if (!frontMatterSlug &&
29
+ (0, docs_1.isCategoryIndex)((0, docs_1.toCategoryIndexMatcherParam)({ source, sourceDirName }))) {
29
30
  return dirNameSlug;
30
31
  }
31
32
  const baseSlug = frontMatterSlug || baseID;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/plugin-content-docs",
3
- "version": "0.0.0-4489",
3
+ "version": "0.0.0-4490",
4
4
  "description": "Docs plugin for Docusaurus.",
5
5
  "main": "lib/index.js",
6
6
  "exports": {
@@ -22,11 +22,11 @@
22
22
  },
23
23
  "license": "MIT",
24
24
  "dependencies": {
25
- "@docusaurus/core": "0.0.0-4489",
26
- "@docusaurus/logger": "0.0.0-4489",
27
- "@docusaurus/mdx-loader": "0.0.0-4489",
28
- "@docusaurus/utils": "0.0.0-4489",
29
- "@docusaurus/utils-validation": "0.0.0-4489",
25
+ "@docusaurus/core": "0.0.0-4490",
26
+ "@docusaurus/logger": "0.0.0-4490",
27
+ "@docusaurus/mdx-loader": "0.0.0-4490",
28
+ "@docusaurus/utils": "0.0.0-4490",
29
+ "@docusaurus/utils-validation": "0.0.0-4490",
30
30
  "combine-promises": "^1.1.0",
31
31
  "fs-extra": "^10.0.0",
32
32
  "import-fresh": "^3.2.2",
@@ -39,8 +39,8 @@
39
39
  "webpack": "^5.61.0"
40
40
  },
41
41
  "devDependencies": {
42
- "@docusaurus/module-type-aliases": "0.0.0-4489",
43
- "@docusaurus/types": "0.0.0-4489",
42
+ "@docusaurus/module-type-aliases": "0.0.0-4490",
43
+ "@docusaurus/types": "0.0.0-4490",
44
44
  "@types/js-yaml": "^4.0.0",
45
45
  "@types/picomatch": "^2.2.1",
46
46
  "commander": "^5.1.0",
@@ -55,5 +55,5 @@
55
55
  "engines": {
56
56
  "node": ">=14"
57
57
  },
58
- "gitHead": "f4020aaf26717ce5e5085a949cf3b7e088bc386f"
58
+ "gitHead": "70934672dc6eab5689cccd41349a03213ff49122"
59
59
  }
package/src/docs.ts CHANGED
@@ -8,7 +8,7 @@
8
8
  import path from 'path';
9
9
  import fs from 'fs-extra';
10
10
  import logger from '@docusaurus/logger';
11
- import {keyBy, last} from 'lodash';
11
+ import {keyBy} from 'lodash';
12
12
  import {
13
13
  aliasedSitePath,
14
14
  getEditUrl,
@@ -41,6 +41,8 @@ import {toDocNavigationLink, toNavigationLink} from './sidebars/utils';
41
41
  import type {
42
42
  MetadataOptions,
43
43
  PluginOptions,
44
+ CategoryIndexMatcher,
45
+ CategoryIndexMatcherParam,
44
46
  } from '@docusaurus/plugin-content-docs';
45
47
 
46
48
  type LastUpdateOptions = Pick<
@@ -367,31 +369,62 @@ export function getMainDocId({
367
369
  return getMainDoc().unversionedId;
368
370
  }
369
371
 
370
- function getLastPathSegment(str: string): string {
371
- return last(str.split('/'))!;
372
- }
373
-
374
372
  // By convention, Docusaurus considers some docs are "indexes":
375
373
  // - index.md
376
374
  // - readme.md
377
375
  // - <folder>/<folder>.md
378
376
  //
377
+ // This function is the default implementation of this convention
378
+ //
379
379
  // Those index docs produce a different behavior
380
380
  // - Slugs do not end with a weird "/index" suffix
381
381
  // - Auto-generated sidebar categories link to them as intro
382
- export function isConventionalDocIndex(doc: {
383
- source: DocMetadataBase['slug'];
384
- sourceDirName: DocMetadataBase['sourceDirName'];
385
- }): boolean {
386
- // "@site/docs/folder/subFolder/subSubFolder/myDoc.md" => "myDoc"
387
- const docName = path.parse(doc.source).name;
388
-
389
- // "folder/subFolder/subSubFolder" => "subSubFolder"
390
- const lastDirName = getLastPathSegment(doc.sourceDirName);
391
-
392
- const eligibleDocIndexNames = ['index', 'readme', lastDirName.toLowerCase()];
382
+ export const isCategoryIndex: CategoryIndexMatcher = ({
383
+ fileName,
384
+ directories,
385
+ }): boolean => {
386
+ const eligibleDocIndexNames = [
387
+ 'index',
388
+ 'readme',
389
+ directories[0]?.toLowerCase(),
390
+ ];
391
+ return eligibleDocIndexNames.includes(fileName.toLowerCase());
392
+ };
393
+
394
+ export function toCategoryIndexMatcherParam({
395
+ source,
396
+ sourceDirName,
397
+ }: Pick<
398
+ DocMetadataBase,
399
+ 'source' | 'sourceDirName'
400
+ >): CategoryIndexMatcherParam {
401
+ // source + sourceDirName are always posix-style
402
+ return {
403
+ fileName: path.posix.parse(source).name,
404
+ extension: path.posix.parse(source).ext,
405
+ directories: sourceDirName.split(path.posix.sep).reverse(),
406
+ };
407
+ }
393
408
 
394
- return eligibleDocIndexNames.includes(docName.toLowerCase());
409
+ /**
410
+ * guides/sidebar/autogenerated.md -> 'autogenerated', '.md', ['sidebar', 'guides']
411
+ */
412
+ export function splitPath(str: string): {
413
+ /**
414
+ * The list of directories, from lowest level to highest.
415
+ * If there's no dir name, directories is ['.']
416
+ */
417
+ directories: string[];
418
+ /** The file name, without extension */
419
+ fileName: string;
420
+ /** The extension, with a leading dot */
421
+ extension: string;
422
+ } {
423
+ return {
424
+ fileName: path.parse(str).name,
425
+ extension: path.parse(str).ext,
426
+ directories: path.dirname(str).split(path.sep).reverse(),
427
+ };
395
428
  }
396
429
 
397
430
  // Return both doc ids
@@ -13,6 +13,15 @@ declare module '@docusaurus/plugin-content-docs' {
13
13
  numberPrefix?: number;
14
14
  };
15
15
 
16
+ export type CategoryIndexMatcherParam = {
17
+ fileName: string;
18
+ directories: string[];
19
+ extension: string;
20
+ };
21
+ export type CategoryIndexMatcher = (
22
+ param: CategoryIndexMatcherParam,
23
+ ) => boolean;
24
+
16
25
  export type EditUrlFunction = (editUrlParams: {
17
26
  version: string;
18
27
  versionDocsDirPath: string;
@@ -25,7 +25,7 @@ import path from 'path';
25
25
  import fs from 'fs-extra';
26
26
  import Yaml from 'js-yaml';
27
27
  import {validateCategoryMetadataFile} from './validation';
28
- import {createDocsByIdIndex, isConventionalDocIndex} from '../docs';
28
+ import {createDocsByIdIndex, toCategoryIndexMatcherParam} from '../docs';
29
29
 
30
30
  const BreadcrumbSeparator = '/';
31
31
  // To avoid possible name clashes with a folder of the same name as the ID
@@ -94,6 +94,7 @@ async function readCategoryMetadataFile(
94
94
  // Comment for this feature: https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
95
95
  export const DefaultSidebarItemsGenerator: SidebarItemsGenerator = async ({
96
96
  numberPrefixParser,
97
+ isCategoryIndex,
97
98
  docs: allDocs,
98
99
  options,
99
100
  item: {dirName: autogenDir},
@@ -210,10 +211,13 @@ export const DefaultSidebarItemsGenerator: SidebarItemsGenerator = async ({
210
211
  }
211
212
 
212
213
  function findConventionalCategoryDocLink(): SidebarItemDoc | undefined {
213
- return allItems.find(
214
- (item) =>
215
- item.type === 'doc' && isConventionalDocIndex(getDoc(item.id)),
216
- ) as SidebarItemDoc | undefined;
214
+ return allItems.find((item) => {
215
+ if (item.type !== 'doc') {
216
+ return false;
217
+ }
218
+ const doc = getDoc(item.id);
219
+ return isCategoryIndex(toCategoryIndexMatcherParam(doc));
220
+ }) as SidebarItemDoc | undefined;
217
221
  }
218
222
 
219
223
  function getCategoryLinkedDocId(): string | undefined {
@@ -25,6 +25,7 @@ import {DefaultSidebarItemsGenerator} from './generator';
25
25
  import {mapValues, memoize, pick} from 'lodash';
26
26
  import combinePromises from 'combine-promises';
27
27
  import {normalizeItem} from './normalization';
28
+ import {isCategoryIndex} from '../docs';
28
29
  import type {Slugger} from '@docusaurus/utils';
29
30
  import type {
30
31
  NumberPrefixParser,
@@ -95,6 +96,7 @@ async function processSidebar(
95
96
  item,
96
97
  numberPrefixParser,
97
98
  defaultSidebarItemsGenerator: DefaultSidebarItemsGenerator,
99
+ isCategoryIndex,
98
100
  ...getSidebarItemsGeneratorDocsAndVersion(),
99
101
  options: sidebarOptions,
100
102
  });
@@ -10,6 +10,7 @@ import type {DocMetadataBase, VersionMetadata} from '../types';
10
10
  import type {
11
11
  NumberPrefixParser,
12
12
  SidebarOptions,
13
+ CategoryIndexMatcher,
13
14
  } from '@docusaurus/plugin-content-docs';
14
15
 
15
16
  // Makes all properties visible when hovering over the type
@@ -195,6 +196,7 @@ export type SidebarItemsGeneratorArgs = {
195
196
  version: SidebarItemsGeneratorVersion;
196
197
  docs: SidebarItemsGeneratorDoc[];
197
198
  numberPrefixParser: NumberPrefixParser;
199
+ isCategoryIndex: CategoryIndexMatcher;
198
200
  options: SidebarOptions;
199
201
  };
200
202
  export type SidebarItemsGenerator = (
package/src/slug.ts CHANGED
@@ -16,7 +16,7 @@ import {
16
16
  stripPathNumberPrefixes,
17
17
  } from './numberPrefix';
18
18
  import type {DocMetadataBase} from './types';
19
- import {isConventionalDocIndex} from './docs';
19
+ import {isCategoryIndex, toCategoryIndexMatcherParam} from './docs';
20
20
  import type {NumberPrefixParser} from '@docusaurus/plugin-content-docs';
21
21
 
22
22
  export default function getSlug({
@@ -29,7 +29,7 @@ export default function getSlug({
29
29
  }: {
30
30
  baseID: string;
31
31
  frontMatterSlug?: string;
32
- source: DocMetadataBase['slug'];
32
+ source: DocMetadataBase['source'];
33
33
  sourceDirName: DocMetadataBase['sourceDirName'];
34
34
  stripDirNumberPrefixes?: boolean;
35
35
  numberPrefixParser?: NumberPrefixParser;
@@ -50,7 +50,10 @@ export default function getSlug({
50
50
  return frontMatterSlug;
51
51
  } else {
52
52
  const dirNameSlug = getDirNameSlug();
53
- if (!frontMatterSlug && isConventionalDocIndex({source, sourceDirName})) {
53
+ if (
54
+ !frontMatterSlug &&
55
+ isCategoryIndex(toCategoryIndexMatcherParam({source, sourceDirName}))
56
+ ) {
54
57
  return dirNameSlug;
55
58
  }
56
59
  const baseSlug = frontMatterSlug || baseID;
package/src/types.ts CHANGED
@@ -82,8 +82,8 @@ export type DocMetadataBase = LastUpdateData & {
82
82
  version: string;
83
83
  title: string;
84
84
  description: string;
85
- source: string; // @site aliased source => "@site/docs/folder/subFolder/subSubFolder/myDoc.md"
86
- sourceDirName: string; // relative to the versioned docs folder (can be ".") => "folder/subFolder/subSubFolder"
85
+ source: string; // @site aliased posix source => "@site/docs/folder/subFolder/subSubFolder/myDoc.md"
86
+ sourceDirName: string; // posix path relative to the versioned docs folder (can be ".") => "folder/subFolder/subSubFolder"
87
87
  slug: string;
88
88
  permalink: string;
89
89
  sidebarPosition?: number;