| 
									
										
										
										
											2020-11-14 13:02:40 +00:00
										 |  |  | module.exports = function processNavigationMap(versionInfo, getPreviousMajorVersions, log) { | 
					
						
							| 
									
										
										
										
											2017-03-20 22:23:20 +00:00
										 |  |  |   return { | 
					
						
							|  |  |  |     $runAfter: ['paths-computed'], | 
					
						
							|  |  |  |     $runBefore: ['rendering-docs'], | 
					
						
							|  |  |  |     $process: function(docs) { | 
					
						
							| 
									
										
										
										
											2017-03-31 21:10:54 +01:00
										 |  |  |       const navigationDoc = docs.find(doc => doc.docType === 'navigation-json'); | 
					
						
							| 
									
										
										
										
											2017-03-20 22:23:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (!navigationDoc) { | 
					
						
							|  |  |  |         throw new Error( | 
					
						
							| 
									
										
										
										
											2020-11-14 13:02:40 +00:00
										 |  |  |             'Missing navigation map document (docType="navigation-json").' + | 
					
						
							|  |  |  |             'Did you forget to add it to the readFileProcessor?'); | 
					
						
							| 
									
										
										
										
											2017-03-20 22:23:20 +00:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Verify that all the navigation paths are to valid docs
 | 
					
						
							|  |  |  |       const pathMap = {}; | 
					
						
							|  |  |  |       docs.forEach(doc => pathMap[doc.path] = true); | 
					
						
							|  |  |  |       const errors = walk(navigationDoc.data, pathMap, []); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (errors.length) { | 
					
						
							|  |  |  |         log.error(`Navigation doc: ${navigationDoc.fileInfo.relativePath} contains invalid urls`); | 
					
						
							| 
									
										
										
										
											2017-04-01 21:34:10 +03:00
										 |  |  |         // eslint-disable-next-line no-console
 | 
					
						
							| 
									
										
										
										
											2017-03-20 22:23:20 +00:00
										 |  |  |         console.log(errors); | 
					
						
							| 
									
										
										
										
											2018-04-04 21:06:25 +01:00
										 |  |  |         throw new Error('processNavigationMap failed'); | 
					
						
							| 
									
										
										
										
											2017-03-20 22:23:20 +00:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-21 22:53:34 +08:00
										 |  |  |       function getArchiveUrl(v) { | 
					
						
							|  |  |  |         if ([4, 5].indexOf(v) !== -1) { | 
					
						
							|  |  |  |           return `https://v${v}.angular.io/`; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           return `https://v${v}.angular.cn/`; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-14 13:02:40 +00:00
										 |  |  |       navigationDoc.data['docVersions'] = getPreviousMajorVersions().map( | 
					
						
							| 
									
										
										
										
											2020-12-21 22:53:34 +08:00
										 |  |  |           v => ({title: `v${v.major}`, url: getArchiveUrl(v.major)})); | 
					
						
							| 
									
										
										
										
											2020-11-14 13:02:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-20 22:23:20 +00:00
										 |  |  |       // Add in the version data in a "secret" field to be extracted in the docs app
 | 
					
						
							|  |  |  |       navigationDoc.data['__versionInfo'] = versionInfo.currentVersion; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-04-01 21:34:10 +03:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2017-03-20 22:23:20 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function walk(node, map, path) { | 
					
						
							|  |  |  |   let errors = []; | 
					
						
							| 
									
										
										
										
											2020-11-14 13:02:40 +00:00
										 |  |  |   for (const key in node) { | 
					
						
							| 
									
										
										
										
											2017-03-20 22:23:20 +00:00
										 |  |  |     const child = node[key]; | 
					
						
							| 
									
										
										
										
											2020-11-14 13:02:40 +00:00
										 |  |  |     if (child !== null) {  // null is allowed
 | 
					
						
							| 
									
										
										
										
											2017-04-25 14:48:01 -07:00
										 |  |  |       if (key === 'url') { | 
					
						
							| 
									
										
										
										
											2020-11-14 13:02:40 +00:00
										 |  |  |         const url = child.replace(/#.*$/, '');  // strip hash
 | 
					
						
							| 
									
										
										
										
											2017-04-25 14:48:01 -07:00
										 |  |  |         if (isRelative(url) && !map[url]) { | 
					
						
							| 
									
										
										
										
											2020-11-14 13:02:40 +00:00
										 |  |  |           errors.push({path: path.join('.'), url}); | 
					
						
							| 
									
										
										
										
											2017-04-25 14:48:01 -07:00
										 |  |  |         } | 
					
						
							|  |  |  |       } else if (typeof child !== 'string') { | 
					
						
							|  |  |  |         errors = errors.concat(walk(child, map, path.concat([key]))); | 
					
						
							| 
									
										
										
										
											2017-03-20 22:23:20 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return errors; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function isRelative(url) { | 
					
						
							|  |  |  |   return !/^(https?:)?\/\//.test(url); | 
					
						
							| 
									
										
										
										
											2017-04-01 21:34:10 +03:00
										 |  |  | } |