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

extract-md-data

v2.0.6

Published

A utility for getting JSON data about all markdown files in a given dir. Useful for building static site generators. Extracts data such as frontmatter, markdown content, relativePath, slug, id.

Downloads

6

Readme

npm version

extract-md-data

A util for getting data and metadata for all markdown files in a given dir. Useful for building static site generators.

Usage

Given this example file structure:

.
└── src
    ├── docs
    │   ├── Day-One.md
    │   ├── Day-Two.md
    │   └── not-markdown.txt
    └── index.js

Day-One.md

---
title: day one
tags:
  - foo
  - bar
---

# On the first day

God created markdown

Day-Two.md

---
title: day two
tags:
  - baz
  - pistachio
---

# On the second day

God created another markdown

To get JSON data for all the markdown files in docs folder:

// index.js

const extract = require('extract-md-data');
const path = require('path');

/* Define project rootDir and srcDir where the markdown files live */
const rootDir = path.resolve(__dirname);
const srcDir = path.resolve(rootDir, 'docs');

const jsons = extract(rootDir, srcDir);

console.log(jsons);
[
  {
    "fm": {
      "title": "day one",
      "tags": ["foo", "bar"]
    },
    "content": "\n# On the first day\n\nGod created markdown\n",
    "relativePath": "docs/Day-One.md",
    "relativeDir": "docs",
    "filename": "Day-One.md",
    "slug": "day-one",
    "id": "be308ccd-71e6-5339-97d9-947670d116ba"
  },
  {
    "fm": {
      "title": "day two",
      "tags": ["baz", "pistachio"]
    },
    "content": "\n# On the second day\n\nGod created another markdown\n",
    "relativePath": "docs/Day-Two.md",
    "relativeDir": "docs",
    "filename": "Day-Two.md",
    "slug": "day-two",
    "id": "c115a245-d83c-5a8e-9d61-2bb40c05afdb"
  }
]

This data can be used with templating libraries like pug or handlebars to build out static websites based off the markdown files.

You can make your own routing logic - perhaps you want to leverage relativeDir? Up to you!

Features

  • gets data for all files in srcDir with .md and .markdown extensions, recursively
  • ignores non-markdown files
  • extracts any custom yaml frontmatter into an object fm
  • extracts relativeDir for use with file-based routing
  • generates a unique id for each file by hashing the relativePath
  • generates a url-safe slug for each file based on the filename

Config

You can optionally pass a config object as a third argument.

const jsons = extract(rootDir, srcDir, opts);

options.slugify

Options for slugify. See available settings. These are the default settings:

{
  slugify: {
    replacement: '-',
    remove: /[*+~.()'"!:@$%^()]/g,
    lower: true,
    strict: true,
    trim: true
  }
}

options.omitContent

Boolean indicating whether or not to return markdown body content. You can set this to true if you don't need the markdown bodies from the files and want to save memory. Defaults to false

{
  // omits markdown body in returned objects
  omitContent: true;
}

Development

Install

npm install

Testing

npm run test