我需要根据文件中的属性使用gulp从乙烯流中删除JSON或markdown文件

时间:2016-07-16 09:25:08

标签: gulp

我在以下结构中有markdown文件:

---
title: some title
tags: [misc]
date: '2015-09-09'
---

some text

我有像以下一样的gulp任务

gulp.task('hits', function(){
    var index = 0;
  gulp.src('source/content/agents/*.md')
   //.pipe(changed())
    .pipe(markdown({
        pedantic: true,
        smartypants: true
    }))
    .pipe( buffer() )
    .pipe(jeditor(function(json) {
     return json; // must return JSON object. 
  }))
    .pipe(gulp.dest('server/content/hits'));
});

如果输入文件在tags数组中没有值,我想从流中删除它。这可以在json步骤之前或之后完成,我猜之前是最好的,但要么是好的。

我很确定这一定是一件简单的事情,因为你知道正确的插件以及如何使用该插件。

1 个答案:

答案 0 :(得分:0)

您在markdown文件开头的所有内容根本不是JSON。一个合适的JSON文档如下所示:

{
  "title": "some title",
  "tags": ["misc"],
  "date": "2015-09-09"
}

您在这里处理的是YAML。更具体地说,这种类型的YAML数据在降价文档的开头充当元数据,称为Front Matter,并由Jekyll静态站点生成器推广。

有一个名为gulp-front-matter的gulp插件,专门用于处理这种元数据。它解析前面的内容并将结果值附加到乙烯基文件中。

这与gulp-filter插件结合使用,可以根据前端问题中的标记过滤掉流中的文件:

var gulp = require('gulp');
var frontMatter = require('gulp-front-matter');
var markdown = require('gulp-markdown');
var filter = require('gulp-filter');

gulp.task('hits', function () {
  return gulp.src('source/content/agents/*.md')
    .pipe(frontMatter())
    .pipe(markdown())
    .pipe(filter(function(file) {
      return file.frontMatter.tags &&
             file.frontMatter.tags.indexOf('hit') >= 0;
    }))
    .pipe(gulp.dest('server/content/hits'))
});