PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/docs/source/api.js

https://github.com/taxilian/mongoose
JavaScript | 218 lines | 193 code | 21 blank | 4 comment | 33 complexity | 5f0cb3a225ac2a2976635b1558874b89 MD5 | raw file
  1. 'use strict';
  2. /*!
  3. * Module dependencies
  4. */
  5. const dox = require('dox');
  6. const fs = require('fs');
  7. const link = require('../helpers/linktype');
  8. const hl = require('highlight.js');
  9. const md = require('marked');
  10. const files = [
  11. 'lib/index.js',
  12. 'lib/schema.js',
  13. 'lib/connection.js',
  14. 'lib/document.js',
  15. 'lib/model.js',
  16. 'lib/query.js',
  17. 'lib/cursor/QueryCursor.js',
  18. 'lib/aggregate.js',
  19. 'lib/cursor/AggregationCursor.js',
  20. 'lib/schematype.js',
  21. 'lib/virtualtype.js',
  22. 'lib/error/index.js',
  23. 'lib/schema/array.js',
  24. 'lib/schema/documentarray.js',
  25. 'lib/schema/SubdocumentPath.js',
  26. 'lib/options/SchemaTypeOptions.js',
  27. 'lib/options/SchemaArrayOptions.js',
  28. 'lib/options/SchemaBufferOptions.js',
  29. 'lib/options/SchemaDateOptions.js',
  30. 'lib/options/SchemaNumberOptions.js',
  31. 'lib/options/SchemaObjectIdOptions.js',
  32. 'lib/options/SchemaStringOptions.js',
  33. 'lib/types/DocumentArray/methods/index.js',
  34. 'lib/types/subdocument.js',
  35. 'lib/types/ArraySubdocument.js'
  36. ];
  37. module.exports = {
  38. docs: [],
  39. github: 'https://github.com/Automattic/mongoose/blob/',
  40. title: 'API docs',
  41. api: true
  42. };
  43. const out = module.exports.docs;
  44. const combinedFiles = [];
  45. for (const file of files) {
  46. const comments = dox.parseComments(fs.readFileSync(`./${file}`, 'utf8'), { raw: true });
  47. comments.file = file;
  48. combinedFiles.push(comments);
  49. }
  50. parse();
  51. function parse() {
  52. for (const props of combinedFiles) {
  53. let name = props.file.
  54. replace('lib/', '').
  55. replace('.js', '').
  56. replace('/index', '').
  57. replace('/methods', '');
  58. const lastSlash = name.lastIndexOf('/');
  59. const fullName = name;
  60. name = name.substr(lastSlash === -1 ? 0 : lastSlash + 1);
  61. if (name === 'core_array') {
  62. name = 'array';
  63. }
  64. if (fullName === 'schema/array') {
  65. name = 'SchemaArray';
  66. }
  67. if (name === 'documentarray') {
  68. name = 'DocumentArrayPath';
  69. }
  70. if (name === 'DocumentArray') {
  71. name = 'MongooseDocumentArray';
  72. }
  73. const data = {
  74. name: name.charAt(0).toUpperCase() === name.charAt(0) ? name : name.charAt(0).toUpperCase() + name.substr(1),
  75. props: []
  76. };
  77. for (const prop of props) {
  78. if (prop.ignore || prop.isPrivate) {
  79. continue;
  80. }
  81. const ctx = prop.ctx || {};
  82. for (const tag of prop.tags) {
  83. switch (tag.type) {
  84. case 'receiver':
  85. ctx.constructor = tag.string;
  86. break;
  87. case 'property':
  88. ctx.type = 'property';
  89. let str = tag.string;
  90. const match = str.match(/^{\w+}/);
  91. if (match != null) {
  92. ctx.type = match[0].substring(1, match[0].length - 1);
  93. str = str.replace(/^{\w+}\s*/, '');
  94. }
  95. ctx.name = str;
  96. ctx.string = `${ctx.constructor}.prototype.${ctx.name}`;
  97. break;
  98. case 'type':
  99. ctx.type = Array.isArray(tag.types) ? tag.types.join('|') : tag.types;
  100. break;
  101. case 'static':
  102. ctx.type = 'property';
  103. ctx.static = true;
  104. ctx.name = tag.string;
  105. ctx.string = `${data.name}.${ctx.name}`;
  106. break;
  107. case 'function':
  108. ctx.type = 'function';
  109. ctx.static = true;
  110. ctx.name = tag.string;
  111. ctx.string = `${data.name}.${ctx.name}()`;
  112. break;
  113. case 'return':
  114. tag.return = tag.description ?
  115. md.parse(tag.description).replace(/^<p>/, '').replace(/<\/p>$/, '') :
  116. '';
  117. ctx.return = tag;
  118. break;
  119. case 'inherits':
  120. ctx[tag.type] = tag.string;
  121. break;
  122. case 'event':
  123. case 'param':
  124. ctx[tag.type] = (ctx[tag.type] || []);
  125. if (tag.types) {
  126. tag.types = tag.types.join('|');
  127. }
  128. ctx[tag.type].push(tag);
  129. if (tag.name != null && tag.name.startsWith('[') && tag.name.endsWith(']') && tag.name.includes('.')) {
  130. tag.nested = true;
  131. }
  132. tag.description = tag.description ?
  133. md.parse(tag.description).replace(/^<p>/, '').replace(/<\/p>$/, '') :
  134. '';
  135. break;
  136. case 'method':
  137. ctx.type = 'method';
  138. ctx.name = tag.string;
  139. ctx.string = `${ctx.constructor}.prototype.${ctx.name}()`;
  140. break;
  141. case 'memberOf':
  142. ctx.constructor = tag.parent;
  143. ctx.string = `${ctx.constructor}.prototype.${ctx.name}`;
  144. if (ctx.type === 'method') {
  145. ctx.string += '()';
  146. }
  147. break;
  148. case 'constructor':
  149. ctx.string = tag.string + '()';
  150. ctx.name = tag.string;
  151. }
  152. }
  153. if (/\.prototype[^.]/.test(ctx.string)) {
  154. ctx.string = `${ctx.constructor}.prototype.${ctx.name}`;
  155. }
  156. // Backwards compat
  157. if (typeof ctx.constructor === 'string') {
  158. ctx.anchorId = `${ctx.constructor.toLowerCase()}_${ctx.constructor}-${ctx.name}`;
  159. } else if (typeof ctx.receiver === 'string') {
  160. ctx.anchorId = `${ctx.receiver.toLowerCase()}_${ctx.receiver}.${ctx.name}`;
  161. } else {
  162. ctx.anchorId = `${ctx.name.toLowerCase()}_${ctx.name}`;
  163. }
  164. ctx.description = prop.description.full.
  165. replace(/<br \/>/ig, ' ').
  166. replace(/&gt;/ig, '>');
  167. if (ctx.description.includes('function capitalize')) {
  168. console.log('\n\n-------\n\n', ctx);
  169. }
  170. ctx.description = md.parse(ctx.description);
  171. data.props.push(ctx);
  172. }
  173. data.props.sort(function(a, b) {
  174. if (a.string < b.string) {
  175. return -1;
  176. } else {
  177. return 1;
  178. }
  179. });
  180. if (props.file.startsWith('lib/options')) {
  181. data.hideFromNav = true;
  182. }
  183. data.file = props.file;
  184. data.editLink = 'https://github.com/Automattic/mongoose/blob/master/' +
  185. props.file;
  186. out.push(data);
  187. }
  188. }
  189. function highlight(str) {
  190. return str.replace(/(<pre><code>)([^<]+)(<\/code)/gm, function(_, $1, $2, $3) {
  191. const code = /^(?:`{3}([^\n]+)\n)?([\s\S]*)/gm.exec($2);
  192. if ('js' === code[1] || !code[1]) {
  193. code[1] = 'javascript';
  194. }
  195. return $1 + hl.highlight(code[2], { language: code[1] }).value.trim() + $3;
  196. });
  197. }