1 | const async = require('async');
|
2 | const ejs = require('ejs');
|
3 | const path = require('path');
|
4 | const fs = require('fs');
|
5 |
|
6 | module.exports = (navigation, section, config, callback) => {
|
7 |
|
8 |
|
9 | async.parallel([
|
10 |
|
11 |
|
12 | async.apply(compilePage, section),
|
13 |
|
14 |
|
15 | (cb) => async.map(section.children, compilePage, cb)
|
16 |
|
17 | ], done);
|
18 |
|
19 |
|
20 | function done(err, result) {
|
21 | if (err) {
|
22 | callback(err);
|
23 | } else {
|
24 | callback(null, section)
|
25 | }
|
26 | }
|
27 |
|
28 | function compilePage(page, cb) {
|
29 |
|
30 |
|
31 | if(page.key === navigation[0].key) {
|
32 | page.filename = 'index.html';
|
33 | page.path = 'index.html';
|
34 | }
|
35 |
|
36 |
|
37 | page.contentCompiled = page.contentCompiled.map((content) => {
|
38 | return ejs.render(content, { styles: config.meta.cssMap }, { cache: false });
|
39 | });
|
40 |
|
41 |
|
42 | var data = {
|
43 | key: page.key,
|
44 | parentKey: page.parentKey,
|
45 | siteTitle: page.meta.title,
|
46 | title: page.title,
|
47 | contents: page.contentCompiled,
|
48 | nav: navigation,
|
49 | meta: config.meta,
|
50 | styles: config.meta.cssMap
|
51 | };
|
52 |
|
53 | var options = {
|
54 | filename: page.layoutSrc,
|
55 | cache: true
|
56 | };
|
57 |
|
58 | fs.readFile(page.layoutSrc, 'utf-8', (err, str) => {
|
59 | page.compiled = ejs.compile(str, options)(data);
|
60 | cb();
|
61 | });
|
62 |
|
63 | };
|
64 | }
|