Skip to content

Latest commit

 

History

History
185 lines (131 loc) · 8.7 KB

README.md

File metadata and controls

185 lines (131 loc) · 8.7 KB

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