feat(v2): plugins injectHtmlTags + configureWebpack should receive content loaded (#5037)

* more lifecycles should receive plugin loaded content

* refactor docs/blog plugins to use newly injected loaded plugin content instead of a mutable variable

* update lifecycle docs

* update lifecycle docs

* fix failing tests
This commit is contained in:
Sébastien Lorber 2021-06-22 17:36:51 +02:00 committed by GitHub
parent 4e88ea0a1a
commit 119c6d143e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 112 additions and 62 deletions

View file

@ -55,7 +55,7 @@ import {
export default function pluginContentBlog(
context: LoadContext,
options: PluginOptions,
): Plugin<BlogContent | null> {
): Plugin<BlogContent> {
if (options.admonitions) {
options.remarkPlugins = options.remarkPlugins.concat([
[admonitions, options.admonitions],
@ -88,8 +88,6 @@ export default function pluginContentBlog(
const aliasedSource = (source: string) =>
`~blog/${posixPath(path.relative(pluginDataDirRoot, source))}`;
let blogPosts: BlogPost[] = [];
return {
name: 'docusaurus-plugin-content-blog',
@ -116,10 +114,19 @@ export default function pluginContentBlog(
async loadContent() {
const {postsPerPage, routeBasePath} = options;
blogPosts = await generateBlogPosts(contentPaths, context, options);
const blogPosts: BlogPost[] = await generateBlogPosts(
contentPaths,
context,
options,
);
if (!blogPosts.length) {
return null;
return {
blogPosts: [],
blogListPaginated: [],
blogTags: {},
blogTagsListPath: null,
};
}
// Colocate next and prev metadata.
@ -242,7 +249,7 @@ export default function pluginContentBlog(
const {addRoute, createData} = actions;
const {
blogPosts: loadedBlogPosts,
blogPosts,
blogListPaginated,
blogTags,
blogTagsListPath,
@ -275,7 +282,7 @@ export default function pluginContentBlog(
// Create routes for blog entries.
await Promise.all(
loadedBlogPosts.map(async (blogPost) => {
blogPosts.map(async (blogPost) => {
const {id, metadata} = blogPost;
await createData(
// Note that this created data path must be in sync with
@ -403,6 +410,7 @@ export default function pluginContentBlog(
_config: Configuration,
isServer: boolean,
{getJSLoader}: ConfigureWebpackUtils,
content,
) {
const {
rehypePlugins,
@ -416,7 +424,7 @@ export default function pluginContentBlog(
siteDir,
contentPaths,
truncateMarker,
sourceToPermalink: getSourceToPermalink(blogPosts),
sourceToPermalink: getSourceToPermalink(content.blogPosts),
onBrokenMarkdownLink: (brokenMarkdownLink) => {
if (onBrokenMarkdownLinks === 'ignore') {
return;
@ -506,8 +514,8 @@ export default function pluginContentBlog(
);
},
injectHtmlTags() {
if (!blogPosts.length) {
injectHtmlTags({content}) {
if (!content.blogPosts.length) {
return {};
}