feat(preset-classic, content-docs/client): JSDoc (#7148)

* refactor: add JSDoc for preset-classic, content-docs/client

* fix
This commit is contained in:
Joshua Chen 2022-04-11 09:36:30 +08:00 committed by GitHub
parent 25ba91fd4d
commit f4ab7c65ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 139 additions and 111 deletions

View file

@ -59,12 +59,10 @@ export function getActivePlugin(
export const getLatestVersion = (data: GlobalPluginData): GlobalVersion =>
data.versions.find((version) => version.isLast)!;
// Note: return undefined on doc-unrelated pages,
// because there's no version currently considered as active
export const getActiveVersion = (
export function getActiveVersion(
data: GlobalPluginData,
pathname: string,
): GlobalVersion | undefined => {
): GlobalVersion | undefined {
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
@ -80,12 +78,12 @@ export const getActiveVersion = (
strict: false,
}),
);
};
}
export const getActiveDocContext = (
export function getActiveDocContext(
data: GlobalPluginData,
pathname: string,
): ActiveDocContext => {
): ActiveDocContext {
const activeVersion = getActiveVersion(data, pathname);
const activeDoc = activeVersion?.docs.find(
(doc) =>
@ -119,15 +117,15 @@ export const getActiveDocContext = (
activeDoc,
alternateDocVersions: alternateVersionDocs,
};
};
}
export const getDocVersionSuggestions = (
export function getDocVersionSuggestions(
data: GlobalPluginData,
pathname: string,
): DocVersionSuggestions => {
): DocVersionSuggestions {
const latestVersion = getLatestVersion(data);
const activeDocContext = getActiveDocContext(data, pathname);
const latestDocSuggestion: GlobalDoc | undefined =
activeDocContext?.alternateDocVersions[latestVersion.name];
return {latestDocSuggestion, latestVersionSuggestion: latestVersion};
};
}

View file

@ -43,67 +43,61 @@ export const useDocsData = (pluginId: string | undefined): GlobalPluginData =>
}) as GlobalPluginData;
// TODO this feature should be provided by docusaurus core
export const useActivePlugin = (
export function useActivePlugin(
options: UseDataOptions = {},
): ActivePlugin | undefined => {
): ActivePlugin | undefined {
const data = useAllDocsData();
const {pathname} = useLocation();
return getActivePlugin(data, pathname, options);
};
}
export const useActivePluginAndVersion = (
export function useActivePluginAndVersion(
options: UseDataOptions = {},
):
| undefined
| {activePlugin: ActivePlugin; activeVersion: GlobalVersion | undefined} => {
| {activePlugin: ActivePlugin; activeVersion: GlobalVersion | undefined}
| undefined {
const activePlugin = useActivePlugin(options);
const {pathname} = useLocation();
if (activePlugin) {
const activeVersion = getActiveVersion(activePlugin.pluginData, pathname);
return {
activePlugin,
activeVersion,
};
if (!activePlugin) {
return undefined;
}
return undefined;
};
const activeVersion = getActiveVersion(activePlugin.pluginData, pathname);
return {
activePlugin,
activeVersion,
};
}
// versions are returned ordered (most recent first)
export const useVersions = (pluginId: string | undefined): GlobalVersion[] => {
export function useVersions(pluginId: string | undefined): GlobalVersion[] {
const data = useDocsData(pluginId);
return data.versions;
};
}
export const useLatestVersion = (
pluginId: string | undefined,
): GlobalVersion => {
export function 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 = (
export function useActiveVersion(
pluginId: string | undefined,
): GlobalVersion | undefined => {
): GlobalVersion | undefined {
const data = useDocsData(pluginId);
const {pathname} = useLocation();
return getActiveVersion(data, pathname);
};
}
export const useActiveDocContext = (
export function useActiveDocContext(
pluginId: string | undefined,
): ActiveDocContext => {
): 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 = (
export function useDocVersionSuggestions(
pluginId: string | undefined,
): DocVersionSuggestions => {
): DocVersionSuggestions {
const data = useDocsData(pluginId);
const {pathname} = useLocation();
return getDocVersionSuggestions(data, pathname);
};
}