1
1
import PropTypes from 'prop-types' ;
2
- import matter from 'gray-matter' ;
3
- import Layout from 'components/Layout' ;
4
- import PostList from 'components/PostList' ;
5
- import { useState , useEffect } from 'react' ;
2
+ import BlogPage from 'components/BlogPage' ;
3
+ import parsePosts from 'helpers/parse-posts' ;
4
+ import getCategories from 'helpers/get-categories' ;
6
5
7
6
const socialMeta = {
8
7
image :
@@ -13,95 +12,33 @@ const socialMeta = {
13
12
url : 'blog'
14
13
} ;
15
14
16
- const getTags = posts =>
17
- posts . reduce ( ( tags , post ) => {
18
- if ( ! post . frontmatter ?. tags ) return tags ;
19
- const newTags = post . frontmatter . tags
20
- . split ( ', ' )
21
- . filter ( tag => ! tags . includes ( tag ) ) ;
22
- return [ ...tags , ...newTags ] ;
23
- } , [ ] ) ;
24
-
25
- const Index = ( { posts, title } ) => {
26
- const [ filter , setFilter ] = useState ( null ) ;
27
-
28
- useEffect ( ( ) => {
29
- const { hash } = window . location ;
30
- if ( hash ) {
31
- setFilter ( hash . replace ( '#' , '' ) ) ;
32
- }
33
-
34
- const onHashChanged = ( ) => {
35
- const { hash : newHash } = window . location ;
36
- setFilter ( newHash . replace ( '#' , '' ) ) ;
37
- } ;
38
-
39
- window . addEventListener ( 'hashchange' , onHashChanged ) ;
40
-
41
- return ( ) => {
42
- window . removeEventListener ( 'hashchange' , onHashChanged ) ;
43
- } ;
44
- } , [ ] ) ;
45
-
46
- return (
47
- < Layout
48
- socialMeta = { { ...socialMeta , title } }
49
- breadcrumbs = { [ { name : 'blog' , item : 'blog/' } ] }
50
- >
51
- < div className = 'container' >
52
- < h2 className = 'section__title' > Blog</ h2 >
53
- < div className = 'tags' >
54
- { getTags ( posts ) . map ( tag => (
55
- < a
56
- className = { tag === filter ? 'active' : '' }
57
- key = { tag }
58
- type = 'button'
59
- href = { tag === filter ? '#' : `#${ tag } ` }
60
- >
61
- { tag }
62
- </ a >
63
- ) ) }
64
- </ div >
65
- < PostList posts = { posts } filter = { filter } />
66
- </ div >
67
- </ Layout >
68
- ) ;
69
- } ;
15
+ const Index = props => (
16
+ < BlogPage
17
+ { ...props }
18
+ socialMeta = { { ...socialMeta , title : props . title } }
19
+ breadcrumbs = { [ { name : 'blog' , item : `blog/${ props . currentCategory } ` } ] }
20
+ />
21
+ ) ;
70
22
71
23
Index . propTypes = {
72
- posts : PropTypes . array ,
73
- title : PropTypes . string
24
+ title : PropTypes . string ,
25
+ currentCategory : PropTypes . string
74
26
} ;
75
27
76
28
export default Index ;
77
29
78
30
export async function getStaticProps ( ) {
79
31
const configData = await import ( '../siteconfig.json' ) ;
80
32
81
- const posts = ( context => {
82
- const keys = context . keys ( ) ;
83
- const values = keys . map ( context ) ;
33
+ const posts = parsePosts ( require . context ( '../posts' , true , / \. \/ .* \. m d $ / ) ) ;
84
34
85
- const data = keys
86
- . map ( ( key , index ) => {
87
- const slug = key . replace ( / ^ .* [ \\ / ] / , '' ) . slice ( 0 , - 3 ) ;
88
- const date = slug . match ( / ( \d { 1 , 4 } ( [ . \- - ] ) \d { 1 , 2 } ( [ . \- - ] ) \d { 1 , 4 } ) / g) ;
89
- const value = values [ index ] ;
90
- const document = matter ( value . default ) ;
91
- return {
92
- frontmatter : document . data ,
93
- markdownBody : document . content ,
94
- slug,
95
- date
96
- } ;
97
- } )
98
- . sort ( ( a , b ) => new Date ( b . date ) - new Date ( a . date ) ) ;
99
- return data ;
100
- } ) ( require . context ( '../posts' , true , / \. \/ .* \. m d $ / ) ) ;
35
+ const categories = getCategories ( posts ) ;
101
36
102
37
return {
103
38
props : {
104
39
posts,
40
+ categories,
41
+ currentCategory : '' ,
105
42
title : 'Blog | ' . concat ( configData . default . title )
106
43
}
107
44
} ;
0 commit comments