-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcustom-blog-plugin.js
More file actions
59 lines (50 loc) · 1.8 KB
/
custom-blog-plugin.js
File metadata and controls
59 lines (50 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const blogPluginExports = require("@docusaurus/plugin-content-blog");
const defaultBlogPlugin = blogPluginExports.default;
async function blogPluginExtended(...pluginArgs) {
const blogPluginInstance = await defaultBlogPlugin(...pluginArgs);
const pluginOptions = pluginArgs[1];
return {
// Add all properties of the default blog plugin so existing functionality is preserved
...blogPluginInstance,
/**
* Override the default `contentLoaded` hook to access blog posts data
*/
contentLoaded: async function (params) {
const { content, actions } = params;
// Get the 5 latest blog posts
const recentPostsLimit = 5;
const recentPosts = [...content.blogPosts].splice(0, recentPostsLimit);
async function createRecentPostModule(blogPost, index) {
return {
// Inject the metadata you need for each recent blog post
metadata: {
title: blogPost.metadata.title,
description: blogPost.metadata.description,
date: blogPost.metadata.date,
permalink: blogPost.metadata.permalink,
frontMatter: blogPost.metadata.frontMatter,
},
};
}
actions.setGlobalData({
default: {
homePageBlogMetadata: {
blogTitle: pluginOptions.blogTitle,
blogDescription: pluginOptions.blogDescription,
totalPosts: content.blogPosts.length,
totalRecentPosts: recentPosts.length,
},
recentPosts: await Promise.all(
recentPosts.map(createRecentPostModule),
),
},
});
// Call the default overridden `contentLoaded` implementation
return blogPluginInstance.contentLoaded(params);
},
};
}
module.exports = {
...blogPluginExports,
default: blogPluginExtended,
};