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

grunt-markdown-to-json-with-content

v0.0.3

Published

Extract YAML front-matter from Markdown files to a single JSON file

Downloads

6

Readme

grunt-markdown-to-json-with-content

Extract YAML front-matter from Markdown files to a single JSON file.

The use case for this markdown-to-yaml-to-json task is somewhat narrow. I use it to strip the YAML front-matter off a set of blog posts written in Markdown. The metadata for each file is combined into a single object, then emitted as a JSON file.

Along the way, a few extra fields are created for each article:

  • an ISO 8601 formatted date
  • a preview of the actual body content
  • the basename of the file, used as a key to get back to the metadata

It wraps the markdown-to-json npm module.

I realize a more descriptive name for this task would be markdown-yaml-frontmatter-to-json but that's pretty wordy. And you can't have dashes in tasks, so the shortname is m2j.

Incidentally, I never had Grunt figured out until I wrote this contrib module. It finally made sense. Try pulling down the source for this and running grunt test for yourself, and inspect the test input (test\fixtures) and output (test\expected).

Getting Started

This plugin requires Grunt ~0.4.1

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

% npm install grunt-markdown-to-json --save-dev

The plugin should load automatically, since your Gruntfile.js parses your package.json, which has a reference to the library.

The "m2j" task

Pretend you have a folder structure like this:

.
├── Gruntfile.coffee
├── component.json
├── package.json
└── source
    ├── articles
    │   ├── bellflower.md    <--
    │   ├── fiddler.md       <--
    │   └── lottery.md       <--
    ├── favicon.ico
    ├── index.jade
    ├── style.styl
    ├── styles
    │   ├── h5bp.css
    │   ├── main.css
    │   └── normalize.css
    └── templates
        └── h5bp.jade

Each Markdown file in the articles directory has a bit of YAML metadata, like the title of the article, author, and tags. We want just the metadata from all three to be combined into a single JSON stringified file, called articles.json.

Now grunt release will build a release folder that looks like this:

.
├── Gruntfile.coffee
├── component.json
├── package.json
├── release
│   ├── articles
│   │   ├── bellflower.html
│   │   ├── fiddle.html
│   │   └── lottery.html
│   ├── articles.json            <---
│   ├── favicon.ico
│   ├── index.html
│   └── style.css
└── source

Configuring your m2j task

In your project's Gruntfile, add a section named m2j to the data object passed into grunt.initConfig().

m2j: {
	release: {
		options: {
			minify: true,
			width: 60
		},
		files: {
			'release/articles.json': ['source/articles/*.md']
		},
	}
}

Options

options.minify

Type: Boolean Default value: false

If true, then the JSON.stringify is instructed to strip unnecessary linebreaks, making the resulting .json file smaller.

options.width

Type: Int Default value: 70

No more than width charactes from the Markdown file's body is saved in the preview element. Trailing ellipses are added.

options.files

This is the common source / destination pairing you see in all Grunt tasks. Note that you have just one destination, one or more sources, per pair.

See the docs, specifically the Compact Format and Files Object Format for examples.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

Version 0.4.0 is the initial version, which matches 0.4.1 of the npm module m2j.

Scott Stanfield
[email protected]
April 2014