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 🙏

© 2025 – Pkg Stats / Ryan Hefner

metalsmith-menu-plus

v0.2.0

Published

Automatic hierarchical navigation generator for Metalsmith sites

Readme

metalsmith-menu-plus

⚠️: This plugin is a fully functional proof-of-concept. It allows you to build a hierarchical navigation structure plus page breadcrumbs. However, it is not yet fully tested and may contain bugs. Use with caution.

Automatic hierarchical navigation generator for Metalsmith sites

metalsmith:plugin npm: version license: MIT coverage ESM/CommonJS Known Vulnerabilities

Features

  • Creates a nested navigation structure reflecting your content hierarchy
  • Supports permalink-style URLs (/page/ instead of /page.html)
  • Custom ordering via frontmatter or global configuration
  • Breadcrumb generation for each page
  • Automatic exclusion of draft content (draft: true)
  • Flexible exclusion patterns for omitting files from navigation
  • Automatic title generation from filenames

Installation

npm install metalsmith-menu-plus

Usage

This plugin follows the standard Metalsmith plugin pattern and can be used both with ESM and CommonJS.

IMPORTANT: This plugin must be used before the layout plugin in the Metalsmith build chain, because it expects HTML files.

ESM (preferred)

import metalsmith from 'metalsmith';
import markdown from 'metalsmith-markdown';
import layouts from 'metalsmith-layouts';
import navMenu from 'metalsmith-menu-plus';

metalsmith(__dirname)
  ...
  .use(markdown()) // Convert Markdown to HTML
  .use(navMenu({   // Then generate navigation
    metadataKey: 'siteNav',
    usePermalinks: true
  }))
  .use(layouts())  // Apply layouts
  .build((err) => {
    if (err) throw err;
    console.log('Build complete!');
  });

CommonJS

const metalsmith = require('metalsmith');
const markdown = require('metalsmith-markdown');
const layouts = require('metalsmith-layouts');
const navigationMenu = require('metalsmith-menu-plus');

metalsmith(__dirname)
  ...
  .use(markdown()) // Convert Markdown to HTML
  .use(navMenu({   // Then generate navigation
    metadataKey: 'siteNav',
    usePermalinks: true
  }))
  .use(layouts())  // Apply layouts
  .build((err) => {
    if (err) throw err;
    console.log('Build complete!');
  });

Options

| Option | Type | Default | Description | |--------------------|----------------------|---------------|----------------------------------------------------------------------------------------------------------| | metadataKey | String | 'navigation' | The key to use in the Metalsmith metadata where the navigation structure will be stored | | usePermalinks | Boolean | false | Whether to use permalink-style URLs (e.g., /page/ instead of /page.html) | | navIndex | Object | {} | Custom ordering for navigation items, with paths as keys and numeric indices as values | | sortBy | Function | null | Custom sorting function for navigation items at the same level with the same navIndex | | navExcludePatterns | Array | [] | Patterns (string, RegExp, or function) to exclude files from navigation | | rootPath | String | '/' | The root path to start building the navigation from (e.g., '/blog/' to only show blog navigation) |

Frontmatter Options

Individual pages can customize their navigation properties using frontmatter:

---
title: About Us
layout: page.njk
draft: true                    # Exclude from navigation (draft content)
navigation:
  navLabel: About Our Company  # Custom navigation label
  navIndex: 5                  # Custom order in navigation
  navExclude: true             # Exclude this page from navigation
---

Navigation Structure

The plugin adds a hierarchical navigation structure to the Metalsmith metadata, accessible via the configured metadataKey. The structure looks like:

[
  {
    title: "Home Page",
    path: "/",
    navIndex: 0,
    children: []
  },
  {
    title: "Blog",
    path: "/blog/",
    navIndex: 10,
    children: [
      {
        title: "First Post",
        path: "/blog/first-post/",
        navIndex: null,
        children: []
      }
    ]
  }
]

Breadcrumbs

The plugin also generates breadcrumbs for each file and adds them to the file's metadata. The breadcrumbs are accessible via:

