'use strict'; var frontMatter = require('front-matter'); var objectPath = require('object-path'); var PluginError = require('gulp-util').PluginError; var Transform = require('readable-stream/transform'); var tryit = require('tryit'); var VinylBufferStream = require('vinyl-bufferstream'); module.exports = function gulpFrontMatter(options) { options = options || {}; var property; if (options.property !== undefined) { if (typeof options.property !== 'string') { throw new PluginError('gulp-front-matter', new TypeError( options.property + ' is not a string. "property" option must be a string.' )); } property = options.property; } else { property = 'frontMatter'; } return new Transform({ objectMode: true, transform: function gulpFrontMatterTransform(file, enc, cb) { var run = new VinylBufferStream(function(buf, done) { var content; tryit(function() { content = frontMatter(String(buf), {filename: file.path}); // start let styleReg = /([^\0]*)/; let jsReg = /([^\0]*)/; let titReg = /@\(\(([^()]*)\)\)/; let body = content.body; let styleBox = body.match(styleReg); let style = ''; if(styleBox && styleBox.length === 2){ style = styleBox[1]; } let javascriptBox = body.match(jsReg); let javascript = ''; if(javascriptBox && javascriptBox.length === 2){ javascript = javascriptBox[1]; } let titBox = body.match(titReg); let tit = ''; if(titBox && titBox.length === 2){ tit = titBox[1]; } content.attributes.style = style.trim(); content.attributes.javascript = javascript.trim(); content.attributes.title = tit.trim(); content.attributes.body = body.replace(styleReg,'').replace(jsReg,'').replace(titReg,'').trim(); // end // console.log(JSON.stringify(content,null,4)) objectPath.set(file, property, content.attributes); }, function(err) { if (err) { err.message = err.stack.replace(/\n +at[\s\S]*/, ''); var errorOption = {}; if (file.path !== undefined) { errorOption.fileName = file.path; } done(new PluginError('gulp-front-matter', err, errorOption)); return; } if (options.remove !== false) { done(null, new Buffer(content.body)); return; } done(null, buf); }); }); var self = this; run(file, function(err, contents) { if (err) { self.emit('error', err); } else { file.contents = contents; self.push(file); } cb(); }); } }); };