forked from Swizec/wordpress-to-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.js
289 lines (245 loc) · 7.99 KB
/
convert.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
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
const { format } = require("date-fns");
const fetch = require("node-fetch");
const path = require("path");
const prettier = require("prettier");
const xml2js = require("xml2js");
const fs = require("fs");
const slugify = require("slugify");
const htmlentities = require("he");
const {
cleanupShortcodes,
fixCodeBlocks,
codeBlockDebugger,
fixBadHTML,
fixEmbeds,
} = require("./articleCleanup");
const unified = require("unified");
const parseHTML = require("rehype-parse");
const rehype2remark = require("rehype-remark");
const stringify = require("remark-stringify");
const imageType = require("image-type");
// includes all sorts of edge cases and weird stuff
//processExport("test-wordpress-dump.xml");
// full dump
processExport("nompbloggen.WordPress.2021-05-12.xml");
function processExport(file) {
const parser = new xml2js.Parser();
fs.readFile(file, function (err, data) {
if (err) {
return console.log("Error: " + err);
}
parser.parseString(data, function (err, result) {
if (err) {
return console.log("Error parsing xml: " + err);
}
console.log("Parsed XML");
const posts = result.rss.channel[0].item;
fs.mkdir("out", function () {
posts
.filter((p) => p["wp:post_type"][0] === "post")
.forEach(processPost);
});
});
});
}
function constructImageName({ urlParts, buffer }) {
const pathParts = path.parse(
urlParts.pathname
.replace(/^\//, "")
.replace(/\//g, "-")
.replace(/\*/g, "")
);
const { ext } = imageType(new Buffer(buffer));
return `${pathParts.name}.${ext}`;
}
async function processImage({ url, postData, images, directory }) {
const cleanUrl = htmlentities.decode(url);
if (cleanUrl.startsWith("./img")) {
console.log(`Already processed ${cleanUrl} in ${directory}`);
return [postData, images];
}
const urlParts = new URL(cleanUrl);
const filePath = `out/img`;
try {
const response = await downloadFile(cleanUrl);
const type = response.headers.get("Content-Type");
if (type.includes("image") || type.includes("octet-stream")) {
const buffer = await response.arrayBuffer();
const imageName = constructImageName({
urlParts,
buffer,
});
//Make the image name local relative in the markdown
postData = postData.replace(url, `./img/${imageName}`);
images = [...images, `./img/${imageName}`];
fs.writeFileSync(`${filePath}/${imageName}`, new Buffer(buffer));
}
} catch (e) {
console.log(`Keeping ref to ${url}`);
}
return [postData, images];
}
async function processImages({ postData, directory }) {
const patt = new RegExp('(?:src="(.*?)")', "gi");
let images = [];
var m;
let matches = [];
while ((m = patt.exec(postData)) !== null) {
if (!m[1].endsWith(".js")) {
matches.push(m[1]);
}
}
if (matches != null && matches.length > 0) {
for (let match of matches) {
try {
[postData, images] = await processImage({
url: match,
postData,
images,
directory,
});
} catch (err) {
console.log("ERROR PROCESSING IMAGE", match);
}
}
}
return [postData, images];
}
async function processPost(post) {
console.log("Processing Post");
const postTitle =
typeof post.title === "string" ? post.title : post.title[0];
console.log("Post title: " + postTitle);
const postDate = isFinite(new Date(post.pubDate))
? new Date(post.pubDate)
: new Date(post["wp:post_date"]);
console.log("Post Date: " + postDate);
let postData = post["content:encoded"][0];
console.log("Post length: " + postData.length + " bytes");
const slug = slugify(postTitle, {
remove: /[^\w\s]/g,
})
.toLowerCase()
.replace(/\*/g, "");
console.log("Post slug: " + slug);
// takes the longest description candidate
const description = [
post.description,
...post["wp:postmeta"].filter(
(meta) =>
meta["wp:meta_key"][0].includes("metadesc") ||
meta["wp:meta_key"][0].includes("description")
),
].sort((a, b) => b.length - a.length)[0];
const heroURLs = post["wp:postmeta"]
.filter(
(meta) =>
meta["wp:meta_key"][0].includes("opengraph-image") ||
meta["wp:meta_key"][0].includes("twitter-image")
)
.map((meta) => meta["wp:meta_value"][0])
.filter((url) => url.startsWith("http"));
let heroImage = "";
let directory = slug;
let fname = `index.mdx`;
try {
fs.mkdirSync(`out/img`);
} catch (e) {
}
//Merge categories and tags into tags
const categories = post.category && post.category.map((cat) => cat["_"]);
//Find all images
let images = [];
if (heroURLs.length > 0) {
const url = heroURLs[0];
[postData, images] = await processImage({
url,
postData,
images,
directory,
});
}
[postData, images] = await processImages({ postData, directory });
heroImage = images.find((img) => !img.endsWith("gif"));
const markdown = await new Promise((resolve, reject) => {
unified()
.use(parseHTML, {
fragment: true,
emitParseErrors: true,
duplicateAttribute: false,
})
.use(fixCodeBlocks)
.use(fixEmbeds)
.use(rehype2remark)
.use(cleanupShortcodes)
.use(stringify, {
fences: true,
listItemIndent: 1,
gfm: false,
pedantic: false,
})
.process(fixBadHTML(postData), (err, markdown) => {
if (err) {
reject(err);
} else {
let content = markdown.contents;
content = content.replace(
/(?<=https?:\/\/.*)\\_(?=.*\n)/g,
"_"
);
resolve(prettier.format(content, { parser: "mdx" }));
}
});
});
try {
postTitle.replace("\\", "\\\\").replace(/"/g, '\\"');
} catch (e) {
console.log("FAILED REPLACE", postTitle);
}
const redirect_from = post.link[0]
.replace("https://swizec.com", "")
.replace("https://www.swizec.com", "");
let frontmatter;
try {
frontmatter = [
"---",
`title: '${postTitle.replace(/'/g, "''")}'`,
`description: "${description}"`,
`published: ${format(postDate, "yyyy-MM-dd")}`,
`redirect_from:
- ${redirect_from}`,
];
} catch (e) {
console.log("----------- BAD TIME", postTitle, postDate);
throw e;
}
if (categories && categories.length > 0) {
frontmatter.push(`categories: "${categories.join(", ")}"`);
}
frontmatter.push(`hero: ${heroImage || "../../../defaultHero.jpg"}`);
frontmatter.push("---");
frontmatter.push("");
fs.writeFile(
`out/${format(postDate, "yyyy-MM-dd")}-${directory}.md`,
frontmatter.join("\n") + markdown,
function (err) {
console.log(err)
}
);
}
async function downloadFile(url) {
const response = await fetch(url);
if (response.status >= 400) {
throw new Error("Bad response from server");
} else {
return response;
}
}
function getPaddedMonthNumber(month) {
if (month < 10) return "0" + month;
else return month;
}
function getPaddedDayNumber(day) {
if (day < 10) return "0" + day;
else return day;
}