-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.mjs
293 lines (221 loc) · 8.81 KB
/
index.mjs
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import fs from 'node:fs';
import path from 'node:path';
import TurndownService from 'turndown';
import * as cheerio from 'cheerio';
import { downloadImage, convertEscapedAscii, stripHtml } from './utils.mjs';
const apiUrl = 'https://www.your-wordpress-url.com/wp-json/wp/v2/';
console.log('Exporting data from Wordpress...');
const dataDirectory = path.resolve(process.cwd(), 'data');
const categoriesFile = path.resolve(dataDirectory, 'categories.json');
const authorsDirectory = path.resolve(dataDirectory, 'authors');
const authorsFile = path.resolve(authorsDirectory, 'authors.json');
const authorsUrl = `${apiUrl}users`;
const categoriesUrl = `${apiUrl}categories`;
const postsUrl = `${apiUrl}posts`;
const tagsUrl = `${apiUrl}tags`;
const mediaUrl = `${apiUrl}media`;
const imagesNotDownloaded = [];
if (!fs.existsSync(dataDirectory)) {
fs.mkdirSync(dataDirectory);
}
async function fetchAuthors() {
console.log('Exporting authors...');
if (!fs.existsSync(authorsDirectory)) {
await fs.promises.mkdir(authorsDirectory);
}
let newAuthors = [];
if (fs.existsSync(authorsFile)) {
const existingAuthors = await fs.promises.readFile(authorsFile, 'utf8');
newAuthors = JSON.parse(existingAuthors);
}
const totalPagesResponse = await fetch(authorsUrl);
const totalPages = totalPagesResponse.headers.get('x-wp-totalpages');
const importData = async page => {
const response = await fetch(`${authorsUrl}?page=${page}`);
const authors = await response.json();
for (const author of authors) {
console.log('Exporting author:', author.name);
const existingAuthorIndex = newAuthors.findIndex(existingAuthor => existingAuthor.id === author.slug);
if (existingAuthorIndex > -1) {
console.log(`Author "${author.slug}" already exists, skipping...`);
newAuthors[existingAuthorIndex].wordpressId = author.id;
continue;
}
const extention = path.extname(author.avatar_urls[96]).split('&')[0];
const avatarFile = `${author.slug}${extention}`;
const avatarFilePath = path.resolve(authorsDirectory, avatarFile);
const imageDownloaded = await downloadImage(author.avatar_urls[96], avatarFilePath);
if (!imageDownloaded) {
imagesNotDownloaded.push(author.avatar_urls[96]);
}
newAuthors.push({
id: author.slug,
name: author.name,
bio: author.description,
website: author.url,
...imageDownloaded && {
avatar: `/images/authors/${avatarFile}`,
},
wordpressId: author.id,
});
}
};
for (let page = 1; page <= totalPages; page++) {
await importData(page);
}
await fs.promises.writeFile(authorsFile, JSON.stringify(newAuthors, null, 2));
}
async function fetchCategories() {
console.log('Exporting categories...');
let newCategories = [];
if (fs.existsSync(categoriesFile)) {
const existingCategories = await fs.promises.readFile(categoriesFile, 'utf8');
newCategories = JSON.parse(existingCategories);
}
const totalPagesResponse = await fetch(categoriesUrl);
const totalPages = totalPagesResponse.headers.get('x-wp-totalpages');
const importData = async page => {
const response = await fetch(`${categoriesUrl}?page=${page}`);
const categories = await response.json();
for (const category of categories) {
if (category.count === 0) {
continue;
}
console.log('Exporting category:', category.name);
const existingCategoryIndex = newCategories.findIndex(existingCategory => existingCategory.id === category.slug);
if (existingCategoryIndex > -1) {
console.log(`Category "${category.slug}" already exists, skipping...`);
newCategories[existingCategoryIndex].wordpressId = category.id;
continue;
}
newCategories.push({
id: category.slug,
name: category.name,
description: category.description,
wordpressId: category.id,
});
}
};
for (let page = 1; page <= totalPages; page++) {
await importData(page);
}
await fs.promises.writeFile(categoriesFile, JSON.stringify(newCategories, null, 2));
}
async function fetchPosts() {
console.log('Exporting posts...');
const totalPagesResponse = await fetch(postsUrl);
const totalPages = totalPagesResponse.headers.get('x-wp-totalpages');
const authorsFileContent = await fs.promises.readFile(authorsFile, 'utf8');
const authors = JSON.parse(authorsFileContent);
const categoriesFileContent = await fs.promises.readFile(categoriesFile, 'utf8');
const categories = JSON.parse(categoriesFileContent);
const downloadPostImage = async (src, pathToPostFolder) => {
if (!src || !pathToPostFolder) {
return;
}
const fileName = path.basename(src).split('?')[0];
const destinationFile = path.resolve(pathToPostFolder, fileName);
if (fs.existsSync(destinationFile)) {
console.log(`Post image "${destinationFile}" already exists, skipping...`);
return fileName;
}
const imageDownloaded = await downloadImage(src, destinationFile);
if (!imageDownloaded) {
imagesNotDownloaded.push(src);
}
return imageDownloaded ? fileName : undefined;
};
const cleanUpHtml = html => {
const $ = cheerio.load(html);
const figures = $('figure');
for (const figure of figures) {
$(figure).removeAttr('class');
}
const images = $('img');
for (const image of images) {
$(image).removeAttr('class width height data-recalc-dims sizes srcset');
}
const captions = $('figcaption');
for (const caption of captions) {
$(caption).removeAttr('class');
}
$('.wp-polls').html('<em>Polls have been temporarily removed while we migrate to a new platform.</em>');
$('.wp-polls-loading').remove();
return $.html();
};
const downloadAndUpdateImages = async (html, pathToPostFolder) => {
const $ = cheerio.load(html);
const images = $('img');
for (const image of images) {
const src = $(image).attr('src');
const newSrc = await downloadPostImage(src, pathToPostFolder);
$(image).attr('src', newSrc);
}
return $.html();
};
const importData = async page => {
const response = await fetch(`${postsUrl}?page=${page}`);
const posts = await response.json();
for (const post of posts) {
const postTitle = convertEscapedAscii(post.title.rendered);
console.log('Exporting post:', postTitle);
const pathToPostFolder = path.resolve(dataDirectory, 'posts', post.slug);
if (!fs.existsSync(pathToPostFolder)) {
await fs.promises.mkdir(pathToPostFolder, { recursive: true });
}
const postAuthor = authors.find(author => post.author === author.wordpressId);
const postCategories = categories.filter(category => post.categories.includes(category.wordpressId));
const titleImageId = post.featured_media;
const titleImageResponse = await fetch(`${mediaUrl}/${titleImageId}`);
const titleImageJson = await titleImageResponse.json();
const titleImage = await downloadPostImage(titleImageJson.source_url, pathToPostFolder);
const tags = [];
for (const tag of post.tags) {
const tagId = await fetchTag(tag);
tags.push(tagId);
}
const metaData = {
id: post.slug,
title: postTitle,
status: post.status === 'publish' ? 'published' : 'draft',
authors: [postAuthor.id],
titleImage,
excerpt: stripHtml(post.excerpt.rendered),
categories: postCategories.map(category => category.id),
tags,
publishedDate: post.date,
updatedAt: post.modified,
wordpressId: post.id,
};
const metaDataFile = path.resolve(pathToPostFolder, 'meta.json');
await fs.promises.writeFile(metaDataFile, JSON.stringify(metaData, null, 2));
const cleanedContent = cleanUpHtml(post.content.rendered);
const htmlWithImages = await downloadAndUpdateImages(cleanedContent, pathToPostFolder);
const turndownService = new TurndownService({
bulletListMarker: '-',
codeBlockStyle: 'fenced',
emDelimiter: '*',
});
turndownService.keep(['figure', 'figcaption']);
const content = turndownService.turndown(htmlWithImages);
const contentFile = path.resolve(pathToPostFolder, 'index.md');
await fs.promises.writeFile(contentFile, content);
}
};
for (let page = 1; page <= totalPages; page++) {
await importData(page);
}
}
async function fetchTag(tagId) {
const response = await fetch(`${tagsUrl}/${tagId}`);
const tag = await response.json();
return tag.name;
}
await fetchAuthors();
await fetchCategories();
await fetchPosts();
if (imagesNotDownloaded.length > 0) {
console.log('The following images could not be downloaded:');
console.log(JSON.stringify(imagesNotDownloaded, null, 2));
}
console.log('Data successfully exported from Wordpress!');