cr-front-matter.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict';
  2. var frontMatter = require('front-matter');
  3. var objectPath = require('object-path');
  4. var PluginError = require('gulp-util').PluginError;
  5. var Transform = require('readable-stream/transform');
  6. var tryit = require('tryit');
  7. var VinylBufferStream = require('vinyl-bufferstream');
  8. module.exports = function gulpFrontMatter(options) {
  9. options = options || {};
  10. var property;
  11. if (options.property !== undefined) {
  12. if (typeof options.property !== 'string') {
  13. throw new PluginError('gulp-front-matter', new TypeError(
  14. options.property +
  15. ' is not a string. "property" option must be a string.'
  16. ));
  17. }
  18. property = options.property;
  19. } else {
  20. property = 'frontMatter';
  21. }
  22. return new Transform({
  23. objectMode: true,
  24. transform: function gulpFrontMatterTransform(file, enc, cb) {
  25. var run = new VinylBufferStream(function(buf, done) {
  26. var content;
  27. tryit(function() {
  28. content = frontMatter(String(buf), {filename: file.path});
  29. // start
  30. let styleReg = /<!-- @style -->([^\0]*)<!-- @style end -->/;
  31. let jsReg = /<!-- @javascript -->([^\0]*)<!-- @javascript end -->/;
  32. let titReg = /@\(\(([^()]*)\)\)/;
  33. let body = content.body;
  34. let styleBox = body.match(styleReg);
  35. let style = '';
  36. if(styleBox && styleBox.length === 2){
  37. style = styleBox[1];
  38. }
  39. let javascriptBox = body.match(jsReg);
  40. let javascript = '';
  41. if(javascriptBox && javascriptBox.length === 2){
  42. javascript = javascriptBox[1];
  43. }
  44. let titBox = body.match(titReg);
  45. let tit = '';
  46. if(titBox && titBox.length === 2){
  47. tit = titBox[1];
  48. }
  49. content.attributes.style = style.trim();
  50. content.attributes.javascript = javascript.trim();
  51. content.attributes.title = tit.trim();
  52. content.attributes.body = body.replace(styleReg,'').replace(jsReg,'').replace(titReg,'').trim();
  53. // end
  54. // console.log(JSON.stringify(content,null,4))
  55. objectPath.set(file, property, content.attributes);
  56. }, function(err) {
  57. if (err) {
  58. err.message = err.stack.replace(/\n +at[\s\S]*/, '');
  59. var errorOption = {};
  60. if (file.path !== undefined) {
  61. errorOption.fileName = file.path;
  62. }
  63. done(new PluginError('gulp-front-matter', err, errorOption));
  64. return;
  65. }
  66. if (options.remove !== false) {
  67. done(null, new Buffer(content.body));
  68. return;
  69. }
  70. done(null, buf);
  71. });
  72. });
  73. var self = this;
  74. run(file, function(err, contents) {
  75. if (err) {
  76. self.emit('error', err);
  77. } else {
  78. file.contents = contents;
  79. self.push(file);
  80. }
  81. cb();
  82. });
  83. }
  84. });
  85. };