wordpress-to-kirby 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -1
- package/index.js +254 -69
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,6 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
A Node.js-based command-line utility to convert a WordPress XML export to a directory of Kirby CMS files.
|
|
4
4
|
|
|
5
|
+
## Getting started
|
|
6
|
+
|
|
7
|
+
Install from npm:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install wordpress-to-kirby
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or run with npx without installing:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npx wordpress-to-kirby
|
|
17
|
+
```
|
|
18
|
+
|
|
5
19
|
## Options
|
|
6
20
|
|
|
7
|
-
Use `wordpress-to-kirby --help` for a list of options.
|
|
21
|
+
Use `wordpress-to-kirby --help` for a list of options.
|
|
22
|
+
|
|
23
|
+
## Prior art
|
|
24
|
+
|
|
25
|
+
* [`splorp/wordpress-xml-to-kirby`](https://github.com/splorp/wordpress-xml-to-kirby)
|
package/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { readFile, writeFile, mkdir } from 'node:fs/promises';
|
|
3
|
-
import { join, dirname } from 'node:path';
|
|
3
|
+
import { join, dirname, extname, relative } from 'node:path';
|
|
4
4
|
|
|
5
5
|
import opsh from 'opsh';
|
|
6
6
|
import { JSDOM } from 'jsdom';
|
|
7
|
-
import { load } from 'js-yaml';
|
|
8
|
-
import { unserialize } from 'php-serialize';
|
|
7
|
+
import { load, dump } from 'js-yaml';
|
|
8
|
+
import { serialize, unserialize } from 'php-serialize';
|
|
9
9
|
|
|
10
|
-
import { serialize } from './src/kirby-txt.js';
|
|
10
|
+
import { serialize as serialize_item } from './src/kirby-txt.js';
|
|
11
11
|
import { uuid } from './src/uuid.js';
|
|
12
12
|
|
|
13
13
|
async function getPackage() {
|
|
@@ -32,13 +32,24 @@ if (options.v || options.version) {
|
|
|
32
32
|
process.exit();
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
const [command, ...files ] = operands;
|
|
36
|
+
|
|
37
|
+
if (command !== 'generate' && command !== 'download') {
|
|
38
|
+
console.error(`Supported commands: generate, download; ${command} provided. Exiting.`);
|
|
39
|
+
process.exit(1);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (files.length !== 1) {
|
|
43
|
+
console.error(`Exactly one file is supported, ${files.length} provided. Exiting.`);
|
|
37
44
|
process.exit(1);
|
|
38
45
|
}
|
|
39
46
|
|
|
40
|
-
const parsers = {
|
|
41
|
-
|
|
47
|
+
const parsers = {
|
|
48
|
+
'postmeta_wp_attachment_metadata': 'php'
|
|
49
|
+
};
|
|
50
|
+
const serializers = {
|
|
51
|
+
'postmeta_wp_attachment_metadata': 'json'
|
|
52
|
+
};
|
|
42
53
|
|
|
43
54
|
function commasep(val) {
|
|
44
55
|
return (val || '').split(/\s*,\s*/).filter(Boolean);
|
|
@@ -64,26 +75,28 @@ commasep(options.serialize).forEach(pair => {
|
|
|
64
75
|
serializers[key] = val;
|
|
65
76
|
});
|
|
66
77
|
|
|
67
|
-
const keys_to_include = new Set(commasep(options
|
|
68
|
-
const keys_to_exclude = new Set(options
|
|
78
|
+
const keys_to_include = new Set(commasep(options['include-keys']));
|
|
79
|
+
const keys_to_exclude = new Set(options['include-keys'] ? [] : commasep(options['exclude-keys']));
|
|
69
80
|
|
|
70
|
-
|
|
81
|
+
const types_to_include = new Set(commasep(options['include-types']));
|
|
82
|
+
const types_to_exclude = new Set(options['include-types'] ? [] : commasep(options['exclude-types']));
|
|
83
|
+
|
|
84
|
+
function process_item(item) {
|
|
85
|
+
let entries = Object.entries(item);
|
|
71
86
|
if (keys_to_include.size) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
if (keys_to_exclude.size) {
|
|
78
|
-
return Object.fromEntries(
|
|
79
|
-
Object.entries(item)
|
|
80
|
-
.filter(entry => !keys_to_exclude.has(entry[0]))
|
|
81
|
-
);
|
|
87
|
+
entries = entries.filter(entry => keys_to_include.has(entry[0]));
|
|
88
|
+
} else if (keys_to_exclude.size) {
|
|
89
|
+
entries = entries.filter(entry => !keys_to_exclude.has(entry[0]));
|
|
82
90
|
}
|
|
83
|
-
return
|
|
91
|
+
return Object.fromEntries(
|
|
92
|
+
entries.map(entry => [
|
|
93
|
+
entry[0],
|
|
94
|
+
serialize_value(entry[0], entry[1])
|
|
95
|
+
])
|
|
96
|
+
);
|
|
84
97
|
}
|
|
85
98
|
|
|
86
|
-
function
|
|
99
|
+
function parse_value(key, value) {
|
|
87
100
|
if (!value) {
|
|
88
101
|
return value;
|
|
89
102
|
}
|
|
@@ -92,21 +105,68 @@ function parse(key, value) {
|
|
|
92
105
|
try {
|
|
93
106
|
return unserialize(value);
|
|
94
107
|
} catch(err) {
|
|
95
|
-
console.error(`Can’t
|
|
108
|
+
console.error(`Warn: Can’t serialize PHP value, skipping: ${value}`);
|
|
96
109
|
return value;
|
|
97
110
|
}
|
|
98
111
|
case 'yaml':
|
|
99
112
|
try {
|
|
100
113
|
return load(value);
|
|
101
114
|
} catch (err) {
|
|
102
|
-
console.error(`Can’t
|
|
115
|
+
console.error(`Warn: Can’t serialize YAML value, skipping: ${value}`);
|
|
103
116
|
return value;
|
|
104
117
|
}
|
|
105
118
|
case 'json':
|
|
106
119
|
try {
|
|
107
120
|
return JSON.parse(value);
|
|
108
121
|
} catch(err) {
|
|
109
|
-
console.error(`Can’t
|
|
122
|
+
console.error(`Warn: Can’t serialize JSON value, skipping: ${value}`);
|
|
123
|
+
return value;
|
|
124
|
+
}
|
|
125
|
+
case 'html':
|
|
126
|
+
try {
|
|
127
|
+
return new JSDOM(
|
|
128
|
+
`<!doctype html><html><head></head><body>${value}</body></html>`
|
|
129
|
+
).window.document;
|
|
130
|
+
} catch(err) {
|
|
131
|
+
console.error(`Warn: Can’t serialize JSON value, skipping: ${value}`);
|
|
132
|
+
return value;
|
|
133
|
+
}
|
|
134
|
+
default:
|
|
135
|
+
return value;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function serialize_value(key, value) {
|
|
140
|
+
if (!value) {
|
|
141
|
+
return value;
|
|
142
|
+
}
|
|
143
|
+
switch(serializers[key]) {
|
|
144
|
+
case 'php':
|
|
145
|
+
try {
|
|
146
|
+
return serialize(value);
|
|
147
|
+
} catch(err) {
|
|
148
|
+
console.error(`Warn: Can’t stringify PHP value, skipping: ${value}`);
|
|
149
|
+
return value;
|
|
150
|
+
}
|
|
151
|
+
case 'yaml':
|
|
152
|
+
try {
|
|
153
|
+
return dump(value);
|
|
154
|
+
} catch (err) {
|
|
155
|
+
console.error(`Warn: Can’t stringify YAML value, skipping: ${value}`);
|
|
156
|
+
return value;
|
|
157
|
+
}
|
|
158
|
+
case 'json':
|
|
159
|
+
try {
|
|
160
|
+
return JSON.stringify(value);
|
|
161
|
+
} catch(err) {
|
|
162
|
+
console.error(`Warn: Can’t stringify JSON value, skipping: ${value}`);
|
|
163
|
+
return value;
|
|
164
|
+
}
|
|
165
|
+
case 'html':
|
|
166
|
+
try {
|
|
167
|
+
return value.body.innerHTML;
|
|
168
|
+
} catch(err) {
|
|
169
|
+
console.error(`Warn: Can’t stringify HTML value, skipping: ${value}`);
|
|
110
170
|
return value;
|
|
111
171
|
}
|
|
112
172
|
default:
|
|
@@ -115,13 +175,22 @@ function parse(key, value) {
|
|
|
115
175
|
}
|
|
116
176
|
|
|
117
177
|
const document = new JSDOM(
|
|
118
|
-
await readFile(
|
|
178
|
+
await readFile(files[0], 'utf8'),
|
|
119
179
|
{
|
|
120
180
|
contentType: 'application/xml'
|
|
121
181
|
}
|
|
122
182
|
).window.document;
|
|
123
183
|
|
|
124
|
-
const items = Array.from(document.querySelectorAll('item'))
|
|
184
|
+
const items = Array.from(document.querySelectorAll('item'))
|
|
185
|
+
.map(extract)
|
|
186
|
+
.filter(item => {
|
|
187
|
+
if (types_to_include.size) {
|
|
188
|
+
return types_to_include.has(item.post_type);
|
|
189
|
+
} else if (types_to_exclude.size) {
|
|
190
|
+
return !types_to_exclude.has(item.post_type);
|
|
191
|
+
}
|
|
192
|
+
return true;
|
|
193
|
+
});
|
|
125
194
|
|
|
126
195
|
function extract(item) {
|
|
127
196
|
const select = (sel, ns, all = false, el) => {
|
|
@@ -133,35 +202,34 @@ function extract(item) {
|
|
|
133
202
|
return (el ?? item).querySelector(sel);
|
|
134
203
|
}
|
|
135
204
|
return {
|
|
136
|
-
title: select('title').textContent,
|
|
137
|
-
link: select('link').textContent,
|
|
138
|
-
pubDate: select('pubDate').textContent,
|
|
139
|
-
creator: select('creator', 'dc').textContent,
|
|
140
|
-
guid: select('guid').textContent,
|
|
141
|
-
guid_isPermaLink: select('guid').getAttribute('isPermaLink'),
|
|
142
|
-
description: select('description').textContent,
|
|
143
|
-
content: select('encoded', 'content').textContent,
|
|
144
|
-
excerpt: select('encoded', 'excerpt').textContent,
|
|
145
|
-
post_id: select('post_id', 'wp').textContent,
|
|
146
|
-
post_date: select('post_date', 'wp').textContent,
|
|
147
|
-
post_date_gmt: select('post_date_gmt', 'wp').textContent,
|
|
148
|
-
post_modified: select('post_modified', 'wp').textContent,
|
|
149
|
-
post_modified_gmt: select('post_modified_gmt', 'wp').textContent,
|
|
150
|
-
comment_status: select('comment_status', 'wp').textContent,
|
|
151
|
-
ping_status: select('ping_status', 'wp').textContent,
|
|
152
|
-
post_name: select('post_name', 'wp').textContent,
|
|
153
|
-
status: select('status', 'wp').textContent,
|
|
154
|
-
post_parent: select('post_parent', 'wp').textContent,
|
|
155
|
-
menu_order: select('menu_order', 'wp').textContent,
|
|
156
|
-
post_type: select('post_type', 'wp').textContent,
|
|
157
|
-
post_password: select('post_password', 'wp').textContent,
|
|
158
|
-
|
|
205
|
+
title: parse_value('title', select('title').textContent),
|
|
206
|
+
link: parse_value('link', select('link').textContent),
|
|
207
|
+
pubDate: parse_value('pubDate', select('pubDate').textContent),
|
|
208
|
+
creator: parse_value('creator', select('creator', 'dc').textContent),
|
|
209
|
+
guid: parse_value('guid', select('guid').textContent),
|
|
210
|
+
guid_isPermaLink: parse_value('guid_isPermaLink', select('guid').getAttribute('isPermaLink')),
|
|
211
|
+
description: parse_value('description', select('description').textContent),
|
|
212
|
+
content: parse_value('content', select('encoded', 'content').textContent),
|
|
213
|
+
excerpt: parse_value('excerpt', select('encoded', 'excerpt').textContent),
|
|
214
|
+
post_id: parse_value('post_id', select('post_id', 'wp').textContent),
|
|
215
|
+
post_date: parse_value('post_date', select('post_date', 'wp').textContent),
|
|
216
|
+
post_date_gmt: parse_value('post_date_gmt', select('post_date_gmt', 'wp').textContent),
|
|
217
|
+
post_modified: parse_value('post_modified', select('post_modified', 'wp').textContent),
|
|
218
|
+
post_modified_gmt: parse_value('post_modified_gmt', select('post_modified_gmt', 'wp').textContent),
|
|
219
|
+
comment_status: parse_value('comment_status', select('comment_status', 'wp').textContent),
|
|
220
|
+
ping_status: parse_value('ping_status', select('ping_status', 'wp').textContent),
|
|
221
|
+
post_name: parse_value('post_name', select('post_name', 'wp').textContent),
|
|
222
|
+
status: parse_value('status', select('status', 'wp').textContent),
|
|
223
|
+
post_parent: parse_value('post_parent', select('post_parent', 'wp').textContent),
|
|
224
|
+
menu_order: parse_value('menu_order', select('menu_order', 'wp').textContent),
|
|
225
|
+
post_type: parse_value('post_type', select('post_type', 'wp').textContent),
|
|
226
|
+
post_password: parse_value('post_password', select('post_password', 'wp').textContent),
|
|
227
|
+
attachment_url: parse_value('attachment_url', select('attachment_url', 'wp')?.textContent),
|
|
228
|
+
is_sticky: parse_value('is_sticky', select('is_sticky', 'wp').textContent),
|
|
159
229
|
...select('postmeta', 'wp', true).reduce((acc, it) => {
|
|
160
230
|
const key = select('meta_key', 'wp', false, it).textContent;
|
|
161
231
|
const value = select('meta_value', 'wp', false, it).textContent;
|
|
162
|
-
|
|
163
|
-
acc['postmeta_' + key] = parse('postmeta_' + key, value);
|
|
164
|
-
}
|
|
232
|
+
acc['postmeta' + key] = parse_value('postmeta' + key, value);
|
|
165
233
|
return acc;
|
|
166
234
|
}, {}),
|
|
167
235
|
uuid: uuid()
|
|
@@ -173,19 +241,111 @@ const outdir = options.o || options.outdir || 'wordpress-to-kirby';
|
|
|
173
241
|
function outpath(item) {
|
|
174
242
|
let parts = (new URL(item.link)).pathname.split('/').filter(Boolean);
|
|
175
243
|
if (item.post_type === 'attachment') {
|
|
176
|
-
|
|
244
|
+
const filename = parts.pop() + (extname(item.attachment_url ?? '') ?? '') + '.txt';
|
|
245
|
+
return join(outdir, 'content', ...parts.filter(p => p !== 'attachment'), filename);
|
|
177
246
|
}
|
|
178
247
|
if (!parts.length) {
|
|
179
248
|
parts = ['_unlinked', item.post_type, item.post_id, ...parts];
|
|
180
249
|
}
|
|
181
|
-
return join(outdir, ...parts, item.post_type + '.txt');
|
|
250
|
+
return join(outdir, 'content', ...parts, item.post_type + '.txt');
|
|
182
251
|
}
|
|
183
252
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
253
|
+
if (command === 'generate') {
|
|
254
|
+
for await (let item of items) {
|
|
255
|
+
const destination = outpath(item);
|
|
256
|
+
await mkdir(dirname(destination), { recursive: true });
|
|
257
|
+
await writeFile(destination, serialize_item(process_item(item)));
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
const metadata = {
|
|
261
|
+
output: [],
|
|
262
|
+
types: {}
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
items.forEach(item => {
|
|
266
|
+
metadata.output.push({
|
|
267
|
+
post_id: +item.post_id,
|
|
268
|
+
post_type: item.post_type,
|
|
269
|
+
url: item.attachment_url ?? item.link,
|
|
270
|
+
uuid: item.uuid,
|
|
271
|
+
path: relative(outdir, outpath(item))
|
|
272
|
+
});
|
|
273
|
+
metadata.types[item.post_type] = (metadata.types[item.post_type] || [])
|
|
274
|
+
.concat(Object.keys(item));
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
const blueprints_path = join(outdir, 'site', 'blueprints');
|
|
278
|
+
await mkdir(join(blueprints_path, 'pages'), { recursive: true });
|
|
279
|
+
|
|
280
|
+
Object.keys(metadata.types).forEach(key => {
|
|
281
|
+
metadata.types[key] = Array.from(new Set(metadata.types[key])).sort();
|
|
282
|
+
// Make blueprints
|
|
283
|
+
const blueprint_content = metadata.types[key].reduce((acc, curr) => {
|
|
284
|
+
acc[curr] = {
|
|
285
|
+
type: 'text'
|
|
286
|
+
};
|
|
287
|
+
return acc;
|
|
288
|
+
}, {});
|
|
289
|
+
writeFile(join(blueprints_path, 'pages', key + '.yml'), dump({
|
|
290
|
+
tabs: {
|
|
291
|
+
fields: {
|
|
292
|
+
label: 'Fields',
|
|
293
|
+
sections: {
|
|
294
|
+
fields: {
|
|
295
|
+
type: 'fields',
|
|
296
|
+
fields: blueprint_content
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
},
|
|
300
|
+
pages: {
|
|
301
|
+
label: 'Pages',
|
|
302
|
+
sections: {
|
|
303
|
+
pages: {
|
|
304
|
+
type: 'pages'
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
},
|
|
308
|
+
files: {
|
|
309
|
+
label: 'Files',
|
|
310
|
+
sections: {
|
|
311
|
+
files: {
|
|
312
|
+
type: 'files'
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}));
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
const default_blueprint = {
|
|
322
|
+
tabs: {
|
|
323
|
+
pages: {
|
|
324
|
+
label: 'Pages',
|
|
325
|
+
sections: {
|
|
326
|
+
pages: {
|
|
327
|
+
type: 'pages'
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
},
|
|
331
|
+
files: {
|
|
332
|
+
label: 'Files',
|
|
333
|
+
sections: {
|
|
334
|
+
files: {
|
|
335
|
+
type: 'files'
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
await writeFile(join(blueprints_path, 'pages', 'default.yml'), dump(default_blueprint));
|
|
342
|
+
await writeFile(join(blueprints_path, 'site.yml'), dump(default_blueprint));
|
|
343
|
+
|
|
344
|
+
await writeFile(join(outdir, 'metadata.json'), JSON.stringify(metadata, null, '\t'));
|
|
345
|
+
} else if (command === 'download') {
|
|
346
|
+
console.error('Not implemented yet, exiting.');
|
|
347
|
+
process.exit(1);
|
|
348
|
+
}
|
|
189
349
|
|
|
190
350
|
function printHelp() {
|
|
191
351
|
console.log(`wordpress-to-kirby:
|
|
@@ -195,7 +355,15 @@ function printHelp() {
|
|
|
195
355
|
|
|
196
356
|
Usage:
|
|
197
357
|
|
|
198
|
-
wordpress-to-kirby <
|
|
358
|
+
wordpress-to-kirby <command> <xml-file> <options>
|
|
359
|
+
|
|
360
|
+
Commands:
|
|
361
|
+
|
|
362
|
+
generate
|
|
363
|
+
Generate a Kirby CMS file structure from the provided XML file.
|
|
364
|
+
|
|
365
|
+
download
|
|
366
|
+
Download attachments. Not implemented.
|
|
199
367
|
|
|
200
368
|
Options:
|
|
201
369
|
|
|
@@ -216,17 +384,32 @@ Options:
|
|
|
216
384
|
php: deserialize PHP object.
|
|
217
385
|
yaml: parse as YAML.
|
|
218
386
|
json: parse as JSON.
|
|
387
|
+
html: parse as HTML.
|
|
219
388
|
Prefix post meta fields with "postmeta".
|
|
220
389
|
|
|
221
390
|
--serialize=<rules>
|
|
222
|
-
|
|
391
|
+
Instructions to serialize some fields, in the form of:
|
|
392
|
+
"<key>=<serializer>,<key>=<serializer>,…"
|
|
393
|
+
Supported parsers:
|
|
394
|
+
php: serialize as PHP object.
|
|
395
|
+
yaml: serialize as YAML.
|
|
396
|
+
json: serialize as JSON.
|
|
397
|
+
html: serialize as HTML.
|
|
398
|
+
Prefix post meta fields with "postmeta".
|
|
223
399
|
|
|
224
|
-
--exclude=<keys>
|
|
400
|
+
--exclude-keys=<keys>
|
|
225
401
|
Exclude certain keys from the output. Comma-separated.
|
|
226
402
|
|
|
227
|
-
--include=<keys>
|
|
403
|
+
--include-keys=<keys>
|
|
228
404
|
Only include certain keys from the output. Comma-separated.
|
|
229
|
-
When '--include' is specified, any '--exclude' is ignored.
|
|
405
|
+
When '--include-keys' is specified, any '--exclude-keys' is ignored.
|
|
406
|
+
|
|
407
|
+
--exclude-types=<types>
|
|
408
|
+
Exclude certain post types from the output. Comma-separated.
|
|
409
|
+
|
|
410
|
+
--include-types=<types>
|
|
411
|
+
Only include certain post types from the output. Comma-separated.
|
|
412
|
+
When '--include-types' is specified, any '--exclude-types' is ignored.
|
|
230
413
|
|
|
231
414
|
Keys extracted from the XML:
|
|
232
415
|
|
|
@@ -236,14 +419,16 @@ Keys extracted from the XML:
|
|
|
236
419
|
description, content, excerpt, post_id, post_date,
|
|
237
420
|
post_date_gmt, post_modified, post_modified_gmt,
|
|
238
421
|
comment_status, ping_status, post_name, status,
|
|
239
|
-
post_parent, menu_order, post_type, post_password, is_sticky
|
|
422
|
+
post_parent, menu_order, post_type, post_password, is_sticky,
|
|
423
|
+
attachment_url
|
|
240
424
|
|
|
241
425
|
Post metadata:
|
|
242
426
|
|
|
243
|
-
|
|
427
|
+
Fields from <wp:postmeta> not starting with an underscore
|
|
428
|
+
are included in the output with the 'postmeta*' prefix.
|
|
244
429
|
|
|
245
430
|
Keys generated from scratch:
|
|
246
431
|
|
|
247
|
-
uuid — sixteen random alphanumeric characters
|
|
432
|
+
uuid — sixteen random alphanumeric characters.
|
|
248
433
|
`);
|
|
249
434
|
}
|