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

@pauliescanlon/gatsby-mdx-routes

v0.0.11

Published

Routes and Recursive Navigation menus for Mdx files

Downloads

27

Readme

npm (scoped)

npm

NPM

gatsby-mdx-routes

gatsby-mdx-routes is a plugin that exposes links to .mdx files sourced from src/pages.

This plugin aims to separate the data from the ui, which means the styling of your navigation is up to you.

👁️ Preview

🚀 Getting started

Install

npm install @pauliescanlon/gatsby-mdx-routes

Setup

To source .mdx files from src/pages you'll need gatsby-source-filesystem and gatsby-plugin-mdx installed.

Your gatsby-config should look something like this...

module.exports = {
  plugins: [
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `pages`,
        path: `${__dirname}/src/pages/`,
      },
    },
    {
      resolve: `gatsby-plugin-mdx`,
      options: {
        defaultLayouts: {
          default: `${__dirname}/src/layouts/layout.js`,
        },
      },
    },
  ],
}

If that's all setup you'll now need to add @pauliescanlon/gatsby-mdx-routes as a plugin of gatsby-plugin-mdx 😅

module.exports = {
  {
    resolve: `gatsby-plugin-mdx`,
    options: {
      defaultLayouts: {
        default: `${__dirname}/src/layouts/layout.js`,
      },
      plugins: [`@pauliescanlon/gatsby-mdx-routes`],
    },
  },
}

Using the defaultLayouts from gatsby-plugin-mdx allows you to create one file that will be repeated across pages.

This is where we'll add MdxRoutes.

MdxRoutes

MdxRoutes returns two arrays, routes which is a flat array and menus which is created recursively and contains a menu array.

You will probably use one or the other, not both.

routes

The routes array returns two object keys, one is the actual route to the file in question (slug), the other is the navigationLabel from frontmatter

| Key | Description | | --------------- | ------------------------------------------ | | slug | Route to .mdx file | | navigationLabel | navigationLabel extracted from frontmatter |

src/pages/a-page.mdx

frontmatter

---
navigationLabel: Page Title
---

src/layouts/layout.js

import React, { Fragment } from "react"
import { Link } from "gatsby"

import { MdxRoutes } from "@pauliescanlon/gatsby-mdx-routes"

export default ({ children }) => (
  <Fragment>
    <nav>
      <MdxRoutes>
        {(routes, _) => (
          <ul>
            {routes.map((route, index) => (
              <li key={index}>
                <Link to={route.slug}>{route.navigationLabel}</Link>
              </li>
            ))}
          </ul>
        )}
      </MdxRoutes>
    </nav>
    <main>{children}</main>
  </Fragment>
)

menus

The menus array also returns the two object keys mentioned above, it also returns the following

| Key | Description | | --------------- | ------------------------------------------- | | slug | Route to .mdx file | | navigationLabel | navigationLabel extracted from frontmatter | | id | key to use in recursive function | | parent | string to determine parent | | menu | array of routes grouped by parent | | paths | internal array to use in recursive function |

The menus array is constructed by looking at the file paths on disk and determining what depth a file lives. This is calculated by the amount of forward slashes in the slug

To create the menus array MdxRoutes will mirror the directory structure in your project

your project

|-- src
    |-- pages
        |-- other-pages
           |-- some-other-page.mdx
        |-- sub-pages
            |-- sub-page-items
               |-- sub-page-items-again
                  |-- sub-page-item-again-1.mdx
               |-- sub-page-item-1.mdx
            |-- sub-page-1.mdx
        |-- about.mdx
        |-- contact.mdx
        |-- index.mdx

To use the menus array you'll also need a recursive Tree function to create your navigation list.

Watch out for the conditional slug, we need this to determine if the object key is a parent or an actual route to a file.

src/layouts/layout.js

import React, { Fragment } from "react"
import { Link } from "gatsby"

import { MdxRoutes } from "@pauliescanlon/gatsby-mdx-routes"

const Tree = ({ menus }) => {
  const createTree = menus => {
    return (
      <ul>
        {menus.map(route => (
          <li key={route.navigationLabel}>
            {route.slug ? (
              <Link to={route.slug}>
                {route.navigationLabel}
                {route.menu && createTree(route.menu)}
              </Link>
            ) : (
              <span>
                {route.navigationLabel}
                {route.menu && createTree(route.menu)}
              </span>
            )}
          </li>
        ))}
      </ul>
    )
  }

  return createTree(menus, null)
}

export default ({ children }) => (
  <Fragment>
    <nav>
      <MdxRoutes>{(_, menus) => <Tree menus={menus} />}</MdxRoutes>
    </nav>
    <main>{children}</main>
  </Fragment>
)

props

| Prop | Type | Required | Description | | --------------- | ------------- | -------- | ----------------------------- | | navigationOrder | Array[string] | no | A reference array to order by |

navigationOrder

By passing in an array of strings MdxRoutes can use this to sort the returned routes or menus array, otherwise everything is just returned alphabetically in an ascending order based on the slug, with the index ("/") being first in the list.

<MdxRoutes navigationOrder={["Contact", "About", "Home", "Sub Page"]}>
  {(routes, menus) => (
    <ul>
      {routes.map((route, index) => (
        <li key={index}>
          <Link to={route.slug}>{route.navigationLabel}</Link>
        </li>
      ))}
    </ul>
  )}
</MdxRoutes>

If you're using gatsby-mdx-routes in your project i'd love to hear from you @pauliescanlon

ko-fi