Created
February 28, 2012 10:12
-
-
Save dansimau/1931720 to your computer and use it in GitHub Desktop.
Markdown-to-HTML file converter using showdown JS library (Node.js)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/** | |
* Takes specified Markdown files outputs them as HTML. | |
* | |
* Requires showdown: | |
* | |
* npm install showdown | |
* | |
* [email protected] | |
* 2012-02-28 | |
*/ | |
var fs = require('fs'); | |
var path = require('path'); | |
var showdown = require('showdown').Showdown; | |
if (process.argv.length < 3) { | |
console.error('Converts specified Markdown file to HTML'); | |
console.error('Usage: ' + require('path').basename(__filename) + ' <file1> [file2 [...]]'); | |
process.exit(99); | |
} | |
// Input file list | |
var inputs = process.argv; | |
// Remove first two arguments as they are node and bin paths | |
inputs.shift(); | |
inputs.shift(); | |
// Process options | |
var options = { | |
outputext: ".html" | |
}; | |
for (f in process.argv) { | |
if (process.argv[f] == "--stdout") { | |
options.stdout = true; | |
// Remove this arg from file list | |
delete inputs[f]; | |
} | |
} | |
for (f in inputs) { | |
var md; | |
var text; | |
var inputf; | |
var outputf; | |
var converter; | |
inputf = inputs[f]; | |
converter = new showdown.converter(); | |
// Read file | |
text = fs.readFileSync(inputf, 'utf-8'); | |
// Convert to Markdown | |
md = converter.makeHtml(text); | |
if (!options.stdout) { | |
// Write file | |
var outputf = path.basename(inputf, path.extname(inputf)) + options.outputext; | |
fs.writeFileSync(outputf, md, 'utf-8'); | |
console.log(outputf); | |
} else { | |
// Output Markdown to console | |
console.log(md); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment