mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-12 16:47:26 +02:00
chore: fix ESLint warnings, restrict export all syntax (#6605)
* chore: fix ESLint warnings, forbid export all syntax * fix... * reorder
This commit is contained in:
parent
3fd99ad8d4
commit
45f6f8b869
22 changed files with 220 additions and 148 deletions
|
@ -1,108 +0,0 @@
|
|||
/**
|
||||
* 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 useGlobalData, {
|
||||
// useAllPluginInstancesData,
|
||||
usePluginData,
|
||||
} from '@docusaurus/useGlobalData';
|
||||
|
||||
import {
|
||||
getActivePlugin,
|
||||
getLatestVersion,
|
||||
getActiveVersion,
|
||||
getActiveDocContext,
|
||||
getDocVersionSuggestions,
|
||||
} from './docsClientUtils';
|
||||
import type {
|
||||
GlobalPluginData,
|
||||
GlobalVersion,
|
||||
ActivePlugin,
|
||||
ActiveDocContext,
|
||||
DocVersionSuggestions,
|
||||
GetActivePluginOptions,
|
||||
} from '@docusaurus/plugin-content-docs/client';
|
||||
|
||||
// Important to use a constant object to avoid React useEffect executions etc.
|
||||
// see https://github.com/facebook/docusaurus/issues/5089
|
||||
const StableEmptyObject = {};
|
||||
|
||||
// Not using useAllPluginInstancesData() because 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 = (): Record<string, GlobalPluginData> =>
|
||||
// useAllPluginInstancesData('docusaurus-plugin-content-docs');
|
||||
useGlobalData()['docusaurus-plugin-content-docs'] ?? StableEmptyObject;
|
||||
|
||||
export const useDocsData = (pluginId: string | undefined): GlobalPluginData =>
|
||||
usePluginData('docusaurus-plugin-content-docs', pluginId) as GlobalPluginData;
|
||||
|
||||
// TODO this feature should be provided by docusaurus core
|
||||
export const useActivePlugin = (
|
||||
options: GetActivePluginOptions = {},
|
||||
): ActivePlugin | undefined => {
|
||||
const data = useAllDocsData();
|
||||
const {pathname} = useLocation();
|
||||
return getActivePlugin(data, pathname, options);
|
||||
};
|
||||
|
||||
export const useActivePluginAndVersion = (
|
||||
options: GetActivePluginOptions = {},
|
||||
):
|
||||
| undefined
|
||||
| {activePlugin: ActivePlugin; activeVersion: GlobalVersion | undefined} => {
|
||||
const activePlugin = useActivePlugin(options);
|
||||
const {pathname} = useLocation();
|
||||
if (activePlugin) {
|
||||
const activeVersion = getActiveVersion(activePlugin.pluginData, pathname);
|
||||
return {
|
||||
activePlugin,
|
||||
activeVersion,
|
||||
};
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
|
||||
// versions are returned ordered (most recent first)
|
||||
export const useVersions = (pluginId: string | undefined): GlobalVersion[] => {
|
||||
const data = useDocsData(pluginId);
|
||||
return data.versions;
|
||||
};
|
||||
|
||||
export const useLatestVersion = (
|
||||
pluginId: string | undefined,
|
||||
): GlobalVersion => {
|
||||
const data = useDocsData(pluginId);
|
||||
return getLatestVersion(data);
|
||||
};
|
||||
|
||||
// Note: return undefined on doc-unrelated pages,
|
||||
// because there's no version currently considered as active
|
||||
export const useActiveVersion = (
|
||||
pluginId: string | undefined,
|
||||
): GlobalVersion | undefined => {
|
||||
const data = useDocsData(pluginId);
|
||||
const {pathname} = useLocation();
|
||||
return getActiveVersion(data, pathname);
|
||||
};
|
||||
|
||||
export const useActiveDocContext = (
|
||||
pluginId: string | undefined,
|
||||
): ActiveDocContext => {
|
||||
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 const useDocVersionSuggestions = (
|
||||
pluginId: string | undefined,
|
||||
): DocVersionSuggestions => {
|
||||
const data = useDocsData(pluginId);
|
||||
const {pathname} = useLocation();
|
||||
return getDocVersionSuggestions(data, pathname);
|
||||
};
|
|
@ -5,4 +5,100 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
export * from './globalDataHooks';
|
||||
import {useLocation} from '@docusaurus/router';
|
||||
import useGlobalData, {usePluginData} from '@docusaurus/useGlobalData';
|
||||
|
||||
import {
|
||||
getActivePlugin,
|
||||
getLatestVersion,
|
||||
getActiveVersion,
|
||||
getActiveDocContext,
|
||||
getDocVersionSuggestions,
|
||||
} from './docsClientUtils';
|
||||
import type {
|
||||
GlobalPluginData,
|
||||
GlobalVersion,
|
||||
ActivePlugin,
|
||||
ActiveDocContext,
|
||||
DocVersionSuggestions,
|
||||
GetActivePluginOptions,
|
||||
} from '@docusaurus/plugin-content-docs/client';
|
||||
|
||||
// Important to use a constant object to avoid React useEffect executions etc.
|
||||
// see https://github.com/facebook/docusaurus/issues/5089
|
||||
const StableEmptyObject = {};
|
||||
|
||||
// Not using useAllPluginInstancesData() because 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 = (): Record<string, GlobalPluginData> =>
|
||||
useGlobalData()['docusaurus-plugin-content-docs'] ?? StableEmptyObject;
|
||||
|
||||
export const useDocsData = (pluginId: string | undefined): GlobalPluginData =>
|
||||
usePluginData('docusaurus-plugin-content-docs', pluginId) as GlobalPluginData;
|
||||
|
||||
// TODO this feature should be provided by docusaurus core
|
||||
export const useActivePlugin = (
|
||||
options: GetActivePluginOptions = {},
|
||||
): ActivePlugin | undefined => {
|
||||
const data = useAllDocsData();
|
||||
const {pathname} = useLocation();
|
||||
return getActivePlugin(data, pathname, options);
|
||||
};
|
||||
|
||||
export const useActivePluginAndVersion = (
|
||||
options: GetActivePluginOptions = {},
|
||||
):
|
||||
| undefined
|
||||
| {activePlugin: ActivePlugin; activeVersion: GlobalVersion | undefined} => {
|
||||
const activePlugin = useActivePlugin(options);
|
||||
const {pathname} = useLocation();
|
||||
if (activePlugin) {
|
||||
const activeVersion = getActiveVersion(activePlugin.pluginData, pathname);
|
||||
return {
|
||||
activePlugin,
|
||||
activeVersion,
|
||||
};
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
|
||||
// versions are returned ordered (most recent first)
|
||||
export const useVersions = (pluginId: string | undefined): GlobalVersion[] => {
|
||||
const data = useDocsData(pluginId);
|
||||
return data.versions;
|
||||
};
|
||||
|
||||
export const useLatestVersion = (
|
||||
pluginId: string | undefined,
|
||||
): GlobalVersion => {
|
||||
const data = useDocsData(pluginId);
|
||||
return getLatestVersion(data);
|
||||
};
|
||||
|
||||
// Note: return undefined on doc-unrelated pages,
|
||||
// because there's no version currently considered as active
|
||||
export const useActiveVersion = (
|
||||
pluginId: string | undefined,
|
||||
): GlobalVersion | undefined => {
|
||||
const data = useDocsData(pluginId);
|
||||
const {pathname} = useLocation();
|
||||
return getActiveVersion(data, pathname);
|
||||
};
|
||||
|
||||
export const useActiveDocContext = (
|
||||
pluginId: string | undefined,
|
||||
): ActiveDocContext => {
|
||||
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 const useDocVersionSuggestions = (
|
||||
pluginId: string | undefined,
|
||||
): DocVersionSuggestions => {
|
||||
const data = useDocsData(pluginId);
|
||||
const {pathname} = useLocation();
|
||||
return getDocVersionSuggestions(data, pathname);
|
||||
};
|
||||
|
|
|
@ -6,7 +6,12 @@
|
|||
*/
|
||||
|
||||
// APIs available to Node.js
|
||||
export * from '../constants';
|
||||
export {
|
||||
CURRENT_VERSION_NAME,
|
||||
VERSIONED_DOCS_DIR,
|
||||
VERSIONED_SIDEBARS_DIR,
|
||||
VERSIONS_JSON_FILE,
|
||||
} from './constants';
|
||||
|
||||
export {
|
||||
filterVersions,
|
||||
|
@ -16,4 +21,4 @@ export {
|
|||
getVersionsFilePath,
|
||||
readVersionsFile,
|
||||
readVersionNames,
|
||||
} from '../versions';
|
||||
} from './versions';
|
|
@ -303,7 +303,6 @@ Available document ids are:
|
|||
label: string;
|
||||
}
|
||||
| undefined {
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const item of sidebar) {
|
||||
if (item.type === 'doc') {
|
||||
return {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue