-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.js
56 lines (50 loc) · 1.58 KB
/
compile.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
var GitHubApi = require("github");
var cheerio = require('cheerio');
var fs = require('fs');
var marked = require('marked');
var Entities = require('html-entities').XmlEntities;
var entities = new Entities();
var sourceFile = process.argv[2];
try {
fs.accessSync(sourceFile, fs.F_OK);
} catch (e) {
console.log(`File ${sourceFile} is not found.`);
process.exit();
}
var github = new GitHubApi({
// required
version: "3.0.0",
// optional
debug: false,
protocol: "https",
//host: "api.github.com", // should be api.github.com for GitHub
//pathPrefix: "/api/v3", // for some GHEs; none for GitHub
timeout: 5000,
});
github.repos.getContent({
headers: {
"Accept": "application/vnd.github.html"
},
user: "OleksandrNechai",
repo: 'blogposts',
path: sourceFile
}, function(err, res) {
$ = cheerio.load(res);
var map = {};
$('img').each(function() {
map[$(this).attr('data-canonical-src')] = $(this).attr('src');
});
var source = fs.readFile(sourceFile, 'utf-8', function(err, source) {
var code = marked(source);
$ = cheerio.load(code);
$('p').attr('style', 'text-align: justify;');
$('pre code').replaceWith(function() { return $(this).contents(); });
$('img').each(function() {
var imageFromCloud = map[$(this).attr('src')];
$(this).attr('src', imageFromCloud);
$(this).wrap(`<a href="${imageFromCloud}" target="_blank"></a>`);
});
console.log(entities.decode($.html()));
});
//console.log(JSON.stringify(map));
});