Skip to content

Commit 62b4455

Browse files
committed
tidy up getStaticProps by creating @utils and test image uploading
1 parent e565163 commit 62b4455

File tree

5 files changed

+34
-20
lines changed

5 files changed

+34
-20
lines changed

content/hello.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: "Hello Friend"
33
date: "2019-01-25"
44
author: "Lorem Ipsum"
5-
cover: "hello.jpg"
5+
cover: "/images/uploads/hello.jpg"
66
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam nec interdum metus. Aenean rutrum ligula sodales ex auctor, sed tempus dui mollis. Curabitur ipsum dui, aliquet nec commodo at, tristique eget ante."
77
---
88

jsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"compilerOptions": {
33
"baseUrl": ".",
44
"paths": {
5-
"@components/*": ["components/*"]
5+
"@components/*": ["components/*"],
6+
"@utils/*": ["utils/*"]
67
}
78
}
89
}

pages/index.js

+3-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import matter from "gray-matter";
22

3+
import { parseMarkdownFiles } from "@utils/utils";
4+
35
import Layout from "@components/Layout";
46
import PostList from "@components/PostList";
57

@@ -24,24 +26,7 @@ export async function getStaticProps() {
2426
// collect all of the markdown files in /content and transform them using
2527
// gray-matter to parse the YAML header and the markdown body
2628
let ctx = require.context("../content", true, /\.md$/);
27-
const posts = ((context) => {
28-
const keys = context.keys();
29-
const values = keys.map(context);
30-
31-
const data = keys.map((key, idx) => {
32-
const slug = key.replace(/^.*[\\\/]/, "").slice(0, -3);
33-
const value = values[idx];
34-
const document = matter(value.default);
35-
36-
return {
37-
frontmatter: document.data,
38-
markdownBody: document.content,
39-
slug,
40-
};
41-
});
42-
43-
return data;
44-
})(ctx);
29+
const posts = parseMarkdownFiles(ctx);
4530

4631
return {
4732
props: {

public/images/uploads/hello.jpg

75.8 KB
Loading

utils/utils.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import matter from "gray-matter";
2+
3+
/*
4+
* inputs a webpack.context object of markdown files and returns an array of
5+
* objects, each containing the frontmatter, markdownBody, and slug for the
6+
* corresponding markdown file
7+
*/
8+
export function parseMarkdownFiles(context) {
9+
// keys are the name(s) of the files and values are the contents of the file
10+
const keys = context.keys();
11+
const values = keys.map(context);
12+
13+
// construct slug from the filename (key)
14+
// pass the file contents through matter to parse frontmatter and markdownBody
15+
const data = keys.map((key, idx) => {
16+
const slug = key.replace(/^.*[\\\/]/, "").slice(0, -3);
17+
const value = values[idx];
18+
const document = matter(value.default);
19+
20+
return {
21+
frontmatter: document.data,
22+
markdownBody: document.content,
23+
slug,
24+
};
25+
});
26+
27+
return data;
28+
}

0 commit comments

Comments
 (0)