forked from fraserxu/electron-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown.js
72 lines (60 loc) · 2.37 KB
/
markdown.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var marked = require('marked')
var fs = require('fs')
var os = require('os')
var path = require('path')
var uuid = require('uuid')
var highlightjs = require('highlight.js')
/**
* parse the markdown content and write it to system tmp directory
* @param {String} input Path of the markdown file
* @param {Object} options Markdown parser options
* @return {Function} The callback function with HTML path
*/
module.exports = function (input, options, cb) {
if (options instanceof Function) {
cb = options
options = {}
}
marked.setOptions({
renderer: options.renderer || new marked.Renderer(),
gfm: options.gfm || true,
tables: options.tables || true,
breaks: options.breaks || false,
pedantic: options.pedantic || false,
sanitize: options.sanitize || true,
smartLists: options.smartLists || true,
smartypants: options.smartypants || false,
highlight: function (code, lang) {
return highlightjs.highlightAuto(code, [ lang ]).value
}
})
fs.readFile(input, function (err, markdownContent) {
if (err) {
cb(err)
}
var htmlBody = marked(markdownContent.toString())
var githubMarkdownCssPath = 'node_modules/github-markdown-css/github-markdown.css'
var highlightjsDefaultCssPath = 'node_modules/highlight.js/styles/default.css'
var highlightjsGithubCssPath = 'node_modules/highlight.js/styles/github.css'
var htmlHeader = '<meta charset="utf-8">' +
'<link rel="stylesheet" href="' + path.resolve(githubMarkdownCssPath) + '">' +
'<link rel="stylesheet" href="' + path.resolve(highlightjsDefaultCssPath) + '">' +
'<link rel="stylesheet" href="' + path.resolve(highlightjsGithubCssPath) + '">'
// inject custom css if exist
// todo: validate css
if (options.customCss) {
htmlHeader += '<link rel="stylesheet" href="' + path.resolve(options.customCss) + '">'
}
htmlHeader += '<style> .markdown-body { min-width: 200px; max-width: 790px; margin: 0 auto; padding: 30px; } </style>' +
'<body><article class="markdown-body">\n'
var htmlFooter = '\n </article></body>'
var htmlContent = htmlHeader + htmlBody + htmlFooter
var tmpHTMLPath = path.join(os.tmpdir(), path.parse(input).name + '-' + uuid() + '.html')
fs.writeFile(tmpHTMLPath, htmlContent, function (err) {
if (err) {
cb(err)
}
cb(null, tmpHTMLPath)
})
})
}