The API is as follows:
Note to-markdown v2+ runs on Node 4+. For a version compatible with Node 0.10 - 0.12, please use to-markdown v1.x.
Download the compiled script located at
Or with Bower:
$ bower install to-markdown
$ npm install to-markdown
Then you can use it like below:
var toMarkdown = require'to-markdown';toMarkdown'<h1>Hello world!</h1>';
(Note it is no longer necessary to call
.toMarkdown on the required module as of v1.)
to-markdown can be extended by passing in an array of converters to the options object:
toMarkdownstringOfHTML converters: converter1 converter2 … ;
A converter object consists of a filter, and a replacement. This example from the source replaces
filter: 'code'return '`' + content + '`';
The filter property determines whether or not an element should be replaced. DOM nodes can be selected simply by filtering by tag name, with strings or with arrays of strings:
filter: 'p'will select
filter: ['em', 'i']will select
Alternatively, the filter can be a function that returns a boolean depending on whether a given node should be replaced. The function is passed a DOM node as its only argument. For example, the following will match any
span element with an
italic font style:
return nodenodeName === 'SPAN' && /italic/itestnodestylefontStyle;
The replacement function determines how an element should be converted. It should return the markdown string for a given node. The function is passed the node’s content, as well as the node itself (used in more complex conversions). It is called in the context of
toMarkdown, and therefore has access to the methods detailed below.
The following converter replaces heading elements (
filter: 'h1' 'h2' 'h3' 'h4' 'h5' 'h6'var hLevel = nodetagNamecharAt1;var hPrefix = '';forvar i = 0; i < hLevel; i++hPrefix += '#';return '\n' + hPrefix + ' ' + innerHTML + '\n\n';
to-markdown has beta support for GitHub flavored markdown (GFM). Set the
gfm option to true:
toMarkdown'<del>Hello world!</del>' gfm: true ;
The following methods can be called on the
false depending on whether the element is block level.
false depending on whether the element is void.
Returns the content of the node along with the element itself.
First make sure you have node.js/npm installed, then:
$ npm install --dev$ bower install --dev
Automatically browserify the module when source files change by running:
$ npm start
To run the tests in the browser, open
To run in node.js:
$ npm test
to-markdown is copyright © 2011+ Dom Christie and released under the MIT license.