44 * See: https://www.gatsbyjs.org/docs/node-apis/
55 */
66// 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
99
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 )
1215 }
1316
1417 // 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 )
1723 // 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
2133
2234 createNodeField ( {
2335 node,
@@ -27,98 +39,100 @@ exports.onCreateNode = ({ node, getNode, boundActionCreators }) => {
2739 }
2840
2941 // 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 )
3247 // relative path structure: comic-title.md
33- const fileName = fileNode . relativePath . replace ( '.md' , '' ) ; // => comic-title
48+ const fileName = fileNode . relativePath . replace ( '.md' , '' ) // => comic-title
3449
3550 createNodeField ( {
3651 node,
3752 name : `path` ,
3853 value : `/${ fileName } ` ,
3954 } )
4055 }
41- } ;
56+ }
4257
58+ const path = require ( 'path' )
4359
44- const path = require ( "path" ) ;
60+ exports . createPages = async ( { actions, graphql } ) => {
61+ const { createPage } = actions
4562
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` )
5064
5165 // 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+ }
8296
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+ } )
94108
95109 // 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
107120 }
108121 }
109122 }
110123 }
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+ }
115129
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