UNPKG

643 BJavaScriptView Raw
1'use strict';
2
3const Promise = require('bluebird');
4const { Pattern } = require('hexo-util');
5
6function Processor() {
7 this.store = [];
8}
9
10Processor.prototype.list = function() {
11 return this.store;
12};
13
14Processor.prototype.register = function(pattern, fn) {
15 if (!fn) {
16 if (typeof pattern === 'function') {
17 fn = pattern;
18 pattern = /(.*)/;
19 } else {
20 throw new TypeError('fn must be a function');
21 }
22 }
23
24 if (fn.length > 1) {
25 fn = Promise.promisify(fn);
26 } else {
27 fn = Promise.method(fn);
28 }
29
30 this.store.push({
31 pattern: new Pattern(pattern),
32 process: fn
33 });
34};
35
36module.exports = Processor;