-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (47 loc) · 1.56 KB
/
index.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
const fs = require('fs');
const path = require('path');
const loaderUtils = require('loader-utils');
const ejs = require('ejs');
const util = require('atool-doc-util');
function calculateHtmlPath(cwd, source) {
const selfPath = path.relative(cwd, source);
return path.join(path.dirname(selfPath), `${path.basename(selfPath, path.extname(selfPath))}.html`);
}
module.exports = function(content) {
this.cacheable && this.cacheable();
const options = this.options;
const resourcePath = this.resourcePath;
const resource = new util.Resource(options.cwd, options.demoSource, resourcePath);
const query = loaderUtils.parseQuery(this.query);
const tpl = query.template;
this.addDependency(tpl);
const scripts = [
path.relative(resourcePath, path.join(resource.demoPath, 'common.js')),
`${resource.name}.js`,
];
const link = {};
link['Index'] = path.relative('../', path.relative(resource.path, './index'));
Object.keys(options.entry).forEach(function(key) {
link[path.relative(options.demoSource, key)] = path.relative('../', path.relative(resource.path, key));
});
const html = ejs.render(fs.readFileSync(tpl, 'utf-8'), {
file: {
link: link,
title: resource.relativeToCwd + resource.ext,
resource: resource,
script: scripts,
desc: util.marked([{
type: 'h2',
children: 'code',
}, {
type: 'code',
props: {
lang: 'js',
},
children: content,
}]),
},
});
this.emitFile(calculateHtmlPath(options.cwd, resourcePath), html);
return content;
}