npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gulp-nav

v0.3.0

Published

gulp plugin to help build navigation elements

Downloads

178

Readme

gulp-nav

NPM Build Status Coverage Status Dependency Status devDependency Status

A gulp plugin to help build navigation or breadcrumb elements implicitly from our file structure. The goal is to be useful with e.g. Bootstrap .nav classes and your favorite templating system. (If you need templates, try Jade.) gulp-nav can handle Vinyl file objects with stream contents and with buffer contents.

Imagine we have some source files in a directory hierarchy:

  .
  ├── greek
  │   ├── alpha.jade
  │   ├── beta.jade
  │   ├── delta.jade
  │   └── gamma.jade
  ├── index.jade
  ├── latin
  │   ├── b.jade
  │   ├── c.jade
  │   ├── index.jade
  │   └── letter-a.jade
  ├── one.jade
  ├── three.jade
  └── two.jade

We know how to .pipe these through appropriate plugins and transforms, leaving us with a bunch of output pages in a similar directory hierarchy. What about links between those pages, however? We don't want to hardcode that stuff! While each file is piped through, an object stored as a property of the Vinyl file object, which stored object knows where other files are located and what they are called, could be really useful to template plugins. With that information, a template could be written to build navbars, breadcrumbs, or whatever we want on our generated page.

What would this look like? If we had this in our gulpfile...

var gulp = require('gulp'),
    data = require('gulp-data'),
    matter = require('jade-var-matter'),
    nav = require('gulp-nav'),
    jade = require('gulp-jade');

gulp.task('default', function() {
    return gulp.src('src/**/*.jade')
        // gulp-nav will use title or order properties if they are included
        // in file.data, but that is completely optional
        .pipe(data(function(file) {
            return matter(String(file.contents));
        }))
        .pipe(nav())
        .pipe(jade())
        .pipe(gulp.dest('dist'));
});

...and our template file had something like this...

  nav
    ul.nav.nav-tabs
      for sibling in nav.siblings
        li
          a(href=sibling.href)= sibling.title

...that would be enough to generate easy, maintainable navbars! This slightly more elaborate template file generates the nav for the demo site.

The nav object referenced above has the following properties:

| property | description | | :------: | ---------------------------------------------------------------- | | title | Identifier for this resource. | | href | Relative link to this resource. null for a directory without an index. | | active | Is this resource the current one? true or false. | | parent | nav object representing this resource's parent. null for the root resource. | | children | Array of nav objects representing this resource's children. Empty when this resource isn't a directory. | | siblings | Array of nav objects representing this resource's parent's children. Includes this resource. | | root | nav object representing the ancestor of all streamed resources. | | hasActiveDescendant | Is a child (or child of child, etc.) of this resource active? |

If you want children and siblings to be in a particular order, just have an order property defined on each file object. order should be a number, small for the "first" resource and large for the "last" resource in the directory.

There are a bunch of options we can pass into the plugin (in an object), which are currently undocumented because they have sensible defaults and they might change. If you want to know all about the options then read the source.

One current default is to expose the nav data at both the nav and data.nav (the latter for use with the new "data API") properties of the file object, although either or both of these may be overridden via the targets option.

Thanks!

gulp-nav is by Jess Austin and is distributed under the terms of the MIT license. Any and all potential contributions of issues and pull requests are welcome!

Special thanks to Arlando Battle for windows compatibility!