md-directory
md-directory copied to clipboard
📂 Convert a directory of Markdown files to HTML
md-directory
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
dirString – The directory to readoptsObjectopts.mdFunction alternate function to parse markdown, default: commonmarkopts.frontmatterFunction alternate function to parse frontmatter, default: gray-matteropts.encodingString – encoding of files, default:utf8opts.filterString – glob pattern for filtering files, default:**\/*.mdopts.ignoreString – glob pattern for ignoring filesopts.ignoreArray – array of glob patterns for ignoring filesopts.dirnamesBoolean – include or exclude subdirectory names in keys of returned object, default:falseopts.transformFunction – 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
dirString – The directory to readoptsObjectopts.mdFunction alternate function to parse markdown, default: commonmarkopts.frontmatterFunction alternate function to parse frontmatter, default: gray-matteropts.encodingString – encoding of files, default:utf8opts.filterString – glob pattern for filtering files, default:**\/*.mdopts.ignoreString – glob pattern for ignoring filesopts.ignoreArray – array of glob patterns for ignoring filesopts.dirnamesBoolean – include or exclude subdirectory names in keys of returned object, default:falseopts.transformFunction – 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
filenameString – The filename to readoptsObjectopts.mdFunction alternate function to parse markdown, default: commonmarkopts.frontmatterFunction alternate function to parse frontmatter, default: gray-matteropts.encodingString – encoding of files, default:utf8opts.transformFunction – 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
filenameString – The filename to readoptsObjectopts.mdFunction alternate function to parse markdown, default: commonmarkopts.frontmatterFunction alternate function to parse frontmatter, default: gray-matteropts.encodingString – encoding of files, default:utf8opts.transformFunction – 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