data.js 771 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. const util = require('hexo-util');
  3. const pathFn = require('path');
  4. const Pattern = util.Pattern;
  5. module.exports = ctx => ({
  6. pattern: new Pattern('_data/*path'),
  7. process: function dataProcessor(file) {
  8. const Data = ctx.model('Data');
  9. const path = file.params.path;
  10. const extname = pathFn.extname(path);
  11. const id = path.substring(0, path.length - extname.length);
  12. const doc = Data.findById(id);
  13. if (file.type === 'skip' && doc) {
  14. return;
  15. }
  16. if (file.type === 'delete') {
  17. if (doc) {
  18. return doc.remove();
  19. }
  20. return;
  21. }
  22. return file.render().then(result => {
  23. if (result == null) return;
  24. return Data.save({
  25. _id: id,
  26. data: result
  27. });
  28. });
  29. }
  30. });