refactor(v2): stronger typing for blog plugin (#2072)

* refactor(v2): stronger typing for blog plugin

* Explicit
This commit is contained in:
Endi 2019-11-30 22:26:28 +07:00 committed by GitHub
parent 6155115280
commit 2297ae9f57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 8 deletions

View file

@ -13,18 +13,19 @@ import {normalizeUrl, docuHash} from '@docusaurus/utils';
import { import {
PluginOptions, PluginOptions,
BlogTags, BlogTags,
Tag,
BlogContent, BlogContent,
BlogItemsToModules, BlogItemsToModules,
TagsModule, TagsModule,
BlogPaginated,
} from './types'; } from './types';
import { import {
LoadContext, LoadContext,
PluginContentLoadedActions, PluginContentLoadedActions,
ConfigureWebpackUtils, ConfigureWebpackUtils,
Props, Props,
Plugin,
} from '@docusaurus/types'; } from '@docusaurus/types';
import {Configuration} from 'webpack'; import {Configuration, Loader} from 'webpack';
import {generateBlogFeed, generateBlogPosts} from './blogUtils'; import {generateBlogFeed, generateBlogPosts} from './blogUtils';
const DEFAULT_OPTIONS: PluginOptions = { const DEFAULT_OPTIONS: PluginOptions = {
@ -44,7 +45,7 @@ const DEFAULT_OPTIONS: PluginOptions = {
export default function pluginContentBlog( export default function pluginContentBlog(
context: LoadContext, context: LoadContext,
opts: Partial<PluginOptions>, opts: Partial<PluginOptions>,
) { ): Plugin<BlogContent | null> {
const options: PluginOptions = {...DEFAULT_OPTIONS, ...opts}; const options: PluginOptions = {...DEFAULT_OPTIONS, ...opts};
const contentPath = path.resolve(context.siteDir, options.path); const contentPath = path.resolve(context.siteDir, options.path);
const dataDir = path.join( const dataDir = path.join(
@ -98,7 +99,7 @@ export default function pluginContentBlog(
} = context; } = context;
const basePageUrl = normalizeUrl([baseUrl, routeBasePath]); const basePageUrl = normalizeUrl([baseUrl, routeBasePath]);
const blogListPaginated = []; const blogListPaginated: BlogPaginated[] = [];
function blogPaginationPermalink(page: number) { function blogPaginationPermalink(page: number) {
return page > 0 return page > 0
@ -155,7 +156,7 @@ export default function pluginContentBlog(
return { return {
label: tag, label: tag,
permalink, permalink,
} as Tag; };
} else { } else {
return tag; return tag;
} }
@ -275,6 +276,10 @@ export default function pluginContentBlog(
); );
// Tags. // Tags.
if (blogTagsListPath === null) {
return;
}
const tagsModule: TagsModule = {}; const tagsModule: TagsModule = {};
await Promise.all( await Promise.all(
@ -372,7 +377,7 @@ export default function pluginContentBlog(
truncateMarker, truncateMarker,
}, },
}, },
].filter(Boolean), ].filter(Boolean) as Loader[],
}, },
], ],
}, },

View file

@ -9,7 +9,7 @@ export interface BlogContent {
blogPosts: BlogPost[]; blogPosts: BlogPost[];
blogListPaginated: BlogPaginated[]; blogListPaginated: BlogPaginated[];
blogTags: BlogTags; blogTags: BlogTags;
blogTagsListPath: string; blogTagsListPath: string | null;
} }
export interface DateLink { export interface DateLink {
@ -53,8 +53,18 @@ export interface BlogPost {
metadata: MetaData; metadata: MetaData;
} }
export interface BlogPaginatedMetadata {
permalink: string;
page: number;
postsPerPage: number;
totalPages: number;
totalCount: number;
previousPage: string | null;
nextPage: string | null;
}
export interface BlogPaginated { export interface BlogPaginated {
metadata: MetaData; metadata: BlogPaginatedMetadata;
items: string[]; items: string[];
} }