UNPKG

1.44 kBJavaScriptView Raw
1const async = require('async');
2const ejs = require('ejs');
3const path = require('path');
4const fs = require('fs');
5
6module.exports = (navigation, section, config, callback) => {
7
8 // Task flow control
9 async.parallel([
10
11 // Compile top level page
12 async.apply(compilePage, section),
13
14 // Compile children if required
15 (cb) => async.map(section.children, compilePage, cb)
16
17 ], done);
18
19 // On complete handler
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 // Determine first page in Sections object
31 if(page.key === navigation[0].key) {
32 page.filename = 'index.html';
33 page.path = 'index.html';
34 }
35
36 // Post process html css classes
37 page.contentCompiled = page.contentCompiled.map((content) => {
38 return ejs.render(content, { styles: config.meta.cssMap }, { cache: false });
39 });
40
41 // Compile main page
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}