@azerion/bunosaurus-plugin-content-docs 0.0.0-stage → 4.0.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.
Files changed (119) hide show
  1. package/README.md +6 -2
  2. package/lib/categoryGeneratedIndex.d.ts +12 -0
  3. package/lib/categoryGeneratedIndex.js +36 -0
  4. package/lib/cli.d.ts +13 -0
  5. package/lib/cli.js +124 -0
  6. package/lib/client/doc.d.ts +29 -0
  7. package/lib/client/doc.js +47 -0
  8. package/lib/client/docSidebarItemsExpandedState.d.ts +30 -0
  9. package/lib/client/docSidebarItemsExpandedState.js +27 -0
  10. package/lib/client/docsClientUtils.d.ts +15 -0
  11. package/lib/client/docsClientUtils.js +86 -0
  12. package/lib/client/docsPreferredVersion.d.ts +29 -0
  13. package/lib/client/docsPreferredVersion.js +124 -0
  14. package/lib/client/docsSearch.d.ts +19 -0
  15. package/lib/client/docsSearch.js +39 -0
  16. package/lib/client/docsSidebar.d.ts +25 -0
  17. package/lib/client/docsSidebar.js +29 -0
  18. package/lib/client/docsUtils.d.ts +112 -0
  19. package/lib/client/docsUtils.js +299 -0
  20. package/lib/client/docsVersion.d.ts +19 -0
  21. package/lib/client/docsVersion.js +25 -0
  22. package/lib/client/index.d.ts +91 -0
  23. package/lib/client/index.js +83 -0
  24. package/lib/client/structuredDataUtils.d.ts +11 -0
  25. package/lib/client/structuredDataUtils.js +35 -0
  26. package/lib/constants.d.ts +14 -0
  27. package/lib/constants.js +17 -0
  28. package/lib/contentHelpers.d.ts +12 -0
  29. package/lib/contentHelpers.js +31 -0
  30. package/lib/docs.d.ts +48 -0
  31. package/lib/docs.js +252 -0
  32. package/lib/frontMatter.d.ts +10 -0
  33. package/lib/frontMatter.js +46 -0
  34. package/lib/globalData.d.ts +9 -0
  35. package/lib/globalData.js +59 -0
  36. package/lib/index.d.ts +10 -0
  37. package/lib/index.js +198 -0
  38. package/lib/numberPrefix.d.ts +11 -0
  39. package/lib/numberPrefix.js +50 -0
  40. package/lib/options.d.ts +10 -0
  41. package/lib/options.js +138 -0
  42. package/lib/props.d.ts +21 -0
  43. package/lib/props.js +156 -0
  44. package/lib/routes.d.ts +22 -0
  45. package/lib/routes.js +184 -0
  46. package/lib/server-export.d.ts +9 -0
  47. package/lib/server-export.js +25 -0
  48. package/lib/sidebars/generator.d.ts +10 -0
  49. package/lib/sidebars/generator.js +217 -0
  50. package/lib/sidebars/index.d.ts +13 -0
  51. package/lib/sidebars/index.js +99 -0
  52. package/lib/sidebars/normalization.d.ts +13 -0
  53. package/lib/sidebars/normalization.js +58 -0
  54. package/lib/sidebars/postProcessor.d.ts +11 -0
  55. package/lib/sidebars/postProcessor.js +82 -0
  56. package/lib/sidebars/processor.d.ts +10 -0
  57. package/lib/sidebars/processor.js +77 -0
  58. package/lib/sidebars/types.d.ts +192 -0
  59. package/lib/sidebars/types.js +8 -0
  60. package/lib/sidebars/utils.d.ts +69 -0
  61. package/lib/sidebars/utils.js +327 -0
  62. package/lib/sidebars/validation.d.ts +11 -0
  63. package/lib/sidebars/validation.js +152 -0
  64. package/lib/slug.d.ts +15 -0
  65. package/lib/slug.js +51 -0
  66. package/lib/translations.d.ts +10 -0
  67. package/lib/translations.js +243 -0
  68. package/lib/types.d.ts +27 -0
  69. package/lib/types.js +8 -0
  70. package/lib/versions/files.d.ts +54 -0
  71. package/lib/versions/files.js +152 -0
  72. package/lib/versions/loadVersion.d.ts +17 -0
  73. package/lib/versions/loadVersion.js +126 -0
  74. package/lib/versions/validation.d.ts +17 -0
  75. package/lib/versions/validation.js +70 -0
  76. package/lib/versions/version.d.ts +40 -0
  77. package/lib/versions/version.js +189 -0
  78. package/package.json +70 -4
  79. package/src/categoryGeneratedIndex.ts +60 -0
  80. package/src/cli.ts +180 -0
  81. package/src/client/doc.tsx +71 -0
  82. package/src/client/docSidebarItemsExpandedState.tsx +55 -0
  83. package/src/client/docsClientUtils.ts +140 -0
  84. package/src/client/docsPreferredVersion.tsx +245 -0
  85. package/src/client/docsSearch.test.ts +15 -0
  86. package/src/client/docsSearch.ts +57 -0
  87. package/src/client/docsSidebar.tsx +50 -0
  88. package/src/client/docsUtils.tsx +442 -0
  89. package/src/client/docsVersion.tsx +36 -0
  90. package/src/client/index.ts +211 -0
  91. package/src/client/structuredDataUtils.ts +54 -0
  92. package/src/constants.ts +15 -0
  93. package/src/contentHelpers.ts +34 -0
  94. package/src/docs.ts +402 -0
  95. package/src/frontMatter.ts +57 -0
  96. package/src/globalData.ts +79 -0
  97. package/src/index.ts +289 -0
  98. package/src/numberPrefix.ts +62 -0
  99. package/src/options.ts +185 -0
  100. package/src/plugin-content-docs.d.ts +667 -0
  101. package/src/props.ts +223 -0
  102. package/src/routes.ts +263 -0
  103. package/src/server-export.ts +26 -0
  104. package/src/sidebars/README.md +10 -0
  105. package/src/sidebars/generator.ts +307 -0
  106. package/src/sidebars/index.ts +130 -0
  107. package/src/sidebars/normalization.ts +91 -0
  108. package/src/sidebars/postProcessor.ts +115 -0
  109. package/src/sidebars/processor.ts +122 -0
  110. package/src/sidebars/types.ts +293 -0
  111. package/src/sidebars/utils.ts +529 -0
  112. package/src/sidebars/validation.ts +189 -0
  113. package/src/slug.ts +79 -0
  114. package/src/translations.ts +365 -0
  115. package/src/types.ts +34 -0
  116. package/src/versions/files.ts +240 -0
  117. package/src/versions/loadVersion.ts +185 -0
  118. package/src/versions/validation.ts +115 -0
  119. package/src/versions/version.ts +300 -0
