| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | const crypto = require('crypto'); | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const commonREs = require('../../utils/regEx'); | 
					
						
							|  |  |  | const readDir = require('../../utils/readDir'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const { isAStubRE } = commonREs; | 
					
						
							| 
									
										
										
										
											2018-10-23 14:18:46 +01:00
										 |  |  | // default locale to english for testing
 | 
					
						
							|  |  |  | const { NODE_ENV: env, LOCALE: locale = 'english' } = process.env; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const guideDir = `../../../${ | 
					
						
							|  |  |  |   env === 'production' ? 'guide' : 'mock-guide' | 
					
						
							|  |  |  | }/${locale}`;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const pagesDir = path.resolve(__dirname, guideDir); | 
					
						
							| 
									
										
										
										
											2018-10-25 12:54:57 +03:00
										 |  |  | const indexMdRe = new RegExp(`\\${path.sep}index.md$`); | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | function withGuidePrefix(str) { | 
					
						
							|  |  |  |   return `/guide${str}`; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-23 14:18:46 +01:00
										 |  |  | exports.createNavigationNode = function createNavigationNode(node) { | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  |   const { | 
					
						
							|  |  |  |     fileAbsolutePath, | 
					
						
							|  |  |  |     frontmatter: { title }, | 
					
						
							|  |  |  |     internal: { content }, | 
					
						
							|  |  |  |     parent | 
					
						
							|  |  |  |   } = node; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-25 12:54:57 +03:00
										 |  |  |   const nodeDir = path.resolve(fileAbsolutePath).replace(indexMdRe, ''); | 
					
						
							|  |  |  |   const dashedName = nodeDir.split(path.sep).slice(-1)[0]; | 
					
						
							| 
									
										
										
										
											2019-02-18 19:32:49 +00:00
										 |  |  |   const nodePath = nodeDir | 
					
						
							|  |  |  |     .split(pagesDir)[1] | 
					
						
							|  |  |  |     .split(path.sep) | 
					
						
							|  |  |  |     .join('/'); | 
					
						
							| 
									
										
										
										
											2018-10-23 14:18:46 +01:00
										 |  |  |   const parentPath = nodePath | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  |     .split('/') | 
					
						
							|  |  |  |     .slice(0, -1) | 
					
						
							|  |  |  |     .join('/'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const categoryChildren = readDir(nodeDir); | 
					
						
							|  |  |  |   const navNode = { | 
					
						
							|  |  |  |     categoryChildren, | 
					
						
							|  |  |  |     hasChildren: !!categoryChildren.length, | 
					
						
							|  |  |  |     dashedName, | 
					
						
							|  |  |  |     isStubbed: isAStubRE.test(content), | 
					
						
							| 
									
										
										
										
											2018-10-23 14:18:46 +01:00
										 |  |  |     path: withGuidePrefix(nodePath), | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  |     parentPath: withGuidePrefix(parentPath), | 
					
						
							|  |  |  |     title | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const gatsbyRequired = { | 
					
						
							|  |  |  |     id: fileAbsolutePath + ' >>> NavigationNode', | 
					
						
							|  |  |  |     parent, | 
					
						
							|  |  |  |     children: [], | 
					
						
							|  |  |  |     internal: { | 
					
						
							|  |  |  |       type: 'NavigationNode', | 
					
						
							|  |  |  |       contentDigest: crypto | 
					
						
							|  |  |  |         .createHash('md5') | 
					
						
							|  |  |  |         .update(JSON.stringify(navNode)) | 
					
						
							|  |  |  |         .digest('hex') | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { ...navNode, ...gatsbyRequired }; | 
					
						
							|  |  |  | }; |