Created
July 22, 2017 01:59
-
-
Save stockwellb/28c46cb631ffb9db233b399cb1e83b8b to your computer and use it in GitHub Desktop.
convert gist md to html
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
// credit https://kosamari.com/notes/Turn-Your-Gist-To-Html | |
var fs = require('fs'); | |
var https = require('https'); | |
var hljs = require('highlight.js') // https://highlightjs.org/ | |
// Actual default values | |
var md = require('markdown-it')({ | |
highlight: function (str, lang) { | |
if (lang && hljs.getLanguage(lang)) { | |
try { | |
return hljs.highlight(lang, str).value; | |
} catch (__) {} | |
} | |
return ''; // use external default escaping | |
} | |
}); | |
var gistID = process.argv[2] | |
fs.readFile('template.html','utf8', function (err, data) { | |
if (err) throw err; | |
var template = data; | |
var options = { | |
host: 'api.github.com', | |
path: '/gists/'+gistID, | |
headers: {'user-agent': 'nodescript'} | |
}; | |
https.request(options, function(response) { | |
var str = ''; | |
response.on('data', function (chunk) { | |
str += chunk; | |
}); | |
response.on('end', function () { | |
var res = JSON.parse(str); | |
var file = res.files[Object.keys(res.files)[0]]; | |
var html = md.render(file.content); | |
var filename = file.filename.split('.')[0]; | |
var date = new Date(res.created_at) | |
template = template | |
.replace(/{{date}}/g,date.toISOString().split('T')[0]) | |
.replace('{{content}}',html) | |
.replace('{{title}}', filename.split('-').join(' ')) | |
.replace('{{description}}', res.description) | |
fs.writeFile(filename+'.html', template , function (err) { | |
if (err) throw err; | |
console.log('file created: ' +filename); | |
}); | |
}); | |
}).end(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment