Путь соединения с Gulp-данными
Как я могу получить имя родительской папки, используя gulp-data? В настоящее время я использую следующее:
По моему мнению
---
title: 'some title'
----
из моего файла глотка:
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"
"relative":"lesson01\\file.html"
"basename":"file.html"
},
{
"title":"some title 2"
"relative":"lesson02\\file2.html"
"basename":"file2.html"
}
]
Я не могу понять, как просто получить родительскую папку файла, чтобы relative
было бы "relative":"lesson01"
а также "relative":"lesson02"
,
1 ответ
Это не самый эффективный способ сделать это. Если это кому-нибудь поможет, это то, чем я закончил.
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'));
}