This repository was archived by the owner on May 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson2wxr.js
More file actions
executable file
·92 lines (87 loc) · 2.51 KB
/
json2wxr.js
File metadata and controls
executable file
·92 lines (87 loc) · 2.51 KB
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
#!/usr/bin/nodejs
var Importer = require('wxr');
var fs = require('fs');
var importer = new Importer();
var json_file = fs.readFileSync('./json-result/export.json', 'utf8');
var json_file = json_file.replace(/<script[^>]*>.*?<\/script>/gi, '').replace(/<blockquote[^>]*>.*?<\/blockquote>/gi, '');
var obj = JSON.parse(json_file);
var wpcat = {};
var string_to_slug = function (str) {
str = str.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase();
// remove accents, swap ñ for n, etc
var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
var to = "aaaaeeeeiiiioooouuuunc------";
for (var i = 0, l = from.length; i < l; i++) {
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
}
str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes
return str;
};
for (var i = 0; i < obj.length; ++i) {
console.log("ID: " + obj[i].ID);
var cat_posts = [];
var tag_posts = [];
var cat_post = '';
var tag_post = '';
// Category
for (var cat_id in obj[i].category) {
if (obj.hasOwnProperty(cat_id)) {
cat_post = {
id: cat_id,
title: obj[i].category[cat_id],
slug: string_to_slug(obj[i].category[cat_id])
};
if (wpcat[obj[i].category[cat_id]] === undefined) {
importer.addCategory(cat_post);
}
cat_posts.push(cat_post);
}
}
// Tag
for (var tag_id in obj[i].tag) {
tag_post = {
title: obj[i].tag[tag_id],
slug: string_to_slug(obj[i].tag[tag_id]),
// id:
};
importer.addCategory(tag_post);
tag_posts.push(tag_post);
}
// Add post
importer.addPost({
id: obj[i].ID,
title: obj[i].post_title,
name: obj[i].post_name,
date: obj[i].post_date,
status: obj[i].post_status,
post: obj[i].post_type,
author: "wp_user_replace",
contentEncoded: obj[i].post_content.trim(),
featured_image_id: obj[i].featured_image_id,
categories: cat_posts,
tags: tag_posts
});
// Add attachment
if (obj[i].hasOwnProperty('featured_image_id')) {
importer.addAttachment({
id: obj[i].featured_image_id,
date: obj[i].post_date,
title: obj[i].featured_image_desc,
name: string_to_slug(obj[i].featured_image_desc),
author: "wp_user_replace",
description: obj[i].featured_image_desc,
parent: obj[i].ID,
attachmentURL: obj[i].featured_image
});
}
}
var file_xml = importer.stringify();
fs.writeFile("./json-result/export.xml", file_xml, function (err) {
if (err) {
return console.log(err);
}
console.log("The file was generated!");
});