-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path[...slug].js
53 lines (47 loc) · 1.65 KB
/
[...slug].js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import matter from 'gray-matter';
import glob from 'glob';
import { fetchGitHubUser } from '@/utils/fetchData';
export { default } from '@/components/pages/blog/post';
export async function getStaticProps({ params }) {
const { slug } = params;
// retrieving the Markdown file associated to the slug
// and reading its data
const content = await import(`../../data/blog/${slug.join('/')}.md`);
const data = await matter(content.default);
// fetching authors user data from GitHub
const author = data.data.author
? await fetchGitHubUser(data.data.author)
: null;
return {
props: {
frontmatter: {
...data.data,
date: new Date(data.data.date).toISOString(),
},
markdownBody: data.content,
author,
},
};
}
export async function getStaticPaths() {
// getting all .md files from the blog directory, including subdirectories
const posts = glob.sync('data/blog/**/*.md');
// converting the file paths to nested slugs (e.g., ['2023', '04', 'my-test-post'])
const postSlugs = posts.map((file) => {
// Split the file path and remove the first 'data/blog' part and '.md' extension
const pathParts = file.split('/').slice(2);
// Remove the '.md' extension from the last part (post title)
pathParts[pathParts.length - 1] = pathParts[pathParts.length - 1].replace(
/\.md$/,
''
);
// Replace spaces with hyphens in each part
return pathParts.map((part) => part.replace(/ /g, '-'));
});
// creating a path for each of the nested `slug` parameter
const paths = postSlugs.map((slugArray) => ({ params: { slug: slugArray } }));
return {
paths,
fallback: false,
};
}