| 
									
										
										
										
											2020-07-31 08:51:41 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright (c) Microsoft Corporation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 09:38:00 +08:00
										 |  |  | // @ts-check
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 08:51:41 +08:00
										 |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2020-12-30 04:12:46 +08:00
										 |  |  | const Documentation = require('./Documentation'); | 
					
						
							| 
									
										
										
										
											2020-12-29 09:38:00 +08:00
										 |  |  | const { MDOutline } = require('./MDBuilder'); | 
					
						
							| 
									
										
										
										
											2020-07-31 08:51:41 +08:00
										 |  |  | const PROJECT_DIR = path.join(__dirname, '..', '..'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-27 06:31:41 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-01-04 00:47:29 +08:00
										 |  |  |   const outline = new MDOutline(path.join(PROJECT_DIR, 'docs', 'src', 'api-body.md'), path.join(PROJECT_DIR, 'docs', 'src', 'api-params.md')); | 
					
						
							| 
									
										
										
										
											2020-12-31 10:04:51 +08:00
										 |  |  |   outline.setLinkRenderer(item => { | 
					
						
							| 
									
										
										
										
											2020-12-30 04:12:46 +08:00
										 |  |  |     const { clazz, member, param, option } = item; | 
					
						
							|  |  |  |     if (param) | 
					
						
							|  |  |  |       return `\`${param}\``; | 
					
						
							|  |  |  |     if (option) | 
					
						
							|  |  |  |       return `\`${option}\``; | 
					
						
							|  |  |  |     if (clazz) | 
					
						
							|  |  |  |       return `\`${clazz.name}\``; | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-01-05 05:50:29 +08:00
										 |  |  |   outline.copyDocsFromSuperclasses([]); | 
					
						
							| 
									
										
										
										
											2020-12-30 04:12:46 +08:00
										 |  |  |   outline.generateSourceCodeComments(); | 
					
						
							|  |  |  |   const result = serialize(outline); | 
					
						
							| 
									
										
										
										
											2020-09-30 04:48:24 +08:00
										 |  |  |   console.log(JSON.stringify(result)); | 
					
						
							| 
									
										
										
										
											2020-12-27 06:31:41 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-07-31 08:51:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-30 04:12:46 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {Documentation} documentation | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-07-31 08:51:41 +08:00
										 |  |  | function serialize(documentation) { | 
					
						
							|  |  |  |   const result = {}; | 
					
						
							|  |  |  |   for (const clazz of documentation.classesArray) | 
					
						
							|  |  |  |     result[clazz.name] = serializeClass(clazz); | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-30 04:12:46 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {Documentation.Class} clazz | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-07-31 08:51:41 +08:00
										 |  |  | function serializeClass(clazz) { | 
					
						
							|  |  |  |   const result = { name: clazz.name }; | 
					
						
							| 
									
										
										
										
											2020-10-03 10:19:19 +08:00
										 |  |  |   if (clazz.extends) | 
					
						
							|  |  |  |     result.extends = clazz.extends; | 
					
						
							| 
									
										
										
										
											2020-11-03 10:31:32 +08:00
										 |  |  |   if (clazz.comment) | 
					
						
							|  |  |  |     result.comment = clazz.comment; | 
					
						
							|  |  |  |   result.methods = {}; | 
					
						
							|  |  |  |   result.events = {}; | 
					
						
							|  |  |  |   result.properties = {}; | 
					
						
							|  |  |  |   for (const member of clazz.membersArray) { | 
					
						
							|  |  |  |     let map; | 
					
						
							|  |  |  |     if (member.kind === 'event') { | 
					
						
							|  |  |  |       map = result.events; | 
					
						
							|  |  |  |     } else if (member.kind === 'method') { | 
					
						
							|  |  |  |       map = result.methods; | 
					
						
							|  |  |  |     } else if (member.kind === 'property') { | 
					
						
							|  |  |  |       map = result.properties; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       throw new Error('Unexpected member kind: ' + member.kind + ' ' + member.name + ' ' + member.type); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     map[member.name] = serializeMember(member); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-07-31 08:51:41 +08:00
										 |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-30 04:12:46 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {Documentation.Member} member | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-07-31 08:51:41 +08:00
										 |  |  | function serializeMember(member) { | 
					
						
							| 
									
										
										
										
											2020-12-30 04:12:46 +08:00
										 |  |  |   const result = /** @type {any} */ ({ ...member }); | 
					
						
							| 
									
										
										
										
											2020-09-30 04:48:24 +08:00
										 |  |  |   sanitize(result); | 
					
						
							| 
									
										
										
										
											2020-07-31 08:51:41 +08:00
										 |  |  |   result.args = {}; | 
					
						
							|  |  |  |   for (const arg of member.argsArray) | 
					
						
							|  |  |  |     result.args[arg.name] = serializeProperty(arg); | 
					
						
							|  |  |  |   if (member.type) | 
					
						
							|  |  |  |     result.type = serializeType(member.type) | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function serializeProperty(arg) { | 
					
						
							|  |  |  |   const result = { ...arg }; | 
					
						
							| 
									
										
										
										
											2020-09-30 04:48:24 +08:00
										 |  |  |   sanitize(result); | 
					
						
							| 
									
										
										
										
											2020-07-31 08:51:41 +08:00
										 |  |  |   if (arg.type) | 
					
						
							|  |  |  |     result.type = serializeType(arg.type) | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-30 04:48:24 +08:00
										 |  |  | function sanitize(result) { | 
					
						
							| 
									
										
										
										
											2020-11-03 10:31:32 +08:00
										 |  |  |   delete result.kind; | 
					
						
							| 
									
										
										
										
											2020-09-30 04:48:24 +08:00
										 |  |  |   delete result.args; | 
					
						
							|  |  |  |   delete result.argsArray; | 
					
						
							|  |  |  |   delete result.templates; | 
					
						
							| 
									
										
										
										
											2020-12-30 04:12:46 +08:00
										 |  |  |   delete result.clazz; | 
					
						
							| 
									
										
										
										
											2020-09-30 04:48:24 +08:00
										 |  |  |   if (result.properties && !Object.keys(result.properties).length) | 
					
						
							|  |  |  |     delete result.properties; | 
					
						
							| 
									
										
										
										
											2020-11-03 10:31:32 +08:00
										 |  |  |   if (result.comment === '') | 
					
						
							|  |  |  |     delete result.comment; | 
					
						
							|  |  |  |   if (result.returnComment === '') | 
					
						
							|  |  |  |     delete result.returnComment; | 
					
						
							| 
									
										
										
										
											2020-09-30 04:48:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 08:51:41 +08:00
										 |  |  | function serializeType(type) { | 
					
						
							|  |  |  |   const result = { ...type }; | 
					
						
							| 
									
										
										
										
											2020-09-30 04:48:24 +08:00
										 |  |  |   if (type.properties && type.properties.length) { | 
					
						
							| 
									
										
										
										
											2020-07-31 08:51:41 +08:00
										 |  |  |     result.properties = {}; | 
					
						
							|  |  |  |     for (const prop of type.properties) | 
					
						
							|  |  |  |       result.properties[prop.name] = serializeProperty(prop); | 
					
						
							| 
									
										
										
										
											2020-09-30 04:48:24 +08:00
										 |  |  |   } else { | 
					
						
							|  |  |  |     delete result.properties; | 
					
						
							| 
									
										
										
										
											2020-07-31 08:51:41 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } |