-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add recent blog posts on homepage (#37)
- Loading branch information
Showing
5 changed files
with
108 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { shouldBeListed } from '@docusaurus/plugin-content-blog/src/blogUtils'; | ||
const blogPluginExports = require('@docusaurus/plugin-content-blog'); | ||
|
||
const defaultBlogPlugin = blogPluginExports.default; | ||
|
||
async function blogPluginExtended(...pluginArgs) { | ||
const blogPluginInstance = await defaultBlogPlugin(...pluginArgs); | ||
|
||
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; | ||
|
||
const posts = content.blogPosts | ||
.filter(shouldBeListed) | ||
.map(({ content: _, ...post }) => post); | ||
actions.createData('blog-posts-metadata.json', posts); | ||
|
||
// Call the default overridden `contentLoaded` implementation | ||
return blogPluginInstance.contentLoaded(params); | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = { | ||
...blogPluginExports, | ||
default: blogPluginExtended, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from 'react'; | ||
|
||
const RecentBlogPost = ({ post }) => { | ||
const { metadata } = post; | ||
const formattedDate = new Date(metadata.date).toLocaleDateString('en-US', { | ||
year: 'numeric', | ||
month: 'long', | ||
day: 'numeric', | ||
}); | ||
const readingTime = Math.round(metadata.readingTime); | ||
|
||
return ( | ||
<a href={metadata.permalink} className="card" style={{ textDecoration: 'none', color: 'inherit' }}> | ||
<div className="card__header"> | ||
<h3>{metadata.title}</h3> | ||
</div> | ||
<div className="card__body"> | ||
<p>{metadata.description}</p> | ||
</div> | ||
<div className="card__footer"> | ||
<p>{formattedDate} · {readingTime} min read</p> | ||
</div> | ||
</a> | ||
); | ||
}; | ||
|
||
export default RecentBlogPost; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from 'react'; | ||
import posts from '@site/.docusaurus/docusaurus-plugin-content-blog/default/blog-posts-metadata.json'; | ||
import RecentBlogPost from '../RecentBlogPost'; | ||
|
||
const RecentBlogPosts = ({ nrPosts }) => { | ||
const recentPosts = posts.slice(0, nrPosts); | ||
|
||
if (recentPosts.length === 0) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div> | ||
<h1>Recent Blog Posts</h1> | ||
<div className="card__wrapper" style={{ display: 'grid', gap: '1rem' }}> | ||
{recentPosts.map((post, index) => ( | ||
<RecentBlogPost key={index} post={post} /> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default RecentBlogPosts; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters