Convert a directory of Markdown files to HTML.
Uses the commonmark Markdown renderer and the gray-matter frontmatter parser.
Install with npm:
npm install --save md-directory
or, if using Yarn:
yarn add md-directory
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.
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.
var path = require('path')
var md = require('md-directory')
var contents = md.parseDirSync(path.join(__dirname, 'posts'))
browserify index.js -t md-directory/transform -o bundle.js
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.
Read the contents of a directory and convert to Markdown asynchronously
Parameters
dir
String β The directory to readopts
Objectopts.md
Function alternate function to parse markdown, default: commonmarkopts.frontmatter
Function alternate function to parse frontmatter, default: gray-matteropts.encoding
String β encoding of files, default:utf8
opts.filter
String β glob pattern for filtering files, default:**\/*.md
opts.ignore
String β glob pattern for ignoring filesopts.ignore
Array β array of glob patterns for ignoring filesopts.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)
})
Read the contents of a directory and convert to Markdown synchronously
Parameters
dir
String β The directory to readopts
Objectopts.md
Function alternate function to parse markdown, default: commonmarkopts.frontmatter
Function alternate function to parse frontmatter, default: gray-matteropts.encoding
String β encoding of files, default:utf8
opts.filter
String β glob pattern for filtering files, default:**\/*.md
opts.ignore
String β glob pattern for ignoring filesopts.ignore
Array β array of glob patterns for ignoring filesopts.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')
Read the contents of a file and convert to Markdown asynchronously
Parameters
filename
String β The filename to readopts
Objectopts.md
Function alternate function to parse markdown, default: commonmarkopts.frontmatter
Function alternate function to parse frontmatter, default: gray-matteropts.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)
})
Read the contents of a file and convert to Markdown synchronously
Parameters
filename
String β The filename to readopts
Objectopts.md
Function alternate function to parse markdown, default: commonmarkopts.frontmatter
Function alternate function to parse frontmatter, default: gray-matteropts.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')