2014-07-25 1 views
1

나는 이것에 대해 가장 좋은 방법은 무엇인지 모르겠습니다.Gulp Yaml JSON 앞면 문제 추가 파일 이름

나는 파일의 이름을 추가하는 동안 json로 변환 인하 파일에서 yamlfront matter를 가져온 다음 하나의 arrayjson 파일에 결합하고 싶습니다.

예. 파일 bananas.mdapples.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 플러그인을 찾았지만 뭔가를 놓치지 않는 한 그것들은 내가 필요로하는 것, 심지어 결합 된 것조차하지 않는 것 같습니다.

답변

1

업데이트, 나는이 과정을 크게 간소화하는 gulp-pluck 플러그인을 만들었습니다. 그것이 어떻게 작동하는지

은 다음과 같습니다


OK

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, event-stream, stream-reducegulp-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 

을 그리고 자바 스크립트 해당 :

LiveScript로 작성된

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