| 
									
										
										
										
											2017-01-26 14:03:53 +00:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @dgProcessor checkUnbalancedBackTicks | 
					
						
							|  |  |  |  * @description | 
					
						
							|  |  |  |  * Searches the rendered content for an odd number of (```) backticks,
 | 
					
						
							|  |  |  |  * which would indicate an unbalanced pair and potentially a typo in the | 
					
						
							|  |  |  |  * source content. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | module.exports = function checkUnbalancedBackTicks(log, createDocMessage) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-12 07:13:16 -04:00
										 |  |  |   const BACKTICK_REGEX = /^ *```/gm; | 
					
						
							|  |  |  |   const UNBALANCED_BACKTICK_WARNING = 'checkUnbalancedBackTicks processor: unbalanced backticks found in rendered content'; | 
					
						
							| 
									
										
										
										
											2017-01-26 14:03:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     $runAfter: ['inlineTagProcessor'], | 
					
						
							|  |  |  |     $runBefore: ['writeFilesProcessor'], | 
					
						
							|  |  |  |     $process: function(docs) { | 
					
						
							| 
									
										
										
										
											2020-05-12 07:13:16 -04:00
										 |  |  |       docs | 
					
						
							|  |  |  |       .forEach(doc => setUnbalancedBackTicks(doc)); | 
					
						
							| 
									
										
										
										
											2017-01-26 14:03:53 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2020-05-12 07:13:16 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   function setUnbalancedBackTicks(doc) { | 
					
						
							|  |  |  |     if (!doc.renderedContent) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const matches = doc.renderedContent.match(BACKTICK_REGEX); | 
					
						
							|  |  |  |     if (matches && matches.length % 2 !== 0) { | 
					
						
							|  |  |  |       doc.unbalancedBackTicks = true; | 
					
						
							|  |  |  |       log.warn(createDocMessage(UNBALANCED_BACKTICK_WARNING, doc)); | 
					
						
							|  |  |  |       log.warn(doc.renderedContent); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-01-26 14:03:53 +00:00
										 |  |  | }; |