forked from esdoc/esdoc
/
IndexDocBuilder.js
61 lines (55 loc) · 1.6 KB
/
IndexDocBuilder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import fs from 'fs';
import path from 'path';
import cheerio from 'cheerio';
import IceCap from 'ice-cap';
import DocBuilder from './DocBuilder.js';
import {markdown} from './util.js';
/**
* Index output builder class.
*/
export default class IndexDocBuilder extends DocBuilder {
/**
* create instance.
* @param {Taffy} data - doc object database.
* @param {ESDocConfig} config - use config to build output.
* @param {CoverageObject} coverage - use coverage to build output.
*/
constructor(data, config, coverage) {
super(data, config);
this._coverage = coverage;
}
/**
* execute building output.
* @param {function(html: string, filePath: string)} callback - is called with output.
*/
exec(callback) {
let ice = this._buildLayoutDoc();
let title = this._getTitle();
ice.load('content', this._buildIndexDoc());
ice.text('title', title, IceCap.MODE_WRITE);
callback(ice.html, 'index.html');
}
/**
* build index output.
* @returns {string} html of index output.
* @private
*/
_buildIndexDoc() {
if (!this._config.index) return 'Please create README.md';
let indexContent;
try {
indexContent = fs.readFileSync(this._config.index, {encode: 'utf8'}).toString();
} catch (e) {
return 'Please create README.md';
}
let html = this._readTemplate('index.html');
let ice = new IceCap(html);
let ext = path.extname(this._config.index);
if (['.md', '.markdown'].includes(ext)) {
ice.load('index', markdown(indexContent));
} else {
ice.load('index', indexContent);
}
return ice.html;
}
}