wordpress-to-kirby 0.3.0 → 0.4.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 +10 -4
- package/index.js +160 -96
- package/package.json +4 -1
- package/src/inside.js +6 -0
- package/src/slurp.js +5 -0
- package/test/inside.test.js +11 -0
package/README.md
CHANGED
|
@@ -26,20 +26,24 @@ Use `wordpress-to-kirby --help` for information on usage and available commands
|
|
|
26
26
|
|
|
27
27
|
### `wordpress-to-kirby generate`
|
|
28
28
|
|
|
29
|
-
Generate a Kirby CMS file structure from the
|
|
29
|
+
Generate a Kirby CMS file structure from the input file. Usage:
|
|
30
30
|
|
|
31
31
|
```bash
|
|
32
|
-
wordpress-to-kirby generate [options] <
|
|
32
|
+
wordpress-to-kirby generate [options] <input-file>
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
+
The input must correspond to a WordPress export XML file. When `-` is provided, the content is read from STDIN.
|
|
36
|
+
|
|
35
37
|
### `wordpress-to-kirby download`
|
|
36
38
|
|
|
37
|
-
Download the attachments found in the
|
|
39
|
+
Download the attachments found in the input file. Usage:
|
|
38
40
|
|
|
39
41
|
```bash
|
|
40
|
-
wordpress-to-kirby download [options] <
|
|
42
|
+
wordpress-to-kirby download [options] <input-file>
|
|
41
43
|
```
|
|
42
44
|
|
|
45
|
+
The input must correspond to either a WordPress export XML file or a metadata JSON file (generated with the `generate` command). When `-` is provided, the content is read from STDIN.
|
|
46
|
+
|
|
43
47
|
## Options
|
|
44
48
|
|
|
45
49
|
### `-h, --help`
|
|
@@ -66,6 +70,8 @@ Root directory to which to write the output, relative to the current working dir
|
|
|
66
70
|
wordpress-to-kirby generate my-blog.xml -o my-blog
|
|
67
71
|
```
|
|
68
72
|
|
|
73
|
+
When using the `--only-metadata` option is provided, the `--outdir` value can be `-`, which will cause the `metadata.json` file to be output to STDOUT.
|
|
74
|
+
|
|
69
75
|
### `--contentdir=<path>`
|
|
70
76
|
|
|
71
77
|
For the `download` command, point to the location of a `wp-content` directory from which to download attachments.
|
package/index.js
CHANGED
|
@@ -9,6 +9,8 @@ import { serialize, unserialize } from 'php-serialize';
|
|
|
9
9
|
|
|
10
10
|
import { serialize as serialize_item } from './src/kirby-txt.js';
|
|
11
11
|
import { uuid } from './src/uuid.js';
|
|
12
|
+
import inside from './src/inside.js';
|
|
13
|
+
import slurp from './src/slurp.js';
|
|
12
14
|
|
|
13
15
|
async function getPackage() {
|
|
14
16
|
return JSON.parse(
|
|
@@ -30,7 +32,7 @@ const { positionals: operands, values: options } = parseArgs({
|
|
|
30
32
|
type: 'string',
|
|
31
33
|
multiple: true
|
|
32
34
|
},
|
|
33
|
-
|
|
35
|
+
serialize: {
|
|
34
36
|
type: 'string',
|
|
35
37
|
multiple: true
|
|
36
38
|
},
|
|
@@ -72,19 +74,24 @@ if (options.help) {
|
|
|
72
74
|
|
|
73
75
|
if (options.version) {
|
|
74
76
|
const pkg = await getPackage();
|
|
75
|
-
|
|
77
|
+
process.stdout.write(pkg.version + '\n');
|
|
76
78
|
process.exit();
|
|
77
79
|
}
|
|
78
80
|
|
|
79
81
|
const [command, ...files ] = operands;
|
|
80
82
|
|
|
81
83
|
if (command !== 'generate' && command !== 'download') {
|
|
82
|
-
|
|
84
|
+
process.stderr.write(`Supported commands: generate, download; ${command} provided. Exiting.\n`);
|
|
83
85
|
process.exit(1);
|
|
84
86
|
}
|
|
85
87
|
|
|
86
88
|
if (files.length !== 1) {
|
|
87
|
-
|
|
89
|
+
process.stderr.write(`Exactly one file is supported, ${files.length} provided. Exiting.\n`);
|
|
90
|
+
process.exit(1);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (options.outdir === '-' && !options['only-metadata']) {
|
|
94
|
+
process.stderr.write('Outputting to STDOUT is only supported with the --only-metadata option. Exiting.\n');
|
|
88
95
|
process.exit(1);
|
|
89
96
|
}
|
|
90
97
|
|
|
@@ -162,21 +169,21 @@ function parse_value(key, value, id) {
|
|
|
162
169
|
try {
|
|
163
170
|
return unserialize(value);
|
|
164
171
|
} catch(err) {
|
|
165
|
-
|
|
172
|
+
process.stderr.write(`Warn: Can’t parse PHP value in post ${id}, skipping: ${abbr(value)}\n`);
|
|
166
173
|
return value;
|
|
167
174
|
}
|
|
168
175
|
case 'yaml':
|
|
169
176
|
try {
|
|
170
177
|
return load(value);
|
|
171
178
|
} catch (err) {
|
|
172
|
-
|
|
179
|
+
process.stderr.write(`Warn: Can’t parse YAML value in post ${id}, skipping: ${abbr(value)}\n`);
|
|
173
180
|
return value;
|
|
174
181
|
}
|
|
175
182
|
case 'json':
|
|
176
183
|
try {
|
|
177
184
|
return JSON.parse(value);
|
|
178
185
|
} catch(err) {
|
|
179
|
-
|
|
186
|
+
process.stderr.write(`Warn: Can’t parse JSON value in post ${id}, skipping: ${abbr(value)}\n`);
|
|
180
187
|
return value;
|
|
181
188
|
}
|
|
182
189
|
case 'html':
|
|
@@ -185,7 +192,7 @@ function parse_value(key, value, id) {
|
|
|
185
192
|
`<!doctype html><html><head></head><body>${value}</body></html>`
|
|
186
193
|
).window.document;
|
|
187
194
|
} catch(err) {
|
|
188
|
-
|
|
195
|
+
process.stderr.write(`Warn: Can’t parse HTML value in post ${id}, skipping: ${abbr(value)}\n`);
|
|
189
196
|
return value;
|
|
190
197
|
}
|
|
191
198
|
default:
|
|
@@ -202,28 +209,28 @@ function serialize_value(key, value, id) {
|
|
|
202
209
|
try {
|
|
203
210
|
return serialize(value);
|
|
204
211
|
} catch(err) {
|
|
205
|
-
|
|
212
|
+
process.stderr.write(`Warn: Can’t serialize PHP value in post ${id}, skipping: ${abbr(value)}\n`);
|
|
206
213
|
return value;
|
|
207
214
|
}
|
|
208
215
|
case 'yaml':
|
|
209
216
|
try {
|
|
210
217
|
return dump(value);
|
|
211
218
|
} catch (err) {
|
|
212
|
-
|
|
219
|
+
process.stderr.write(`Warn: Can’t serialize YAML value in post ${id}, skipping: ${abbr(value)}\n`);
|
|
213
220
|
return value;
|
|
214
221
|
}
|
|
215
222
|
case 'json':
|
|
216
223
|
try {
|
|
217
224
|
return JSON.stringify(value);
|
|
218
225
|
} catch(err) {
|
|
219
|
-
|
|
226
|
+
process.stderr.write(`Warn: Can’t serialize JSON value in post ${id}, skipping: ${abbr(value)}\n`);
|
|
220
227
|
return value;
|
|
221
228
|
}
|
|
222
229
|
case 'html':
|
|
223
230
|
try {
|
|
224
231
|
return value.body.innerHTML;
|
|
225
232
|
} catch(err) {
|
|
226
|
-
|
|
233
|
+
process.stderr.write(`Warn: Can’t serialize HTML value in post ${id}, skipping: ${abbr(value)}\n`);
|
|
227
234
|
return value;
|
|
228
235
|
}
|
|
229
236
|
default:
|
|
@@ -231,24 +238,6 @@ function serialize_value(key, value, id) {
|
|
|
231
238
|
}
|
|
232
239
|
}
|
|
233
240
|
|
|
234
|
-
const document = new JSDOM(
|
|
235
|
-
await readFile(files[0], 'utf8'),
|
|
236
|
-
{
|
|
237
|
-
contentType: 'application/xml'
|
|
238
|
-
}
|
|
239
|
-
).window.document;
|
|
240
|
-
|
|
241
|
-
const items = Array.from(document.querySelectorAll('item'))
|
|
242
|
-
.map(extract)
|
|
243
|
-
.filter(item => {
|
|
244
|
-
if (types_to_include.size) {
|
|
245
|
-
return types_to_include.has(item.post_type);
|
|
246
|
-
} else if (types_to_exclude.size) {
|
|
247
|
-
return !types_to_exclude.has(item.post_type);
|
|
248
|
-
}
|
|
249
|
-
return true;
|
|
250
|
-
});
|
|
251
|
-
|
|
252
241
|
function extract(item) {
|
|
253
242
|
const select = (sel, ns, all = false, el) => {
|
|
254
243
|
if (ns) {
|
|
@@ -313,8 +302,30 @@ function outpath(item) {
|
|
|
313
302
|
return join(outdir, 'content', ...parts, item.post_type + '.txt');
|
|
314
303
|
}
|
|
315
304
|
|
|
305
|
+
function readFromExportXML(file) {
|
|
306
|
+
const document = new JSDOM(file, {
|
|
307
|
+
contentType: 'application/xml'
|
|
308
|
+
}).window.document;
|
|
309
|
+
|
|
310
|
+
return Array.from(document.querySelectorAll('item'))
|
|
311
|
+
.map(extract)
|
|
312
|
+
.filter(item => {
|
|
313
|
+
if (types_to_include.size) {
|
|
314
|
+
return types_to_include.has(item.post_type);
|
|
315
|
+
} else if (types_to_exclude.size) {
|
|
316
|
+
return !types_to_exclude.has(item.post_type);
|
|
317
|
+
}
|
|
318
|
+
return true;
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
|
|
316
322
|
if (command === 'generate') {
|
|
317
323
|
|
|
324
|
+
const items = readFromExportXML(
|
|
325
|
+
files[0] === '-' ?
|
|
326
|
+
await slurp(process.stdin) :
|
|
327
|
+
await readFile(files[0], 'utf8')
|
|
328
|
+
);
|
|
318
329
|
const processed_items = items.map(process_item);
|
|
319
330
|
|
|
320
331
|
const metadata = {
|
|
@@ -335,25 +346,32 @@ if (command === 'generate') {
|
|
|
335
346
|
uuid: full_item.uuid,
|
|
336
347
|
path: relative(outdir, outpath(full_item))
|
|
337
348
|
});
|
|
338
|
-
metadata.types[full_item.post_type] =
|
|
339
|
-
metadata.types[full_item.post_type]
|
|
340
|
-
|
|
341
|
-
);
|
|
349
|
+
metadata.types[full_item.post_type] =
|
|
350
|
+
(metadata.types[full_item.post_type] || [])
|
|
351
|
+
.concat(Object.keys(item));
|
|
342
352
|
});
|
|
343
353
|
|
|
344
354
|
Object.keys(metadata.types).forEach(key => {
|
|
345
355
|
metadata.types[key] = Array.from(new Set(metadata.types[key])).sort();
|
|
346
356
|
});
|
|
347
357
|
|
|
348
|
-
|
|
349
|
-
|
|
358
|
+
if (outdir === '-') {
|
|
359
|
+
process.stdout.write(JSON.stringify(metadata, null, '\t'));
|
|
360
|
+
} else {
|
|
361
|
+
await mkdir(outdir, { recursive: true });
|
|
362
|
+
await writeFile(join(outdir, 'metadata.json'), JSON.stringify(metadata, null, '\t'));
|
|
363
|
+
}
|
|
350
364
|
|
|
351
365
|
if (!options['only-metadata']) {
|
|
352
366
|
|
|
353
367
|
for (let item of processed_items) {
|
|
354
368
|
const destination = outpath(item);
|
|
355
|
-
|
|
356
|
-
|
|
369
|
+
if (inside(outdir, destination)) {
|
|
370
|
+
await mkdir(dirname(destination), { recursive: true });
|
|
371
|
+
await writeFile(destination, serialize_item(item));
|
|
372
|
+
} else {
|
|
373
|
+
process.stderr.write(`Path not inside output directory, skipping: ${destination}\n`);
|
|
374
|
+
}
|
|
357
375
|
};
|
|
358
376
|
|
|
359
377
|
const blueprints_path = join(outdir, 'site', 'blueprints');
|
|
@@ -376,9 +394,13 @@ if (command === 'generate') {
|
|
|
376
394
|
key + '.yml'
|
|
377
395
|
);
|
|
378
396
|
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
397
|
+
if (inside(outdir, blueprint_dest)) {
|
|
398
|
+
await writeFile(blueprint_dest, dump({
|
|
399
|
+
fields: blueprint_content
|
|
400
|
+
}));
|
|
401
|
+
} else {
|
|
402
|
+
process.stderr.write(`Path not inside output directory, skipping: ${blueprint_dest}\n`);
|
|
403
|
+
}
|
|
382
404
|
|
|
383
405
|
} else {
|
|
384
406
|
const blueprint_dest = join(
|
|
@@ -387,35 +409,39 @@ if (command === 'generate') {
|
|
|
387
409
|
key + '.yml'
|
|
388
410
|
);
|
|
389
411
|
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
},
|
|
401
|
-
pages: {
|
|
402
|
-
label: 'Pages',
|
|
403
|
-
sections: {
|
|
404
|
-
pages: {
|
|
405
|
-
type: 'pages'
|
|
412
|
+
if (inside(outdir, blueprint_dest)) {
|
|
413
|
+
await writeFile(blueprint_dest, dump({
|
|
414
|
+
tabs: {
|
|
415
|
+
fields: {
|
|
416
|
+
label: 'Fields',
|
|
417
|
+
sections: {
|
|
418
|
+
fields: {
|
|
419
|
+
type: 'fields',
|
|
420
|
+
fields: blueprint_content
|
|
421
|
+
}
|
|
406
422
|
}
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
423
|
+
},
|
|
424
|
+
pages: {
|
|
425
|
+
label: 'Pages',
|
|
426
|
+
sections: {
|
|
427
|
+
pages: {
|
|
428
|
+
type: 'pages'
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
},
|
|
432
|
+
files: {
|
|
433
|
+
label: 'Files',
|
|
434
|
+
sections: {
|
|
435
|
+
files: {
|
|
436
|
+
type: 'files'
|
|
437
|
+
}
|
|
414
438
|
}
|
|
415
439
|
}
|
|
416
440
|
}
|
|
417
|
-
}
|
|
418
|
-
}
|
|
441
|
+
}));
|
|
442
|
+
} else {
|
|
443
|
+
process.stderr.write(`Path not inside output directory, skipping: ${blueprint_dest}\n`);
|
|
444
|
+
}
|
|
419
445
|
}
|
|
420
446
|
});
|
|
421
447
|
|
|
@@ -444,6 +470,13 @@ if (command === 'generate') {
|
|
|
444
470
|
}
|
|
445
471
|
}
|
|
446
472
|
|
|
473
|
+
function readFromMetadataJSON(file) {
|
|
474
|
+
try {
|
|
475
|
+
return JSON.parse(file);
|
|
476
|
+
} catch(err) {}
|
|
477
|
+
return null;
|
|
478
|
+
}
|
|
479
|
+
|
|
447
480
|
if (command === 'download') {
|
|
448
481
|
|
|
449
482
|
const contentdir = options.contentdir ?
|
|
@@ -452,46 +485,75 @@ if (command === 'download') {
|
|
|
452
485
|
options.contentdir
|
|
453
486
|
) : null;
|
|
454
487
|
|
|
488
|
+
const file = files[0] === '-' ?
|
|
489
|
+
await slurp(process.stdin) :
|
|
490
|
+
await readFile(files[0], 'utf8');
|
|
491
|
+
|
|
492
|
+
const items = readFromMetadataJSON(file)?.output
|
|
493
|
+
.filter(item => item.post_type === 'attachment') ??
|
|
494
|
+
readFromExportXML(file)
|
|
495
|
+
.filter(item => item.post_type === 'attachment')
|
|
496
|
+
.map(process_item)
|
|
497
|
+
.map(item => ({
|
|
498
|
+
url: item.attachment_url,
|
|
499
|
+
path: relative(outdir, outpath(item))
|
|
500
|
+
}));
|
|
501
|
+
|
|
455
502
|
const fetched = new Set();
|
|
456
503
|
|
|
457
|
-
for (let item of items
|
|
458
|
-
const
|
|
459
|
-
const dest = outpath(attachment).replace(/\.txt$/, '');
|
|
504
|
+
for (let item of items) {
|
|
505
|
+
const dest = join(outdir, item.path.replace(/\.txt$/, ''));
|
|
460
506
|
if (contentdir) {
|
|
461
|
-
const pathname = new URL(
|
|
507
|
+
const pathname = new URL(item.url).pathname
|
|
462
508
|
.split('/')
|
|
463
509
|
.filter(p => p && p !== 'wp-content')
|
|
464
510
|
.join(sep);
|
|
465
511
|
if (typeof contentdir === 'string') {
|
|
466
|
-
|
|
467
|
-
|
|
512
|
+
if (inside(outdir, dest)) {
|
|
513
|
+
await mkdir(dirname(dest), { recursive: true });
|
|
514
|
+
await copyFile(join(contentdir, pathname), dest);
|
|
515
|
+
} else {
|
|
516
|
+
process.stderr.write(`Path not inside output directory, skipping: ${dest}\n`);
|
|
517
|
+
}
|
|
468
518
|
} else {
|
|
469
519
|
const url = new URL(pathname, contentdir);
|
|
470
|
-
if (fetched.has(url)) {
|
|
471
|
-
|
|
520
|
+
if (fetched.has(url.toString())) {
|
|
521
|
+
process.stderr.write(`Already fetched, skipping: ${url}\n`);
|
|
472
522
|
} else {
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
523
|
+
if (inside(outdir, dest)) {
|
|
524
|
+
process.stderr.write(`Fetching: ${url}\n`);
|
|
525
|
+
const res = await fetch(url);
|
|
526
|
+
if (res.ok) {
|
|
527
|
+
await mkdir(dirname(dest), { recursive: true });
|
|
528
|
+
await writeFile(dest, res.body);
|
|
529
|
+
process.stderr.write(`Fetched: ${url}\n`);
|
|
530
|
+
} else {
|
|
531
|
+
process.stderr.write(`Error fetching: ${url}\n`);
|
|
532
|
+
}
|
|
533
|
+
fetched.add(url.toString());
|
|
534
|
+
} else {
|
|
535
|
+
process.stderr.write(`Path not inside output directory, skipping: ${dest}\n`);
|
|
536
|
+
}
|
|
481
537
|
}
|
|
482
538
|
}
|
|
483
539
|
} else {
|
|
484
|
-
if (fetched.has(
|
|
485
|
-
|
|
540
|
+
if (fetched.has(item.url)) {
|
|
541
|
+
process.stderr.write(`Already fetched, skipping: ${item.url}\n`);
|
|
486
542
|
} else {
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
543
|
+
if (inside(outdir, dest)) {
|
|
544
|
+
process.stderr.write(`Fetching: ${item.url}\n`);
|
|
545
|
+
const res = await fetch(item.url);
|
|
546
|
+
if (res.ok) {
|
|
547
|
+
await mkdir(dirname(dest), { recursive: true });
|
|
548
|
+
await writeFile(dest, res.body);
|
|
549
|
+
process.stderr.write(`Fetched: ${item.url}\n`);
|
|
550
|
+
} else {
|
|
551
|
+
process.stderr.write(`Error fetching: ${item.url}\n`);
|
|
552
|
+
}
|
|
553
|
+
fetched.add(item.url);
|
|
554
|
+
} else {
|
|
555
|
+
process.stderr.write(`Path not inside output directory, skipping: ${dest}\n`);
|
|
556
|
+
}
|
|
495
557
|
}
|
|
496
558
|
}
|
|
497
559
|
|
|
@@ -499,14 +561,14 @@ if (command === 'download') {
|
|
|
499
561
|
}
|
|
500
562
|
|
|
501
563
|
function printHelp() {
|
|
502
|
-
|
|
564
|
+
process.stdout.write(`wordpress-to-kirby:
|
|
503
565
|
|
|
504
566
|
transform a WordPress XML export into a directory of Kirby CMS files.
|
|
505
567
|
|
|
506
568
|
Usage:
|
|
507
569
|
|
|
508
|
-
wordpress-to-kirby generate [options] <
|
|
509
|
-
wordpress-to-kirby download [options] <
|
|
570
|
+
wordpress-to-kirby generate [options] <input-file>
|
|
571
|
+
wordpress-to-kirby download [options] <input-file>
|
|
510
572
|
|
|
511
573
|
Commands available:
|
|
512
574
|
|
|
@@ -516,7 +578,9 @@ Commands available:
|
|
|
516
578
|
|
|
517
579
|
download
|
|
518
580
|
|
|
519
|
-
Download attachments
|
|
581
|
+
Download attachments from the input file, which can be either
|
|
582
|
+
a WordPress XML export file or a JSON metadata file produced
|
|
583
|
+
by the 'generate' command.
|
|
520
584
|
|
|
521
585
|
Options available:
|
|
522
586
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wordpress-to-kirby",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "A CLI to convert WordPress XML to Kirby CMS files",
|
|
5
5
|
"homepage": "https://github.com/tocmai/wordpress-to-kirby#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -22,5 +22,8 @@
|
|
|
22
22
|
"js-yaml": "^5.5.0",
|
|
23
23
|
"jsdom": "^31.0.1",
|
|
24
24
|
"php-serialize": "^5.1.3"
|
|
25
|
+
},
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=22"
|
|
25
28
|
}
|
|
26
29
|
}
|
package/src/inside.js
ADDED
package/src/slurp.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import test from 'node:test';
|
|
2
|
+
import { equal } from 'node:assert';
|
|
3
|
+
|
|
4
|
+
import inside from '../src/inside.js';
|
|
5
|
+
|
|
6
|
+
test('inside()', () => {
|
|
7
|
+
equal(inside('a', ''), false);
|
|
8
|
+
equal(inside('a', 'a/.'), false);
|
|
9
|
+
equal(inside('a', 'a/b'), true);
|
|
10
|
+
equal(inside('a', 'a/../a/b'), true);
|
|
11
|
+
});
|