| 
									
										
										
										
											2017-04-16 22:13:09 +01:00
										 |  |  | module.exports = function convertToJsonProcessor(log, createDocMessage) { | 
					
						
							| 
									
										
										
										
											2017-02-21 16:57:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     $runAfter: ['checkUnbalancedBackTicks'], | 
					
						
							|  |  |  |     $runBefore: ['writeFilesProcessor'], | 
					
						
							|  |  |  |     docTypes: [], | 
					
						
							|  |  |  |     $process: function(docs) { | 
					
						
							| 
									
										
										
										
											2017-04-01 21:34:10 +03:00
										 |  |  |       const docTypes = this.docTypes; | 
					
						
							| 
									
										
										
										
											2017-02-21 16:57:19 +00:00
										 |  |  |       docs.forEach((doc) => { | 
					
						
							|  |  |  |         if (docTypes.indexOf(doc.docType) !== -1) { | 
					
						
							| 
									
										
										
										
											2017-04-16 22:13:09 +01:00
										 |  |  |           let contents = doc.renderedContent || ''; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-19 09:56:39 +01:00
										 |  |  |           let title = doc.title; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           // We do allow an empty `title` but resort to `name` if it is not even defined
 | 
					
						
							|  |  |  |           if (title === undefined) { | 
					
						
							|  |  |  |             title = doc.name; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-16 22:13:09 +01:00
										 |  |  |           // If there is no title then try to extract it from the first h1 in the renderedContent
 | 
					
						
							|  |  |  |           if (title === undefined) { | 
					
						
							|  |  |  |             const match = /<h1[^>]*>(.+?)<\/h1>/.exec(contents); | 
					
						
							|  |  |  |             if (match) { | 
					
						
							|  |  |  |               title = match[1]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           // If there is still no title then log a warning
 | 
					
						
							|  |  |  |           if (title === undefined) { | 
					
						
							|  |  |  |             title = ''; | 
					
						
							|  |  |  |             log.warn(createDocMessage('Title property expected', doc)); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-20 12:55:34 +01:00
										 |  |  |           doc.renderedContent = JSON.stringify({ id: doc.path, title, contents }, null, 2); | 
					
						
							| 
									
										
										
										
											2017-02-21 16:57:19 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2017-04-16 22:13:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-01 21:34:10 +03:00
										 |  |  | }; |