将Yaml前端事务打包到JSON添加文件名
我不知道最好的办法是什么 我想从标记文件中获取将Yaml前端事务打包到JSON添加文件名,json,yaml,markdown,gulp,Json,Yaml,Markdown,Gulp,我不知道最好的办法是什么 我想从标记文件中获取yaml前端,在添加文件名的同时将其转换为json,然后将它们合并到一个数组json文件中 例如,文件banana.md和apples.md --- title: Bananas type: yellow count: - 1 - 2 --- # My Markdown File apples.md: --- title: Apples type: red count: - 3 - 4 --- # My Markdown Fil
yaml
前端
,在添加文件名的同时将其转换为json
,然后将它们合并到一个数组json
文件中
例如,文件banana.md
和apples.md
---
title: Bananas
type: yellow
count:
- 1
- 2
---
# My Markdown File
apples.md
:
---
title: Apples
type: red
count:
- 3
- 4
---
# My Markdown File 2
转换为all.json
:
[{"title":"Bananas","type":"yellow","count":[1,2],"file":"bananas"},
{"title":"Apples","type":"red","count":[3,4],"file":"apples"}]
当然,不会有回报,因为它将是紧凑的
我发现了一些gulp
插件,但它们似乎都不能完全满足我的需要,即使是组合在一起,除非我遗漏了什么。Update,我创建了插件gulp pulk
,大大简化了过程
下面是它的工作原理:
var gulp = require('gulp');
var data = require('gulp-data');
var pluck = require('gulp-pluck');
var frontMatter = require('gulp-front-matter');
gulp.task('front-matter-to-json', function(){
return gulp.src('./posts/*.md')
.pipe(frontMatter({property: 'meta'}))
.pipe(data(function(file){
file.meta.path = file.path;
}))
.pipe(pluck('meta', 'posts-metadata.json'))
.pipe(data(function(file){
file.contents = new Buffer(JSON.stringify(file.meta))
}))
.pipe(gulp.dest('dist'))
})
结束更新
好吧,我花了点时间来解决这个问题Gulp
需要内置的reduce
功能!(也许有一天我会做这件事。)
依赖项包括:gulp
,gulp front matter
,gulp filter
,事件流
,流reduce
,以及gulp rename
用书写:
gulp.task 'concatYaml' ->
devDest = './dev/public/'
gulp.src './src/posts/*.md'
.pipe filter posted
.pipe front-matter {property: 'meta'}
.pipe es.map (file, cb) ->
file.meta.name = path.basename file.path
file.meta.url = toUrlPath file.meta.name
cb null, file
.pipe reduce ((acc, file) ->
| acc =>
acc.meta.push file.meta
acc
| _ =>
acc = file
acc.meta = [file.meta]
acc
), void
.pipe es.map (file, cb) ->
file.contents = new Buffer JSON.stringify file.meta
cb null, file
.pipe rename 'posts.json'
.pipe gulp.dest devDest
和JavaScript等价物:
gulp.task('concatYaml', function(){
var devDest;
devDest = './dev/public/';
return gulp.src('./src/posts/*.md')
.pipe(filter(posted))
.pipe(frontMatter({ property: 'meta' }))
.pipe(es.map(function(file, cb){
file.meta.name = path.basename(file.path);
file.meta.url = toUrlPath(file.meta.name);
return cb(null, file);
}))
.pipe(reduce(function(acc, file){
switch (false) {
case !acc:
acc.meta.push(file.meta);
return acc;
default:
acc = file;
acc.meta = [file.meta];
return acc;
}
}, void 8))
.pipe(es.map(function(file, cb){
file.contents = new Buffer(JSON.stringify(file.meta));
return cb(null, file);
}))
.pipe(rename('posts.json'))
.pipe(gulp.dest(devDest));
});