@@ -6,13 +6,11 @@ const { readFile } = require("fs-extra")
66const { promisify } = require ( "util" )
77
88exports . createSchemaCustomization = ( { actions, schema } ) => {
9- const gql = String . raw ;
10- const { createTypes } = actions ;
9+ const gql = String . raw
10+ const { createTypes } = actions
1111
1212 createTypes ( gql `
13- type BlogPost implements Node
14- @childOf(types: ["MarkdownRemark"])
15- {
13+ type BlogPost implements Node @childOf(types: ["MarkdownRemark"]) {
1614 postId: String!
1715 title: String!
1816 tags: [String!]!
@@ -21,8 +19,8 @@ exports.createSchemaCustomization = ({ actions, schema }) => {
2119 guestBio: String
2220 remark: MarkdownRemark! @link # backlink to the parent
2321 }
24- ` ) ;
25- } ;
22+ ` )
23+ }
2624
2725// Transform nodes, each of logic inside here can be extracted to a separated plugin later.
2826exports . onCreateNode = async ( {
@@ -32,60 +30,62 @@ exports.onCreateNode = async ({
3230 createNodeId,
3331 createContentDigest,
3432} ) => {
35- const { createNode, createParentChildLink } = actions ;
33+ const { createNode, createParentChildLink } = actions
3634
3735 // Derive content nodes from remark nodes
38- if ( node . internal . type === 'MarkdownRemark' ) {
39- if ( node . frontmatter . layout === 'blog' ) {
40- const nodeId = createNodeId ( `${ node . id } >>> BlogPost` ) ;
41-
42- const permalink = node . frontmatter . permalink ;
43- if ( ! permalink ?. startsWith ( '/blog/' ) ) {
44- reporter . panicOnBuild ( `${ permalink } is not valid permalink for blog post` ) ;
45- return ;
36+ if ( node . internal . type === "MarkdownRemark" ) {
37+ if ( node . frontmatter . layout === "blog" ) {
38+ const nodeId = createNodeId ( `${ node . id } >>> BlogPost` )
39+
40+ const permalink = node . frontmatter . permalink
41+ if ( ! permalink ?. startsWith ( "/blog/" ) ) {
42+ reporter . panicOnBuild (
43+ `${ permalink } is not valid permalink for blog post`
44+ )
45+ return
4646 }
4747
4848 // It contains a kind of transform logic. However, those logics can be extracted to resolvers into ahead of sourcing (createTypes)
4949 const blogPostContent = {
5050 id : nodeId ,
51- postId : permalink . replace ( ' /blog/' , '' ) . replace ( / \/ $ / , '' ) ,
51+ postId : permalink . replace ( " /blog/" , "" ) . replace ( / \/ $ / , "" ) ,
5252 title : node . frontmatter . title ,
5353 tags : node . frontmatter . tags ?? [ ] ,
5454 date : node . frontmatter . date ,
55- authors : ( node . frontmatter . byline ?? '' )
56- . split ( ',' )
55+ authors : ( node . frontmatter . byline ?? "" )
56+ . split ( "," )
5757 . map ( name => name . trim ( ) )
5858 . filter ( Boolean ) ,
5959 guestBio : node . frontmatter . guestBio ?? null ,
60- } ;
60+ }
6161
6262 createNode ( {
6363 ...blogPostContent ,
6464 remark : node . id ,
6565 parent : node . id ,
6666 children : [ ] ,
6767 internal : {
68- type : ' BlogPost' ,
68+ type : " BlogPost" ,
6969 contentDigest : createContentDigest ( blogPostContent ) ,
7070 } ,
71- } ) ;
71+ } )
7272
7373 createParentChildLink ( {
7474 parent : node ,
7575 child : blogPostContent ,
76- } ) ;
76+ } )
7777 }
7878 }
79- } ;
79+ }
8080
8181exports . onCreatePage = async ( { page, actions } ) => {
8282 // trying to refactor code to be "the Gatsby way".
8383 // from the paths on ready, ignores a bunch of existing custom logic below.
84- if ( page . path . startsWith ( ' /blog' ) ) {
85- return ;
84+ if ( page . path . startsWith ( " /blog" ) ) {
85+ return
8686 }
87- if ( page . path . startsWith ( ' /tags' ) ) {
88- return ;
87+ if ( page . path . startsWith ( " /tags" ) ) {
88+ return
8989 }
9090
9191 const { createPage, deletePage } = actions
@@ -400,10 +400,8 @@ exports.createPages = async ({ graphql, actions }) => {
400400 let i = 0
401401 while ( page && i ++ < 1000 ) {
402402 const { frontmatter } = page
403- const {
404- category : definedCategory ,
405- next : definedNextPageUrl ,
406- } = frontmatter
403+ const { category : definedCategory , next : definedNextPageUrl } =
404+ frontmatter
407405 let category = definedCategory || folder
408406 if ( ! currentCategory || category !== currentCategory . name ) {
409407 if ( currentCategory ) {
@@ -444,27 +442,27 @@ exports.createPages = async ({ graphql, actions }) => {
444442 // Use all the set up data to now tell Gatsby to create pages
445443 // on the site
446444 allPages
447- . filter ( page => ! page . permalink . startsWith ( ' /blog' ) )
445+ . filter ( page => ! page . permalink . startsWith ( " /blog" ) )
448446 . forEach ( page => {
449- createPage ( {
450- path : `${ page . permalink } ` ,
451- component : docTemplate ,
452- context : {
453- permalink : page . permalink ,
454- nextPermalink : page . nextPermalink ,
455- sideBarData : sideBardata [ page . relativeDirectory ] ,
456- sourcePath : page . sourcePath ,
457- } ,
447+ createPage ( {
448+ path : `${ page . permalink } ` ,
449+ component : docTemplate ,
450+ context : {
451+ permalink : page . permalink ,
452+ nextPermalink : page . nextPermalink ,
453+ sideBarData : sideBardata [ page . relativeDirectory ] ,
454+ sourcePath : page . sourcePath ,
455+ } ,
456+ } )
458457 } )
459- } )
460458}
461459
462460exports . onCreateWebpackConfig = ( { actions } ) => {
463461 actions . setWebpackConfig ( {
464462 resolve : {
465463 fallback : {
466- " assert" : require . resolve ( "assert/" ) ,
467- }
468- }
464+ assert : require . resolve ( "assert/" ) ,
465+ } ,
466+ } ,
469467 } )
470468}
0 commit comments