| 
									
										
										
										
											2018-09-27 16:00:11 +05:30
										 |  |  | const visit = require('unist-util-visit'); | 
					
						
							|  |  |  | const YAML = require('js-yaml'); | 
					
						
							| 
									
										
										
										
											2020-05-05 14:50:30 +02:00
										 |  |  | const unified = require('unified'); | 
					
						
							|  |  |  | const markdown = require('remark-parse'); | 
					
						
							|  |  |  | const remark2rehype = require('remark-rehype'); | 
					
						
							|  |  |  | const html = require('rehype-stringify'); | 
					
						
							|  |  |  | const raw = require('rehype-raw'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const processor = unified() | 
					
						
							|  |  |  |   .use(markdown) | 
					
						
							|  |  |  |   .use(remark2rehype, { allowDangerousHTML: true }) | 
					
						
							|  |  |  |   .use(raw) | 
					
						
							|  |  |  |   .use(html); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function mdToHTML(str) { | 
					
						
							|  |  |  |   return processor.processSync(str).toString(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-09-27 16:00:11 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | function plugin() { | 
					
						
							|  |  |  |   return transformer; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function transformer(tree, file) { | 
					
						
							|  |  |  |     visit(tree, 'code', visitor); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function visitor(node) { | 
					
						
							|  |  |  |       const { lang, value } = node; | 
					
						
							|  |  |  |       if (lang === 'yml') { | 
					
						
							|  |  |  |         const tests = YAML.load(value); | 
					
						
							| 
									
										
										
										
											2020-05-05 14:50:30 +02:00
										 |  |  |         if (tests.question) { | 
					
						
							| 
									
										
										
										
											2020-06-02 18:52:44 +02:00
										 |  |  |           // mdToHTML can not parse numbers. If an answer is a number
 | 
					
						
							|  |  |  |           // (i.e. 5, not '5') it has to be converted.
 | 
					
						
							| 
									
										
										
										
											2020-05-05 14:50:30 +02:00
										 |  |  |           tests.question.answers = tests.question.answers.map(answer => | 
					
						
							| 
									
										
										
										
											2020-06-02 18:52:44 +02:00
										 |  |  |             mdToHTML(answer.toString()) | 
					
						
							| 
									
										
										
										
											2020-05-05 14:50:30 +02:00
										 |  |  |           ); | 
					
						
							|  |  |  |           tests.question.text = mdToHTML(tests.question.text); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-03 23:13:18 +02:00
										 |  |  |         // since tests are overloaded (they're both a list of projects and
 | 
					
						
							|  |  |  |         // actual tests), it's necessary to check which they are:
 | 
					
						
							|  |  |  |         if (tests.tests && tests.tests[0] && tests.tests[0].text) { | 
					
						
							| 
									
										
										
										
											2020-07-03 16:53:46 +02:00
										 |  |  |           tests.tests = tests.tests.map(({ text, testString }) => ({ | 
					
						
							|  |  |  |             text: mdToHTML(text), | 
					
						
							|  |  |  |             testString | 
					
						
							|  |  |  |           })); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-09-27 16:00:11 +05:30
										 |  |  |         file.data = { | 
					
						
							|  |  |  |           ...file.data, | 
					
						
							|  |  |  |           ...tests | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = plugin; | 
					
						
							| 
									
										
										
										
											2020-05-05 14:50:30 +02:00
										 |  |  | module.exports.mdToHTML = mdToHTML; |