Skip to content

Commit

Permalink
revert sort by date
Browse files Browse the repository at this point in the history
  • Loading branch information
WanderedToLa committed Jul 19, 2024
1 parent 81ea6ed commit bc4e312
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/plugins/blog-data.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
const fs = require('fs');
const path = require('path');
const matter = require('gray-matter');

async function createBlogDataPlugin(context, options) {
return {
name: 'docusaurus-plugin-blog-data',

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)) {
Expand All @@ -21,14 +25,21 @@ async function createBlogDataPlugin(context, options) {
posts.push({
slug: frontMatter.slug,
title: frontMatter.title,
date: dateStr,
date: new Date(dateStr), // Date 객체로 변환
});
}
}
}
});

// 날짜를 기준으로 내림차순 정렬 (최신 날짜가 앞으로)
posts.sort((a, b) => b.date - a.date);
return posts.slice(0, 5);

// 상위 5개 포스트만 선택하고 날짜를 문자열로 변환
return posts.slice(0, 5).map((post) => ({
...post,
date: post.date.toISOString().split('T')[0], // 'YYYY-MM-DD' 형식으로 변환
}));
},

async contentLoaded({ content, actions }) {
Expand All @@ -37,4 +48,5 @@ async function createBlogDataPlugin(context, options) {
},
};
}

module.exports = createBlogDataPlugin;

0 comments on commit bc4e312

Please sign in to comment.