@docusaurus/plugin-content-docs 2.4.1 → 3.0.0-beta.0

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.
@@ -20,16 +20,6 @@ function getVersionFileName(versionName) {
20
20
  // but it's for consistency with site/versioned_docs
21
21
  return `version-${versionName}`;
22
22
  }
23
- // TODO legacy, the sidebar name is like "version-2.0.0-alpha.66/docs"
24
- // input: "version-2.0.0-alpha.66/docs"
25
- // output: "docs"
26
- function getNormalizedSidebarName({ versionName, sidebarName, }) {
27
- if (versionName === constants_1.CURRENT_VERSION_NAME || !sidebarName.includes('/')) {
28
- return sidebarName;
29
- }
30
- const [, ...rest] = sidebarName.split('/');
31
- return rest.join('/');
32
- }
33
23
  function getSidebarTranslationFileContent(sidebar, sidebarName) {
34
24
  const categories = (0, utils_2.collectSidebarCategories)(sidebar);
35
25
  const categoryContent = Object.fromEntries(categories.flatMap((category) => {
@@ -127,21 +117,12 @@ function translateSidebar({ sidebar, sidebarName, sidebarsTranslations, }) {
127
117
  });
128
118
  }
129
119
  function getSidebarsTranslations(version) {
130
- return (0, utils_1.mergeTranslations)(Object.entries(version.sidebars).map(([sidebarName, sidebar]) => {
131
- const normalizedSidebarName = getNormalizedSidebarName({
132
- sidebarName,
133
- versionName: version.versionName,
134
- });
135
- return getSidebarTranslationFileContent(sidebar, normalizedSidebarName);
136
- }));
120
+ return (0, utils_1.mergeTranslations)(Object.entries(version.sidebars).map(([sidebarName, sidebar]) => getSidebarTranslationFileContent(sidebar, sidebarName)));
137
121
  }
138
122
  function translateSidebars(version, sidebarsTranslations) {
139
123
  return lodash_1.default.mapValues(version.sidebars, (sidebar, sidebarName) => translateSidebar({
140
124
  sidebar,
141
- sidebarName: getNormalizedSidebarName({
142
- sidebarName,
143
- versionName: version.versionName,
144
- }),
125
+ sidebarName,
145
126
  sidebarsTranslations,
146
127
  }));
147
128
  }
package/lib/types.d.ts CHANGED
@@ -7,28 +7,29 @@
7
7
  import type { BrokenMarkdownLink, Tag } from '@docusaurus/utils';
8
8
  import type { VersionMetadata, LoadedVersion, CategoryGeneratedIndexMetadata } from '@docusaurus/plugin-content-docs';
9
9
  import type { SidebarsUtils } from './sidebars/utils';
10
- export declare type DocFile = {
10
+ export type DocFile = {
11
11
  contentPath: string;
12
12
  filePath: string;
13
13
  source: string;
14
14
  content: string;
15
15
  };
16
- export declare type SourceToPermalink = {
16
+ export type SourceToPermalink = {
17
17
  [source: string]: string;
18
18
  };
19
- export declare type VersionTag = Tag & {
19
+ export type VersionTag = Tag & {
20
20
  /** All doc ids having this tag. */
21
21
  docIds: string[];
22
+ unlisted: boolean;
22
23
  };
23
- export declare type VersionTags = {
24
+ export type VersionTags = {
24
25
  [permalink: string]: VersionTag;
25
26
  };
26
- export declare type FullVersion = LoadedVersion & {
27
+ export type FullVersion = LoadedVersion & {
27
28
  sidebarsUtils: SidebarsUtils;
28
29
  categoryGeneratedIndices: CategoryGeneratedIndexMetadata[];
29
30
  };
30
- export declare type DocBrokenMarkdownLink = BrokenMarkdownLink<VersionMetadata>;
31
- export declare type DocsMarkdownOption = {
31
+ export type DocBrokenMarkdownLink = BrokenMarkdownLink<VersionMetadata>;
32
+ export type DocsMarkdownOption = {
32
33
  versionsMetadata: VersionMetadata[];
33
34
  siteDir: string;
34
35
  sourceToPermalink: SourceToPermalink;
@@ -4,9 +4,10 @@
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 type { PluginOptions, VersionBanner, VersionMetadata } from '@docusaurus/plugin-content-docs';
7
+ import type { FullVersion } from '../types';
8
8
  import type { LoadContext } from '@docusaurus/types';
9
- export declare type VersionContext = {
9
+ import type { LoadedVersion, PluginOptions, VersionBanner, VersionMetadata } from '@docusaurus/plugin-content-docs';
10
+ export type VersionContext = {
10
11
  /** The version name to get banner of. */
11
12
  versionName: string;
12
13
  /** All versions, ordered from newest to oldest. */
@@ -35,3 +36,4 @@ export declare function readVersionsMetadata({ context, options, }: {
35
36
  context: LoadContext;
36
37
  options: PluginOptions;
37
38
  }): Promise<VersionMetadata[]>;
39
+ export declare function toFullVersion(version: LoadedVersion): FullVersion;
@@ -6,13 +6,15 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.readVersionsMetadata = exports.filterVersions = exports.getVersionNoIndex = exports.getVersionBadge = exports.getVersionBanner = exports.getDefaultVersionBanner = void 0;
9
+ exports.toFullVersion = exports.readVersionsMetadata = exports.filterVersions = exports.getVersionNoIndex = exports.getVersionBadge = exports.getVersionBanner = exports.getDefaultVersionBanner = void 0;
10
10
  const tslib_1 = require("tslib");
11
11
  const path_1 = tslib_1.__importDefault(require("path"));
12
12
  const utils_1 = require("@docusaurus/utils");
13
13
  const constants_1 = require("../constants");
14
14
  const validation_1 = require("./validation");
15
15
  const files_1 = require("./files");
16
+ const utils_2 = require("../sidebars/utils");
17
+ const categoryGeneratedIndex_1 = require("../categoryGeneratedIndex");
16
18
  function getVersionEditUrls({ contentPath, contentPathLocalized, context, options, }) {
17
19
  // If the user is using the functional form of editUrl,
18
20
  // she has total freedom and we can't compute a "version edit url"
@@ -157,3 +159,15 @@ async function readVersionsMetadata({ context, options, }) {
157
159
  return versionsMetadata;
158
160
  }
159
161
  exports.readVersionsMetadata = readVersionsMetadata;
162
+ function toFullVersion(version) {
163
+ const sidebarsUtils = (0, utils_2.createSidebarsUtils)(version.sidebars);
164
+ return {
165
+ ...version,
166
+ sidebarsUtils,
167
+ categoryGeneratedIndices: (0, categoryGeneratedIndex_1.getCategoryGeneratedIndexMetadataList)({
168
+ docs: version.docs,
169
+ sidebarsUtils,
170
+ }),
171
+ };
172
+ }
173
+ exports.toFullVersion = toFullVersion;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/plugin-content-docs",
3
- "version": "2.4.1",
3
+ "version": "3.0.0-beta.0",
4
4
  "description": "Docs plugin for Docusaurus.",
5
5
  "main": "lib/index.js",
6
6
  "sideEffects": false,
@@ -35,22 +35,22 @@
35
35
  },
36
36
  "license": "MIT",
37
37
  "dependencies": {
38
- "@docusaurus/core": "2.4.1",
39
- "@docusaurus/logger": "2.4.1",
40
- "@docusaurus/mdx-loader": "2.4.1",
41
- "@docusaurus/module-type-aliases": "2.4.1",
42
- "@docusaurus/types": "2.4.1",
43
- "@docusaurus/utils": "2.4.1",
44
- "@docusaurus/utils-validation": "2.4.1",
45
- "@types/react-router-config": "^5.0.6",
38
+ "@docusaurus/core": "3.0.0-beta.0",
39
+ "@docusaurus/logger": "3.0.0-beta.0",
40
+ "@docusaurus/mdx-loader": "3.0.0-beta.0",
41
+ "@docusaurus/module-type-aliases": "3.0.0-beta.0",
42
+ "@docusaurus/types": "3.0.0-beta.0",
43
+ "@docusaurus/utils": "3.0.0-beta.0",
44
+ "@docusaurus/utils-validation": "3.0.0-beta.0",
45
+ "@types/react-router-config": "^5.0.7",
46
46
  "combine-promises": "^1.1.0",
47
- "fs-extra": "^10.1.0",
47
+ "fs-extra": "^11.1.1",
48
48
  "import-fresh": "^3.3.0",
49
49
  "js-yaml": "^4.1.0",
50
50
  "lodash": "^4.17.21",
51
- "tslib": "^2.4.0",
51
+ "tslib": "^2.6.0",
52
52
  "utility-types": "^3.10.0",
53
- "webpack": "^5.73.0"
53
+ "webpack": "^5.88.1"
54
54
  },
55
55
  "devDependencies": {
56
56
  "@types/js-yaml": "^4.0.5",
@@ -60,11 +60,11 @@
60
60
  "shelljs": "^0.8.5"
61
61
  },
62
62
  "peerDependencies": {
63
- "react": "^16.8.4 || ^17.0.0",
64
- "react-dom": "^16.8.4 || ^17.0.0"
63
+ "react": "^18.0.0",
64
+ "react-dom": "^18.0.0"
65
65
  },
66
66
  "engines": {
67
67
  "node": ">=16.14"
68
68
  },
69
- "gitHead": "60e657d8ae5a4a9ed1c2d777f9defd882cc12681"
69
+ "gitHead": "27a1e90d9fff88af90ecad35bea16d4d7230482a"
70
70
  }
@@ -37,7 +37,8 @@ export type GlobalDoc = {
37
37
  */
38
38
  id: string;
39
39
  path: string;
40
- sidebar: string | undefined;
40
+ sidebar?: string;
41
+ unlisted?: boolean;
41
42
  };
42
43
 
43
44
  export type GlobalVersion = {
package/src/docs.ts CHANGED
@@ -7,6 +7,7 @@
7
7
 
8
8
  import path from 'path';
9
9
  import fs from 'fs-extra';
10
+ import _ from 'lodash';
10
11
  import logger from '@docusaurus/logger';
11
12
  import {
12
13
  aliasedSitePath,
@@ -18,11 +19,12 @@ import {
18
19
  posixPath,
19
20
  Globby,
20
21
  normalizeFrontMatterTags,
22
+ isUnlisted,
23
+ isDraft,
21
24
  } from '@docusaurus/utils';
22
25
 
23
26
  import {getFileLastUpdate} from './lastUpdate';
24
27
  import getSlug from './slug';
25
- import {CURRENT_VERSION_NAME} from './constants';
26
28
  import {stripPathNumberPrefixes} from './numberPrefix';
27
29
  import {validateDocFrontMatter} from './frontMatter';
28
30
  import {toDocNavigationLink, toNavigationLink} from './sidebars/utils';
@@ -35,7 +37,6 @@ import type {
35
37
  PropNavigationLink,
36
38
  LastUpdateData,
37
39
  VersionMetadata,
38
- DocFrontMatter,
39
40
  LoadedVersion,
40
41
  FileChange,
41
42
  } from '@docusaurus/plugin-content-docs';
@@ -125,17 +126,6 @@ export async function readVersionDocs(
125
126
 
126
127
  export type DocEnv = 'production' | 'development';
127
128
 
128
- /** Docs with draft front matter are only considered draft in production. */
129
- function isDraftForEnvironment({
130
- env,
131
- frontMatter,
132
- }: {
133
- frontMatter: DocFrontMatter;
134
- env: DocEnv;
135
- }): boolean {
136
- return (env === 'production' && frontMatter.draft) ?? false;
137
- }
138
-
139
129
  async function doProcessDocMetadata({
140
130
  docFile,
141
131
  versionMetadata,
@@ -198,14 +188,6 @@ async function doProcessDocMetadata({
198
188
  const sidebarPosition: number | undefined =
199
189
  frontMatter.sidebar_position ?? numberPrefix;
200
190
 
201
- // TODO legacy retrocompatibility
202
- // The same doc in 2 distinct version could keep the same id,
203
- // we just need to namespace the data by version
204
- const versionIdPrefix =
205
- versionMetadata.versionName === CURRENT_VERSION_NAME
206
- ? undefined
207
- : `version-${versionMetadata.versionName}`;
208
-
209
191
  // TODO legacy retrocompatibility
210
192
  // I think it's bad to affect the front matter id with the dirname?
211
193
  function computeDirNameIdPrefix() {
@@ -218,13 +200,7 @@ async function doProcessDocMetadata({
218
200
  : sourceDirName;
219
201
  }
220
202
 
221
- const unversionedId = [computeDirNameIdPrefix(), baseID]
222
- .filter(Boolean)
223
- .join('/');
224
-
225
- // TODO is versioning the id very useful in practice?
226
- // legacy versioned id, requires a breaking change to modify this
227
- const id = [versionIdPrefix, unversionedId].filter(Boolean).join('/');
203
+ const id = [computeDirNameIdPrefix(), baseID].filter(Boolean).join('/');
228
204
 
229
205
  const docSlug = getSlug({
230
206
  baseID,
@@ -268,7 +244,8 @@ async function doProcessDocMetadata({
268
244
  return undefined;
269
245
  }
270
246
 
271
- const draft = isDraftForEnvironment({env, frontMatter});
247
+ const draft = isDraft({env, frontMatter});
248
+ const unlisted = isUnlisted({env, frontMatter});
272
249
 
273
250
  const formatDate = (locale: string, date: Date, calendar: string): string => {
274
251
  try {
@@ -290,7 +267,6 @@ async function doProcessDocMetadata({
290
267
  // Adding properties to object after instantiation will cause hidden
291
268
  // class transitions.
292
269
  return {
293
- unversionedId,
294
270
  id,
295
271
  title,
296
272
  description,
@@ -299,6 +275,7 @@ async function doProcessDocMetadata({
299
275
  slug: docSlug,
300
276
  permalink,
301
277
  draft,
278
+ unlisted,
302
279
  editUrl: customEditURL !== undefined ? customEditURL : getDocEditUrl(),
303
280
  tags: normalizeFrontMatterTags(versionMetadata.tagsPath, frontMatter.tags),
304
281
  version: versionMetadata.versionName,
@@ -333,25 +310,27 @@ export async function processDocMetadata(args: {
333
310
  }
334
311
  }
335
312
 
336
- export function addDocNavigation(
337
- docsBase: DocMetadataBase[],
338
- sidebarsUtils: SidebarsUtils,
339
- sidebarFilePath: string,
340
- ): LoadedVersion['docs'] {
341
- const docsById = createDocsByIdIndex(docsBase);
313
+ function getUnlistedIds(docs: DocMetadataBase[]): Set<string> {
314
+ return new Set(docs.filter((doc) => doc.unlisted).map((doc) => doc.id));
315
+ }
342
316
 
343
- sidebarsUtils.checkSidebarsDocIds(
344
- docsBase.flatMap(getDocIds),
345
- sidebarFilePath,
346
- );
317
+ export function addDocNavigation({
318
+ docs,
319
+ sidebarsUtils,
320
+ }: {
321
+ docs: DocMetadataBase[];
322
+ sidebarsUtils: SidebarsUtils;
323
+ }): LoadedVersion['docs'] {
324
+ const docsById = createDocsByIdIndex(docs);
325
+ const unlistedIds = getUnlistedIds(docs);
347
326
 
348
327
  // Add sidebar/next/previous to the docs
349
328
  function addNavData(doc: DocMetadataBase): DocMetadata {
350
- const navigation = sidebarsUtils.getDocNavigation(
351
- doc.unversionedId,
352
- doc.id,
353
- doc.frontMatter.displayed_sidebar,
354
- );
329
+ const navigation = sidebarsUtils.getDocNavigation({
330
+ docId: doc.id,
331
+ displayedSidebar: doc.frontMatter.displayed_sidebar,
332
+ unlistedIds,
333
+ });
355
334
 
356
335
  const toNavigationLinkByDocId = (
357
336
  docId: string | null | undefined,
@@ -367,6 +346,10 @@ export function addDocNavigation(
367
346
  `Error when loading ${doc.id} in ${doc.sourceDirName}: the pagination_${type} front matter points to a non-existent ID ${docId}.`,
368
347
  );
369
348
  }
349
+ // Gracefully handle explicitly providing an unlisted doc ID in production
350
+ if (navDoc.unlisted) {
351
+ return undefined;
352
+ }
370
353
  return toDocNavigationLink(navDoc);
371
354
  };
372
355
 
@@ -382,7 +365,7 @@ export function addDocNavigation(
382
365
  return {...doc, sidebar: navigation.sidebarName, previous, next};
383
366
  }
384
367
 
385
- const docsWithNavigation = docsBase.map(addNavData);
368
+ const docsWithNavigation = docs.map(addNavData);
386
369
  // Sort to ensure consistent output for tests
387
370
  docsWithNavigation.sort((a, b) => a.id.localeCompare(b.id));
388
371
  return docsWithNavigation;
@@ -409,16 +392,12 @@ export function getMainDocId({
409
392
  if (versionHomeDoc) {
410
393
  return versionHomeDoc;
411
394
  } else if (firstDocIdOfFirstSidebar) {
412
- return docs.find(
413
- (doc) =>
414
- doc.id === firstDocIdOfFirstSidebar ||
415
- doc.unversionedId === firstDocIdOfFirstSidebar,
416
- )!;
395
+ return docs.find((doc) => doc.id === firstDocIdOfFirstSidebar)!;
417
396
  }
418
397
  return docs[0]!;
419
398
  }
420
399
 
421
- return getMainDoc().unversionedId;
400
+ return getMainDoc().id;
422
401
  }
423
402
 
424
403
  // By convention, Docusaurus considers some docs are "indexes":
@@ -462,25 +441,9 @@ export function toCategoryIndexMatcherParam({
462
441
  };
463
442
  }
464
443
 
465
- // Return both doc ids
466
- // TODO legacy retro-compatibility due to old versioned sidebars using
467
- // versioned doc ids ("id" should be removed & "versionedId" should be renamed
468
- // to "id")
469
- export function getDocIds(doc: DocMetadataBase): [string, string] {
470
- return [doc.unversionedId, doc.id];
471
- }
472
-
473
- // Docs are indexed by both versioned and unversioned ids at the same time
474
- // TODO legacy retro-compatibility due to old versioned sidebars using
475
- // versioned doc ids ("id" should be removed & "versionedId" should be renamed
476
- // to "id")
477
- export function createDocsByIdIndex<
478
- Doc extends {id: string; unversionedId: string},
479
- >(docs: Doc[]): {[docId: string]: Doc} {
480
- return Object.fromEntries(
481
- docs.flatMap((doc) => [
482
- [doc.unversionedId, doc],
483
- [doc.id, doc],
484
- ]),
485
- );
444
+ // Docs are indexed by their id
445
+ export function createDocsByIdIndex<Doc extends {id: string}>(
446
+ docs: Doc[],
447
+ ): {[docId: string]: Doc} {
448
+ return _.keyBy(docs, (d) => d.id);
486
449
  }
@@ -11,6 +11,7 @@ import {
11
11
  FrontMatterTagsSchema,
12
12
  FrontMatterTOCHeadingLevels,
13
13
  validateFrontMatter,
14
+ ContentVisibilitySchema,
14
15
  } from '@docusaurus/utils-validation';
15
16
  import type {DocFrontMatter} from '@docusaurus/plugin-content-docs';
16
17
 
@@ -43,7 +44,6 @@ const DocFrontMatterSchema = Joi.object<DocFrontMatter>({
43
44
  parse_number_prefixes: Joi.boolean(),
44
45
  pagination_next: Joi.string().allow(null),
45
46
  pagination_prev: Joi.string().allow(null),
46
- draft: Joi.boolean(),
47
47
  ...FrontMatterTOCHeadingLevels,
48
48
  last_update: Joi.object({
49
49
  author: Joi.string(),
@@ -54,7 +54,9 @@ const DocFrontMatterSchema = Joi.object<DocFrontMatter>({
54
54
  'object.missing': FrontMatterLastUpdateErrorMessage,
55
55
  'object.base': FrontMatterLastUpdateErrorMessage,
56
56
  }),
57
- }).unknown();
57
+ })
58
+ .unknown()
59
+ .concat(ContentVisibilitySchema);
58
60
 
59
61
  export function validateDocFrontMatter(frontMatter: {
60
62
  [key: string]: unknown;
package/src/globalData.ts CHANGED
@@ -21,8 +21,13 @@ import type {Sidebars} from './sidebars/types';
21
21
 
22
22
  function toGlobalDataDoc(doc: DocMetadata): GlobalDoc {
23
23
  return {
24
- id: doc.unversionedId,
24
+ id: doc.id,
25
25
  path: doc.permalink,
26
+
27
+ // optimize global data size: do not add unlisted: false/undefined
28
+ ...(doc.unlisted && {unlisted: doc.unlisted}),
29
+
30
+ // TODO optimize size? remove attribute when no sidebar (breaking change?)
26
31
  sidebar: doc.sidebar,
27
32
  };
28
33
  }
@@ -51,10 +56,7 @@ function toGlobalSidebars(
51
56
  path:
52
57
  firstLink.type === 'generated-index'
53
58
  ? firstLink.permalink
54
- : version.docs.find(
55
- (doc) =>
56
- doc.id === firstLink.id || doc.unversionedId === firstLink.id,
57
- )!.permalink,
59
+ : version.docs.find((doc) => doc.id === firstLink.id)!.permalink,
58
60
  label: firstLink.label,
59
61
  },
60
62
  };
@@ -71,7 +73,7 @@ export function toGlobalDataVersion(version: FullVersion): GlobalVersion {
71
73
  docs: version.docs
72
74
  .map(toGlobalDataDoc)
73
75
  .concat(version.categoryGeneratedIndices.map(toGlobalDataGeneratedIndex)),
74
- draftIds: version.drafts.map((doc) => doc.unversionedId),
76
+ draftIds: version.drafts.map((doc) => doc.id),
75
77
  sidebars: toGlobalSidebars(version.sidebars, version),
76
78
  };
77
79
  }