Skip to content

Latest commit

 

History

History
95 lines (69 loc) · 2.24 KB

readme.md

File metadata and controls

95 lines (69 loc) · 2.24 KB

Site-Mapper

A simple utility for programmatically generating navigations based on a sitemap tree

'use strict';

import SiteMapper from 'site-mapper';

const sitemap = new SiteMapper([
	{ label: 'Section A', url: '/section-a' },
    {
        label: 'Section B', url: '/section-b',
        children: [
        	{ label: 'Page A', url: '/section-b/page-a' },
        ],
    },
]);

API

getPath(str)

Returns an array of url strings pathing from root to the provided url.

sitemap.getPath('/section-a');
// ['/section-a']

sitemap.getPath('/section-b/page-a');
// ['/section-b', '/section-b/page-a']

getObj(str)

Returns the full object (including children) of provided url from the site tree.

sitemap.getObj('/section-a');
// { label: 'Section A', url: '/section-a' }

sitemap.getObj('/section-b');
// {
//     label: 'Section B', url: '/section-b',
//     children: [
//         { label: 'Page A', url: '/section-b/page-a' },
//     ],
// },

getLink(str, [bool])

Returns link object (without children) of provided url. Can also optionally be passed true to mark the link as being active.

sitemap.getLink('/section-a');
// { label: 'Section A', url: '/section-a' }

sitemap.getObj('/section-b');
// { label: 'Section B', url: '/section-b' }

sitemap.getObj('/section-b/page-a');
// { label: 'Page A', url: '/section-b/page-a' }

getSubnav(str, [str])

Returns an array of first-child links to provided url. Can also optionally be passed the url of current active url.

sitemap.getSubnav('/section-b', '/section-b/page-a');
// [
//     { label: 'Page A', url: '/section-b/page-a', active: true },
//     { label: 'Page B', url: '/section-b/page-b', active: false },
// ],

getBreadcrumbNav(str, [bool])

Returns an path array of link objects to provided url. By default marks last link in array as active, can be overridden with optional second parameter.

sitemap.getSubnav('/section-a');
// [
//   { label: 'Section A', url: '/section-a', active: true },
// ]

sitemap.getSubnav('/section-b/page-a');
// [
//     { label: 'Section B', url: '/section-b', active: false },
//     { label: 'Page A', url: '/section-b/page-a', active: true },
// ],