4
4
* See: https://www.gatsbyjs.org/docs/node-apis/
5
5
*/
6
6
// add all markdown content to gatsby graphQL from src/data with correct paths
7
- exports . onCreateNode = ( { node, getNode, boundActionCreators } ) => {
8
- const { createNodeField } = boundActionCreators ;
7
+ exports . onCreateNode = ( { node, getNode, actions } ) => {
8
+ const { createNodeField } = actions
9
9
10
- if ( node . internal . mediaType !== 'text/markdown' && node . internal . type === 'File' ) {
11
- console . log ( node ) ;
10
+ if (
11
+ node . internal . mediaType !== 'text/markdown' &&
12
+ node . internal . type === 'File'
13
+ ) {
14
+ console . log ( node )
12
15
}
13
16
14
17
// BLOG POSTS
15
- if ( node . internal . type === `MarkdownRemark` && node . frontmatter . layout === 'post' ) {
16
- const fileNode = getNode ( node . parent ) ;
18
+ if (
19
+ node . internal . type === `MarkdownRemark` &&
20
+ node . frontmatter . layout === 'post'
21
+ ) {
22
+ const fileNode = getNode ( node . parent )
17
23
// relative path structure: YYYY-MM-DD-blog-post-title.md
18
- const fragments = fileNode . relativePath . split ( '-' ) ;
19
- const folderName = fragments . slice ( 0 , 3 ) . join ( '' ) . slice ( 2 ) ; // => YYMMDD
20
- const fileName = fragments . slice ( 3 ) . join ( '-' ) . replace ( '.md' , '' ) ; // => blog-post-title
24
+ const fragments = fileNode . relativePath . split ( '-' )
25
+ const folderName = fragments
26
+ . slice ( 0 , 3 )
27
+ . join ( '' )
28
+ . slice ( 2 ) // => YYMMDD
29
+ const fileName = fragments
30
+ . slice ( 3 )
31
+ . join ( '-' )
32
+ . replace ( '.md' , '' ) // => blog-post-title
21
33
22
34
createNodeField ( {
23
35
node,
@@ -27,98 +39,100 @@ exports.onCreateNode = ({ node, getNode, boundActionCreators }) => {
27
39
}
28
40
29
41
// COMICS
30
- if ( node . internal . type === `MarkdownRemark` && node . frontmatter . layout === 'comic' ) {
31
- const fileNode = getNode ( node . parent ) ;
42
+ if (
43
+ node . internal . type === `MarkdownRemark` &&
44
+ node . frontmatter . layout === 'comic'
45
+ ) {
46
+ const fileNode = getNode ( node . parent )
32
47
// relative path structure: comic-title.md
33
- const fileName = fileNode . relativePath . replace ( '.md' , '' ) ; // => comic-title
48
+ const fileName = fileNode . relativePath . replace ( '.md' , '' ) // => comic-title
34
49
35
50
createNodeField ( {
36
51
node,
37
52
name : `path` ,
38
53
value : `/${ fileName } ` ,
39
54
} )
40
55
}
41
- } ;
56
+ }
42
57
58
+ const path = require ( 'path' )
43
59
44
- const path = require ( "path" ) ;
60
+ exports . createPages = async ( { actions, graphql } ) => {
61
+ const { createPage } = actions
45
62
46
- exports . createPages = ( { boundActionCreators, graphql } ) => {
47
- const { createPage } = boundActionCreators ;
48
-
49
- const blogPostTemplate = path . resolve ( `src/templates/blogTemplate.js` ) ;
63
+ const blogPostTemplate = path . resolve ( `src/templates/blogTemplate.js` )
50
64
51
65
// create blog post pages
52
- graphql ( `
53
- {
54
- allMarkdownRemark(
55
- filter: { frontmatter: { layout: { eq: "post" } } }
56
- sort: { order: DESC, fields: [frontmatter___date] }
57
- limit: 10000
58
- ) {
59
- edges {
60
- next {
61
- fields {
62
- path
63
- }
64
- }
65
- node {
66
- fields {
67
- path
68
- }
69
- }
70
- previous {
71
- fields {
72
- path
73
- }
74
- }
75
- }
76
- }
77
- }
78
- ` ) . then ( result => {
79
- if ( result . errors ) {
80
- return Promise . reject ( result . errors ) ;
81
- }
66
+ await graphql ( `
67
+ {
68
+ allMarkdownRemark(
69
+ filter: { frontmatter: { layout: { eq: "post" } } }
70
+ sort: { order: DESC, fields: [frontmatter___date] }
71
+ limit: 10000
72
+ ) {
73
+ edges {
74
+ next {
75
+ fields {
76
+ path
77
+ }
78
+ }
79
+ node {
80
+ fields {
81
+ path
82
+ }
83
+ }
84
+ previous {
85
+ fields {
86
+ path
87
+ }
88
+ }
89
+ }
90
+ }
91
+ }
92
+ ` ) . then ( ( result ) => {
93
+ if ( result . errors ) {
94
+ return Promise . reject ( result . errors )
95
+ }
82
96
83
- result . data . allMarkdownRemark . edges . forEach ( ( { node, next, previous } ) => {
84
- createPage ( {
85
- path : node . fields . path ,
86
- component : blogPostTemplate ,
87
- context : {
88
- nextPostPath : next && next . fields && next . fields . path ,
89
- previousPostPath : previous && previous . fields && previous . fields . path ,
90
- } , // additional data can be passed via context
91
- } ) ;
92
- } ) ;
93
- } ) ;
97
+ result . data . allMarkdownRemark . edges . forEach ( ( { node, next, previous } ) => {
98
+ createPage ( {
99
+ path : node . fields . path ,
100
+ component : blogPostTemplate ,
101
+ context : {
102
+ nextPostPath : next && next . fields && next . fields . path ,
103
+ previousPostPath : previous && previous . fields && previous . fields . path ,
104
+ } , // additional data can be passed via context
105
+ } )
106
+ } )
107
+ } )
94
108
95
109
// create comics pages
96
- graphql ( `
97
- {
98
- allMarkdownRemark(
99
- filter: { frontmatter: { layout: { eq: "comic" } } }
100
- limit: 10000
101
- ) {
102
- edges {
103
- node {
104
- fields {
105
- path
106
- }
110
+ await graphql ( `
111
+ {
112
+ allMarkdownRemark(
113
+ filter: { frontmatter: { layout: { eq: "comic" } } }
114
+ limit: 10000
115
+ ) {
116
+ edges {
117
+ node {
118
+ fields {
119
+ path
107
120
}
108
121
}
109
122
}
110
123
}
111
- ` ) . then ( result => {
112
- if ( result . errors ) {
113
- return Promise . reject ( result . errors ) ;
114
- }
124
+ }
125
+ ` ) . then ( ( result ) => {
126
+ if ( result . errors ) {
127
+ return Promise . reject ( result . errors )
128
+ }
115
129
116
- result . data . allMarkdownRemark . edges . forEach ( ( { node, next, previous } ) => {
117
- createPage ( {
118
- path : node . fields . path ,
119
- component : blogPostTemplate ,
120
- context : { } , // additional data can be passed via context
121
- } ) ;
122
- } ) ;
123
- } ) ;
124
- } ;
130
+ result . data . allMarkdownRemark . edges . forEach ( ( { node, next, previous } ) => {
131
+ createPage ( {
132
+ path : node . fields . path ,
133
+ component : blogPostTemplate ,
134
+ context : { } , // additional data can be passed via context
135
+ } )
136
+ } )
137
+ } )
138
+ }
0 commit comments