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

gulp-markdown-to-json

v1.1.0

Published

Parse Markdown + YAML, compile Markdown to HTML, wrap it in JSON.

Downloads

1,206

Readme

gulp-markdown-to-json

CircleCI Status Semistandard Style

Parse Markdown + YAML, compile Markdown to HTML, wrap it in JSON.

The neutral format of JSON opens new possibilities for your handcrafted content. Send it onward to other plugins such as gulp-hb. When their powers combine, you get a static site generator! Pipe to request to send it to a search index or import into a CMS. Write a plugin to tap into the stream if you need a client library.

Table of Contents

Install

npm install gulp-markdown-to-json --save-dev

Dependencies

This plugin does not bundle any Markdown parser to keep your options open. BYOM!

Pick one of these or write a new parser for fun!

Install, configure, and pass a rendering method to this plugin with a source string as its first argument. Output goes straight into the JSON file’s body property. If your parser requires instantiation pass a context to call it with by defining context in the config object. If yet more hoop jumping is required, write a wrapper function such as this example for remark:

function render (string) {
  const remark = require('remark');
  const html = require('remark-html');
  return remark().use(html).process(string).toString();
}

Note: YAML frontmatter blocks are stripped and handled before Markdown rendering with front-matter

Usage

/gulpfile.js

const gulp = require('gulp');
const markdownToJSON = require('gulp-markdown-to-json');
const marked = require('marked');

marked.setOptions({
  pedantic: true,
  smartypants: true
});

gulp.task('markdown', () => {
  gulp.src('./content/**/*.md')
    .pipe(markdownToJSON(marked))
    .pipe(gulp.dest('.'))
});

Transformed source files flow onward to the destination of your choice with directory structure preserved. Any valid JSON files matched by your gulp.src glob passthrough.

/blog/posts/bushwick-artisan.md

---
slug: bushwick-artisan
title: Wes Anderson pop-up Bushwick artisan
layout: centered
---

## YOLO
Chia quinoa meh, you probably haven't heard of them sartorial Holowaychuk pickled post-ironic. Plaid ugh vegan, Sixpoint 8-bit sartorial artisan semiotics put a bird on it Mission bicycle rights Club-Mate vinyl.

/blog/posts/bushwick-artisan.json

{
  "slug": "bushwick-artisan",
  "title": "Wes Anderson pop-up Bushwick artisan",
  "layout": "centered",
  "body": "<h2 id=\"yolo\">YOLO</h2>\n<p>Chia quinoa meh, you probably haven't heard of them sartorial Holowaychuk pickled post-ironic. Plaid ugh vegan, Sixpoint 8-bit sartorial artisan semiotics put a bird on it Mission bicycle rights Club-Mate vinyl.</p>",
  "updatedAt": "1970-01-01T00:00:00Z"
}

Consolidated Output

Gather Markdown files before piping with list-stream to combine output into a single JSON file. Directory structure is preserved and represented as nested JSON for iteration with Handlebars.js and friends. This is handy for navigation and other global content. Valid JSON files are included in the object if matched by your gulp.src glob.

The consolidated file is named content.json by default and optionally renamed.

const gulp = require('gulp');
const ListStream = require('list-stream');
const markdownToJSON = require('gulp-markdown-to-json');
const marked = require('marked');

gulp.task('markdown', () => {
  gulp.src('./content/**/*.md')
    .pipe(ListStream.obj())
    .pipe(markdownToJSON(marked, 'blog.json'))
    .pipe(gulp.dest('.'))
});

blog.json

{
  "blog": {
    "blog": {
      "title": "ipsum dipsum",
      "body": "<p>From west to \"east\"!</p>",
      "updatedAt": "1970-01-01T00:00:00Z"
    },
    "posts": {
      "bushwick-artisan": {
        "slug": "bushwick-artisan",
        "title": "Wes Anderson pop-up Bushwick artisan", 
        "layout": "centered",
        "body": "<h2 id=\"yolo\">YOLO</h2>\n<p>Chia quinoa meh, you probably haven't heard of them sartorial Holowaychuk pickled post-ironic. Plaid ugh vegan, Sixpoint 8-bit sartorial artisan semiotics put a bird on it Mission bicycle rights Club-Mate vinyl.</p>",
        "updatedAt": "1970-01-01T00:00:00Z"
      }
    }
  },
  "mission": {
    ...
  }
}

Specify flattenIndex: true in the config object to unwrap home page/index-style content and merge it into the parent object. Name these files index or the same as a parent directory.

{
  "blog": {
    "title": "ipsum dipsum",
    "body": "<p>From west to \"east\"!</p>",
    "updatedAt": "1970-01-01T00:00:00Z",
    "posts": {
      ...
    }
  },
  "mission": {
    ...
  }
}

This avoids redundant-feeling blog.blog scenarios when iterating and selecting from this content.

Title Extraction and Stripping

Define titles as title in the YAML frontmatter. Text of the first <h1> is assigned to title automatically if this is not specified.

Specify stripTitle: true in the config object to remove the first <h1> from the body. Use this if you are displaying the title outside of the body, in a page header for example.

/blog/posts/bushwick-artisan.md

Wes Anderson pop-up Bushwick artisan
====================================

## YOLO
Chia quinoa meh, you probably haven't heard of them sartorial Holowaychuk pickled post-ironic. Plaid ugh vegan, Sixpoint 8-bit sartorial artisan semiotics put a bird on it Mission bicycle rights Club-Mate vinyl.

/blog/posts/bushwick-artisan.json

{
  "title": "Wes Anderson pop-up Bushwick artisan", 
  "body": "<h2 id=\"yolo\">YOLO</h2>\n<p>Chia quinoa meh, you probably haven't heard of them sartorial Holowaychuk pickled post-ironic. Plaid ugh vegan, Sixpoint 8-bit sartorial artisan semiotics put a bird on it Mission bicycle rights Club-Mate vinyl.</p>",
  "updatedAt": "1970-01-01T00:00:00Z"
}

Transforms

To change or add to the JSON data for each file, specify a transform function and return your desired object. This function is passed the default data and the Vinyl file object for the source file.

For example:

gulp.src('./content/**/*.md')
  .pipe(ListStream.obj())
  .pipe(markdownToJSON(marked, 'blog.json', (data, file) => {
    delete data.body;
    data.path = file.path;
    return data;
  }))
  .pipe(gulp.dest('.'))

API

markdownToJSON((renderer: Function, name?: String, transform?: Function) | config: Object) => TransformStream

config

  • renderer Function accepts Markdown source string, returns an escaped HTML string. Required
  • context Object to use when calling renderer
  • name String to rename consolidated output file, if using. Default: content.json
  • flattenIndex Boolean unwrap files named index or after parent dirs in consolidated output. Default: false
  • stripTitle Boolean strips the first <h1> from body, if extracted as title. Default: false
  • transform Function to access and change the JSON data for each file before outputting

Contribute

Pull requests accepted!

License

MIT
Copyright © 2017 Sparkart Group, Inc.