-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmdown.js
80 lines (66 loc) · 1.73 KB
/
mdown.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
73
74
75
76
77
78
79
80
var fs = require('fs');
var path = require('path');
var md = require('markdown-it');
var hjs = require('highlight.js');
var got = require('got');
var util = require('../../lib/util');
var tpl = fs.readFileSync(path.join(__dirname, '../../template.html'), 'utf8');
var md = require('markdown-it')({
html: true,
linkify: true,
typographer: true,
highlight: function (str, lang) {
if (!lang) return '';
if (!hjs.getLanguage(lang)) return '';
try {
return hljs.highlight(lang, str).value;
} catch (__) {}
}
});
plugin.functionSync('MdownPreview', preview);
plugin.functionSync('MdownReload', renderAndRefresh);
function render(content, done) {
return tpl.replace('$body', md.render(content));
}
function renderAndRefresh(nvim, args, done) {
debug('render and refresh');
util.createServer(function(err) {
if (err) return done(err);
markdownFromBuffer(nvim, function(err) {
if (err) return done(err);
done();
});
});
}
function preview(nvim, args, done) {
util.createServer(function(err) {
if (err) return done(err);
markdownFromBuffer(nvim, function(err) {
if (err) return done(err);
util.openBrowser(function(err) {
debug('opened, all done', err);
if (err) return done(err);
done();
});
});
});
}
function markdownFromBuffer(nvim, done) {
util.getBufferContent(nvim, function(err, content) {
if (err) return done(err);
var html = render(content);
util.setHtml(html);
refresh(function(err) {
if (err) return done(err);
done();
});
});
}
function refresh(done) {
debug('Refresh file');
got('http://localhost:35729/changed?files=markdown')
.catch(done)
.then(function() {
done();
});
}