如何将gulp frontmatters输出传递给Gulp中的另一个插件?

我想通过gulp frontmatters输出到另一个我在gulp(gulp模板)中使用的函数。 我认为我的语法是正确的,但一定是做错了,因为它不工作。

我正在使用的Gulp插件是: Gulp frontmatter和Gulp模板 。 我试图从frontmatter()传递一个名为page的对象到template()如下所示:

 .pipe(frontMatter({ property: 'page' // the name of the property added to the file object })) .pipe(template(page)) // passing the page object to the template plugin here 

这不起作用。 我究竟做错了什么?


作为参考:这是从我的gulpfile.js的整个代码

 // Gulp modules var gulp = require( 'gulp' ); var markdown = require( 'gulp-markdown' ); var frontMatter = require( 'gulp-front-matter' ); var template = require( 'gulp-template' ); gulp.task('default', function () { return gulp.src( ['./**/*.{md,markdown}'] ) .pipe(frontMatter({ property: 'page' // property added to file object })) .pipe(template(page)) .pipe(markdown()) .pipe(gulp.dest(grOutput)); }); 

这是我使用的模板(test / test.md):

 --- name: MyName --- Text <%= page.name %> Text 

这是我得到的错误消息:

 [gulp] Using gulpfile D:\Dropbox\Github\graphene\gulpfile.js [gulp] Starting 'default'... [gulp] 'default' errored after 7.49 ms page is not defined D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\event-stream\node_modules\map-stream\index.js:103 throw err ^ expected '<document start>', but found <block mapping end> in "undefined", line 12, column 1 at ParserError.YAMLError (D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\front-matter\node_modules\yaml-js\lib\errors.js:72:46) at ParserError.MarkedYAMLError (D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\front-matter\node_modules\yaml-js\lib\errors.js:88:45) at new ParserError (D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\front-matter\node_modules\yaml-js\lib\parser.js:17:48) at Constructor.Parser.Parser.parse_document_start (D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\front-matter\node_modules\yaml-js\lib\parser.js:158:17) at Constructor.Parser.Parser.check_event (D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\front-matter\node_modules\yaml-js\lib\parser.js:63:48) at Constructor.Composer.Composer.get_single_node (D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\front-matter\node_modules\yaml-js\lib\composer.js:55:17) at Constructor.BaseConstructor.BaseConstructor.get_single_data (D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\front-matter\node_modules\yaml-js\lib\constructor.js:78:19) at load (D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\front-matter\node_modules\yaml-js\lib\yaml.js:113:19) at parse (D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\front-matter\index.js:26:27) at extractor (D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\front-matter\index.js:19:34) 

你从来没有定义的page 。所以你会得到:

没有定义7.49毫秒页面后,[错误]'缺省'错误

gulp-front-matter插件将gulp-front-matter添加到文件对象上的一个属性,该属性通过pipe方法传递。 所以你需要使用另一个可以使用这个属性的插件,如果你想在前面做一些事情的话。 否则,只需将remove: true选项添加到前端选项哈希。

从那里你应该能够调用template() (没有选项),我不知道,但也许page属性可以从您的模板访问..

编辑

grunt-template插件不会从文件对象获取任何数据,当我们的数据发送时,gulp会发送它。 我的解决scheme是直接使用lodash模板,并以这种方式传递块和前端数据。

 var gulp = require('gulp'); var markdown = require('gulp-markdown'); var frontMatter = require('gulp-front-matter'); var through = require('through2'); var template = require('lodash').template; gulp.task('default', function () { return gulp.src('./template.md') .pipe(frontMatter({ // optional configuration property: 'page' })) .pipe(through.obj(function (file, enc, callback) { if (file.isBuffer()) { file.contents = new Buffer(template(file.contents, file.page)); } this.push(file); return callback(); })) .pipe(markdown()) .pipe(gulp.dest('dist')); }); 

看起来像gulp-template被设置为侦听名为“file.data”的文件对象的属性。 在这种情况下,使用gulp-data插件https://www.npmjs.org/package/gulp-data很容易实现&#x3002; 自述文件中有一个关于如何通过frontmatter的例子。

所以不幸的是,这个问题的答案没有对我有效。 我使用了gulp-vartree ,它可以将一个属性存储在一个variables中,这也是我想要做的。

但是,最后我遇到了新的问题,所以我最终select了咕噜咕噜的模具 ,因为它确实是我想要的而没有任何麻烦。

在寻找解决这个问题的办法之后,我什么也没find,我自己build了一个(新的用户,所以我很抱歉,如果这是一个可怕的解决scheme,但至less是这样的):

 var fs = require('fs'); var gulp = require('gulp'); var gutil = require('gulp-util'); var tap = require('gulp-tap'); var header = require('gulp-header'); var footer = require('gulp-footer'); var concat = require('gulp-concat'); var fm = require('gulp-front-matter'); var marked = require('gulp-markdown'); // Build a single index.html from a glob of markdown files, specifically for use with impressjs. gulp.task('build:index', function (callback) { // Parse a glob of markdown files. gulp.src(src + '/markup/**/*.md') // Strip the frontmatter - it gets shoved into file.fm['...'] .pipe(fm ({property: 'fm', remove: true})) // Expose file.fm['..'] variables generated by gulp-front-matter using gulp-tap. // Because we don't want to write empty tags if the values aren't defined, we // set default variables that initialize the entire tag attribute. .pipe(tap(function(file, t) { slide_id = file.fm['id']==undefined ? '' : 'id="' + file.fm['id'] + '"' slide_class = file.fm['class']==undefined ? '' : 'class="' + file.fm['class'] + '"' slide_properties = file.fm['properties'] })) // Generate html from the remaining markdown. .pipe(marked()) // Wrap the markup in div tags, generated from the frontmatter. // If the variables are not defined, an error is not generated, they simply are not // written. Perfect behavior for my use case, but probably should be more explicit. .pipe(header('<div <%= slide_id %> <%= slide_class %> <%= slide_properties %>>\n')) .pipe(footer('</div>\n')) // Concatenate all the html into a single file. .pipe(concat('index.html')) // Wrap the generating html in our impressjs header and footer. .pipe(header(fs.readFileSync('src/templates/impress-header.tpl', 'utf8'))) .pipe(footer(fs.readFileSync('src/templates/impress-footer.tpl', 'utf8'))) // Write the index file. .pipe(gulp.dest(dist)) // Fill up our logs. gutil.log('Recompiled index.html') }); 

对我来说,解决方法是使用gulp-data进行小的更改(插件自述文件中介绍的方法不起作用):

 ... .pipe(frontMatter()) .pipe(data(function(file) { return file.frontMatter; })) .pipe(template()) ... 

或与咖啡的脚本:

 ... .pipe frontMatter() .pipe data (file) -> file.frontMatter .pipe template() ... 

似乎是好的,如果你显示错误

error_reporting(E_ALL);

或类似?