-
Notifications
You must be signed in to change notification settings - Fork 6
/
get-medium.js
222 lines (180 loc) · 5.39 KB
/
get-medium.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const request = require('request');
const turndown = require('turndown');
const fs = require('fs');
const cheerio = require('cheerio');
const slugify = require('slugify');
const uniqid = require('uniqid');
const readline = require('readline');
function getPost(url) {
return new Promise((resolve, reject) => {
request(url, (error, response, body) => {
if (error) {
reject(error);
}
resolve(body);
});
});
}
function saveToFile(markdown, filename) {
return new Promise((resolve, reject) => {
fs.writeFile(filename, markdown, (error) => {
if (error) {
reject(error);
}
console.log(`Saved to ${filename}`);
resolve();
});
});
}
function getImageFilename(url) {
// the image url is like this:
// https://miro.medium.com/v2/resize:fit:1400/format:webp/1*06UMOvj5RLFcF7poD83Qhg.png
// get the part after `*` and before `.`
const ext = '.webp';
const regex = /\d\*([\w-]+)\.\w+$/;
const match = url.match(regex);
if (match) {
return match[1] + ext;
}
const id = uniqid();
let filename = slugify(figcaption, { lower: true });
filename += '-' + id;
filename += ext;
return filename;
}
async function downloadImage(url, path) {
return new Promise((resolve, reject) => {
request(url)
.pipe(fs.createWriteStream(path))
.on('close', resolve)
.on('error', reject);
});
}
async function parsePost(post, options = {}) {
const { path } = options;
const articleContent = post.match(/<article[^>]*>[\s\S]*<\/article>/)[0];
const $ = cheerio.load(articleContent);
const title = $('h1').eq(0).text();
// remove unwanted elements
$('h1').eq(0).remove();
$('h2').eq(0).remove();
$('.speechify-ignore').eq(0).remove();
$('figure').eq(0).remove();
const article = $.html();
// convert to markdown
const turndownService = new turndown({
headingStyle: 'atx',
bulletListMarker: '-',
codeBlockStyle: 'fenced',
emDelimiter: '*',
});
turndown.prototype.escape = (text) => {
return text;
};
turndownService.addRule('pre', {
filter: 'pre',
replacement: (content, node) => {
console.log(node.textContent);
let lang = 'tsx';
let attrs = '';
// if content contains 'npm', 'npx', 'git'
if (/npm|npx|git/.test(content)) {
lang = 'bash';
}
// if first line contains something like '// dirname/index.tsx'
// get the content after '//', it's filename
// and remove the first line
const firstLine = content.split('\n')[0];
const match = firstLine.match(/\/\/ (.+)/);
if (match) {
attrs = ` filename="${match[1].trim()}"`;
content = content.replace(firstLine + '\n', '');
content = content.trim();
}
return '```' + lang + attrs + '\n' + content + '\n```';
},
});
turndownService.addRule('figure', {
filter: 'figure',
replacement: (content, node) => {
const srcset = node.querySelector('source').getAttribute('srcset');
const figcaption = node.querySelector('figcaption').textContent;
// get the last image in srcset
const imageUrl = srcset.split(',');
const lastImage = imageUrl[imageUrl.length - 1].trim().split(' ')[0];
// download image
const filename = getImageFilename(lastImage);
const imagepath = `${path}/${filename}`;
console.log('Downloading image:');
console.log('From:', lastImage);
console.log('To:', imagepath);
console.log('Caption:', figcaption);
console.log('-'.repeat(20));
downloadImage(lastImage, imagepath);
// imagepath without `public/`
const imagesrc = imagepath.replace(/^public\//, '');
return `\n<Image src="/${imagesrc}" alt="${figcaption}" />`;
},
});
turndownService.addRule('increaseHeading', {
filter: ['h1', 'h2', 'h3'],
replacement: (content, node) => {
const tagName = node.tagName.toLowerCase();
const level = parseInt(tagName[1]) + 1;
return `${'#'.repeat(level)} ${content}`;
},
});
let markdown = turndownService.turndown(article);
// add title at the top
markdown = `# ${title}\n\n` + markdown;
// check if there are any images from markdown
// if there are, then add the import statement
if (markdown.includes('<Image')) {
markdown = `import { Image } from '@/components/image';\n\n` + markdown;
}
return markdown;
}
function parseUrl(url) {
const urlObj = new URL(url);
let slug = urlObj.pathname.split('/').pop();
// remove the last '-' and the words after it
slug = slug.replace(/-\w+$/, '');
return {
slug,
url,
};
}
async function convert(url) {
// get url from command line
if (!url) {
console.error('Please provide a URL');
process.exit(1);
}
const parsedUrl = parseUrl(url);
const wd = `public/${parsedUrl.slug}`;
// make directory if not exists
if (!fs.existsSync(wd)) {
fs.mkdirSync(wd, { recursive: true });
}
// get the post
console.log(`Getting post from ${url}`);
const post = await getPost(url);
// parse the post
const markdown = await parsePost(post, {
path: wd,
});
// save to file
const filename = parsedUrl.slug;
await saveToFile(markdown, `pages/raw/${filename}.mdx`);
}
function main() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the URL: ', async (url) => {
await convert(url);
rl.close();
});
}
main();