简体   繁体   English

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

[英]How do I pass gulp frontmatters output to another plugin in Gulp?

I want to pass gulp frontmatters output to another function I'm using in Gulp (gulp template). 我想将gulp frontmatters输出传递给我在Gulp中使用的另一个函数(gulp模板)。 I think I've got the syntax right, but must be doing something wrong because it isn't working. 我认为我的语法正确,但必须做错事,因为它不起作用。

The gulp plugins I'm using are: Gulp frontmatter and Gulp template . 我正在使用的gulp插件是: Gulp frontmatterGulp模板 I'm trying to pass an object called page from frontmatter() to template() like so: 我试图将一个名为page的对象从frontmatter()传递给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

This isn't working. 这不起作用。 What am I doing wrong? 我究竟做错了什么?


For reference: this is the entire code from my gulpfile.js 供参考:这是我的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));
});

This is the template I'm using (test/test.md): 这是我正在使用的模板(test / test.md):

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

And this is the error message I'm getting: 这是我得到的错误信息:

[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)

You never defined page .. So you get: 你从未定义过page ..所以你得到:

[gulp] 'default' errored after 7.49 ms page is not defined 没有定义7.49 ms页面之后的[gulp]'default'错误

The gulp-front-matter plugin adds the front matter to a property on the file object, which gulp passes around via the pipe method. gulp-front-matter插件将前端物质添加到文件对象上的属性中,gulp通过pipe方法传递。 So you need to use another plugin that can consume that property, if you want to do something with the front matter. 所以你需要使用另一个可以使用该属性的插件,如果你想对前面的事情做些什么的话。 Otherwise just add the remove: true option to the front matter options hash. 否则只需将remove: true选项添加到front matter选项哈希中。

From there you should be able to just call template() (with no options), and I'm not sure, but maybe the page property is accessible from your template.. 从那里你应该能够只调用template() (没有选项),我不确定,但也许page属性可以从你的模板访问..

Edit 编辑

The grunt-template plugin does not take any data from the file object, which has our data, when gulp sends it. 当gulp发送时,grunt-template插件不会从文件对象中获取任何数据,这些数据包含我们的数据。 My solution is to use the lodash template directly, and to pass the chunk and frontmatter data that way. 我的解决方案是直接使用lodash模板,并以这种方式传递chunk和frontmatter数据。

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'));
});

Looks like gulp-template is set up to listen for a property on the file object called "file.data". 看起来像gulp-template被设置为侦听名为“file.data”的文件对象上的属性。 This being the case, this would be very easy to accomplish using the gulp-data plugin https://www.npmjs.org/package/gulp-data . 在这种情况下,使用gulp-data插件https://www.npmjs.org/package/gulp-data很容易实现。 There's an example in the README on how to pass frontmatter. README中有一个关于如何传递frontmatter的例子。

So none of the answers to this question have worked for me, unfortunately. 因此,不幸的是,这个问题的答案都没有对我有用。 I got reasonably far using gulp-vartree , which can store a property in a variable and that is kind of what I wanted to do. 我使用gulp-vartree合理地使用了gulp-vartree ,它可以在变量中存储属性,这就是我想要做的事情。

In the end however, I ran into new issues, so I ended up switching to grunt stencil , since it does exactly what I want without any of the hassle. 然而,最后,我遇到了新的问题,所以我最终切换到grunt模板 ,因为它完全符合我的要求,没有任何麻烦。

After searching for solutions to this problem and finding nothing, I built one myself (new gulp-user, so I apologize if this is a horrible solution, but at least it's something): 在寻找这个问题的解决方案并找不到任何东西之后,我自己构建了一个(新的gulp用户,所以如果这是一个可怕的解决方案,我很抱歉,但至少它是这样的):

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')
});

For me the solution was to use gulp-data with minor changes (the method described in the plugin README, was not working): 对我来说,解决方案是使用gulp-data进行微小的更改(插件自述文件中描述的方法不起作用):

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

or with coffeescript: 或与coffeescript:

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

seems to be good what if you display errors with 如果你显示错误似乎很好

error_reporting(E_ALL);

or similar? 或者类似的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM