1.0.2 • Published 7 years ago

nodecrumbs v1.0.2

Weekly downloads
1
License
MIT
Repository
-
Last release
7 years ago

Nodecrumbs

Easy and compact breadcrumbs library for node

To install:

npm install nodecrumbs --save

The way it works is it parses the request URI, ie. /home/about/company into an an iterable array of breadcrumb objects so that you can render something like: "Home | About | Company" in your markup with all the necessary links without you having to worry about the formatting, names or links.

Examples

In your router create a breadcrumbs array like the following:

var nodecrumbs = require('nodecrumbs').Nodecrumbs;

app.get('/', (req, res) => {
    var crumbs = new Nodecrumbs(req);
    //parse the crumbs into an array and then pass it into your view

This will return you an instance of the Nodecrumbs object of which you can:

var crumbsArray = crumbs.parse();

This will give you an iterable object which you pass into and loop in your templates, though a premade render function using the initial nodecrumbs object (not the parsed array) is available to the lazier ones (example used uses EJS template library):

<%- nodecrumbs.render(); %>

Will render the default breadcrumbs markup

To manually render the breadcrumbs, you would iterate the crumbs array and use the getUri() and getName() methods respectively:

<ul>
<% for(var crumb in crumbs) {%>
    <li><a href="<%= crumbsArray[crumb].getUri(); %>"><%= crumbsArray[crumb].getName(); %></a></li>
<% } %>
</ul>

Documentaion

The Nodecrumbs constructor takes the following arguments:

Nodecrumbs(req, format = 'standard', home='home')

Request

The request object captured by your request listener (e.g express.js)

Format:

'upper', 'lower', ''standard' - (string) how the breadcrumbs should be formatted

Home:

defaults to ''home' - (string) alias for the first breadcrumb linking to home

Testing

Test cases can be checked with npm test

Feel free to contribute!