Refactor getStaticProps in blog page

This commit is contained in:
Defectink
2022-03-22 17:15:17 +08:00
parent 1179cc3c1f
commit 8f3493c23a
4 changed files with 35 additions and 32 deletions

29
lib/posts.ts Normal file
View File

@ -0,0 +1,29 @@
import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
import { MyMatters, Post } from 'types';
import { sortByDate } from 'lib/utils';
/**
* Read all posts with matter info.
* @returns
*/
export const postLists = async (): Promise<Post[]> => {
const files = fs.readdirSync(path.join('pages/p'));
const posts = files
.map((filename) => {
const markdownWithMeta = fs.readFileSync(
path.join('pages/p', filename),
'utf-8'
);
const slug = filename.replace(/\.mdx$/, '');
const { data: meta } = matter(markdownWithMeta);
return {
slug,
...({ ...meta } as MyMatters),
};
})
.sort(sortByDate);
return posts;
};

View File

@ -1,4 +0,0 @@
export const server =
process.env.NODE_ENV === 'development'
? 'http://localhost:3000'
: 'https://rua.plus';