我有如下结构的markdown文件:
---
title: some title
tags: [misc]
date: '2015-09-09'
---
some text我有一个像下面这样的吞咽任务
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'));
});如果输入文件在标记数组中没有命中的值,我希望将其从流中删除。这既可以在json步骤之前完成,也可以在json步骤之后完成,我想之前是最好的,但这两个都可以。
我确信这一定是一件很简单的事情,因为你知道正确的插件以及如何使用它。
发布于 2016-07-16 20:36:42
标记文件开头的内容根本不是JSON。一个合适的JSON文档应该是这样的:
{
"title": "some title",
"tags": ["misc"],
"date": "2015-09-09"
}您在这里处理的是YAML。更具体地说,这种类型的YAML数据在标记文档的开头充当元数据,称为Front Matter,并由Jekyll静态站点生成器推广。
有一个名为gulp-front-matter的吞咽插件,专门为处理这种元数据而设计。它解析前面的内容,并将结果值附加到乙烯基文件。
与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'))
});https://stackoverflow.com/questions/38409658
复制相似问题