Created
July 16, 2018 05:37
-
-
Save rene-d/8571000293dbed18e5f69024d1ac8a31 to your computer and use it in GitHub Desktop.
Markdown to HTML converter with markdown-it and highlight.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 | |
// rené 07/2018 | |
// markdown-it: https://markdown-it.github.io | |
// highlight.js: https://highlightjs.org/ | |
// Bootstrap: https://getbootstrap.com/ | |
'use strict'; | |
// hack to require() modules in current directory | |
process.env.NODE_PATH = __dirname; | |
require('module').Module._initPaths(); | |
var path = require('path'); | |
var http = require('http'); | |
var fs = require('fs'); | |
var download = function(url, dest, cb) { | |
var file = fs.createWriteStream(dest); | |
var request = http.get(url, function(response) { | |
response.pipe(file); | |
file.on('finish', function() { | |
file.close(cb); | |
}); | |
}); | |
} | |
var conv_all = function(args) | |
{ | |
var hljs = require('highlight.min'); | |
// Actual default values | |
var md = require('markdown-it.min')({ | |
html: true, | |
highlight: function (str, lang) { | |
if (lang && hljs.getLanguage(lang)) { | |
try { | |
return hljs.highlight(lang, str).value; | |
} catch (__) { | |
console.log("error with language", lang); | |
} | |
} | |
return ''; // use external default escaping | |
} | |
}); | |
var conv = function(filename) | |
{ | |
var name = path.basename(filename, path.extname(filename)); | |
console.log("converting " + filename) | |
fs.readFile(filename, 'utf8', function(err,data) { | |
if (err) { | |
return console.log(err); | |
} | |
let html = md.render(data); | |
let head = `<html> | |
<head> | |
<meta charset="UTF-8"> <meta name="description" content="@@NAME@@"> | |
<title>@@NAME@@</title> | |
<link rel="stylesheet" type="text/css" href="bootstrap.min.css"> | |
<link rel="stylesheet" type="text/css" href="default.min.css"> | |
</head> | |
<body> | |
<div class='container'> | |
`; | |
let tail = '</div></body></html>'; | |
head = head.replace(/@@NAME@@/g, name); | |
//html = html.replace('<table>', '<table class="table table-dark table-striped">'); | |
fs.writeFile(filename + ".html", head + html + tail, (err) => { | |
if (err) { | |
return console.log(err); | |
} else { | |
return console.log("file created:", filename + ".html"); | |
} | |
}); | |
}); | |
} | |
args.forEach(filename => { conv(filename) }); | |
} | |
var assets = function() | |
{ | |
console.log('downloading assets'); | |
['http://cdnjs.cloudflare.com/ajax/libs/markdown-it/8.4.1/markdown-it.min.js', | |
'http://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js', | |
'http://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css', | |
'http://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css' | |
].map(function(url) { | |
console.log("download", url,"to", path.basename(url)); | |
download(url, path.basename(url)); | |
}); | |
} | |
var args = process.argv.slice(2); | |
console.log(args); | |
if (args[0] == '--assets') | |
{ | |
args.shift(); | |
assets(); | |
} | |
else // all is async, required modules won't be downloaded yet... | |
{ | |
conv_all(args); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment