hammerdown
v2.0.1
Published
Streaming HTML To Markdown Writer
Downloads
476
Maintainers
Readme
hammerdown
Streaming html to markdown writer
Get Started
Checkout The Live Demo here!
Simple Example
var hammerdown = require("hammerdown");
//HTML
var htmlToConvert = = "<h1>A Markdown Header</h1><p>Some text <b>bold text</b></p>"
//Write markdown
hammerdown().parse(htmlToConvert,function(error, markdown){
if(error)
throw error;
console.log(markdown);
});
//Outputs
// # A Markdown Header
//
// Some text **bold text**
Simple Example with streams
var hammerdown = require("hammerdown");
//HTML
var htmlToConvert = = "<h1>A Markdown Header</h1><p>Some text <b>bold text</b></p>"
//Write markdown
hammerdown().parse(htmlToConvert).pipe(process.stdout);
//Outputs
// # A Markdown Header
//
// Some text **bold text**
Installation
npm Install
npm install hammerdown
Browser Install
bower install hammerdown
NOTE: If using in the browser use the following:
//HTML
var htmlToConvert = = "<h1>A Markdown Header</h1><p>Some text <b>bold text</b></p>"
hammerdown().parse(htmlToConvert,function(error, markdown){
if(error)
throw error;
console.log(markdown);
});
Github Flavored Markdown Example
var hammerdown = require("hammerdown");
//Write markdown
var htmlString = "<pre>" +
"<code class='language-javascript'>"+
"var awesomeoFunction = function(){"+
"return true;"+
"}"+
"</code>"+
"</pre>";
hammerdown({type:"gfm"}).parse(htmlString).pipe(process.stdout);
//Outputs
// ```javascript
// function myFunction(params){return true;};
// ```
Purpose
Existing html to markdown writers didn't:
- Support windows easily (html.md)
- Lack of support for github flavored markdown (to-markdown)
- Didn't support streams
Why convert html to markdown?
To have an easy way to programatically generate a stream of markdown text from html. This library includes converters for standard markdown and it also includes Github-Flavored-Markdown definitions.
Markdown is a mechanism to create documents. See for more details. Hammerdown allows developers to leverage the simplicity of Markdown from html text.
Gulp Plugin!
For adding to your build process checkout gulp-hammerdown
Options
Hammerdown currently accepts one option: type
. If type
is "gfm"
then hammerdown will produce markdown using github flavored markdown(gfm).
Examples
Below is a select group of examples. More examples can be found by looking at the integration tests.
Github Flavored Markdown Examples
Simple Example using file stream
var fs = require('fs');
var hammerdown = require('hammerdown');
var htmlFileStream = fs.createReadStream("anyHtmlFile.html");
//Output markdown
htmlFileStream.pipe(hammerdown()).pipe(process.stdout);
//Outputs
// # Any header
//
// Any **Content**
<!-- anyHtmlFile.html-->
<!doctype html>
<html>
<head>
<title>Any Title</title>
</head>
<body>
<h1>Any header</h1>
<p>Any <b>Content</b></p>
</body>
</html>
Tables
var fs = require('fs')
var hammerdown = require('hammerdown');
var htmlFileStream = fs.createReadStream("anyTable.html");
//Output markdown
htmlFileStream.pipe(hammerdown({type:"gfm"})).pipe(process.stdout);
//Outputs
// |Header1|Header1|
// |---|---|
// |row1-col1|row1-col2|
// |row2-col1|row2-col2|
<!-- anyTable.html -->
<!doctype html>
<html>
<head>
<title>Table Html</title>
</head>
<body>
<table>
<tr>
<th>
Header1
</th>
<th>
Header1
</th>
</tr>
<tr>
<td>
row1-col1
</td>
<td>
row1-col2
</td>
</tr>
<tr>
<td>
row2-col1
</td>
<td>
row2-col2
</td>
</tr>
</table>
</body>
</html>
Fenced Code Block
var fs = require('fs')
var hammerdown = require('hammerdown');
var htmlFileStream = fs.createReadStream("anyTable.html");
//Output markdown
htmlFileStream.pipe(hammerdown({type:"gfm"})).pipe(process.stdout);
//Outputs
// ```javascript
// var awesomeoFunction = function(){
// return true;
// };
// ```
<!-- anyFencedCodeBlock.html -->
<!doctype html>
<html>
<head>
<title>Fenced Code Block</title>
</head>
<body>
<pre>
<code class='language-javascript'>
var awesomeoFunction = function(){
return true;
};
</code>
</pre>
</body>
</html>
Credits and other frameworks
Thanks to karlcows Markdown Test Runner I was able to provide a solid set of tests
to_markdown another good html to markdown tool by domchristie