-
Notifications
You must be signed in to change notification settings - Fork 10
/
import.js
97 lines (92 loc) · 2.67 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
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");
const mediumToMarkdown = require('medium-to-markdown-enhanced');
function convert(mediumUrl, filePath, fileName)
{
if (!fileName)
{
var parsed = url.parse(mediumUrl);
fileName = path.basename(parsed.pathname);
}
mediumToMarkdown.convertFromUrl(mediumUrl)
.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("mmorpg-kit-", "");
fileName = fileName.substr(0, fileName.length - 13);
fileName = pad(fileIndex, 3) + "-" + fileName;
convert(mediumUrl, filePath, fileName);
return fileName;
}
// Get index document with all links
request({
uri: "https://medium.com/suriyun-production/mmorpg-kit-setup-guide-table-of-content-2a794f21871d",
method: 'GET'
}, function (err, httpResponse, body) {
if (err)
return reject(err);
let $ = cheerio.load(body);
$('.postArticle-content .uiScale').remove();
$('.postArticle-content a').each(function (i, result) {
if ($(result).attr("href").startsWith("https://medium.com/suriyun-production/"))
{
var oldLink = $(result).attr("href");
var newLink = "pages/" + convert2(oldLink, "./docs/pages/", ++fileIndex);
$(result).attr("href", newLink);
}
});
let html = $('.postArticle-content').html() || '';
let markdown = turndownService.turndown(html);
fs.writeFile("./docs/README.md", markdown, function(err) {
if(err) {
return console.log(err);
}
});
});