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

md-directory

v1.1.1

Published

Convert a directory of Markdown files to HTML

Downloads

3,160

Readme

md-directory

npm version build status stability

Convert a directory of Markdown files to HTML.

Uses the commonmark Markdown renderer and the gray-matter frontmatter parser.

Install

Install with npm:

npm install --save md-directory

or, if using Yarn:

yarn add md-directory

Usage

Given a directory posts with a file hi.md:

---
title: foo
---
# bar
var md = require('md-directory')
md.parseDirSync('./posts')

returns:

{
  "hi.md": {
    "data": {
      "title": "foo"
    },
    "content": "<h1>bar</h1>\n"
  }
}

Since version 1.0, md-directory no longer supports the extensions option since it was dropped by read-directory.

Inlining results with Browserify

Use transform.js to convert calls to md-directory methods into the contents they return. It is highly recommended that you use the synchronous methods md.parseDirSync and md.parseSync with Browserify.

Source

var path = require('path')
var md = require('md-directory')
var contents = md.parseDirSync(path.join(__dirname, 'posts'))

Browserify

browserify index.js -t md-directory/transform -o bundle.js 

Output

var contents = {"hi":{"data":{"title":"foo"},"content":"<h1>bar</h1>\n"}};

Note: to use this transform, the path to the file directory can not be a variable. If you use the async methods, the callback must be an ES5 function (not an ES6 arrow function) and the results will be inlined with process.nextTick. See brfs for more details on this behavior.

API

parseDir

Read the contents of a directory and convert to Markdown asynchronously

Parameters

  • dir String – The directory to read
  • opts Object
    • opts.md Function alternate function to parse markdown, default: commonmark
    • opts.frontmatter Function alternate function to parse frontmatter, default: gray-matter
    • opts.encoding String – encoding of files, default: utf8
    • opts.filter String – glob pattern for filtering files, default: **\/*.md
    • opts.ignore String – glob pattern for ignoring files
    • opts.ignore Array – array of glob patterns for ignoring files
    • opts.dirnames Boolean – include or exclude subdirectory names in keys of returned object, default: false
    • opts.transform Function – A function you can use to transform the contents of files after they are converted
  • cb

Examples

var md = require('md-directory')
md.parseDir('./posts', function (err, contents) {
  console.log(contents)
})

parseDirSync

Read the contents of a directory and convert to Markdown synchronously

Parameters

  • dir String – The directory to read
  • opts Object
    • opts.md Function alternate function to parse markdown, default: commonmark
    • opts.frontmatter Function alternate function to parse frontmatter, default: gray-matter
    • opts.encoding String – encoding of files, default: utf8
    • opts.filter String – glob pattern for filtering files, default: **\/*.md
    • opts.ignore String – glob pattern for ignoring files
    • opts.ignore Array – array of glob patterns for ignoring files
    • opts.dirnames Boolean – include or exclude subdirectory names in keys of returned object, default: false
    • opts.transform Function – A function you can use to transform the contents of files after they are converted

Examples

var md = require('md-directory')
var contents = md.parseDirSync('./posts')

parse

Read the contents of a file and convert to Markdown asynchronously

Parameters

  • filename String – The filename to read
  • opts Object
    • opts.md Function alternate function to parse markdown, default: commonmark
    • opts.frontmatter Function alternate function to parse frontmatter, default: gray-matter
    • opts.encoding String – encoding of files, default: utf8
    • opts.transform Function – A function you can use to transform the contents of files after they are converted
  • cb

Examples

var md = require('md-directory')
md.parse('./post.md', function (err, contents) {
  console.log(contents)
})

parseSync

Read the contents of a file and convert to Markdown synchronously

Parameters

  • filename String – The filename to read
  • opts Object
    • opts.md Function alternate function to parse markdown, default: commonmark
    • opts.frontmatter Function alternate function to parse frontmatter, default: gray-matter
    • opts.encoding String – encoding of files, default: utf8
    • opts.transform Function – A function you can use to transform the contents of files after they are converted

Examples

var md = require('md-directory')
var contents = md.parseSync('./post.md')

See also

License

MIT