gulp-vartree

Create a variable tree from the property values of given files.

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.

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.

Type: Object

An object used as the root of the collected tree.

Type: String Default value: 'metadata'

Indicates which property metadata will be collected from.

Type: String

The base directory where you want the tree to begin.

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).

Type: String Default value: 'childs'

Indicates which property children will be pushed to.

Type: String Default value: 'path'

Indicates which property shall contain the file path.

Type: String Default value: 'name'

Indicates which property shall contain the file name.

Type: String Default value: 'ext'

Indicates which property shall contain the file extension.

Type: String Default value: 'href'

Indicates which property shall contain the file href.

Type: String Default: Value: 'folder'

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

Type: String Default value: undefined

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

Type: Boolean Default value: false

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

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.