/
index.js
367 lines (322 loc) · 9.93 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
'use strict';
/**
* Module dependencies
*/
const assert = require('assert');
const deepBind = require('deep-bind');
const merge = require('mixin-deep');
const parse = require('parse-filepath');
const typeOf = require('kind-of');
const use = require('use');
/**
* Local dependencies
*/
const convert = require('./lib/convert');
const slugify = require('./lib/slugify');
const utils = require('./lib/utils');
/**
* Create an instance of `Permalinks` with the given `options`
*
* ```js
* const Permalinks = require('permalinks').Permalinks;
* const permalinks = new Permalinks();
* console.log(permalinks.format(':stem/index.html'), {path: 'src/about.hbs'});
* //=> 'about/index.html'
* ```
* @name Permalinks
* @param {Options|String} `options`
* @api public
*/
class Permalinks {
constructor(options) {
this.format = this.format.bind(this);
this.parse = this.parse.bind(this);
this.options = Object.assign({}, options);
this.helpers = this.options.helpers || {};
this.presets = this.options.presets || {};
this.data = this.options.data || {};
this.fns = [];
use(this, { prop: '_fns' });
if (!this.helpers.helperMissing) {
this.helper('helperMissing', helperMissing);
}
if (!this.helpers.slugify) {
this.helper('slugify', slugify);
}
}
/**
* Uses [parse-filepath][] to parse the `file.path` on the given file
* object. This method is called by the [format](#format) method, but
* you can use it directly and pass the results as `locals` (the last
* argument) to the `.format` method if you need to override or modify
* any path segments.
*
* ```js
* console.log(permalinks.parse({path: 'foo/bar/baz.md'}));
* // { root: '',
* // dir: 'foo/bar',
* // base: 'baz.md',
* // ext: '.md',
* // name: 'baz',
* // extname: '.md',
* // basename: 'baz.md',
* // dirname: 'foo/bar',
* // stem: 'baz',
* // path: 'foo/bar/baz.md',
* // absolute: [Getter/Setter],
* // isAbsolute: [Getter/Setter] }
* ```
* @name .parse
* @param {Object} `file`
* @return {Object}
* @api public
*/
parse(file) {
if (typeOf(file) !== 'object') {
throw new TypeError('expected file to be an object');
}
if (!file.path || this.options.file === false) {
return file;
}
const data = parse(file.path);
for (const key of Object.keys(file)) {
if (typeof file[key] === 'string') {
data[key] = file[key];
}
}
return data;
}
/**
* Generate a permalink by replacing `:prop` placeholders in the specified
* `structure` with data from the given `file` and `locals`.
*
* ```js
* const fp = permalinks.format('blog/:stem/index.html', {path: 'src/about.hbs'});
* console.log(fp);
* //=> 'blog/about/index.html'
* ```
* @name .format
* @param {String} `structure` Permalink structure or the name of a registered [preset](#preset).
* @param {Object|String} `file` File object or file path string.
* @param {Object} `locals` Any additional data to use for resolving placeholders.
* @return {String}
* @api public
*/
format(structure, file, locals) {
if (typeof structure !== 'string') {
locals = file;
file = structure;
structure = null;
}
file = this.normalizeFile(file);
const context = this.buildContext(file, locals, this.options);
const pattern = utils.getStructure(file) || this.preset(structure);
assert.equal(typeOf(pattern), 'string', 'expected pattern to be a string');
return this.render(pattern, context);
}
/**
* Define a permalink `preset` with the given `name` and `structure`.
*
* ```js
* permalinks.preset('blog', 'blog/:stem/index.html');
* const url = permalinks.format('blog', {path: 'src/about.hbs'});
* console.log(url);
* //=> 'blog/about/index.html'
* ```
* @name .preset
* @param {String} `name` If only the name is passed,
* @param {String} `structure`
* @return {Object} Returns the `Permalinks` instance for chaining
* @api public
*/
preset(name, structure) {
if (arguments.length === 1) {
return this.presets[name] || name;
}
this.presets[name] = structure;
return this;
}
/**
* Define permalink helper `name` with the given `fn`. Helpers work like any
* other variable on the context, but they can optionally take any number of
* arguments and can be nested to build up the resulting string.
*
* ```js
* permalinks.helper('date', function(file, format) {
* return moment(file.data.date).format(format);
* });
*
* const structure1 = ':date(file, "YYYY/MM/DD")/:stem/index.html';
* const file1 = permalinks.format(structure1, {
* data: {date: '2017-01-01'},
* path: 'src/about.tmpl'
* });
*
* const structure2 = ':name(upper(stem))/index.html';
* const file2 = permalinks.format(structure2, {
* data: {date: '2017-01-01'},
* path: 'src/about.tmpl'
* });
*
* console.log(file1);
* //=> '2017/01/01/about/index.html'
*
* console.log(file2);
* //=> '2017/01/01/about/index.html'
* ```
* @name .helper
* @param {String} `name` Helper name
* @param {Function} `fn`
* @return {Object} Returns the Permalink instance for chaining.
* @api public
*/
helper(name, fn) {
this.helpers[name] = fn;
return this;
}
/**
* Add a function for calculating the context at render time. Any
* number of context functions may be used, and they are called in
* the order in which they are defined.
*
* ```js
* permalinks.context(function(file, context) {
* context.site = { title: 'My Blog' };
* });
*
* permalinks.helper('title', function() {
* return this.file.data.title || this.context.site.title;
* });
* ```
* @name .context
* @param {Function} `fn` Function that takes the `file` being rendered and the `context` as arguments. The permalinks instance is exposed as `this` inside the function.
* @return {Object} Returns the instance for chaining.
* @api public
*/
context(fn) {
this.fns.push(fn);
return this;
}
/**
* Create the context to use when rendering permalinks. In addition to creating
* the data object that is used for resolving `:props`, this method also binds
* a context that is exposed as `this` inside helpers. In particular, the `this`
* object in helpers exposes the Permalinks instance as `this.app`, the file
* being rendered as `this.file`, and the [.format](#format) and [.parse](#parse)
* methods, allowing you to create reusable helper functions that can be published
* and shared without requiring a local instance of Permalinks to be used in
* the library.
*
* ```js
* permalinks.context(file, locals, options);
* ```
* @name .buildContext
* @param {Object} `file`
* @param {Object} `locals`
* @param {Object} `options`
* @return {Object}
*/
buildContext(file, locals) {
const opts = Object.assign({}, this.options);
const fileData = merge({}, file.data, file.data.permalink);
const context = merge({}, this.parse(file), this.data, locals, fileData);
const ctx = merge({}, {app: this}, {options: opts});
const data = {};
for (const key of Object.keys(context)) {
const val = context[key];
if (typeof val === 'function') {
helpers[key] = val;
} else {
data[key] = val;
}
}
// add special properties to context
ctx.app.format = this.format;
ctx.app.parse = this.parse;
ctx.context = data;
ctx.file = file;
for (const name of Object.keys(this.presets)) {
if (!this.helpers[name]) {
this.helper(name, () => this.format(this.presets[name], file));
}
}
// bind the context to helpers
let helpers = deepBind(Object.assign({}, this.helpers), ctx);
// call user-defined context functions
for (const fn of this.fns) {
fn.call(this, ctx.file, ctx.context);
}
// call "file" helper
if (typeof helpers.file === 'function') {
helpers.file(file, data, locals);
delete helpers.file;
}
data.file = file;
return {
options: opts,
helpers: helpers,
data: data
};
}
/**
* Calls [handlebars][] to render the specified template `string` using
* the given `options`.
*
* @name .render
* @param {String} `str`
* @param {Object} `options`
* @return {String} Returns the fully resolved permalink string.
*/
render(structure, config) {
const handlebars = require('handlebars');
const hbs = handlebars.create();
hbs.registerHelper(config.helpers);
const str = convert(structure, config.options);
const fn = hbs.compile(str);
return fn(config.data);
}
/**
* Normalize the given `file` to be a [vinyl][] file object.
*
* ```js
* const file = permalinks.normalizeFile('foo.hbs');
* console.log(file);
* //=> '<File "foo.hbs">'
* ```
* @name .normalizeFile
* @param {String|Object} `file` If `file` is a string, it will be converted to the `file.path` on a file object.
* @param {Object} `file`
* @param {Object} `options`
* @return {Object} Returns the normalize [vinyl][] file.
* @api public
*/
normalizeFile(file, options) {
const opts = Object.assign({}, this.options, options);
if (typeof file === 'string') {
file = { path: file, created: true };
}
if (opts.file === false) {
return file;
}
return utils.normalizeFile(file, opts);
}
}
/**
* Default helper for handling missing ":params"
*/
function helperMissing() {
const args = [].slice.call(arguments);
const options = args.pop();
throw new Error(`cannot resolve permalink variable: ":${options.name}"`);
}
/**
* Expose `Permalinks`
*/
module.exports = function(structure, options) {
if (typeof structure === 'string') {
const permalinks = new Permalinks(options);
return permalinks.format(...arguments);
}
return new Permalinks(...arguments);
};
module.exports.Permalinks = Permalinks;