forked from rbren/mediumexporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
179 lines (167 loc) · 5.33 KB
/
utils.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var request = require('request');
var MEDIUM_IMG_CDN = "https://cdn-images-1.medium.com/max/";
var utils = {
loadMediumPost: function(mediumURL, cb) {
if(mediumURL.match(/^http/i)) {
mediumURL = mediumURL.replace(/#.+$/, '');
request(mediumURL, {qs: {format: 'json'}}, function(err, res, body) {
if(err) return cb(err);
var json_string = body.substr(body.indexOf('{'));
var json = JSON.parse(json_string);
return cb(null, json);
});
}
else {
json = require(process.cwd() + "/" + mediumURL);
return cb(null, json);
}
},
processSection: function(s) {
var section = "";
if(s.backgroundImage) {
var imgwidth = parseInt(s.backgroundImage.originalWidth,10);
var imgsrc = MEDIUM_IMG_CDN+Math.max(imgwidth*2,2000)+"/"+s.backgroundImage.id;
section = "\n![]("+imgsrc+")";
}
return section;
},
getYouTubeEmbed: function(iframesrc, cb) {
request(iframesrc, function(err, res) {
var tokens = res.body.match(/youtube.com%2Fembed%2F([^%]+)%3F/);
if (tokens && tokens.length > 1) {
var videoId = tokens[1];
return cb(null, `<iframe width="560" height="315" src="https://www.youtube.com/embed/${videoId}" frameborder="0" allowfullscreen></iframe>`);
}
cb(null, `<iframe src="${iframesrc}" frameborder=0></iframe>`);
});
},
getGitHubEmbed: function(iframesrc, cb) {
request(iframesrc, function(err, res) {
if (err) return cb(err);
var tokens = res.body.match(/script src="https:\/\/gist.github.com\/([^"]*)"/);
if (!tokens) return cb(new Error("GitHub embed not found"));
var scriptsrc = `https://gist.github.com/${tokens[1]}`;
request(scriptsrc, function(err, res) {
if (err) return cb(err);
var html = res.body.split('\n').map((line, idx) => {
if (idx === 0) return ''; // skip stylesheet
line = line.replace(/document.write\('(.*)'\)/, '$1');
line = line
.replace(/\\n\s*/g, '\n')
.replace(/\n+/g, '\n')
.replace(/\\(.)/g, '$1')
.replace(/`/g, '`');
return line;
}).join('\n');
return cb(null, html);
});
});
},
processParagraph: function(p, cb) {
var markups_array = utils.createMarkupsArray(p.markups);
if(markups_array.length > 0) {
var previousIndex=0, text=p.text, tokens=[];
for(var j=0;j<markups_array.length;j++) {
if(markups_array[j]) {
token = text.substring(previousIndex, j);
previousIndex = j;
tokens.push(token);
tokens.push(markups_array[j]);
}
}
tokens.push(text.substring(j-1));
p.text = tokens.join('');
}
if (p.type !== 8 && p.type !== 10) {
p.text = p.text
.replace(/>/g, '>')
.replace(/</g, '<');
}
var markup = "";
switch(p.type) {
case 1:
markup = "\n";
break;
case 2:
p.text = "\n# "+p.text.replace(/\n/g,'\n# ');
break;
case 3:
p.text = "\n## "+p.text.replace(/\n/g,'\n## ');
break;
case 4: // image & caption
var imgwidth = parseInt(p.metadata.originalWidth,10);
var imgsrc = MEDIUM_IMG_CDN+Math.max(imgwidth*2,2000)+"/"+p.metadata.id;
var text = "\n!["+p.text+"]("+imgsrc+")";
if (p.text) {
text += "*"+p.text+"*";
}
p.text = text;
break;
case 6:
markup = "> ";
break;
case 7: // quote
p.text = "> # "+p.text.replace(/\n/g,'\n> # ');
break;
case 8:
p.text = "\n```\n" + p.text + "\n```\n";
break;
case 9:
markup = "\n* ";
break;
case 10:
markup = "\n1. ";
break;
case 11:
return utils.getYouTubeEmbed('https://medium.com/media/'+p.iframe.mediaResourceId, function(err, embed) {
cb(null, `\n${embed}`);
});
case 13:
markup = "\n### ";
break;
case 15: // caption for section image
p.text = "*"+p.text+"*";
break;
}
p.text = markup + p.text;
if(p.alignment == 2&& p.type != 6 && p.type != 7) p.text = "<center>" + p.text + "</center>";
return cb(null, p.text);
},
addMarkup: function(markups_array, open, close, start, end) {
if(markups_array[start])
markups_array[start] += open;
else
markups_array[start] = open;
if(markups_array[end])
markups_array[end] += close;
else
markups_array[end] = close;
return markups_array;
},
createMarkupsArray: function(markups) {
if(!markups || markups.length == 0) return [];
var markups_array = [];
for(var j=0;j<markups.length;j++) {
var m = markups[j];
switch(m.type) {
case 1: // bold
utils.addMarkup(markups_array, "**","**",m.start,m.end);
break;
case 2: // italic
utils.addMarkup(markups_array, "*","*",m.start,m.end);
break;
case 3: // anchor tag
utils.addMarkup(markups_array, "[", "]("+m.href+")", m.start, m.end);
break;
case 10: // code
utils.addMarkup(markups_array, '`', '`', m.start, m.end);
break;
default:
console.error("Unknown markup type "+m.type, m);
break;
}
}
return markups_array;
}
}
module.exports = utils;