navigation.breadcrumbs

Each breadcrumb is an object with a title and path property.

Active State Path

The plugin adds the current page's path to the file's navigation object, making it easy to detect the active page in templates:

navigation.path

This path can be compared with navigation item paths to highlight the active page in the navigation menu.

Template Usage

Basic Navigation Menu

{% if siteNav %}
  <nav>
    <ul>
      {% for item in siteNav %}
        <li>
          <a href="{{ item.path }}" {% if navigation.path === item.path %}class="active"{% endif %}>
            {{ item.title }}
          </a>
          
          {% if item.children.length > 0 %}
            <ul>
              {% for child in item.children %}
                <li>
                  <a href="{{ child.path }}" {% if navigation.path === child.path %}class="active"{% endif %}>
                    {{ child.title }}
                  </a>
                </li>
              {% endfor %}
            </ul>
          {% endif %}
          
        </li>
      {% endfor %}
    </ul>
  </nav>
{% endif %}

Breadcrumbs

{% if navigation.breadcrumbs and navigation.breadcrumbs.length > 1 %}
  <nav aria-label="Breadcrumb">
    <ol class="breadcrumbs">
      {% for crumb in navigation.breadcrumbs %}
        <li>
          {% if loop.last %}
            <span aria-current="page">{{ crumb.title }}</span>
          {% else %}
            <a href="{{ crumb.path }}">{{ crumb.title }}</a>
          {% endif %}
        </li>
      {% endfor %}
    </ol>
  </nav>
{% endif %}

Advanced Configuration

Section-Specific Navigation

You can create navigation that only shows a specific section:

// Main site navigation
.use(navigationMenu({
  metadataKey: 'siteNav',
  usePermalinks: true
}))

// Blog-specific navigation
.use(navigationMenu({
  metadataKey: 'blogNav',
  usePermalinks: true,
  rootPath: '/blog/'  // Only show blog section
}))

This is useful for creating specialized navigation for different sections of your site.

Custom Ordering

.use(navigationMenu({
  metadataKey: 'siteNav',
  usePermalinks: true,
  navIndex: {
    '/': 0,           // Home page first
    '/blog': 10,      // Blog section second
    '/about': 20      // About page third  
  },
  sortBy: (a, b) => {
    // Sort by title (applies to items with same navIndex)
    return a.title.localeCompare(b.title);
  }
}))

Custom Exclusion Rules

The plugin automatically excludes files with draft: true in their frontmatter. You can also define additional custom exclusion patterns:

.use(navigationMenu({
  metadataKey: 'siteNav',
  navExcludePatterns: [
    // Exclude specific paths
    'private/secret-page.html',
    // Exclude with regex
    /\/temp\//,
    // Exclude with custom function
    (path, file) => file && file.private === true
  ]
}))

Note: Files with draft: true are automatically excluded and don't need to be specified in navExcludePatterns.

Test Coverage

This plugin maintains high test coverage to ensure reliability. Current test coverage is displayed in the badge at the top of this README.

To run tests locally:

npm test

To view coverage details:

npm run coverage

Debug

This plugin uses the debug module for debugging. To enable debug logs, set the DEBUG environment variable:

DEBUG=metalsmith-menu-plus node your-metalsmith-build.js

CLI Usage

To use this plugin with the Metalsmith CLI, add it to your metalsmith.json file:

{
  "plugins": {
    "metalsmith-menu-plus": {
      "metadataKey": "siteNav",
      "usePermalinks": true,
      "navIndex": {
        "/": 0,
        "/blog": 10,
        "/about": 20
      },
      "rootPath": "/"
    }
  }
}

You can also use the plugin multiple times to create different navigation structures:

{
  "plugins": {
    "metalsmith-menu-plus#main": {
      "metadataKey": "siteNav",
      "usePermalinks": true,
      "rootPath": "/"
    },
    "metalsmith-menu-plus#blog": {
      "metadataKey": "blogNav",
      "usePermalinks": true,
      "rootPath": "/blog/"
    }
  }
}

License

MIT