2017-03-24 1 views
0

gulp-dataを使用して親フォルダの名前を取得するにはどうすればよいですか?現在、私は使用しています次:Gulp-dataを使用したスプライスパス

function fm2json() { 
    return gulp.src('src/pages/**/*.html') 
    .pipe(require('gulp-gray-matter')()) 
    .pipe($.data(function(file){ 
     file.data.relative = file.relative, 
     file.data.basename = file.basename, 
    })) 
    .pipe($.pluck('data', 'new.json')) 
    .pipe($.data(function(file){ 
     file.contents = new Buffer(JSON.stringify(file.data)) 
    })) 
    .pipe(require('gulp-json-format')(2)) 
    .pipe(gulp.dest('src/data')); 
} 

をnew.jsonするには、次のように出力します。私の一気ファイルからの私の前の問題

--- 
title: 'some title' 
---- 

[ { "title":"some title" "relative":"lesson01\\file.html" "basename":"file.html" }, { "title":"some title 2" "relative":"lesson02\\file2.html" "basename":"file2.html" } ] 

relative"relative":"lesson01""relative":"lesson02"になるように、ファイルの親フォルダを取得する方法がわかりません。

答えて

0

これは最も効果的な方法ではありません。それが誰かを助けるならば、これが私の結末です。

function fm2json() { 
    return gulp.src('src/pages/**/*.html') 
    .pipe(require('gulp-gray-matter')()) 
    .pipe($.data(function(file){ 

     // What I ended up with 
     var relpath = file.relative; 
     var path = relpath.replace(/\\/g,"/"); //flip slashes 
     var split = path.split('/'); //split the path into an array 
     var parent = split[split.length - 2]; // find the array position 

     file.data.parent = parent, 
     file.data.file = file.basename, 
     file.data.path = path, 

    })) 
    .pipe($.pluck('data', 'new.json')) 
    .pipe($.data(function(file){ 
     file.contents = new Buffer(JSON.stringify(file.data)) 
    })) 
    .pipe(require('gulp-json-format')(2)) 
    .pipe(gulp.dest('src/data')); 
} 
関連する問題