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

gatsby-plugin-pathdata

v1.1.0

Published

A gatsby plugin that extracts info from the path of a file

Downloads

1,428

Readme

gatsby-plugin-pathdata

A plugin for gatsby which loads data from the path of a file, and adds it into the graphql-database. This way you can avoid duplication of data. For example avoiding to define a date in the name of the markdown file and in the frontmatter.

Inspiration

This plugin was inspired by the situation mentioned in the introduction. I wanted to make a blog with gatsby, but I know it can quickly get messy if I define a date in the name of a file and in the frontmatter. I also wanted to be able to use the name of the file as the path, so that the frontmatter of the file only contained information that was relevant to the article.

Installation

npm install --save gatsby-plugin-pathdata

Configuration

The plugin needs a few options to work. First you need to include gatsby-source-filesystem to source files into graphql. After that you can use this plugin to take data from the path of certain nodes and add to the fields.

This plugin takes two options: matchNodeType and extract.

  • matchNodeType - Takes a string of a node type to match against (e.g. 'MarkdownRemark')
  • extract - An array of extractor-objects

An extractor-object is an object which has the following keys:

  • name - The name of the field which will be in the graphql database
  • selector - Javascript RegEx which selects areas to extract from path
  • replacer - String which will be the replacement of the selector

Important to note is that behind the scenes the string.prototype.replace() function is used. Hence the selector needs to match with the entire filepath, and the replacer should select certain match-groups from the selector. I recommend to use regexr.com to customize and try out your regex. Remember that the entire path should be selected by the regex, and then choose with groups what data you want.

Example

Under is an example which creates a path and a date field on all 'MarkdownRemark' nodes.

In gatsby-config.js:

module.exports = {
  plugins: [
    {
      resolve: 'gatsby-source-filesystem',
      options: {
        name: 'posts',
        path: `${__dirname}/src/pages/posts/`
      }
    },
    'gatsby-transformer-remark',
    {
      resolve: 'gatsby-plugin-pathdata',
      options: {
        matchNodeType: 'MarkdownRemark',
        extract: [
          {
            name: 'path',
            selector: /.+\/(\d+-\d+-\d+-[\w-]+)\.md$/,
            replacer: '/$1/'
          },
          {
            name: 'date',
            selector: /.+\/(\d+-\d+-\d+)-[\w-]+\.md$/,
            replacer: '$1'
          }
        ]
      }
    }
  ]
}

This gatsby-config.js will result in a graphql database where the 'MarkdownRemark' nodes have a fields object that contains a path and date field. These can be queried in the creation of pages from markdown.

Example query

We have a file named 2018-01-20-hello-world.md inside the src/pages/posts/ folder. Then we execute the following query:

{
  allMarkdownRemark {
    edges {
      node {
        fields {
          path
          date
        }
        frontmatter {
          title
        }
        html
      }
    }
  }
}

The result of this query will then be:

{
  "data": {
    "allMarkdownRemark": {
      "edges": [
        {
          "node": {
            "fields": {
              "path": "/2018-01-20-hello-world/",
              "date": "2018-01-20"
            },
            "frontmatter": {
              "title": "Hello world"
            },
            "html": "<h1>Hello world!</h1>\n<p>This is a blogpost!</p>"
          }
        }
      ]
    }
  }
}