-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimport.js
162 lines (150 loc) · 4.76 KB
/
import.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
const request = require('request');
const cheerio = require('cheerio');
const TurndownService = require('turndown');
const turndownService = new TurndownService({ gfm: true, converters: [
{
filter: 'section',
replacement: function(content) {
return content;
}
},
{
filter: 'div',
replacement: function(content) {
return content;
}
},
{
filter: 'figure',
replacement: function(content) {
return content;
}
},
{
filter: 'figcaption',
replacement: function(content) {
return content;
}
}
]
});
const fs = require('fs');
const url = require("url");
const path = require("path");
var download = function(uri, filename, callback){
request.head(uri, function(err, res, body){
if (res)
{
console.log('content-type:', res.headers['content-type']);
console.log('content-length:', res.headers['content-length']);
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
}
});
};
function converyFromWebUrl(url)
{
return new Promise(function(resolve, reject) {
request({
uri: url,
method: 'GET'
}, function (err, httpResponse, body) {
if (err)
return reject(err);
let $ = cheerio.load(body);
$('.e-content .uiScale').remove();
// Remove first header
$('.e-content h3').eq(0).remove();
// Download an images
$('.e-content img').each(function (i, result) {
var oldLink = $(result).attr("src");
console.log("Downloading image: " + oldLink);
var newLink = "images/" + oldLink.substring(oldLink.lastIndexOf("/") + 1, oldLink.length);
newLink = newLink.replace("*", "");
newLink = newLink.replace("<", "");
newLink = newLink.replace(">", "");
newLink = newLink.replace(":", "");
newLink = newLink.replace("|", "");
newLink = newLink.replace("\"", "");
newLink = newLink.replace("?", "");
newLink = newLink + '.png';
newLink = newLink.replace(".png.png", ".png");
newLink = newLink.replace("..png", ".png");
$(result).attr("src", "../" + newLink);
download(oldLink, "./docs/" + newLink, function() {
console.log("Downloaded image: " + oldLink + " to " + newLink);
});
});
let html = $('.e-content').html() || '';
let markdown = turndownService.turndown(html);
resolve(markdown);
});
});
}
function convert(url, filePath, fileName)
{
if (!fileName)
{
var parsed = url.parse(url);
fileName = path.basename(parsed.pathname);
}
converyFromWebUrl(url)
.then(function (markdown) {
fs.writeFile(filePath + fileName + ".md", markdown, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
});
}
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
var fileIndex = 0;
function convert2(mediumUrl, filePath, fileIndex)
{
var parsed = url.parse(mediumUrl);
var fileName = path.basename(parsed.pathname);
fileName = fileName.replace("_Turnbase-RPG-Template---", "");
fileName = fileName.replace("_turnbase-rpg-template---", "");
fileName = fileName.substr(10, fileName.length - 28);
fileName = pad(fileIndex, 3) + "-" + fileName;
convert(mediumUrl, filePath, fileName);
return fileName;
}
// Get index document with all links
request({
uri: "http://localhost/medium-backup/2018-01-22_Turnbase-RPG-Template---Setup-guide-table-of-content-27c0e2832b80.html",
method: 'GET'
}, function (err, httpResponse, body) {
if (err)
return reject(err);
let $ = cheerio.load(body);
let sidebarContent = '- [Index](/)\n';
$('.e-content .uiScale').remove();
// Remove first header
$('.e-content h3').eq(0).remove();
$('.e-content a').each(function (i, result) {
if ($(result).attr("href").startsWith("http://localhost/medium-backup/"))
{
var oldLink = $(result).attr("href");
var newLink = "pages/" + convert2(oldLink, "./docs/pages/", ++fileIndex);
$(result).attr("href", newLink);
sidebarContent += ' - ['+$(result).text()+']('+newLink+')\n';
}
});
let html = $('.e-content').html() || '';
let markdown = turndownService.turndown(html);
fs.writeFile("./docs/README.md", markdown, function(err) {
if(err) {
return console.log(err);
}
});
fs.writeFile("./docs/_sidebar.md", sidebarContent, function(err) {
if(err) {
return console.log(err);
}
});
});