@@ -0,0 +1,83 @@
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 { useLocation } from '@docusaurus/router';
8
+ import { useAllPluginInstancesData, usePluginData, } from '@docusaurus/useGlobalData';
9
+ import { getActivePlugin, getLatestVersion, getActiveVersion, getActiveDocContext, getDocVersionSuggestions, } from './docsClientUtils';
10
+ export { useDocById, findSidebarCategory, findFirstSidebarItemLink, isActiveSidebarItem, isVisibleSidebarItem, useVisibleSidebarItems, useSidebarBreadcrumbs, useDocsVersionCandidates, useLayoutDoc, useLayoutDocsSidebar, useDocRootMetadata, useCurrentSidebarCategory, useCurrentSidebarSiblings, filterDocCardListItems, } from './docsUtils';
11
+ export { useDocsPreferredVersion } from './docsPreferredVersion';
12
+ export { DocSidebarItemsExpandedStateProvider, useDocSidebarItemsExpandedState, } from './docSidebarItemsExpandedState';
13
+ export { DocsVersionProvider, useDocsVersion } from './docsVersion';
14
+ export { DocsSidebarProvider, useDocsSidebar } from './docsSidebar';
15
+ export { DocProvider, useDoc } from './doc';
16
+ export { useDocsPreferredVersionByPluginId, DocsPreferredVersionContextProvider, } from './docsPreferredVersion';
17
+ export { useDocsContextualSearchTags, getDocsVersionSearchTag, } from './docsSearch';
18
+ export { useBreadcrumbsStructuredData } from './structuredDataUtils';
19
+ // Important to use a constant object to avoid React useEffect executions etc.
20
+ // see https://github.com/facebook/docusaurus/issues/5089
21
+ const StableEmptyObject = {};
22
+ // In blog-only mode, docs hooks are still used by the theme. We need a fail-
23
+ // safe fallback when the docs plugin is not in use
24
+ export const useAllDocsData = () => useAllPluginInstancesData('docusaurus-plugin-content-docs') ?? StableEmptyObject;
25
+ export const useDocsData = (pluginId) => {
26
+ try {
27
+ return usePluginData('docusaurus-plugin-content-docs', pluginId, {
28
+ failfast: true,
29
+ });
30
+ }
31
+ catch (error) {
32
+ throw new Error(`You are using a feature of the Docusaurus docs plugin, but this plugin does not seem to be enabled${pluginId === 'Default' ? '' : ` (pluginId=${pluginId}`}`, { cause: error });
33
+ }
34
+ };
35
+ // TODO this feature should be provided by docusaurus core
36
+ export function useActivePlugin(options = {}) {
37
+ const data = useAllDocsData();
38
+ const { pathname } = useLocation();
39
+ return getActivePlugin(data, pathname, options);
40
+ }
41
+ export function useActivePluginAndVersion(options = {}) {
42
+ const activePlugin = useActivePlugin(options);
43
+ const { pathname } = useLocation();
44
+ if (!activePlugin) {
45
+ return undefined;
46
+ }
47
+ const activeVersion = getActiveVersion(activePlugin.pluginData, pathname);
48
+ return {
49
+ activePlugin,
50
+ activeVersion,
51
+ };
52
+ }
53
+ /** Versions are returned ordered (most recent first). */
54
+ export function useVersions(pluginId) {
55
+ const data = useDocsData(pluginId);
56
+ return data.versions;
57
+ }
58
+ export function useLatestVersion(pluginId) {
59
+ const data = useDocsData(pluginId);
60
+ return getLatestVersion(data);
61
+ }
62
+ /**
63
+ * Returns `undefined` on doc-unrelated pages, because there's no version
64
+ * currently considered as active.
65
+ */
66
+ export function useActiveVersion(pluginId) {
67
+ const data = useDocsData(pluginId);
68
+ const { pathname } = useLocation();
69
+ return getActiveVersion(data, pathname);
70
+ }
71
+ export function useActiveDocContext(pluginId) {
72
+ const data = useDocsData(pluginId);
73
+ const { pathname } = useLocation();
74
+ return getActiveDocContext(data, pathname);
75
+ }
76
+ /**
77
+ * Useful to say "hey, you are not on the latest docs version, please switch"
78
+ */
79
+ export function useDocVersionSuggestions(pluginId) {
80
+ const data = useDocsData(pluginId);
81
+ const { pathname } = useLocation();
82
+ return getDocVersionSuggestions(data, pathname);
83
+ }
@@ -0,0 +1,11 @@
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 { PropSidebarBreadcrumbsItem } from '@docusaurus/plugin-content-docs';
8
+ import type { WithContext, BreadcrumbList } from 'schema-dts';
9
+ export declare function useBreadcrumbsStructuredData({ breadcrumbs, }: {
10
+ breadcrumbs: PropSidebarBreadcrumbsItem[];
11
+ }): WithContext<BreadcrumbList>;
@@ -0,0 +1,35 @@
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 useDocusaurusContext from '@docusaurus/useDocusaurusContext';
8
+ import { applyTrailingSlash } from '@docusaurus/utils-common';
9
+ function getAbsoluteUrl(permalink, params) {
10
+ const absoluteUrl = `${params.url}${permalink}`;
11
+ return applyTrailingSlash(absoluteUrl, {
12
+ trailingSlash: params.trailingSlash,
13
+ baseUrl: params.baseUrl,
14
+ });
15
+ }
16
+ function getBreadcrumbsStructuredData({ breadcrumbs, params, }) {
17
+ return {
18
+ '@context': 'https://schema.org',
19
+ '@type': 'BreadcrumbList',
20
+ itemListElement: breadcrumbs
21
+ // We filter breadcrumb items without links, they are not allowed
22
+ // See also https://github.com/facebook/docusaurus/issues/9319#issuecomment-2643560845
23
+ .filter((breadcrumb) => breadcrumb.href)
24
+ .map((breadcrumb, index) => ({
25
+ '@type': 'ListItem',
26
+ position: index + 1,
27
+ name: breadcrumb.label,
28
+ item: getAbsoluteUrl(breadcrumb.href, params),
29
+ })),
30
+ };
31
+ }
32
+ export function useBreadcrumbsStructuredData({ breadcrumbs, }) {
33
+ const { siteConfig } = useDocusaurusContext();
34
+ return getBreadcrumbsStructuredData({ breadcrumbs, params: siteConfig });
35
+ }
@@ -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
+ /** The name of the version that's actively worked on (e.g. `website/docs`) */
8
+ export declare const CURRENT_VERSION_NAME = "current";
9
+ /** All doc versions are stored here by version names */
10
+ export declare const VERSIONED_DOCS_DIR = "versioned_docs";
11
+ /** All doc versioned sidebars are stored here by version names */
12
+ export declare const VERSIONED_SIDEBARS_DIR = "versioned_sidebars";
13
+ /** The version names. Should 1-1 map to the content of versioned docs dir. */
14
+ export declare const VERSIONS_JSON_FILE = "versions.json";
@@ -0,0 +1,17 @@
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.VERSIONS_JSON_FILE = exports.VERSIONED_SIDEBARS_DIR = exports.VERSIONED_DOCS_DIR = exports.CURRENT_VERSION_NAME = void 0;
10
+ /** The name of the version that's actively worked on (e.g. `website/docs`) */
11
+ exports.CURRENT_VERSION_NAME = 'current';
12
+ /** All doc versions are stored here by version names */
13
+ exports.VERSIONED_DOCS_DIR = 'versioned_docs';
14
+ /** All doc versioned sidebars are stored here by version names */
15
+ exports.VERSIONED_SIDEBARS_DIR = 'versioned_sidebars';
16
+ /** The version names. Should 1-1 map to the content of versioned docs dir. */
17
+ exports.VERSIONS_JSON_FILE = 'versions.json';
@@ -0,0 +1,12 @@
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 { DocMetadata, LoadedContent } from '@docusaurus/plugin-content-docs';
8
+ export declare function createContentHelpers(): {
9
+ updateContent: (content: LoadedContent) => void;
10
+ sourceToDoc: Map<string, DocMetadata>;
11
+ sourceToPermalink: Map<string, string>;
12
+ };
@@ -0,0 +1,31 @@
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.createContentHelpers = createContentHelpers;
10
+ function indexDocsBySource(content) {
11
+ const allDocs = content.loadedVersions.flatMap((v) => v.docs);
12
+ return new Map(allDocs.map((doc) => [doc.source, doc]));
13
+ }
14
+ // TODO this is bad, we should have a better way to do this (new lifecycle?)
15
+ // The source to doc/permalink is a mutable map passed to the mdx loader
16
+ // See https://github.com/facebook/docusaurus/pull/10457
17
+ // See https://github.com/facebook/docusaurus/pull/10185
18
+ function createContentHelpers() {
19
+ const sourceToDoc = new Map();
20
+ const sourceToPermalink = new Map();
21
+ // Mutable map update :/
22
+ function updateContent(content) {
23
+ sourceToDoc.clear();
24
+ sourceToPermalink.clear();
25
+ indexDocsBySource(content).forEach((value, key) => {
26
+ sourceToDoc.set(key, value);
27
+ sourceToPermalink.set(key, value.permalink);
28
+ });
29
+ }
30
+ return { updateContent, sourceToDoc, sourceToPermalink };
31
+ }
package/lib/docs.d.ts ADDED
@@ -0,0 +1,48 @@
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 { TagsFile } from '@docusaurus/utils';
8
+ import type { MetadataOptions, PluginOptions, CategoryIndexMatcher, DocMetadataBase, VersionMetadata, LoadedVersion } from '@docusaurus/plugin-content-docs';
9
+ import type { LoadContext } from '@docusaurus/types';
10
+ import type { SidebarsUtils } from './sidebars/utils';
11
+ import type { DocFile } from './types';
12
+ export declare function readDocFile(versionMetadata: Pick<VersionMetadata, 'contentPath' | 'contentPathLocalized'>, source: string): Promise<DocFile>;
13
+ export declare function readVersionDocs(versionMetadata: VersionMetadata, options: Pick<PluginOptions, 'include' | 'exclude' | 'showLastUpdateAuthor' | 'showLastUpdateTime'>): Promise<DocFile[]>;
14
+ export type DocEnv = 'production' | 'development';
15
+ export declare function processDocMetadata(args: {
16
+ docFile: DocFile;
17
+ versionMetadata: VersionMetadata;
18
+ context: LoadContext;
19
+ options: MetadataOptions;
20
+ env: DocEnv;
21
+ tagsFile: TagsFile | null;
22
+ }): Promise<DocMetadataBase>;
23
+ export declare function addDocNavigation({ docs, sidebarsUtils, }: {
24
+ docs: DocMetadataBase[];
25
+ sidebarsUtils: SidebarsUtils;
26
+ }): LoadedVersion['docs'];
27
+ /**
28
+ * The "main doc" is the "version entry point"
29
+ * We browse this doc by clicking on a version:
30
+ * - the "home" doc (at '/docs/')
31
+ * - the first doc of the first sidebar
32
+ * - a random doc (if no docs are in any sidebar... edge case)
33
+ */
34
+ export declare function getMainDocId({ docs, sidebarsUtils, }: {
35
+ docs: DocMetadataBase[];
36
+ sidebarsUtils: SidebarsUtils;
37
+ }): string;
38
+ export declare const isCategoryIndex: CategoryIndexMatcher;
39
+ /**
40
+ * `guides/sidebar/autogenerated.md` ->
41
+ * `'autogenerated', '.md', ['sidebar', 'guides']`
42
+ */
43
+ export declare function toCategoryIndexMatcherParam({ source, sourceDirName, }: Pick<DocMetadataBase, 'source' | 'sourceDirName'>): Parameters<CategoryIndexMatcher>[0];
44
+ export declare function createDocsByIdIndex<Doc extends {
45
+ id: string;
46
+ }>(docs: Doc[]): {
47
+ [docId: string]: Doc;
48
+ };
package/lib/docs.js ADDED
@@ -0,0 +1,252 @@
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.isCategoryIndex = void 0;
10
+ exports.readDocFile = readDocFile;
11
+ exports.readVersionDocs = readVersionDocs;
12
+ exports.processDocMetadata = processDocMetadata;
13
+ exports.addDocNavigation = addDocNavigation;
14
+ exports.getMainDocId = getMainDocId;
15
+ exports.toCategoryIndexMatcherParam = toCategoryIndexMatcherParam;
16
+ exports.createDocsByIdIndex = createDocsByIdIndex;
17
+ const tslib_1 = require("tslib");
18
+ const node_path_1 = tslib_1.__importDefault(require("node:path"));
19
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
20
+ const utils_1 = require("@docusaurus/utils");
21
+ const glob_1 = require("@docusaurus/glob");
22
+ const fs_1 = require("@docusaurus/fs");
23
+ const frontMatter_1 = require("./frontMatter");
24
+ const slug_1 = tslib_1.__importDefault(require("./slug"));
25
+ const numberPrefix_1 = require("./numberPrefix");
26
+ const utils_2 = require("./sidebars/utils");
27
+ async function readDocFile(versionMetadata, source) {
28
+ const contentPath = await (0, utils_1.getFolderContainingFile)((0, utils_1.getContentPathList)(versionMetadata), source);
29
+ const filePath = node_path_1.default.join(contentPath, source);
30
+ const content = await fs_1.fs.readFile(filePath, 'utf-8');
31
+ return { source, content, contentPath, filePath };
32
+ }
33
+ async function readVersionDocs(versionMetadata, options) {
34
+ const sources = await (0, glob_1.glob)(options.include, {
35
+ cwd: versionMetadata.contentPath,
36
+ ignore: options.exclude,
37
+ });
38
+ return Promise.all(sources.map((source) => readDocFile(versionMetadata, source)));
39
+ }
40
+ async function doProcessDocMetadata({ docFile, versionMetadata, context, options, env, tagsFile, }) {
41
+ const { source, content, contentPath, filePath } = docFile;
42
+ const { siteDir, siteConfig: { markdown: { parseFrontMatter }, vcs, }, } = context;
43
+ const { frontMatter: unsafeFrontMatter, contentTitle, excerpt, } = await (0, utils_1.parseMarkdownFile)({
44
+ filePath,
45
+ fileContent: content,
46
+ parseFrontMatter,
47
+ });
48
+ const frontMatter = (0, frontMatter_1.validateDocFrontMatter)(unsafeFrontMatter);
49
+ const { custom_edit_url: customEditURL,
50
+ // Strip number prefixes by default
51
+ // (01-MyFolder/01-MyDoc.md => MyFolder/MyDoc)
52
+ // but allow to disable this behavior with front matter
53
+ parse_number_prefixes: parseNumberPrefixes = true, last_update: lastUpdateFrontMatter, } = frontMatter;
54
+ const lastUpdate = await (0, utils_1.readLastUpdateData)(filePath, options, lastUpdateFrontMatter, vcs);
55
+ // E.g. api/plugins/myDoc -> myDoc; myDoc -> myDoc
56
+ const sourceFileNameWithoutExtension = node_path_1.default.basename(source, node_path_1.default.extname(source));
57
+ // E.g. api/plugins/myDoc -> api/plugins; myDoc -> .
58
+ const sourceDirName = node_path_1.default.dirname(source);
59
+ const { filename: unprefixedFileName, numberPrefix } = parseNumberPrefixes
60
+ ? options.numberPrefixParser(sourceFileNameWithoutExtension)
61
+ : { filename: sourceFileNameWithoutExtension, numberPrefix: undefined };
62
+ const baseID = frontMatter.id ?? unprefixedFileName;
63
+ if (baseID.includes('/')) {
64
+ throw new Error(`Document id "${baseID}" cannot include slash.`);
65
+ }
66
+ // For autogenerated sidebars, sidebar position can come from filename number
67
+ // prefix or front matter
68
+ const sidebarPosition = frontMatter.sidebar_position ?? numberPrefix;
69
+ // TODO legacy retrocompatibility
70
+ // I think it's bad to affect the front matter id with the dirname?
71
+ function computeDirNameIdPrefix() {
72
+ if (sourceDirName === '.') {
73
+ return undefined;
74
+ }
75
+ // Eventually remove the number prefixes from intermediate directories
76
+ return parseNumberPrefixes
77
+ ? (0, numberPrefix_1.stripPathNumberPrefixes)(sourceDirName, options.numberPrefixParser)
78
+ : sourceDirName;
79
+ }
80
+ const id = [computeDirNameIdPrefix(), baseID].filter(Boolean).join('/');
81
+ const docSlug = (0, slug_1.default)({
82
+ baseID,
83
+ source,
84
+ sourceDirName,
85
+ frontMatterSlug: frontMatter.slug,
86
+ stripDirNumberPrefixes: parseNumberPrefixes,
87
+ numberPrefixParser: options.numberPrefixParser,
88
+ });
89
+ // Note: the title is used by default for page title, sidebar label,
90
+ // pagination buttons... frontMatter.title should be used in priority over
91
+ // contentTitle (because it can contain markdown/JSX syntax)
92
+ const title = frontMatter.title ?? contentTitle ?? baseID;
93
+ const description = frontMatter.description ?? excerpt ?? '';
94
+ const permalink = (0, utils_1.normalizeUrl)([versionMetadata.path, docSlug]);
95
+ function getDocEditUrl() {
96
+ const relativeFilePath = node_path_1.default.relative(contentPath, filePath);
97
+ if (typeof options.editUrl === 'function') {
98
+ return options.editUrl({
99
+ version: versionMetadata.versionName,
100
+ versionDocsDirPath: (0, utils_1.posixPath)(node_path_1.default.relative(siteDir, versionMetadata.contentPath)),
101
+ docPath: (0, utils_1.posixPath)(relativeFilePath),
102
+ permalink,
103
+ locale: context.i18n.currentLocale,
104
+ });
105
+ }
106
+ else if (typeof options.editUrl === 'string') {
107
+ const isLocalized = typeof versionMetadata.contentPathLocalized !== 'undefined' &&
108
+ contentPath === versionMetadata.contentPathLocalized;
109
+ const baseVersionEditUrl = isLocalized && options.editLocalizedFiles
110
+ ? versionMetadata.editUrlLocalized
111
+ : versionMetadata.editUrl;
112
+ return (0, utils_1.getEditUrl)(relativeFilePath, baseVersionEditUrl);
113
+ }
114
+ return undefined;
115
+ }
116
+ const draft = (0, utils_1.isDraft)({ env, frontMatter });
117
+ const unlisted = (0, utils_1.isUnlisted)({ env, frontMatter });
118
+ const tags = (0, utils_1.normalizeTags)({
119
+ options,
120
+ source,
121
+ frontMatterTags: frontMatter.tags,
122
+ tagsBaseRoutePath: versionMetadata.tagsPath,
123
+ tagsFile,
124
+ });
125
+ // Assign all of object properties during instantiation (if possible) for
126
+ // NodeJS optimization.
127
+ // Adding properties to object after instantiation will cause hidden
128
+ // class transitions.
129
+ return {
130
+ id,
131
+ title,
132
+ description,
133
+ source: (0, utils_1.aliasedSitePath)(filePath, siteDir),
134
+ sourceDirName,
135
+ slug: docSlug,
136
+ permalink,
137
+ draft,
138
+ unlisted,
139
+ editUrl: customEditURL !== undefined ? customEditURL : getDocEditUrl(),
140
+ tags,
141
+ version: versionMetadata.versionName,
142
+ lastUpdatedBy: lastUpdate.lastUpdatedBy,
143
+ lastUpdatedAt: lastUpdate.lastUpdatedAt,
144
+ sidebarPosition,
145
+ frontMatter,
146
+ };
147
+ }
148
+ async function processDocMetadata(args) {
149
+ try {
150
+ return await doProcessDocMetadata(args);
151
+ }
152
+ catch (err) {
153
+ throw new Error(`Can't process doc metadata for doc at path path=${args.docFile.filePath} in version name=${args.versionMetadata.versionName}`, { cause: err });
154
+ }
155
+ }
156
+ function getUnlistedIds(docs) {
157
+ return new Set(docs.filter((doc) => doc.unlisted).map((doc) => doc.id));
158
+ }
159
+ function addDocNavigation({ docs, sidebarsUtils, }) {
160
+ const docsById = createDocsByIdIndex(docs);
161
+ const unlistedIds = getUnlistedIds(docs);
162
+ // Add sidebar/next/previous to the docs
163
+ function addNavData(doc) {
164
+ const navigation = sidebarsUtils.getDocNavigation({
165
+ docId: doc.id,
166
+ displayedSidebar: doc.frontMatter.displayed_sidebar,
167
+ unlistedIds,
168
+ });
169
+ const toNavigationLinkByDocId = (docId, type) => {
170
+ if (!docId) {
171
+ return undefined;
172
+ }
173
+ const navDoc = docsById[docId];
174
+ if (!navDoc) {
175
+ // This could only happen if user provided the ID through front matter
176
+ throw new Error(`Error when loading ${doc.id} in ${doc.sourceDirName}: the pagination_${type} front matter points to a non-existent ID ${docId}.`);
177
+ }
178
+ // Gracefully handle explicitly providing an unlisted doc ID in production
179
+ if (navDoc.unlisted) {
180
+ return undefined;
181
+ }
182
+ return (0, utils_2.toDocNavigationLink)(navDoc);
183
+ };
184
+ const previous = doc.frontMatter.pagination_prev !== undefined
185
+ ? toNavigationLinkByDocId(doc.frontMatter.pagination_prev, 'prev')
186
+ : (0, utils_2.toNavigationLink)(navigation.previous, docsById);
187
+ const next = doc.frontMatter.pagination_next !== undefined
188
+ ? toNavigationLinkByDocId(doc.frontMatter.pagination_next, 'next')
189
+ : (0, utils_2.toNavigationLink)(navigation.next, docsById);
190
+ return { ...doc, sidebar: navigation.sidebarName, previous, next };
191
+ }
192
+ const docsWithNavigation = docs.map(addNavData);
193
+ // Sort to ensure consistent output for tests
194
+ docsWithNavigation.sort((a, b) => a.id.localeCompare(b.id));
195
+ return docsWithNavigation;
196
+ }
197
+ /**
198
+ * The "main doc" is the "version entry point"
199
+ * We browse this doc by clicking on a version:
200
+ * - the "home" doc (at '/docs/')
201
+ * - the first doc of the first sidebar
202
+ * - a random doc (if no docs are in any sidebar... edge case)
203
+ */
204
+ function getMainDocId({ docs, sidebarsUtils, }) {
205
+ function getMainDoc() {
206
+ const versionHomeDoc = docs.find((doc) => doc.slug === '/');
207
+ const firstDocIdOfFirstSidebar = sidebarsUtils.getFirstDocIdOfFirstSidebar();
208
+ if (versionHomeDoc) {
209
+ return versionHomeDoc;
210
+ }
211
+ else if (firstDocIdOfFirstSidebar) {
212
+ return docs.find((doc) => doc.id === firstDocIdOfFirstSidebar);
213
+ }
214
+ return docs[0];
215
+ }
216
+ return getMainDoc().id;
217
+ }
218
+ // By convention, Docusaurus considers some docs are "indexes":
219
+ // - index.md
220
+ // - readme.md
221
+ // - <folder>/<folder>.md
222
+ //
223
+ // This function is the default implementation of this convention
224
+ //
225
+ // Those index docs produce a different behavior
226
+ // - Slugs do not end with a weird "/index" suffix
227
+ // - Auto-generated sidebar categories link to them as intro
228
+ const isCategoryIndex = ({ fileName, directories, }) => {
229
+ const eligibleDocIndexNames = [
230
+ 'index',
231
+ 'readme',
232
+ directories[0]?.toLowerCase(),
233
+ ];
234
+ return eligibleDocIndexNames.includes(fileName.toLowerCase());
235
+ };
236
+ exports.isCategoryIndex = isCategoryIndex;
237
+ /**
238
+ * `guides/sidebar/autogenerated.md` ->
239
+ * `'autogenerated', '.md', ['sidebar', 'guides']`
240
+ */
241
+ function toCategoryIndexMatcherParam({ source, sourceDirName, }) {
242
+ // source + sourceDirName are always posix-style
243
+ return {
244
+ fileName: node_path_1.default.posix.parse(source).name,
245
+ extension: node_path_1.default.posix.parse(source).ext,
246
+ directories: sourceDirName.split(node_path_1.default.posix.sep).reverse(),
247
+ };
248
+ }
249
+ // Docs are indexed by their id
250
+ function createDocsByIdIndex(docs) {
251
+ return lodash_1.default.keyBy(docs, (d) => d.id);
252
+ }
@@ -0,0 +1,10 @@
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 { DocFrontMatter } from '@docusaurus/plugin-content-docs';
8
+ export declare function validateDocFrontMatter(frontMatter: {
9
+ [key: string]: unknown;
10
+ }): DocFrontMatter;
@@ -0,0 +1,46 @@
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
+ /* eslint-disable camelcase */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.validateDocFrontMatter = validateDocFrontMatter;
11
+ const utils_validation_1 = require("@docusaurus/utils-validation");
12
+ // NOTE: we don't add any default value on purpose here
13
+ // We don't want default values to magically appear in doc metadata and props
14
+ // While the user did not provide those values explicitly
15
+ // We use default values in code instead
16
+ const DocFrontMatterSchema = utils_validation_1.JoiFrontMatter.object({
17
+ id: utils_validation_1.JoiFrontMatter.string(),
18
+ // See https://github.com/facebook/docusaurus/issues/4591#issuecomment-822372398
19
+ title: utils_validation_1.JoiFrontMatter.string().allow(''),
20
+ hide_title: utils_validation_1.JoiFrontMatter.boolean(),
21
+ hide_table_of_contents: utils_validation_1.JoiFrontMatter.boolean(),
22
+ keywords: utils_validation_1.JoiFrontMatter.array().items(utils_validation_1.JoiFrontMatter.string().required()),
23
+ image: utils_validation_1.URISchema,
24
+ // See https://github.com/facebook/docusaurus/issues/4591#issuecomment-822372398
25
+ description: utils_validation_1.JoiFrontMatter.string().allow(''),
26
+ slug: utils_validation_1.JoiFrontMatter.string(),
27
+ sidebar_key: utils_validation_1.JoiFrontMatter.string(),
28
+ sidebar_label: utils_validation_1.JoiFrontMatter.string(),
29
+ sidebar_position: utils_validation_1.JoiFrontMatter.number(),
30
+ sidebar_class_name: utils_validation_1.JoiFrontMatter.string(),
31
+ sidebar_custom_props: utils_validation_1.JoiFrontMatter.object().unknown(),
32
+ displayed_sidebar: utils_validation_1.JoiFrontMatter.string().allow(null),
33
+ tags: utils_validation_1.FrontMatterTagsSchema,
34
+ pagination_label: utils_validation_1.JoiFrontMatter.string(),
35
+ custom_edit_url: utils_validation_1.URISchema.allow('', null),
36
+ parse_number_prefixes: utils_validation_1.JoiFrontMatter.boolean(),
37
+ pagination_next: utils_validation_1.JoiFrontMatter.string().allow(null),
38
+ pagination_prev: utils_validation_1.JoiFrontMatter.string().allow(null),
39
+ ...utils_validation_1.FrontMatterTOCHeadingLevels,
40
+ last_update: utils_validation_1.FrontMatterLastUpdateSchema,
41
+ })
42
+ .unknown()
43
+ .concat(utils_validation_1.ContentVisibilitySchema);
44
+ function validateDocFrontMatter(frontMatter) {
45
+ return (0, utils_validation_1.validateFrontMatter)(frontMatter, DocFrontMatterSchema);
46
+ }
@@ -0,0 +1,9 @@
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 { FullVersion } from './types';
8
+ import type { GlobalVersion } from '@docusaurus/plugin-content-docs/client';
9
+ export declare function toGlobalDataVersion(version: FullVersion): GlobalVersion;
@@ -0,0 +1,59 @@
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.toGlobalDataVersion = toGlobalDataVersion;
10
+ const tslib_1 = require("tslib");
11
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
12
+ const docs_1 = require("./docs");
13
+ function toGlobalDataDoc(doc) {
14
+ return {
15
+ id: doc.id,
16
+ path: doc.permalink,
17
+ // optimize global data size: do not add unlisted: false/undefined
18
+ ...(doc.unlisted && { unlisted: doc.unlisted }),
19
+ // TODO optimize size? remove attribute when no sidebar (breaking change?)
20
+ sidebar: doc.sidebar,
21
+ };
22
+ }
23
+ function toGlobalDataGeneratedIndex(doc) {
24
+ return {
25
+ id: doc.slug,
26
+ path: doc.permalink,
27
+ sidebar: doc.sidebar,
28
+ };
29
+ }
30
+ function toGlobalSidebars(sidebars, version) {
31
+ return lodash_1.default.mapValues(sidebars, (sidebar, sidebarId) => {
32
+ const firstLink = version.sidebarsUtils.getFirstLink(sidebarId);
33
+ if (!firstLink) {
34
+ return {};
35
+ }
36
+ return {
37
+ link: {
38
+ path: firstLink.type === 'generated-index'
39
+ ? firstLink.permalink
40
+ : version.docs.find((doc) => doc.id === firstLink.id).permalink,
41
+ label: firstLink.label,
42
+ },
43
+ };
44
+ });
45
+ }
46
+ function toGlobalDataVersion(version) {
47
+ return {
48
+ name: version.versionName,
49
+ label: version.label,
50
+ isLast: version.isLast,
51
+ path: version.path,
52
+ mainDocId: (0, docs_1.getMainDocId)(version),
53
+ docs: version.docs
54
+ .map(toGlobalDataDoc)
55
+ .concat(version.categoryGeneratedIndices.map(toGlobalDataGeneratedIndex)),
56
+ draftIds: version.drafts.map((doc) => doc.id),
57
+ sidebars: toGlobalSidebars(version.sidebars, version),
58
+ };
59
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1,10 @@
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 { PluginOptions, LoadedContent } from '@docusaurus/plugin-content-docs';
8
+ import type { LoadContext, Plugin } from '@docusaurus/types';
9
+ export default function pluginContentDocs(context: LoadContext, options: PluginOptions): Promise<Plugin<LoadedContent>>;
10
+ export { validateOptions } from './options';