Skip to content

Commit b0a2eb5

Browse files
committed
adding blogs pages
1 parent 6c88b25 commit b0a2eb5

File tree

8 files changed

+121
-32
lines changed

8 files changed

+121
-32
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
},
3434
"dependencies": {
3535
"@preact/prerender-data-provider": "^0.0.1",
36+
"markdown-to-jsx": "^6.10.3",
3637
"preact": "^10.0.0",
3738
"preact-cli-plugin-netlify": "^1.5.0",
3839
"preact-render-to-string": "^5.0.6",

prerender-urls.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
const { generateFileList } = require('./src/crawler');
22
const { join } = require('path');
3+
const fs = require('fs');
34

45
const [blogs, images] = generateFileList(join(__dirname, 'content')).nodes;
5-
66
module.exports = () => {
77
const pages = [
88
{ url: '/' }
99
];
1010

11-
// adding all blog posts page
11+
// adding blogs list posts page
1212
pages.push({
13-
url: '/blog/',
13+
url: '/blogs/',
1414
data: blogs
1515
});
1616

17+
// adding all blog pages
18+
pages.push(...blogs.edges.map(blog => {
19+
const data = fs.readFileSync(join('content', 'blog', blog.id), 'utf-8').replace(/---(.*\n)*---/, '');
20+
return {
21+
url: `/blog/${blog.id}`,
22+
data: {
23+
content: data
24+
}
25+
};
26+
}));
27+
28+
1729
pages.push({
1830
url: '/photography/',
1931
data: images
2032
});
2133

22-
console.log({pages});
23-
2434
return pages;
2535
};

src/components/app.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Header from './header';
55

66
// Code-splitting is automated for routes
77
import Home from '../routes/home';
8+
import Blogs from '../routes/blogs';
89
import Blog from '../routes/blog';
910
import Photographs from '../routes/photographs';
1011

@@ -25,7 +26,8 @@ export default class App extends Component {
2526
<Header />
2627
<Router onChange={this.handleRoute}>
2728
<Home path="/" />
28-
<Blog path="/blog/" />
29+
<Blogs path="/blogs/" />
30+
<Blog path="/blog/:name" />
2931
<Photographs path="/photography/" />
3032
</Router>
3133
</div>

src/components/header/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const Header = () => (
66
<header class={style.header}>
77
<Link href="/"><h1>Jane Doe</h1></Link>
88
<nav>
9-
<Link activeClassName={style.active} href="/blog">Blogs</Link>
9+
<Link activeClassName={style.active} href="/blogs">Blogs</Link>
1010
<Link activeClassName={style.active} href="/photography">Photography</Link>
1111
</nav>
1212
</header>

src/routes/blog/index.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,20 @@
11
import { h } from 'preact';
22
import { Link } from 'preact-router';
33
import { usePrerenderData } from '@preact/prerender-data-provider';
4+
import Markdown from 'markdown-to-jsx';
45
import style from './style';
56

67
const blogs = (props) => {
78
const [data, isLoading] = usePrerenderData(props);
89
if (isLoading) {
910
return (<h1>Loading</h1>);
1011
}
11-
if (data && data.data) {
12-
const { data: blogs } = data;
13-
return (
14-
<div class={style.pageBlogs}>
15-
<h1 class={style.pageTitle}>My Blogs</h1>
16-
{blogs.edges.map(blog => (
17-
<Link href={`/blog/${blog.id}`}>
18-
<article>
19-
<h2>{blog.details.title}</h2>
20-
<div>
21-
{
22-
(blog.details.tags.substr(1, blog.details.tags.length - 2).split(',') || []).map(tag => <span class={style.tag}>{tag}</span>)
23-
}
24-
</div>
25-
<p class={style.preview}>
26-
{blog.preview}
27-
</p>
28-
</article>
29-
</Link>
30-
))}
31-
</div>
32-
);
12+
13+
if (data && data.data && data.data.content) {
14+
return (<Markdown>{ data.data.content }</Markdown>);
3315
}
3416

35-
return (<h1>Error</h1>);
17+
return (<h1>Blog</h1>);
3618
};
3719

3820
export default blogs;

src/routes/blogs/index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { h } from 'preact';
2+
import { Link } from 'preact-router';
3+
import { usePrerenderData } from '@preact/prerender-data-provider';
4+
import style from './style';
5+
6+
const blogs = (props) => {
7+
const [data, isLoading] = usePrerenderData(props);
8+
if (isLoading) {
9+
return (<h1>Loading</h1>);
10+
}
11+
if (data && data.data) {
12+
const { data: blogs } = data;
13+
return (
14+
<div class={style.pageBlogs}>
15+
<h1 class={style.pageTitle}>My Blogs</h1>
16+
{blogs.edges.map(blog => (
17+
<Link href={`/blog/${blog.id}`}>
18+
<article>
19+
<h2>{blog.details.title}</h2>
20+
<div>
21+
{
22+
(blog.details.tags.substr(1, blog.details.tags.length - 2).split(',') || []).map(tag => <span class={style.tag}>{tag}</span>)
23+
}
24+
</div>
25+
<p class={style.preview}>
26+
{blog.preview}
27+
</p>
28+
</article>
29+
</Link>
30+
))}
31+
</div>
32+
);
33+
}
34+
35+
return (<h1>Error</h1>);
36+
};
37+
38+
export default blogs;

src/routes/blogs/style.css

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.pageBlogs {
2+
padding: 0 5%;
3+
}
4+
5+
.pageTitle {
6+
position: relative;
7+
display: inline-block;
8+
font-weight: 500;
9+
font-family: serif;
10+
}
11+
12+
.pageTitle:after {
13+
position: absolute;
14+
bottom: -5px;
15+
left: 0px;
16+
content: '';
17+
width: 75%;
18+
border-bottom: 2px solid #FFC107;
19+
}
20+
21+
h2 {
22+
font-weight: 400;
23+
font-size: 24px;
24+
margin: 48px 0 12px 0;
25+
}
26+
27+
.tag {
28+
padding: 4px 8px;
29+
margin-left: 8px;
30+
text-transform: lowercase;
31+
background-color: #ffdd037c;
32+
border-radius: 5px;
33+
color: #999;
34+
font-size: 12px;
35+
}
36+
37+
.tag:first-child {
38+
margin-left: 0px;
39+
}
40+
41+
.preview {
42+
width: 50%;
43+
line-height: 1.3;
44+
color: #666;
45+
max-height: 80px;
46+
overflow: hidden;
47+
text-overflow: ellipsis;
48+
}

yarn.lock

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6233,6 +6233,14 @@ map-visit@^1.0.0:
62336233
dependencies:
62346234
object-visit "^1.0.0"
62356235

6236+
markdown-to-jsx@^6.10.3:
6237+
version "6.10.3"
6238+
resolved "https://registry.yarnpkg.com/markdown-to-jsx/-/markdown-to-jsx-6.10.3.tgz#7f0946684acd321125ff2de7fd258a9b9c7c40b7"
6239+
integrity sha512-PSoUyLnW/xoW6RsxZrquSSz5eGEOTwa15H5eqp3enmrp8esmgDJmhzd6zmQ9tgAA9TxJzx1Hmf3incYU/IamoQ==
6240+
dependencies:
6241+
prop-types "^15.6.2"
6242+
unquote "^1.1.0"
6243+
62366244
markdown@^0.5.0:
62376245
version "0.5.0"
62386246
resolved "https://registry.yarnpkg.com/markdown/-/markdown-0.5.0.tgz#28205b565a8ae7592de207463d6637dc182722b2"
@@ -7961,7 +7969,7 @@ prompts@^2.2.1:
79617969
kleur "^3.0.3"
79627970
sisteransi "^1.0.3"
79637971

7964-
prop-types@^15.6.1, prop-types@^15.7.2:
7972+
prop-types@^15.6.1, prop-types@^15.6.2, prop-types@^15.7.2:
79657973
version "15.7.2"
79667974
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.7.2.tgz#52c41e75b8c87e72b9d9360e0206b99dcbffa6c5"
79677975
integrity sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==
@@ -9678,7 +9686,7 @@ [email protected], unpipe@~1.0.0:
96789686
resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec"
96799687
integrity sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=
96809688

9681-
unquote@~1.1.1:
9689+
unquote@^1.1.0, unquote@~1.1.1:
96829690
version "1.1.1"
96839691
resolved "https://registry.yarnpkg.com/unquote/-/unquote-1.1.1.tgz#8fded7324ec6e88a0ff8b905e7c098cdc086d544"
96849692
integrity sha1-j97XMk7G6IoP+LkF58CYzcCG1UQ=

0 commit comments

Comments
 (0)