1 | 'use strict';
|
2 |
|
3 | var fs = require('fs'),
|
4 | path = require('path'),
|
5 | File = require('vinyl'),
|
6 | vfs = require('vinyl-fs'),
|
7 | _ = require('lodash'),
|
8 | concat = require('concat-stream'),
|
9 | GithubSlugger = require('github-slugger'),
|
10 | createFormatters = require('documentation').util.createFormatters,
|
11 | LinkerStack = require('documentation').util.LinkerStack,
|
12 | hljs = require('highlight.js');
|
13 |
|
14 | module.exports = function(
|
15 | comments /*: Array<Comment> */,
|
16 | config /*: DocumentationConfig */
|
17 | ) {
|
18 | var linkerStack = new LinkerStack(
|
19 | config
|
20 | ).namespaceResolver(comments, function(namespace) {
|
21 | var slugger = new GithubSlugger();
|
22 | return '#' + slugger.slug(namespace);
|
23 | });
|
24 |
|
25 | var formatters = createFormatters(linkerStack.link);
|
26 |
|
27 | hljs.configure(config.hljs || {});
|
28 |
|
29 | var sharedImports = {
|
30 | imports: {
|
31 | slug(str) {
|
32 | var slugger = new GithubSlugger();
|
33 | return slugger.slug(str);
|
34 | },
|
35 | shortSignature(section) {
|
36 | var prefix = '';
|
37 | if (section.kind === 'class') {
|
38 | prefix = 'new ';
|
39 | } else if (section.kind !== 'function') {
|
40 | return section.name;
|
41 | }
|
42 | return prefix + section.name + formatters.parameters(section, true);
|
43 | },
|
44 | signature(section) {
|
45 | var returns = '';
|
46 | var prefix = '';
|
47 | if (section.kind === 'class') {
|
48 | prefix = 'new ';
|
49 | } else if (section.kind !== 'function') {
|
50 | return section.name;
|
51 | }
|
52 | if (section.returns.length) {
|
53 | returns = ': ' + formatters.type(section.returns[0].type);
|
54 | }
|
55 | return prefix + section.name + formatters.parameters(section) + returns;
|
56 | },
|
57 | md(ast, inline) {
|
58 | if (
|
59 | inline &&
|
60 | ast &&
|
61 | ast.children.length &&
|
62 | ast.children[0].type === 'paragraph'
|
63 | ) {
|
64 | ast = {
|
65 | type: 'root',
|
66 | children: ast.children[0].children.concat(ast.children.slice(1))
|
67 | };
|
68 | }
|
69 | return formatters.markdown(ast);
|
70 | },
|
71 | formatType: formatters.type,
|
72 | autolink: formatters.autolink,
|
73 | highlight(example) {
|
74 | if (config.hljs && config.hljs.highlightAuto) {
|
75 | return hljs.highlightAuto(example).value;
|
76 | }
|
77 | return hljs.highlight('js', example).value;
|
78 | }
|
79 | }
|
80 | };
|
81 |
|
82 | sharedImports.imports.renderSectionList = _.template(
|
83 | fs.readFileSync(path.join(__dirname, 'section_list._'), 'utf8'),
|
84 | sharedImports
|
85 | );
|
86 | sharedImports.imports.renderSection = _.template(
|
87 | fs.readFileSync(path.join(__dirname, 'section._'), 'utf8'),
|
88 | sharedImports
|
89 | );
|
90 | sharedImports.imports.renderNote = _.template(
|
91 | fs.readFileSync(path.join(__dirname, 'note._'), 'utf8'),
|
92 | sharedImports
|
93 | );
|
94 |
|
95 | var pageTemplate = _.template(
|
96 | fs.readFileSync(path.join(__dirname, 'index._'), 'utf8'),
|
97 | sharedImports
|
98 | );
|
99 |
|
100 |
|
101 | return new Promise(resolve => {
|
102 | vfs.src([__dirname + '/assets/**'], { base: __dirname }).pipe(
|
103 | concat(function(files) {
|
104 | resolve(
|
105 | files.concat(
|
106 | new File({
|
107 | path: 'index.html',
|
108 | contents: new Buffer(
|
109 | pageTemplate({
|
110 | docs: comments,
|
111 | config: config
|
112 | }),
|
113 | 'utf8'
|
114 | )
|
115 | })
|
116 | )
|
117 | );
|
118 | })
|
119 | );
|
120 | });
|
121 | };
|