1+ const path = require ( 'path' ) ;
2+ const glob = require ( 'glob' ) ;
3+ const fs = require ( 'fs' ) ;
4+ const frontmatter = require ( '@github-docs/frontmatter' ) ;
5+ const { v4 : uuidv4 } = require ( 'uuid' ) ;
6+ const { createFilePath } = require ( 'gatsby-source-filesystem' ) ;
7+ const redirects = require ( './src/redirects.json' ) ;
8+
9+ const ignorePaths = [ ] ;
10+
11+ exports . onCreateNode = ( { node, getNode, actions } ) => {
12+ const { createNodeField } = actions ;
13+ if ( node . internal . type === 'MarkdownRemark' ) {
14+ const slug = createFilePath ( { node, getNode, basePath : 'pages' } ) ;
15+ createNodeField ( {
16+ node,
17+ name : 'slug' ,
18+ value : slug ,
19+ } ) ;
20+ }
21+ } ;
22+
23+
24+ exports . createPages = async ( { graphql, actions } ) => {
25+ const { createRedirect, createPage } = actions ;
26+
27+ redirects . forEach ( ( { from, to } ) => {
28+ createRedirect ( {
29+ fromPath : from ,
30+ isPermanent : true ,
31+ redirectInBrowser : true ,
32+ toPath : to ,
33+ } ) ;
34+ } ) ;
35+
36+ const result = await graphql ( `
37+ query {
38+ allMarkdownRemark(sort: {fields: frontmatter___order, order: ASC}) {
39+ edges {
40+ node {
41+ fields {
42+ slug
43+ }
44+ frontmatter {
45+ title
46+ }
47+ }
48+ }
49+ }
50+ }
51+ ` ) ;
52+ result . data . allMarkdownRemark . edges . forEach ( ( { node } , index ) => {
53+ if ( node . fields . slug . includes ( '-' ) ) {
54+ const underscoreSlug = node . fields . slug . replace ( / - / g, '_' ) ;
55+
56+ createRedirect ( {
57+ fromPath : underscoreSlug ,
58+ isPermanent : true ,
59+ redirectInBrowser : true ,
60+ toPath : node . fields . slug ,
61+ } ) ;
62+ }
63+ createPage ( {
64+ path : node . fields . slug ,
65+ component : path . resolve ( './src/templates/page.jsx' ) ,
66+ context : {
67+ slug : node . fields . slug ,
68+ prev : index === 0 ? null : result . data . allMarkdownRemark . edges [ index - 1 ] . node ,
69+ next : index === result . data . allMarkdownRemark . edges . length - 1 ? null : result . data . allMarkdownRemark . edges [ index + 1 ] . node
70+ } ,
71+ } ) ;
72+ } ) ;
73+ } ;
74+
75+
76+
77+ /* Create Header and Footer
78+ /************************************************************************ */
79+ exports . sourceNodes = async ( {
80+ actions,
81+ createNodeId,
82+ createContentDigest,
83+ } ) => {
84+ const prepareNode = ( obj , name ) => {
85+ const data = {
86+ key : uuidv4 ( ) ,
87+ value : JSON . stringify ( obj ) ,
88+ } ;
89+ const node = JSON . stringify ( data ) ;
90+ const nodeMeta = {
91+ id : createNodeId ( `my-data-${ data . key } ` ) ,
92+ parent : null ,
93+ children : [ ] ,
94+ internal : {
95+ type : name ,
96+ mediaType : 'text/json' ,
97+ content : node ,
98+ contentDigest : createContentDigest ( data ) ,
99+ } ,
100+ } ;
101+
102+ const output = { ...data , ...nodeMeta } ;
103+ return output ;
104+ } ;
105+
106+ const { createNode } = actions ;
107+
108+ const getDirectories = ( src ) => glob . sync ( `${ src } /**/*` ) ;
109+ const paths = getDirectories ( './src/pages/tutorials' )
110+ . filter ( ( val ) => val . slice ( - 3 ) === '.md' )
111+ . map ( ( val ) => {
112+ const { data } = frontmatter ( fs . readFileSync ( val ) ) ;
113+ const order = data . order || 200 ;
114+ return [ val , order ] ;
115+ } )
116+ . sort ( ( a , b ) => Number ( a [ 1 ] ) - Number ( b [ 1 ] ) )
117+ . map ( ( val ) => {
118+ let newVal = '' ;
119+ newVal = val [ 0 ] . replace ( / \. \/ s r c \/ p a g e s / g, '' ) ;
120+ newVal = newVal . substring ( 0 , newVal . length - 3 ) ;
121+ newVal = newVal . slice ( - 5 ) === 'index' ? newVal . substring ( 0 , newVal . length - 5 ) : newVal ;
122+ return `${ newVal } /` ;
123+ } )
124+ . filter ( ( val ) => ! ignorePaths . includes ( val ) ) ;
125+
126+ const output = { } ;
127+
128+ paths . forEach ( ( val ) => {
129+ let split = val . split ( '/' ) ;
130+ split = split . filter ( ( url ) => url !== '' ) ;
131+
132+ let current = output ;
133+ split . forEach ( ( part ) => {
134+ current [ part ] = current [ part ] || { } ;
135+ current = current [ part ] ;
136+ } ) ;
137+ current . url = `/${ split . join ( '/' ) } /` ;
138+ } ) ;
139+
140+ createNode ( prepareNode ( output . tutorials , 'leftNavLinks' ) ) ;
141+ } ;
0 commit comments