This commit is contained in:
2024-03-22 03:47:51 +05:30
parent 8bcf3d211e
commit 89819f6fe2
28440 changed files with 3211033 additions and 2 deletions

View File

@@ -0,0 +1,15 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import type { GlobalPluginData, GlobalVersion, ActivePlugin, ActiveDocContext, DocVersionSuggestions } from '@docusaurus/plugin-content-docs/client';
import type { UseDataOptions } from '@docusaurus/types';
export declare function getActivePlugin(allPluginData: {
[pluginId: string]: GlobalPluginData;
}, pathname: string, options?: UseDataOptions): ActivePlugin | undefined;
export declare const getLatestVersion: (data: GlobalPluginData) => GlobalVersion;
export declare function getActiveVersion(data: GlobalPluginData, pathname: string): GlobalVersion | undefined;
export declare function getActiveDocContext(data: GlobalPluginData, pathname: string): ActiveDocContext;
export declare function getDocVersionSuggestions(data: GlobalPluginData, pathname: string): DocVersionSuggestions;

View File

@@ -0,0 +1,78 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { matchPath } from '@docusaurus/router';
// This code is not part of the api surface, not in ./theme on purpose
// get the data of the plugin that is currently "active"
// ie the docs of that plugin are currently browsed
// it is useful to support multiple docs plugin instances
export function getActivePlugin(allPluginData, pathname, options = {}) {
const activeEntry = Object.entries(allPluginData)
// Route sorting: '/android/foo' should match '/android' instead of '/'
.sort((a, b) => b[1].path.localeCompare(a[1].path))
.find(([, pluginData]) => !!matchPath(pathname, {
path: pluginData.path,
exact: false,
strict: false,
}));
const activePlugin = activeEntry
? { pluginId: activeEntry[0], pluginData: activeEntry[1] }
: undefined;
if (!activePlugin && options.failfast) {
throw new Error(`Can't find active docs plugin for "${pathname}" pathname, while it was expected to be found. Maybe you tried to use a docs feature that can only be used on a docs-related page? Existing docs plugin paths are: ${Object.values(allPluginData)
.map((plugin) => plugin.path)
.join(', ')}`);
}
return activePlugin;
}
export const getLatestVersion = (data) => data.versions.find((version) => version.isLast);
export function getActiveVersion(data, pathname) {
const lastVersion = getLatestVersion(data);
// Last version is a route like /docs/*,
// we need to match it last or it would match /docs/version-1.0/* as well
const orderedVersionsMetadata = [
...data.versions.filter((version) => version !== lastVersion),
lastVersion,
];
return orderedVersionsMetadata.find((version) => !!matchPath(pathname, {
path: version.path,
exact: false,
strict: false,
}));
}
export function getActiveDocContext(data, pathname) {
const activeVersion = getActiveVersion(data, pathname);
const activeDoc = activeVersion?.docs.find((doc) => !!matchPath(pathname, {
path: doc.path,
exact: true,
strict: false,
}));
function getAlternateVersionDocs(docId) {
const result = {};
data.versions.forEach((version) => {
version.docs.forEach((doc) => {
if (doc.id === docId) {
result[version.name] = doc;
}
});
});
return result;
}
const alternateVersionDocs = activeDoc
? getAlternateVersionDocs(activeDoc.id)
: {};
return {
activeVersion,
activeDoc,
alternateDocVersions: alternateVersionDocs,
};
}
export function getDocVersionSuggestions(data, pathname) {
const latestVersion = getLatestVersion(data);
const activeDocContext = getActiveDocContext(data, pathname);
const latestDocSuggestion = activeDocContext.alternateDocVersions[latestVersion.name];
return { latestDocSuggestion, latestVersionSuggestion: latestVersion };
}

View File

