processor.js 647 B

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