UNPKG

1.02 kBJavaScriptView Raw
1var trim = require('../utils/trim');
2var unindent = require('../utils/unindent');
3
4function parse(content, source) {
5 source = trim(source);
6
7 var title = '';
8 var text = '';
9 var type;
10
11 // Search for @apiExample "[{type}] title and content
12 // /^(@\w*)?\s?(?:(?:\{(.+?)\})\s*)?(.*)$/gm;
13 var parseRegExpFirstLine = /(@\w*)?(?:(?:\s*\{\s*([a-zA-Z0-9\.\/\\\[\]_-]+)\s*\}\s*)?\s*(.*)?)?/;
14 var parseRegExpFollowing = /(^.*\s?)/gm;
15
16 var matches;
17 if ( (matches = parseRegExpFirstLine.exec(source)) ) {
18 type = matches[2];
19 title = matches[3];
20 }
21
22 parseRegExpFollowing.exec(content); // ignore line 1
23 while ( (matches = parseRegExpFollowing.exec(source)) ) {
24 text += matches[1];
25 }
26
27 if (text.length === 0)
28 return null;
29
30 return {
31 title : title,
32 content: unindent(text),
33 type : type || 'json'
34 };
35}
36
37/**
38 * Exports
39 */
40module.exports = {
41 parse : parse,
42 path : 'local.examples',
43 method: 'push'
44};