processor.js 642 B

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