【问题标题】:Splice path with Gulp-data使用 Gulp-data 的拼接路径
【发布时间】:2017-08-18 00:42:08
【问题描述】:

如何使用 gulp-data 获取父文件夹的名称?目前我正在使用以下内容:

在我的前面问题

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

来自我的 gulp 文件:

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"

【问题讨论】:

    标签: json gulp yaml-front-matter gulp-data


    【解决方案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'));
    }
    

    【讨论】:

      猜你喜欢
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-11
      相关资源
      最近更新 更多