-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6de4301
commit e3a0a6f
Showing
4 changed files
with
63 additions
and
7 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "blog", | ||
"name": "wanderedtola.blog", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
|
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,48 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const matter = require('gray-matter'); | ||
|
||
async function createRecentPostsPlugin(context, options) { | ||
return { | ||
name: 'docusaurus-plugin-recent-posts', | ||
|
||
async loadContent() { | ||
const blogDir = path.join(__dirname, '../../blog'); | ||
const files = fs.readdirSync(blogDir); | ||
|
||
const posts = []; | ||
|
||
files.forEach((file) => { | ||
const filePath = path.join(blogDir, file); | ||
const stat = fs.statSync(filePath); | ||
|
||
if (stat.isDirectory()) { | ||
const indexFilePath = path.join(filePath, 'index.md'); | ||
if (fs.existsSync(indexFilePath)) { | ||
const content = fs.readFileSync(indexFilePath, 'utf-8'); | ||
const frontMatter = matter(content).data; | ||
if (frontMatter.slug && frontMatter.title) { | ||
const dateStr = file.split('-').slice(0, 3).join('-'); | ||
|
||
posts.push({ | ||
slug: frontMatter.slug, | ||
title: frontMatter.title, | ||
date: dateStr, | ||
}); | ||
} | ||
} | ||
} | ||
}); | ||
posts.sort((a, b) => b.date - a.date); | ||
|
||
return posts.slice(0, 5); | ||
}, | ||
|
||
async contentLoaded({ content, actions }) { | ||
const { setGlobalData } = actions; | ||
setGlobalData({ recentPosts: content }); | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = createRecentPostsPlugin; |