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

@ -5,11 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/
import {
type BlogPostFrontMatter,
validateBlogPostFrontMatter,
} from '../blogFrontMatter';
import {validateBlogPostFrontMatter} from '../blogFrontMatter';
import escapeStringRegexp from 'escape-string-regexp';
import type {BlogPostFrontMatter} from '@docusaurus/plugin-content-blog';
// TODO this abstraction reduce verbosity but it makes it harder to debug
// It would be preferable to just expose helper methods

View file

@ -8,10 +8,11 @@
import path from 'path';
import {generateBlogFeed} from '../feed';
import type {LoadContext, I18n} from '@docusaurus/types';
import type {PluginOptions, BlogContentPaths} from '../types';
import type {BlogContentPaths} from '../types';
import {DEFAULT_OPTIONS} from '../pluginOptionSchema';
import {generateBlogPosts} from '../blogUtils';
import type {Feed} from 'feed';
import type {PluginOptions} from '@docusaurus/plugin-content-blog';
const DefaultI18N: I18n = {
currentLocale: 'en',

View file

@ -10,9 +10,13 @@ import path from 'path';
import pluginContentBlog from '../index';
import type {DocusaurusConfig, LoadContext, I18n} from '@docusaurus/types';
import {PluginOptionSchema} from '../pluginOptionSchema';
import type {PluginOptions, EditUrlFunction, BlogPost} from '../types';
import type {BlogPost} from '../types';
import type {Joi} from '@docusaurus/utils-validation';
import {posixPath} from '@docusaurus/utils';
import type {
PluginOptions,
EditUrlFunction,
} from '@docusaurus/plugin-content-blog';
function findByTitle(
blogPosts: BlogPost[],

View file

@ -5,10 +5,11 @@
* LICENSE file in the root directory of this source tree.
*/
import type {BlogPost, BlogContent, PluginOptions} from '../types';
import type {BlogPost, BlogContent} from '../types';
import {getTranslationFiles, translateContent} from '../translations';
import {DEFAULT_OPTIONS} from '../pluginOptionSchema';
import {updateTranslationFileMessages} from '@docusaurus/utils';
import type {PluginOptions} from '@docusaurus/plugin-content-blog';
const sampleBlogOptions: PluginOptions = {
...DEFAULT_OPTIONS,

View file

@ -5,14 +5,15 @@
* LICENSE file in the root directory of this source tree.
*/
import type {Author, BlogContentPaths} from './types';
import type {BlogContentPaths} from './types';
import {getDataFileData} from '@docusaurus/utils';
import {Joi, URISchema} from '@docusaurus/utils-validation';
import type {
Author,
BlogPostFrontMatter,
BlogPostFrontMatterAuthor,
BlogPostFrontMatterAuthors,
} from './blogFrontMatter';
} from '@docusaurus/plugin-content-blog';
export type AuthorsMap = Record<string, Author>;

View file

@ -12,21 +12,7 @@ import {
FrontMatterTagsSchema,
FrontMatterTOCHeadingLevels,
} from '@docusaurus/utils-validation';
import type {FrontMatterTag} from '@docusaurus/utils';
export type BlogPostFrontMatterAuthor = Record<string, unknown> & {
key?: string;
name?: string;
imageURL?: string;
url?: string;
title?: string;
};
// All the possible variants that the user can use for convenience
export type BlogPostFrontMatterAuthors =
| string
| BlogPostFrontMatterAuthor
| (string | BlogPostFrontMatterAuthor)[];
import type {BlogPostFrontMatter} from '@docusaurus/plugin-content-blog';
const BlogPostFrontMatterAuthorSchema = Joi.object({
key: Joi.string(),
@ -38,37 +24,6 @@ const BlogPostFrontMatterAuthorSchema = Joi.object({
.or('key', 'name')
.rename('image_url', 'imageURL', {alias: true});
export type BlogPostFrontMatter = {
id?: string;
title?: string;
description?: string;
tags?: FrontMatterTag[];
slug?: string;
draft?: boolean;
date?: Date | string; // Yaml automagically convert some string patterns as Date, but not all
authors?: BlogPostFrontMatterAuthors;
// We may want to deprecate those older author frontmatter fields later:
author?: string;
author_title?: string;
author_url?: string;
author_image_url?: string;
/** @deprecated */
authorTitle?: string;
/** @deprecated */
authorURL?: string;
/** @deprecated */
authorImageURL?: string;
image?: string;
keywords?: string[];
hide_table_of_contents?: boolean;
toc_min_heading_level?: number;
toc_max_heading_level?: number;
};
const FrontMatterAuthorErrorMessage =
'{{#label}} does not look like a valid blog post author. Please use an author key or an author object (with a key and/or name).';

View file

@ -10,12 +10,10 @@ import path from 'path';
import readingTime from 'reading-time';
import {keyBy, mapValues} from 'lodash';
import type {
PluginOptions,
BlogPost,
BlogContentPaths,
BlogMarkdownLoaderOptions,
BlogTags,
ReadingTimeFunction,
} from './types';
import {
parseMarkdownFile,
@ -34,6 +32,10 @@ import type {LoadContext} from '@docusaurus/types';
import {validateBlogPostFrontMatter} from './blogFrontMatter';
import {type AuthorsMap, getAuthorsMap, getBlogPostAuthors} from './authors';
import logger from '@docusaurus/logger';
import type {
PluginOptions,
ReadingTimeFunction,
} from '@docusaurus/plugin-content-blog';
export function truncate(fileString: string, truncateMarker: RegExp): string {
return fileString.split(truncateMarker, 1).shift()!;

View file

@ -6,11 +6,16 @@
*/
import {Feed, type Author as FeedAuthor, type Item as FeedItem} from 'feed';
import type {PluginOptions, Author, BlogPost, FeedType} from './types';
import type {BlogPost} from './types';
import {normalizeUrl, mdxToHtml} from '@docusaurus/utils';
import type {DocusaurusConfig} from '@docusaurus/types';
import path from 'path';
import fs from 'fs-extra';
import type {
FeedType,
PluginOptions,
Author,
} from '@docusaurus/plugin-content-blog';
// TODO this is temporary until we handle mdxToHtml better
// It's hard to convert reliably JSX/require calls to an html feed content

View file

@ -23,7 +23,6 @@ import {
import {translateContent, getTranslationFiles} from './translations';
import type {
PluginOptions,
BlogTags,
BlogContent,
BlogItemsToMetadata,
@ -32,7 +31,6 @@ import type {
BlogContentPaths,
BlogMarkdownLoaderOptions,
MetaData,
Assets,
} from './types';
import {PluginOptionSchema} from './pluginOptionSchema';
import type {
@ -50,8 +48,12 @@ import {
getSourceToPermalink,
getBlogTags,
} from './blogUtils';
import type {BlogPostFrontMatter} from './blogFrontMatter';
import {createBlogFeedFiles} from './feed';
import type {
PluginOptions,
BlogPostFrontMatter,
Assets,
} from '@docusaurus/plugin-content-blog';
export default async function pluginContentBlog(
context: LoadContext,

View file

@ -6,7 +6,145 @@
*/
declare module '@docusaurus/plugin-content-blog' {
export type Options = Partial<import('./types').UserPluginOptions>;
import type {RemarkAndRehypePluginOptions} from '@docusaurus/mdx-loader';
import type {FrontMatterTag} from '@docusaurus/utils';
import type {Overwrite} from 'utility-types';
export interface Assets {
image?: string;
authorsImageUrls: (string | undefined)[]; // Array of same size as the original MetaData.authors array
}
// We allow passing custom fields to authors, e.g., twitter
export interface Author extends Record<string, unknown> {
name?: string;
imageURL?: string;
url?: string;
title?: string;
}
export type BlogPostFrontMatter = {
id?: string;
title?: string;
description?: string;
tags?: FrontMatterTag[];
slug?: string;
draft?: boolean;
date?: Date | string; // Yaml automagically convert some string patterns as Date, but not all
authors?: BlogPostFrontMatterAuthors;
// We may want to deprecate those older author frontmatter fields later:
author?: string;
author_title?: string;
author_url?: string;
author_image_url?: string;
/** @deprecated */
authorTitle?: string;
/** @deprecated */
authorURL?: string;
/** @deprecated */
authorImageURL?: string;
image?: string;
keywords?: string[];
hide_table_of_contents?: boolean;
toc_min_heading_level?: number;
toc_max_heading_level?: number;
};
export type BlogPostFrontMatterAuthor = Record<string, unknown> & {
key?: string;
name?: string;
imageURL?: string;
url?: string;
title?: string;
};
// All the possible variants that the user can use for convenience
export type BlogPostFrontMatterAuthors =
| string
| BlogPostFrontMatterAuthor
| (string | BlogPostFrontMatterAuthor)[];
export type EditUrlFunction = (editUrlParams: {
blogDirPath: string;
blogPath: string;
permalink: string;
locale: string;
}) => string | undefined;
export type FeedType = 'rss' | 'atom' | 'json';
export type FeedOptions = {
type?: FeedType[] | null;
title?: string;
description?: string;
copyright: string;
language?: string;
};
// Feed options, as provided by user config
export type UserFeedOptions = Overwrite<
Partial<FeedOptions>,
{type?: FeedOptions['type'] | 'all'} // Handle the type: "all" shortcut
>;
// Duplicate from ngryman/reading-time to keep stability of API
type ReadingTimeOptions = {
wordsPerMinute?: number;
wordBound?: (char: string) => boolean;
};
export type ReadingTimeFunction = (params: {
content: string;
frontMatter?: BlogPostFrontMatter & Record<string, unknown>;
options?: ReadingTimeOptions;
}) => number;
export type ReadingTimeFunctionOption = (
params: Required<Omit<Parameters<ReadingTimeFunction>[0], 'options'>> & {
defaultReadingTime: ReadingTimeFunction;
},
) => number | undefined;
export type PluginOptions = RemarkAndRehypePluginOptions & {
id?: string;
path: string;
routeBasePath: string;
tagsBasePath: string;
archiveBasePath: string;
include: string[];
exclude: string[];
postsPerPage: number | 'ALL';
blogListComponent: string;
blogPostComponent: string;
blogTagsListComponent: string;
blogTagsPostsComponent: string;
blogTitle: string;
blogDescription: string;
blogSidebarCount: number | 'ALL';
blogSidebarTitle: string;
truncateMarker: RegExp;
showReadingTime: boolean;
feedOptions: {
type?: FeedType[] | null;
title?: string;
description?: string;
copyright: string;
language?: string;
};
editUrl?: string | EditUrlFunction;
editLocalizedFiles?: boolean;
admonitions: Record<string, unknown>;
authorsMapPath: string;
readingTime: ReadingTimeFunctionOption;
sortPosts: 'ascending' | 'descending';
};
// Options, as provided in the user config (before normalization)
export type Options = Overwrite<
Partial<PluginOptions>,
{feedOptions?: UserFeedOptions}
>;
}
declare module '@theme/BlogSidebar' {
@ -27,9 +165,13 @@ declare module '@theme/BlogSidebar' {
declare module '@theme/BlogPostPage' {
import type {BlogSidebar} from '@theme/BlogSidebar';
import type {TOCItem} from '@docusaurus/types';
import type {
BlogPostFrontMatter,
Author,
Assets,
} from '@docusaurus/plugin-content-blog';
export type FrontMatter = import('./blogFrontMatter').BlogPostFrontMatter;
export type Assets = import('./types').Assets;
export type FrontMatter = BlogPostFrontMatter;
export type Metadata = {
readonly title: string;
@ -42,7 +184,7 @@ declare module '@theme/BlogPostPage' {
readonly truncated?: string;
readonly nextItem?: {readonly title: string; readonly permalink: string};
readonly prevItem?: {readonly title: string; readonly permalink: string};
readonly authors: import('./types').Author[];
readonly authors: Author[];
readonly frontMatter: FrontMatter & Record<string, unknown>;
readonly tags: readonly {
readonly label: string;

View file

@ -13,7 +13,7 @@ import {
URISchema,
} from '@docusaurus/utils-validation';
import {GlobExcludeDefault} from '@docusaurus/utils';
import type {PluginOptions} from './types';
import type {PluginOptions} from '@docusaurus/plugin-content-blog';
export const DEFAULT_OPTIONS: PluginOptions = {
feedOptions: {type: ['rss', 'atom'], copyright: ''},

View file

@ -5,8 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/
import type {BlogContent, PluginOptions, BlogPaginated} from './types';
import type {BlogContent, BlogPaginated} from './types';
import type {TranslationFileContent, TranslationFiles} from '@docusaurus/types';
import type {PluginOptions} from '@docusaurus/plugin-content-blog';
function translateListPage(
blogListPaginated: BlogPaginated[],

View file

@ -5,14 +5,15 @@
* LICENSE file in the root directory of this source tree.
*/
import type {RemarkAndRehypePluginOptions} from '@docusaurus/mdx-loader';
import type {Tag} from '@docusaurus/utils';
import type {
BrokenMarkdownLink,
ContentPaths,
} from '@docusaurus/utils/lib/markdownLinks';
import type {Overwrite} from 'utility-types';
import type {BlogPostFrontMatter} from './blogFrontMatter';
import type {
BlogPostFrontMatter,
Author,
} from '@docusaurus/plugin-content-blog';
export type BlogContentPaths = ContentPaths;
@ -24,87 +25,6 @@ export interface BlogContent {
blogTagsListPath: string | null;
}
export type FeedType = 'rss' | 'atom' | 'json';
export type FeedOptions = {
type?: FeedType[] | null;
title?: string;
description?: string;
copyright: string;
language?: string;
};
// Feed options, as provided by user config
export type UserFeedOptions = Overwrite<
Partial<FeedOptions>,
{type?: FeedOptions['type'] | 'all'} // Handle the type: "all" shortcut
>;
export type EditUrlFunction = (editUrlParams: {
blogDirPath: string;
blogPath: string;
permalink: string;
locale: string;
}) => string | undefined;
// Duplicate from ngryman/reading-time to keep stability of API
type ReadingTimeOptions = {
wordsPerMinute?: number;
wordBound?: (char: string) => boolean;
};
export type ReadingTimeFunction = (params: {
content: string;
frontMatter?: BlogPostFrontMatter & Record<string, unknown>;
options?: ReadingTimeOptions;
}) => number;
export type ReadingTimeFunctionOption = (
params: Required<Omit<Parameters<ReadingTimeFunction>[0], 'options'>> & {
defaultReadingTime: ReadingTimeFunction;
},
) => number | undefined;
export type PluginOptions = RemarkAndRehypePluginOptions & {
id?: string;
path: string;
routeBasePath: string;
tagsBasePath: string;
archiveBasePath: string;
include: string[];
exclude: string[];
postsPerPage: number | 'ALL';
blogListComponent: string;
blogPostComponent: string;
blogTagsListComponent: string;
blogTagsPostsComponent: string;
blogTitle: string;
blogDescription: string;
blogSidebarCount: number | 'ALL';
blogSidebarTitle: string;
truncateMarker: RegExp;
showReadingTime: boolean;
feedOptions: {
type?: FeedType[] | null;
title?: string;
description?: string;
copyright: string;
language?: string;
};
editUrl?: string | EditUrlFunction;
editLocalizedFiles?: boolean;
admonitions: Record<string, unknown>;
authorsMapPath: string;
readingTime: ReadingTimeFunctionOption;
sortPosts: 'ascending' | 'descending';
};
// Options, as provided in the user config (before normalization)
export type UserPluginOptions = Overwrite<
Partial<PluginOptions>,
{feedOptions?: UserFeedOptions}
>;
export interface BlogTags {
[key: string]: BlogTag;
}
@ -138,14 +58,6 @@ export interface BlogPaginated {
items: string[];
}
// We allow passing custom fields to authors, e.g., twitter
export interface Author extends Record<string, unknown> {
name?: string;
imageURL?: string;
url?: string;
title?: string;
}
export interface MetaData {
permalink: string;
source: string;
@ -163,11 +75,6 @@ export interface MetaData {
frontMatter: BlogPostFrontMatter & Record<string, unknown>;
}
export interface Assets {
image?: string;
authorsImageUrls: (string | undefined)[]; // Array of same size as the original MetaData.authors array
}
export interface Paginator {
title: string;
permalink: string;