-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
36 lines (28 loc) · 919 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var fs = require('fs')
var marked = require('marked')
var inlineStyles = require('inline-styles')
var githubMarkdownCss = require('generate-github-markdown-css')
var template = fs.readFileSync(__dirname + '/template.html').toString()
module.exports = function(markdown, callback) {
// Accept a buffer or string
markdown = markdown.toString ? markdown.toString() : markdown
ensureCSS(function(err) {
if (err) return callback(err)
var body = marked(markdown)
var html = template.replace('{{markdown}}', body)
var inlinedHtml = inlineStyles(html, __dirname)
callback(null, inlinedHtml.toString())
})
}
function ensureCSS(callback) {
var path = __dirname + '/github-markdown.css'
try {
fs.readFileSync(path)
return callback()
} catch(err) {}
githubMarkdownCss(function(err, css) {
if (err) return callback(err)
fs.writeFileSync(path, css)
callback()
})
}