@@ -0,0 +1,82 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import type { UseDataOptions } from '@docusaurus/types';
export type ActivePlugin = {
pluginId: string;
pluginData: GlobalPluginData;
};
export type ActiveDocContext = {
activeVersion?: GlobalVersion;
activeDoc?: GlobalDoc;
alternateDocVersions: {
[versionName: string]: GlobalDoc;
};
};
export type GlobalDoc = {
/**
* For generated index pages, this is the `slug`, **not** `permalink`
* (without base URL). Because slugs have leading slashes but IDs don't,
* there won't be clashes.
*/
id: string;
path: string;
sidebar?: string;
unlisted?: boolean;
};
export type GlobalVersion = {
name: string;
label: string;
isLast: boolean;
path: string;
/** The doc with `slug: /`, or first doc in first sidebar */
mainDocId: string;
docs: GlobalDoc[];
/** Unversioned IDs. In development, this list is empty. */
draftIds: string[];
sidebars?: {
[sidebarId: string]: GlobalSidebar;
};
};
export type GlobalSidebar = {
link?: {
label: string;
path: string;
};
};
export type GlobalPluginData = {
path: string;
versions: GlobalVersion[];
breadcrumbs: boolean;
};
export type DocVersionSuggestions = {
/** Suggest the latest version */
latestVersionSuggestion: GlobalVersion;
/** Suggest the same doc, in latest version (if one exists) */
latestDocSuggestion?: GlobalDoc;
};
export declare const useAllDocsData: () => {
[pluginId: string]: GlobalPluginData;
};
export declare const useDocsData: (pluginId: string | undefined) => GlobalPluginData;
export declare function useActivePlugin(options?: UseDataOptions): ActivePlugin | undefined;
export declare function useActivePluginAndVersion(options?: UseDataOptions): {
activePlugin: ActivePlugin;
activeVersion: GlobalVersion | undefined;
} | undefined;
/** Versions are returned ordered (most recent first). */
export declare function useVersions(pluginId: string | undefined): GlobalVersion[];
export declare function useLatestVersion(pluginId: string | undefined): GlobalVersion;
/**
* Returns `undefined` on doc-unrelated pages, because there's no version
* currently considered as active.
*/
export declare function useActiveVersion(pluginId: string | undefined): GlobalVersion | undefined;
export declare function useActiveDocContext(pluginId: string | undefined): ActiveDocContext;
/**
* Useful to say "hey, you are not on the latest docs version, please switch"
*/
export declare function useDocVersionSuggestions(pluginId: string | undefined): DocVersionSuggestions;

View File

@@ -0,0 +1,67 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { useLocation } from '@docusaurus/router';
import { useAllPluginInstancesData, usePluginData, } from '@docusaurus/useGlobalData';
import { getActivePlugin, getLatestVersion, getActiveVersion, getActiveDocContext, getDocVersionSuggestions, } from './docsClientUtils';
// Important to use a constant object to avoid React useEffect executions etc.
// see https://github.com/facebook/docusaurus/issues/5089
const StableEmptyObject = {};
// In blog-only mode, docs hooks are still used by the theme. We need a fail-
// safe fallback when the docs plugin is not in use
export const useAllDocsData = () => useAllPluginInstancesData('docusaurus-plugin-content-docs') ?? StableEmptyObject;
export const useDocsData = (pluginId) => usePluginData('docusaurus-plugin-content-docs', pluginId, {
failfast: true,
});
// TODO this feature should be provided by docusaurus core
export function useActivePlugin(options = {}) {
const data = useAllDocsData();
const { pathname } = useLocation();
return getActivePlugin(data, pathname, options);
}
export function useActivePluginAndVersion(options = {}) {
const activePlugin = useActivePlugin(options);
const { pathname } = useLocation();
if (!activePlugin) {
return undefined;
}
const activeVersion = getActiveVersion(activePlugin.pluginData, pathname);
return {
activePlugin,
activeVersion,
};
}
/** Versions are returned ordered (most recent first). */
export function useVersions(pluginId) {
const data = useDocsData(pluginId);
return data.versions;
}
export function useLatestVersion(pluginId) {
const data = useDocsData(pluginId);
return getLatestVersion(data);
}
/**
* Returns `undefined` on doc-unrelated pages, because there's no version
* currently considered as active.
*/
export function useActiveVersion(pluginId) {
const data = useDocsData(pluginId);
const { pathname } = useLocation();
return getActiveVersion(data, pathname);
}
export function useActiveDocContext(pluginId) {
const data = useDocsData(pluginId);
const { pathname } = useLocation();
return getActiveDocContext(data, pathname);
}
/**
* Useful to say "hey, you are not on the latest docs version, please switch"
*/
export function useDocVersionSuggestions(pluginId) {
const data = useDocsData(pluginId);
const { pathname } = useLocation();
return getDocVersionSuggestions(data, pathname);
}