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' },
],
},
]);
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']
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' },
// ],
// },
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' }
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 },
// ],
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 },
// ],