refactor: move exported type definitions to declaration file (#6300)

* refactor: move exported type definitions to declaration file

* fix

* fix
This commit is contained in:
Joshua Chen 2022-01-09 22:02:31 +08:00 committed by GitHub
parent 9c0e659a44
commit cf265c051e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
53 changed files with 482 additions and 452 deletions

View file

@ -7,7 +7,10 @@
import path from 'path';
import {cliDocsVersionCommand} from '../cli';
import type {PathOptions, SidebarOptions} from '../types';
import type {
PathOptions,
SidebarOptions,
} from '@docusaurus/plugin-content-docs';
import fs from 'fs-extra';
import {
getVersionedDocsDirPath,

View file

@ -19,12 +19,14 @@ import {readVersionsMetadata} from '../versions';
import type {
DocFile,
DocMetadataBase,
MetadataOptions,
VersionMetadata,
PluginOptions,
EditUrlFunction,
DocNavLink,
} from '../types';
import type {
MetadataOptions,
PluginOptions,
EditUrlFunction,
} from '@docusaurus/plugin-content-docs';
import type {LoadContext} from '@docusaurus/types';
import {DEFAULT_OPTIONS} from '../options';
import type {Optional} from 'utility-types';

View file

@ -13,7 +13,7 @@ import {
DisabledNumberPrefixParser,
} from '../numberPrefix';
import {GlobExcludeDefault} from '@docusaurus/utils';
import type {PluginOptions} from '../types';
import type {PluginOptions} from '@docusaurus/plugin-content-docs';
// the type of remark/rehype plugins is function
const markdownPluginsFunctionStub = () => {};

View file

@ -14,8 +14,9 @@ import {
} from '../versions';
import {DEFAULT_OPTIONS} from '../options';
import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
import type {PluginOptions, VersionMetadata} from '../types';
import type {VersionMetadata} from '../types';
import type {I18n} from '@docusaurus/types';
import type {PluginOptions} from '@docusaurus/plugin-content-docs';
const DefaultI18N: I18n = {
currentLocale: 'en',

View file

@ -12,7 +12,10 @@ import {
} from './versions';
import fs from 'fs-extra';
import path from 'path';
import type {PathOptions, SidebarOptions} from './types';
import type {
PathOptions,
SidebarOptions,
} from '@docusaurus/plugin-content-docs';
import {loadSidebarsFile, resolveSidebarPathOption} from './sidebars';
import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
import logger from '@docusaurus/logger';

View file

@ -7,21 +7,18 @@
import {matchPath} from '@docusaurus/router';
import type {GlobalPluginData, GlobalVersion, GlobalDoc} from '../types';
import type {
GlobalPluginData,
GlobalVersion,
GlobalDoc,
GetActivePluginOptions,
ActivePlugin,
ActiveDocContext,
DocVersionSuggestions,
} from '@docusaurus/plugin-content-docs/client';
// This code is not part of the api surface, not in ./theme on purpose
// Short/convenient type aliases
type Version = GlobalVersion;
type Doc = GlobalDoc;
export type ActivePlugin = {
pluginId: string;
pluginData: GlobalPluginData;
};
export type GetActivePluginOptions = {failfast?: boolean}; // use fail-fast option if you know for sure one plugin instance is active
// 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
@ -56,13 +53,7 @@ export function getActivePlugin(
return activePlugin;
}
export type ActiveDocContext = {
activeVersion?: Version;
activeDoc?: Doc;
alternateDocVersions: Record<string, Doc>;
};
export const getLatestVersion = (data: GlobalPluginData): Version =>
export const getLatestVersion = (data: GlobalPluginData): GlobalVersion =>
data.versions.find((version) => version.isLast)!;
// Note: return undefined on doc-unrelated pages,
@ -70,7 +61,7 @@ export const getLatestVersion = (data: GlobalPluginData): Version =>
export const getActiveVersion = (
data: GlobalPluginData,
pathname: string,
): Version | undefined => {
): GlobalVersion | undefined => {
const lastVersion = getLatestVersion(data);
// Last version is a route like /docs/*,
// we need to try to match it last or it would match /docs/version-1.0/* as well
@ -127,13 +118,6 @@ export const getActiveDocContext = (
};
};
export type DocVersionSuggestions = {
// suggest the latest version
latestVersionSuggestion: GlobalVersion;
// suggest the same doc, in latest version (if exist)
latestDocSuggestion?: GlobalDoc;
};
export const getDocVersionSuggestions = (
data: GlobalPluginData,
pathname: string,

View file

@ -11,18 +11,21 @@ import useGlobalData, {
usePluginData,
} from '@docusaurus/useGlobalData';
import type {GlobalPluginData, GlobalVersion} from '../types';
import {
getActivePlugin,
getLatestVersion,
getActiveVersion,
getActiveDocContext,
getDocVersionSuggestions,
type ActivePlugin,
type ActiveDocContext,
type DocVersionSuggestions,
type GetActivePluginOptions,
} 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

View file

@ -28,8 +28,6 @@ import type {
DocMetadata,
DocNavLink,
LastUpdateData,
MetadataOptions,
PluginOptions,
VersionMetadata,
LoadedVersion,
} from './types';
@ -40,6 +38,10 @@ import {stripPathNumberPrefixes} from './numberPrefix';
import {validateDocFrontMatter} from './docFrontMatter';
import type {SidebarsUtils} from './sidebars/utils';
import {toDocNavigationLink, toNavigationLink} from './sidebars/utils';
import type {
MetadataOptions,
PluginOptions,
} from '@docusaurus/plugin-content-docs';
type LastUpdateOptions = Pick<
PluginOptions,

View file

@ -9,13 +9,12 @@ import {mapValues} from 'lodash';
import {normalizeUrl} from '@docusaurus/utils';
import type {Sidebars} from './sidebars/types';
import {createSidebarsUtils} from './sidebars/utils';
import type {DocMetadata, LoadedVersion} from './types';
import type {
DocMetadata,
GlobalDoc,
LoadedVersion,
GlobalVersion,
GlobalSidebar,
} from './types';
GlobalDoc,
} from '@docusaurus/plugin-content-docs/client';
export function toGlobalDataDoc(doc: DocMetadata): GlobalDoc {
return {

View file

@ -30,11 +30,9 @@ import {
import {getDocsDirPaths, readVersionsMetadata} from './versions';
import type {
PluginOptions,
LoadedContent,
SourceToPermalink,
DocMetadataBase,
GlobalPluginData,
VersionMetadata,
LoadedVersion,
DocFile,
@ -54,7 +52,11 @@ import {
import logger from '@docusaurus/logger';
import {getVersionTags} from './tags';
import {createVersionRoutes} from './routes';
import type {PropTagsListPage} from '@docusaurus/plugin-content-docs';
import type {
PropTagsListPage,
PluginOptions,
} from '@docusaurus/plugin-content-docs';
import type {GlobalPluginData} from '@docusaurus/plugin-content-docs/client';
import {createSidebarsUtils} from './sidebars/utils';
import {getCategoryGeneratedIndexMetadataList} from './categoryGeneratedIndex';

View file

@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
import type {NumberPrefixParser} from './types';
import type {NumberPrefixParser} from '@docusaurus/plugin-content-docs';
// Best-effort to avoid parsing some patterns as number prefix
const IgnoredPrefixPatterns = (function () {

View file

@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
import type {PluginOptions} from './types';
import type {PluginOptions} from '@docusaurus/plugin-content-docs';
import {
Joi,
RemarkPluginsSchema,

View file

@ -6,15 +6,76 @@
*/
declare module '@docusaurus/plugin-content-docs' {
export type Options = Partial<import('./types').PluginOptions>;
export type SidebarsConfig = import('./sidebars/types').SidebarsConfig;
export type VersionBanner = import('./types').VersionBanner;
type GlobalDataVersion = import('./types').GlobalVersion;
type GlobalDataDoc = import('./types').GlobalDoc;
type GlobalDataSidebar = import('./types').GlobalSidebar;
type VersionTag = import('./types').VersionTag;
import type {RemarkAndRehypePluginOptions} from '@docusaurus/mdx-loader';
export type {GlobalDataVersion, GlobalDataDoc, GlobalDataSidebar};
export type NumberPrefixParser = (filename: string) => {
filename: string;
numberPrefix?: number;
};
export type EditUrlFunction = (editUrlParams: {
version: string;
versionDocsDirPath: string;
docPath: string;
permalink: string;
locale: string;
}) => string | undefined;
export type MetadataOptions = {
routeBasePath: string;
editUrl?: string | EditUrlFunction;
editCurrentVersion: boolean;
editLocalizedFiles: boolean;
showLastUpdateTime?: boolean;
showLastUpdateAuthor?: boolean;
numberPrefixParser: NumberPrefixParser;
};
export type PathOptions = {
path: string;
sidebarPath?: string | false | undefined;
};
// TODO support custom version banner? {type: "error", content: "html content"}
export type VersionBanner = 'unreleased' | 'unmaintained';
export type VersionOptions = {
path?: string;
label?: string;
banner?: 'none' | VersionBanner;
badge?: boolean;
className?: string;
};
export type VersionsOptions = {
lastVersion?: string;
versions: Record<string, VersionOptions>;
onlyIncludeVersions?: string[];
};
export type SidebarOptions = {
sidebarCollapsible: boolean;
sidebarCollapsed: boolean;
};
export type PluginOptions = MetadataOptions &
PathOptions &
VersionsOptions &
RemarkAndRehypePluginOptions &
SidebarOptions & {
id: string;
include: string[];
exclude: string[];
docLayoutComponent: string;
docItemComponent: string;
docTagDocListComponent: string;
docTagsListComponent: string;
docCategoryGeneratedIndexComponent: string;
admonitions: Record<string, unknown>;
disableVersioning: boolean;
includeCurrentVersion: boolean;
sidebarItemsGenerator: import('./sidebars/types').SidebarItemsGeneratorOption;
tagsBasePath: string;
};
export type Options = Partial<PluginOptions>;
export type SidebarsConfig = import('./sidebars/types').SidebarsConfig;
export type PropNavigationLink = {
readonly title: string;
@ -241,18 +302,54 @@ declare module '@theme/Seo' {
export default Seo;
}
// TODO can't we infer types directly from code?
// TODO until TS supports exports field... hope it's in 4.6
declare module '@docusaurus/plugin-content-docs/client' {
type GlobalPluginData = import('./types').GlobalPluginData;
type GlobalVersion = import('./types').GlobalVersion;
type ActivePlugin = import('./client/docsClientUtils').ActivePlugin;
type ActiveDocContext = import('./client/docsClientUtils').ActiveDocContext;
type DocVersionSuggestions =
import('./client/docsClientUtils').DocVersionSuggestions;
type GetActivePluginOptions =
import('./client/docsClientUtils').GetActivePluginOptions;
export type ActivePlugin = {
pluginId: string;
pluginData: GlobalPluginData;
};
export type ActiveDocContext = {
activeVersion?: GlobalVersion;
activeDoc?: GlobalDoc;
alternateDocVersions: Record<string, GlobalDoc>;
};
export type GlobalDoc = {
id: string;
path: string;
sidebar: string | undefined;
};
export type GlobalVersion = {
name: string;
label: string;
isLast: boolean;
path: string;
mainDocId: string; // home doc (if docs homepage configured), or first doc
docs: GlobalDoc[];
sidebars?: Record<string, GlobalSidebar>;
};
export type GlobalSidebarLink = {
label: string;
path: string;
};
export type GlobalSidebar = {
link?: GlobalSidebarLink;
// ... we may add other things here later
};
export type GlobalPluginData = {
path: string;
versions: GlobalVersion[];
};
export type DocVersionSuggestions = {
// suggest the latest version
latestVersionSuggestion: GlobalVersion;
// suggest the same doc, in latest version (if exist)
latestDocSuggestion?: GlobalDoc;
};
export type GetActivePluginOptions = {failfast?: boolean}; // use fail-fast option if you know for sure one plugin instance is active
export type {GlobalPluginData, GlobalVersion};
export const useAllDocsData: () => Record<string, GlobalPluginData>;
export const useDocsData: (pluginId?: string) => GlobalPluginData;
export const useActivePlugin: (

View file

@ -8,12 +8,13 @@
import fs from 'fs-extra';
import importFresh from 'import-fresh';
import type {SidebarsConfig, Sidebars, NormalizedSidebars} from './types';
import type {NormalizeSidebarsParams, PluginOptions} from '../types';
import type {NormalizeSidebarsParams} from '../types';
import {validateSidebars} from './validation';
import {normalizeSidebars} from './normalization';
import {processSidebars, type SidebarProcessorParams} from './processor';
import path from 'path';
import {createSlugger} from '@docusaurus/utils';
import type {PluginOptions} from '@docusaurus/plugin-content-docs';
export const DefaultSidebars: SidebarsConfig = {
defaultSidebar: [

View file

@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
import type {NormalizeSidebarsParams, SidebarOptions} from '../types';
import type {NormalizeSidebarsParams} from '../types';
import type {
NormalizedSidebarItem,
NormalizedSidebar,
@ -21,6 +21,7 @@ import type {
import {isCategoriesShorthand} from './utils';
import {mapValues} from 'lodash';
import {normalizeUrl} from '@docusaurus/utils';
import type {SidebarOptions} from '@docusaurus/plugin-content-docs';
function normalizeCategoryLink(
category: SidebarItemCategoryConfig,

View file

@ -5,12 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
import type {
NumberPrefixParser,
DocMetadataBase,
VersionMetadata,
SidebarOptions,
} from '../types';
import type {DocMetadataBase, VersionMetadata} from '../types';
import type {
Sidebars,
Sidebar,
@ -31,6 +26,10 @@ import {mapValues, memoize, pick} from 'lodash';
import combinePromises from 'combine-promises';
import {normalizeItem} from './normalization';
import type {Slugger} from '@docusaurus/utils';
import type {
NumberPrefixParser,
SidebarOptions,
} from '@docusaurus/plugin-content-docs';
export type SidebarProcessorParams = {
sidebarItemsGenerator: SidebarItemsGeneratorOption;

View file

@ -6,12 +6,11 @@
*/
import type {Optional, Required} from 'utility-types';
import type {DocMetadataBase, VersionMetadata} from '../types';
import type {
DocMetadataBase,
VersionMetadata,
NumberPrefixParser,
SidebarOptions,
} from '../types';
} from '@docusaurus/plugin-content-docs';
// Makes all properties visible when hovering over the type
type Expand<T extends Record<string, unknown>> = {[P in keyof T]: T[P]};

View file

@ -15,8 +15,9 @@ import {
DefaultNumberPrefixParser,
stripPathNumberPrefixes,
} from './numberPrefix';
import type {DocMetadataBase, NumberPrefixParser} from './types';
import type {DocMetadataBase} from './types';
import {isConventionalDocIndex} from './docs';
import type {NumberPrefixParser} from '@docusaurus/plugin-content-docs';
export default function getSlug({
baseID,

View file

@ -7,13 +7,16 @@
/// <reference types="@docusaurus/module-type-aliases" />
import type {RemarkAndRehypePluginOptions} from '@docusaurus/mdx-loader';
import type {Sidebars} from './sidebars/types';
import type {Tag, FrontMatterTag, Slugger} from '@docusaurus/utils';
import type {
BrokenMarkdownLink as IBrokenMarkdownLink,
ContentPaths,
} from '@docusaurus/utils/lib/markdownLinks';
import type {SidebarItemsGeneratorOption, Sidebars} from './sidebars/types';
import type {
VersionBanner,
SidebarOptions,
} from '@docusaurus/plugin-content-docs';
export type DocFile = {
contentPath: string; // /!\ may be localized
@ -23,10 +26,8 @@ export type DocFile = {
lastUpdate: LastUpdateData;
};
export type VersionName = string;
export type VersionMetadata = ContentPaths & {
versionName: VersionName; // 1.0.0
versionName: string; // 1.0.0
versionLabel: string; // Version 1.0.0
versionPath: string; // /baseUrl/docs/1.0.0
tagsPath: string;
@ -40,76 +41,11 @@ export type VersionMetadata = ContentPaths & {
routePriority: number | undefined; // -1 for the latest docs
};
export type EditUrlFunction = (editUrlParams: {
version: string;
versionDocsDirPath: string;
docPath: string;
permalink: string;
locale: string;
}) => string | undefined;
export type MetadataOptions = {
routeBasePath: string;
editUrl?: string | EditUrlFunction;
editCurrentVersion: boolean;
editLocalizedFiles: boolean;
showLastUpdateTime?: boolean;
showLastUpdateAuthor?: boolean;
numberPrefixParser: NumberPrefixParser;
};
export type PathOptions = {
path: string;
sidebarPath?: string | false | undefined;
};
// TODO support custom version banner? {type: "error", content: "html content"}
export type VersionBanner = 'unreleased' | 'unmaintained';
export type VersionOptions = {
path?: string;
label?: string;
banner?: 'none' | VersionBanner;
badge?: boolean;
className?: string;
};
export type VersionsOptions = {
lastVersion?: string;
versions: Record<string, VersionOptions>;
onlyIncludeVersions?: string[];
};
export type SidebarOptions = {
sidebarCollapsible: boolean;
sidebarCollapsed: boolean;
};
export type NormalizeSidebarsParams = SidebarOptions & {
version: VersionMetadata;
categoryLabelSlugger: Slugger;
};
export type PluginOptions = MetadataOptions &
PathOptions &
VersionsOptions &
RemarkAndRehypePluginOptions &
SidebarOptions & {
id: string;
include: string[];
exclude: string[];
docLayoutComponent: string;
docItemComponent: string;
docTagDocListComponent: string;
docTagsListComponent: string;
docCategoryGeneratedIndexComponent: string;
admonitions: Record<string, unknown>;
disableVersioning: boolean;
includeCurrentVersion: boolean;
sidebarItemsGenerator: SidebarItemsGeneratorOption;
tagsBasePath: string;
};
export type LastUpdateData = {
lastUpdatedAt?: number;
formattedLastUpdatedAt?: string;
@ -142,7 +78,7 @@ export type DocFrontMatter = {
export type DocMetadataBase = LastUpdateData & {
id: string; // TODO legacy versioned id => try to remove
unversionedId: string; // TODO new unversioned id => try to rename to "id"
version: VersionName;
version: string;
title: string;
description: string;
source: string; // @site aliased source => "@site/docs/folder/subFolder/subSubFolder/myDoc.md"
@ -203,37 +139,6 @@ export type LoadedContent = {
loadedVersions: LoadedVersion[];
};
export type GlobalDoc = {
id: string;
path: string;
sidebar: string | undefined;
};
export type GlobalVersion = {
name: VersionName;
label: string;
isLast: boolean;
path: string;
mainDocId: string; // home doc (if docs homepage configured), or first doc
docs: GlobalDoc[];
sidebars?: Record<string, GlobalSidebar>;
};
export type GlobalSidebarLink = {
label: string;
path: string;
};
export type GlobalSidebar = {
link?: GlobalSidebarLink;
// ... we may add other things here later
};
export type GlobalPluginData = {
path: string;
versions: GlobalVersion[];
};
export type BrokenMarkdownLink = IBrokenMarkdownLink<VersionMetadata>;
export type DocsMarkdownOption = {
@ -242,8 +147,3 @@ export type DocsMarkdownOption = {
sourceToPermalink: SourceToPermalink;
onBrokenMarkdownLink: (brokenMarkdownLink: BrokenMarkdownLink) => void;
};
export type NumberPrefixParser = (filename: string) => {
filename: string;
numberPrefix?: number;
};

View file

@ -7,19 +7,19 @@
import path from 'path';
import fs from 'fs-extra';
import type {
PluginOptions,
VersionBanner,
VersionMetadata,
VersionOptions,
VersionsOptions,
} from './types';
import type {VersionMetadata} from './types';
import {
VERSIONS_JSON_FILE,
VERSIONED_DOCS_DIR,
VERSIONED_SIDEBARS_DIR,
CURRENT_VERSION_NAME,
} from './constants';
import type {
PluginOptions,
VersionBanner,
VersionOptions,
VersionsOptions,
} from '@docusaurus/plugin-content-docs';
import type {LoadContext} from '@docusaurus/types';
import {