node package manager
Stop wasting time. Easily manage code sharing in your team. Create a free org »

gulp-vartree

gulp-vartree

Create a variable tree with the files that are passed in with Gulp.

gulp-vartree is mainly meant to build static websites with Gulp. You can see a real world usage example in my blog repository.

NPM version Build status Dependency Status devDependency Status Coverage Status

Usage

First, install gulp-vartree as a development dependency:

npm install --save-dev gulp-vartree

Then, add it to your gulpfile.js:

var mdvars = require('gulp-mdvars');
var vartree = require('gulp-vartree');
var root = {};
 
gulp.task('vartree', function() {
  gulp.src(['assets/contents/*.md'])
    .pipe(mdvars({
      prop: 'meta' // will put metadata in the file.meta property 
    }))
    .pipe(vartree({
      root: root, // the root in which the vartree will be put 
      prop: 'metas', // metadata property to collect 
      parent: 'parent' // keep a ref to the parent scope in the file.parent property 
    }))
    .pipe(marked()) // Do whatever you want with the files later 
    .pipe(gulp.dest('www/')).on('end', function() {
      console.log(root);
    });
});

The created variable tree looks like this:

{
  "title":"index",
  "childs":[
    {"title":"file1"},
    {"title":"file2"},
    {
      "name": "test",
      "title":"test-index",
      "childs":[
        {"title":"test-file1"},
        {"title":"test-file2"}
      ]
    }
  ]
}

for the following directory structure :

  • index.md
  • file1.md
  • file2.md
  • test/
    • index.md
    • file1.md
    • file2.md

gulp-vartree can be used with gulp-mdvars, or with any Gulp plugin that places data in a file property, such as gulp-frontmatter.

API

vartree(options)

options.root (required)

Type: Object

An object used as the root of the collected tree.

options.prop

Type: String Default value: 'metadata'

Indicates which property metadata will be collected from.

options.base

Type: String

The base directory where you want the tree to begin.

options.parentProp

Type: String

Indicates the name of the property in which you want to keep a reference to the parent scope. This property is not populated per default, because using it will make the tree structure circular (and this interferes with e.g. serialization).

options.childsProp

Type: String Default value: 'childs'

Indicates which property children will be pushed to.

options.pathProp

Type: String Default value: 'path'

Indicates which property shall contain the file path.

options.nameProp

Type: String Default value: 'name'

Indicates which property shall contain the file name.

options.extProp

Type: String Default value: 'ext'

Indicates which property shall contain the file extension.

options.hrefProp

Type: String Default value: 'href'

Indicates which property shall contain the file href.

options.folderProp

Type: String Default: Value: 'folder'

This property will be used to indicate nodes that are directories.

options.sortProp

Type: String Default value: undefined

If sorting is desired, the metadata property to sort the tree by.

options.sortDesc

Type: Boolean Default value: false

If true, the tree will be sorted in descending order instead of ascending order.

options.varEvents

Type: Array of String Default value: ['end']

For stream mode only. Indicates which events must be listened for to be sure that the vars are populated into the files in order to process the tree on up to date metadata.

Stats

NPM NPM