Skip to content

Commit e417383

Browse files
author
Taylor Griffith
committed
blog rendering, but not posts
1 parent b6e6b84 commit e417383

File tree

6 files changed

+68
-66
lines changed

6 files changed

+68
-66
lines changed

2018-02-15-test-post.md

Lines changed: 0 additions & 24 deletions
This file was deleted.

package-lock.json

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"dependencies": {
1313
"axios": "^0.16.2",
14+
"gray-matter": "^3.1.1",
1415
"klaw": "^2.1.1",
1516
"marked": "^0.3.12",
1617
"react": "^16.0.0",

src/containers/Blog.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import React from 'react'
32
import { withRouteData, Link } from 'react-static'
43
//
@@ -10,10 +9,10 @@ export default withRouteData(({ posts }) => (
109
All Posts:
1110
<ul>
1211
{posts.map(post => (
13-
<li key={post.id}>
14-
<Link to={`/blog/post/${post.id}/`}>{post.title}</Link>
12+
<li key={post.data.slug}>
13+
<Link to={`/blog/post/${post.data.slug}`}>{post.data.title}</Link>
1514
</li>
1615
))}
1716
</ul>
1817
</div>
19-
))
18+
))

src/containers/Post.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default withRouteData(({ post }) => (
66
<div>
77
<Link to="/blog/">{'<'} Back</Link>
88
<br />
9-
<h3>{post.title}</h3>
10-
<p>{post.body}</p>
9+
{/* <h3>{post.data.title}</h3> */}
10+
{/* <p>{post.content}</p> */}
1111
</div>
1212
))

static.config.js

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,37 @@ const fs = require('fs');
33
const klaw = require('klaw')
44
const marked = require('marked');
55
const path = require('path')
6+
const matter = require('gray-matter');
67

7-
// Filter function to retrieve .md files //
8+
export default {
9+
getSiteData: () => ({
10+
title: 'React Static with Netlify CMS',
11+
}),
12+
getRoutes: async () => {
813

9-
let filterFn = function(item) {
10-
return path.extname(item) === ".md";
11-
}
14+
// Walk ("klaw") through posts directory and push file paths into posts array //
1215

13-
// Walk (klaw) through posts directory and push file paths into items array //
14-
const items = []
15-
klaw('./src/posts')
16-
.on('readable', function () {
17-
let item
18-
while ((item = this.read())) {
19-
if (filterFn(item.path)) {
20-
items.push(item.path)
16+
// Filter function to retrieve .md files //
17+
let filterFn = function (item) {
18+
return path.extname(item) === ".md";
2119
}
22-
}
23-
})
24-
.on('error', (err, item) => {
25-
console.log(err.message)
26-
console.log(item.path) // the file the error occurred on
27-
})
28-
.on('end', () => {
29-
// Loop through items array and read each post //
30-
for (let post of items ) {
31-
try {
32-
var data = fs.readFileSync(post, 'utf8');
33-
console.log(data);
34-
} catch(e) {
35-
console.log('Error:', e.stack);
36-
}
37-
}
38-
})
3920

21+
const posts = []
4022

23+
await klaw('./src/posts')
24+
.on('data', item => {
25+
if (filterFn(item.path)) {
26+
// If markdown file, read contents //
27+
let data = fs.readFileSync(item.path, 'utf8')
28+
// Convert to frontmatter object and markdown content //
29+
let dataObj = matter(data)
30+
dataObj.content = marked(dataObj.content)
31+
// Create slug for URL //
32+
dataObj.data.slug = dataObj.data.title.toLowerCase().replace(/ /g, '-').replace(/[^\w-]+/g, '')
33+
posts.push(dataObj)
34+
}
35+
})
4136

42-
export default {
43-
getSiteData: () => ({
44-
title: 'React Static with Netlify CMS',
45-
}),
46-
getRoutes: async () => {
47-
const { data: posts } = await axios.get('https://jsonplaceholder.typicode.com/posts')
4837
return [
4938
{
5039
path: '/',
@@ -61,7 +50,7 @@ export default {
6150
posts,
6251
}),
6352
children: posts.map(post => ({
64-
path: `/post/${post.id}`,
53+
path: `/post/${post.data.slug}`,
6554
component: 'src/containers/Post',
6655
getData: () => ({
6756
post,

0 commit comments

Comments
 (0)