-
-
Notifications
You must be signed in to change notification settings - Fork 278
/
index.js
162 lines (135 loc) · 3.07 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict';
/**
* module dependencies
*/
var path = require('path');
var Core = require('assemble-core');
var plugins = require('./lib/plugins');
var utils = require('./lib/utils');
/**
* Create an `assemble` app. This is the main function exported
* by the assemble module.
*
* ```js
* var assemble = require('assemble');
* var app = assemble();
* ```
* @param {Object} `options` Optionally pass default options to use.
* @api public
*/
function Assemble(options) {
if (!(this instanceof Assemble)) {
return new Assemble(options);
}
Core.call(this, options);
this.is('assemble');
this.initAssemble();
}
/**
* Inherit `Core`
*/
Core.extend(Assemble);
Core.bubble(Assemble);
/**
* Initialize assemble and emit pre and post init events
*/
Assemble.prototype.initAssemble = function() {
Assemble.emit('assemble.preInit', this);
Assemble.initAssemble(this);
Assemble.emit('assemble.postInit', this);
};
/**
* Initialize Assemble defaults, plugins and views
*/
Assemble.initAssemble = function(app) {
Assemble.initDefaults(app);
Assemble.initPlugins(app);
Assemble.initViews(app);
};
/**
* Initialize defaults
*/
Assemble.initDefaults = function(app) {
var exts = app.options.exts || ['md', 'hbs', 'html'];
/**
* Default engine
*/
app.engine(exts, require('engine-handlebars'));
/**
* Middleware for parsing front matter
*/
app.onLoad(utils.extRegex(exts), function(view, next) {
// check options inside the middleware to account for options defined after init
if (view.options.frontMatter === false) {
next();
return;
}
if (app.options.frontMatter === false) {
next();
return;
}
utils.matter.parse(view, function(err) {
if (err) {
next(err);
return;
}
utils.expand.middleware(app)(view, next);
});
});
};
/**
* Load default plugins. Built-in plugins can be disabled
* on the `assemble` options.
*
* ```js
* var app = assemble({
* plugins: {
* loader: false,
* store: false
* }
* });
* ```
*/
Assemble.initPlugins = function(app) {
enable('logger', plugins.logger);
enable('loader', plugins.loader);
enable('config', plugins.config);
enable('argv', plugins.argv);
enable('cli', plugins.cli);
function enable(name, fn) {
if (app.option('plugins') === false) return;
if (app.option('plugins.' + name) !== false) {
app.use(fn());
}
}
};
/**
* Built-in view collections
* | partials
* | layouts
* | pages
*/
Assemble.initViews = function(app) {
if (app.isFalse('collections')) return;
app.create('partials', {
engine: app.options.engine || 'hbs',
viewType: 'partial',
renameKey: function(fp) {
return path.basename(fp, path.extname(fp));
}
});
app.create('layouts', {
engine: app.options.engine || 'hbs',
viewType: 'layout',
renameKey: function(fp) {
return path.basename(fp, path.extname(fp));
}
});
app.create('pages', {
engine: app.options.engine || 'hbs'
});
};
/**
* Expose the `Assemble` constructor
*/
module.exports = Assemble;