|
| 1 | +const blogPluginExports = require("@docusaurus/plugin-content-blog"); |
| 2 | + |
| 3 | +const defaultBlogPlugin = blogPluginExports.default; |
| 4 | + |
| 5 | +async function blogPluginExtended(...pluginOptions) { |
| 6 | + const blogPluginInstance = await defaultBlogPlugin(...pluginOptions); |
| 7 | + |
| 8 | + return { |
| 9 | + // Add all properties of the default blog plugin so existing functionality is preserved |
| 10 | + ...blogPluginInstance, |
| 11 | + /** |
| 12 | + * Override the default `contentLoaded` hook to access blog posts data |
| 13 | + */ |
| 14 | + contentLoaded: async function (data) { |
| 15 | + const { content, actions } = data; |
| 16 | + |
| 17 | + const allBlogPosts = content.blogPosts; |
| 18 | + |
| 19 | + async function createRecentPostModule(blogPost, index) { |
| 20 | + return { |
| 21 | + // Inject the metadata you need for each recent blog post |
| 22 | + metadata: await actions.createData( |
| 23 | + `blogpost-metadata-${index}.json`, |
| 24 | + JSON.stringify({ |
| 25 | + title: blogPost.metadata.title, |
| 26 | + description: blogPost.metadata.description, |
| 27 | + frontMatter: blogPost.metadata.frontMatter, |
| 28 | + image: blogPost.metadata.frontMatter.image, |
| 29 | + link: blogPost.metadata.permalink, |
| 30 | + date: blogPost.metadata.date, |
| 31 | + }), |
| 32 | + ), |
| 33 | + |
| 34 | + // Inject the MDX excerpt as a JSX component prop |
| 35 | + // (what's above the <!-- truncate --> marker) |
| 36 | + Preview: { |
| 37 | + __import: true, |
| 38 | + // The markdown file for the blog post will be loaded by webpack |
| 39 | + path: blogPost.metadata.source, |
| 40 | + query: { |
| 41 | + truncated: true, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + // Create the gallery page |
| 48 | + data.actions.addRoute({ |
| 49 | + // Add route for the home page |
| 50 | + path: "/home", |
| 51 | + exact: true, |
| 52 | + |
| 53 | + // The component to use for the "Home" page route |
| 54 | + component: "@site/src/components/Home/Home.tsx", |
| 55 | + // These are the props that will be passed to our "Home" page component |
| 56 | + modules: { |
| 57 | + blogPosts: await Promise.all( |
| 58 | + allBlogPosts.map(createRecentPostModule), |
| 59 | + ), |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + // Call the default overridden `contentLoaded` implementation |
| 64 | + return blogPluginInstance.contentLoaded(data); |
| 65 | + }, |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +module.exports = { |
| 70 | + ...blogPluginExports, |
| 71 | + default: blogPluginExtended, |
| 72 | +}; |
0 commit comments