1 | 'use strict';
|
2 |
|
3 | const Promise = require('bluebird');
|
4 | const { Pattern } = require('hexo-util');
|
5 |
|
6 | function Processor() {
|
7 | this.store = [];
|
8 | }
|
9 |
|
10 | Processor.prototype.list = function() {
|
11 | return this.store;
|
12 | };
|
13 |
|
14 | Processor.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 |
|
36 | module.exports = Processor;
|