wordpress-to-kirby 0.0.0-stage → 0.0.2
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 +24 -2
- package/index.js +300 -0
- package/package.json +25 -4
- package/src/kirby-txt.js +46 -0
- package/src/uuid.js +12 -0
- package/test/kirby-txt.test.js +16 -0
- package/test/uuid.test.js +8 -0
package/README.md
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
-
#
|
|
1
|
+
# wordpress-to-kirby
|
|
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
|
+
|
|
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
|
+
|
|
19
|
+
## Options
|
|
20
|
+
|
|
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
ADDED
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFile, writeFile, mkdir } from 'node:fs/promises';
|
|
3
|
+
import { join, dirname, relative } from 'node:path';
|
|
4
|
+
|
|
5
|
+
import opsh from 'opsh';
|
|
6
|
+
import { JSDOM } from 'jsdom';
|
|
7
|
+
import { load } from 'js-yaml';
|
|
8
|
+
import { serialize, unserialize } from 'php-serialize';
|
|
9
|
+
|
|
10
|
+
import { serialize as serialize_item } from './src/kirby-txt.js';
|
|
11
|
+
import { uuid } from './src/uuid.js';
|
|
12
|
+
|
|
13
|
+
async function getPackage() {
|
|
14
|
+
return JSON.parse(
|
|
15
|
+
await readFile(new URL('./package.json', import.meta.url))
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const { operands, options } = opsh(
|
|
20
|
+
process.argv.slice(2),
|
|
21
|
+
['help', 'h', 'version', 'v']
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
if (options.h || options.help) {
|
|
25
|
+
await printHelp();
|
|
26
|
+
process.exit();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (options.v || options.version) {
|
|
30
|
+
const pkg = await getPackage();
|
|
31
|
+
console.log(pkg.version);
|
|
32
|
+
process.exit();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (operands.length !== 1) {
|
|
36
|
+
console.error(`Exactly one operand is supported, ${operands.length} provided. Exiting.`);
|
|
37
|
+
process.exit(1);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const parsers = {};
|
|
41
|
+
const serializers = {};
|
|
42
|
+
|
|
43
|
+
function commasep(val) {
|
|
44
|
+
return (val || '').split(/\s*,\s*/).filter(Boolean);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
commasep(options.parse).forEach(pair => {
|
|
48
|
+
let [key, val] = pair.split('=');
|
|
49
|
+
key = key.trim();
|
|
50
|
+
val = val.trim();
|
|
51
|
+
if (!key || !val) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
parsers[key] = val;
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
commasep(options.serialize).forEach(pair => {
|
|
58
|
+
let [key, val] = pair.split('=');
|
|
59
|
+
key = key.trim();
|
|
60
|
+
val = val.trim();
|
|
61
|
+
if (!key || !val) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
serializers[key] = val;
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const keys_to_include = new Set(commasep(options.include));
|
|
68
|
+
const keys_to_exclude = new Set(options.include ? [] : commasep(options.exclude));
|
|
69
|
+
|
|
70
|
+
function process_item(item) {
|
|
71
|
+
let entries = Object.entries(item);
|
|
72
|
+
if (keys_to_include.size) {
|
|
73
|
+
entries = entries.filter(entry => keys_to_include.has(entry[0]));
|
|
74
|
+
} else if (keys_to_exclude.size) {
|
|
75
|
+
entries = entries.filter(entry => !keys_to_exclude.has(entry[0]));
|
|
76
|
+
}
|
|
77
|
+
return Object.fromEntries(
|
|
78
|
+
entries.map(entry => [
|
|
79
|
+
entry[0],
|
|
80
|
+
serialize_value(entry[0], entry[1])
|
|
81
|
+
])
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function parse_value(key, value) {
|
|
86
|
+
if (!value) {
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
switch(parsers[key]) {
|
|
90
|
+
case 'php':
|
|
91
|
+
try {
|
|
92
|
+
return unserialize(value);
|
|
93
|
+
} catch(err) {
|
|
94
|
+
console.error(`Can’t serialize PHP value, skipping: ${value}`);
|
|
95
|
+
return value;
|
|
96
|
+
}
|
|
97
|
+
case 'yaml':
|
|
98
|
+
try {
|
|
99
|
+
return load(value);
|
|
100
|
+
} catch (err) {
|
|
101
|
+
console.error(`Can’t serialize YAML value, skipping: ${value}`);
|
|
102
|
+
return value;
|
|
103
|
+
}
|
|
104
|
+
case 'json':
|
|
105
|
+
try {
|
|
106
|
+
return JSON.parse(value);
|
|
107
|
+
} catch(err) {
|
|
108
|
+
console.error(`Can’t serialize JSON value, skipping: ${value}`);
|
|
109
|
+
return value;
|
|
110
|
+
}
|
|
111
|
+
default:
|
|
112
|
+
return value;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function serialize_value(key, value) {
|
|
117
|
+
if (!value) {
|
|
118
|
+
return value;
|
|
119
|
+
}
|
|
120
|
+
switch(serializers[key]) {
|
|
121
|
+
case 'php':
|
|
122
|
+
try {
|
|
123
|
+
return serialize(value);
|
|
124
|
+
} catch(err) {
|
|
125
|
+
console.error(`Can’t stringify PHP value, skipping: ${value}`);
|
|
126
|
+
return value;
|
|
127
|
+
}
|
|
128
|
+
case 'yaml':
|
|
129
|
+
try {
|
|
130
|
+
return dump(value);
|
|
131
|
+
} catch (err) {
|
|
132
|
+
console.error(`Can’t stringify YAML value, skipping: ${value}`);
|
|
133
|
+
return value;
|
|
134
|
+
}
|
|
135
|
+
case 'json':
|
|
136
|
+
try {
|
|
137
|
+
return JSON.stringify(value);
|
|
138
|
+
} catch(err) {
|
|
139
|
+
console.error(`Can’t stringify JSON value, skipping: ${value}`);
|
|
140
|
+
return value;
|
|
141
|
+
}
|
|
142
|
+
default:
|
|
143
|
+
return value;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const document = new JSDOM(
|
|
148
|
+
await readFile(operands[0], 'utf8'),
|
|
149
|
+
{
|
|
150
|
+
contentType: 'application/xml'
|
|
151
|
+
}
|
|
152
|
+
).window.document;
|
|
153
|
+
|
|
154
|
+
const items = Array.from(document.querySelectorAll('item')).map(extract);
|
|
155
|
+
|
|
156
|
+
function extract(item) {
|
|
157
|
+
const select = (sel, ns, all = false, el) => {
|
|
158
|
+
if (ns) {
|
|
159
|
+
const candidates = Array.from((el ?? item).querySelectorAll(sel) ?? [])
|
|
160
|
+
.filter(it => it.nodeName === `${ns}:${sel}`);
|
|
161
|
+
return all ? candidates : candidates[0];
|
|
162
|
+
}
|
|
163
|
+
return (el ?? item).querySelector(sel);
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
title: select('title').textContent,
|
|
167
|
+
link: select('link').textContent,
|
|
168
|
+
pubDate: select('pubDate').textContent,
|
|
169
|
+
creator: select('creator', 'dc').textContent,
|
|
170
|
+
guid: select('guid').textContent,
|
|
171
|
+
guid_isPermaLink: select('guid').getAttribute('isPermaLink'),
|
|
172
|
+
description: select('description').textContent,
|
|
173
|
+
content: select('encoded', 'content').textContent,
|
|
174
|
+
excerpt: select('encoded', 'excerpt').textContent,
|
|
175
|
+
post_id: select('post_id', 'wp').textContent,
|
|
176
|
+
post_date: select('post_date', 'wp').textContent,
|
|
177
|
+
post_date_gmt: select('post_date_gmt', 'wp').textContent,
|
|
178
|
+
post_modified: select('post_modified', 'wp').textContent,
|
|
179
|
+
post_modified_gmt: select('post_modified_gmt', 'wp').textContent,
|
|
180
|
+
comment_status: select('comment_status', 'wp').textContent,
|
|
181
|
+
ping_status: select('ping_status', 'wp').textContent,
|
|
182
|
+
post_name: select('post_name', 'wp').textContent,
|
|
183
|
+
status: select('status', 'wp').textContent,
|
|
184
|
+
post_parent: select('post_parent', 'wp').textContent,
|
|
185
|
+
menu_order: select('menu_order', 'wp').textContent,
|
|
186
|
+
post_type: select('post_type', 'wp').textContent,
|
|
187
|
+
post_password: select('post_password', 'wp').textContent,
|
|
188
|
+
is_sticky: select('is_sticky', 'wp').textContent,
|
|
189
|
+
...select('postmeta', 'wp', true).reduce((acc, it) => {
|
|
190
|
+
const key = select('meta_key', 'wp', false, it).textContent;
|
|
191
|
+
const value = select('meta_value', 'wp', false, it).textContent;
|
|
192
|
+
if (!key.match(/^_/)) {
|
|
193
|
+
acc['postmeta_' + key] = parse_value('postmeta_' + key, value);
|
|
194
|
+
}
|
|
195
|
+
return acc;
|
|
196
|
+
}, {}),
|
|
197
|
+
uuid: uuid()
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const outdir = options.o || options.outdir || 'wordpress-to-kirby';
|
|
202
|
+
|
|
203
|
+
function outpath(item) {
|
|
204
|
+
let parts = (new URL(item.link)).pathname.split('/').filter(Boolean);
|
|
205
|
+
if (item.post_type === 'attachment') {
|
|
206
|
+
parts = ['attachments', ...parts];
|
|
207
|
+
}
|
|
208
|
+
if (!parts.length) {
|
|
209
|
+
parts = ['_unlinked', item.post_type, item.post_id, ...parts];
|
|
210
|
+
}
|
|
211
|
+
return join(outdir, ...parts, item.post_type + '.txt');
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
for await (let item of items) {
|
|
215
|
+
const destination = outpath(item);
|
|
216
|
+
await mkdir(dirname(destination), { recursive: true });
|
|
217
|
+
await writeFile(destination, serialize_item(process_item(item)));
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
const metadata = {
|
|
221
|
+
output: []
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
items.forEach(item => {
|
|
225
|
+
metadata.output.push({
|
|
226
|
+
post_id: +item.post_id,
|
|
227
|
+
uuid: item.uuid,
|
|
228
|
+
path: relative(outdir, outpath(item))
|
|
229
|
+
});
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
await writeFile(join(outdir, 'metadata.json'), JSON.stringify(metadata, null, '\t'));
|
|
233
|
+
|
|
234
|
+
function printHelp() {
|
|
235
|
+
console.log(`wordpress-to-kirby:
|
|
236
|
+
|
|
237
|
+
transform a WordPress XML export
|
|
238
|
+
into a directory of Kirby CMS files.
|
|
239
|
+
|
|
240
|
+
Usage:
|
|
241
|
+
|
|
242
|
+
wordpress-to-kirby <options> <xml-file>
|
|
243
|
+
|
|
244
|
+
Options:
|
|
245
|
+
|
|
246
|
+
-h, --help
|
|
247
|
+
Print out this help information.
|
|
248
|
+
|
|
249
|
+
-v, --version
|
|
250
|
+
Show the program version.
|
|
251
|
+
|
|
252
|
+
-o, --outdir
|
|
253
|
+
Root directory to output to, relative to CWD.
|
|
254
|
+
Default: 'wordpress-to-kirby'
|
|
255
|
+
|
|
256
|
+
--parse=<rules>
|
|
257
|
+
Instructions to parse some fields, in the form of:
|
|
258
|
+
"<key>=<parser>,<key>=<parser>,…"
|
|
259
|
+
Supported parsers:
|
|
260
|
+
php: deserialize PHP object.
|
|
261
|
+
yaml: parse as YAML.
|
|
262
|
+
json: parse as JSON.
|
|
263
|
+
Prefix post meta fields with "postmeta".
|
|
264
|
+
|
|
265
|
+
--serialize=<rules>
|
|
266
|
+
Instructions to serialize some fields, in the form of:
|
|
267
|
+
"<key>=<serializer>,<key>=<serializer>,…"
|
|
268
|
+
Supported parsers:
|
|
269
|
+
php: serialize as PHP object.
|
|
270
|
+
yaml: serialize as YAML.
|
|
271
|
+
json: serialize as JSON.
|
|
272
|
+
Prefix post meta fields with "postmeta".
|
|
273
|
+
|
|
274
|
+
--exclude=<keys>
|
|
275
|
+
Exclude certain keys from the output. Comma-separated.
|
|
276
|
+
|
|
277
|
+
--include=<keys>
|
|
278
|
+
Only include certain keys from the output. Comma-separated.
|
|
279
|
+
When '--include' is specified, any '--exclude' is ignored.
|
|
280
|
+
|
|
281
|
+
Keys extracted from the XML:
|
|
282
|
+
|
|
283
|
+
Top-level:
|
|
284
|
+
|
|
285
|
+
title, link, pubDate, creator, guid, guid_isPermaLink,
|
|
286
|
+
description, content, excerpt, post_id, post_date,
|
|
287
|
+
post_date_gmt, post_modified, post_modified_gmt,
|
|
288
|
+
comment_status, ping_status, post_name, status,
|
|
289
|
+
post_parent, menu_order, post_type, post_password, is_sticky
|
|
290
|
+
|
|
291
|
+
Post metadata:
|
|
292
|
+
|
|
293
|
+
Fields from <wp:postmeta> not starting with an underscore
|
|
294
|
+
are included in the output with the 'postmeta_*' prefix.
|
|
295
|
+
|
|
296
|
+
Keys generated from scratch:
|
|
297
|
+
|
|
298
|
+
uuid — sixteen random alphanumeric characters.
|
|
299
|
+
`);
|
|
300
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wordpress-to-kirby",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "A CLI to convert WordPress XML to Kirby CMS files",
|
|
5
|
+
"homepage": "https://github.com/tocmai/wordpress-to-kirby#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/tocmai/wordpress-to-kirby/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/tocmai/wordpress-to-kirby.git"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"test": "node --test 'test/**/*.test.js'"
|
|
15
|
+
},
|
|
16
|
+
"bin": {
|
|
17
|
+
"wordpress-to-kirby": "index.js"
|
|
18
|
+
},
|
|
19
|
+
"type": "module",
|
|
20
|
+
"main": "index.js",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"js-yaml": "^5.5.0",
|
|
23
|
+
"jsdom": "^31.0.1",
|
|
24
|
+
"opsh": "^1.1.0",
|
|
25
|
+
"php-serialize": "^5.1.3"
|
|
26
|
+
}
|
|
27
|
+
}
|
package/src/kirby-txt.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { load, dump } from 'js-yaml';
|
|
2
|
+
|
|
3
|
+
// Based on: https://github.com/getkirby/kirby/blob/main/src/Data/Txt.php
|
|
4
|
+
|
|
5
|
+
// https://danburzo.ro/snippets/slugify/
|
|
6
|
+
function slugify(str = '') {
|
|
7
|
+
return str
|
|
8
|
+
.trim()
|
|
9
|
+
.normalize('NFD')
|
|
10
|
+
.replace(/[\u0300-\u036f]/g, '')
|
|
11
|
+
.toLowerCase()
|
|
12
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
13
|
+
.replace(/(^\-)|(\-$)/g, '');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function cap(str = '') {
|
|
17
|
+
return str[0].toUpperCase() + str.slice(1);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function parse(str = '') {
|
|
21
|
+
const data = {};
|
|
22
|
+
str.split(/\n----\s*\n*/).forEach(field => {
|
|
23
|
+
let [_, key, value] = field.match(/\s*([^:]+?)\s*:([^:]*)/) ?? [];
|
|
24
|
+
if (!key) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
data[key
|
|
28
|
+
.toLowerCase()
|
|
29
|
+
.replace(/[ -]/g, '_')
|
|
30
|
+
] = value.trim().replace(/(\n|^)\\\\----/, '----');
|
|
31
|
+
});
|
|
32
|
+
return data;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function serialize(data = {}) {
|
|
36
|
+
return Object.entries(data)
|
|
37
|
+
.filter(entry => entry[0] && entry[1])
|
|
38
|
+
.map(entry => {
|
|
39
|
+
const value = (Array.isArray(entry[1]) ? dump(entry[1]) : entry[1] + '')
|
|
40
|
+
.replace(/(?:\n|^)----/g, '\\----')
|
|
41
|
+
.trim();
|
|
42
|
+
const key = cap(slugify(entry[0]));
|
|
43
|
+
return key + (value.indexOf('\n') > -1 ? ':\n\n' : ': ') + value;
|
|
44
|
+
})
|
|
45
|
+
.join("\n\n----\n\n");
|
|
46
|
+
}
|
package/src/uuid.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/* generate random UUIDs */
|
|
2
|
+
import { randomBytes } from 'node:crypto';
|
|
3
|
+
|
|
4
|
+
export function uuid(len = 16) {
|
|
5
|
+
let res = '';
|
|
6
|
+
while (res.length < len) {
|
|
7
|
+
res += randomBytes(len)
|
|
8
|
+
.toString('base64')
|
|
9
|
+
.replace(/[^a-z0-9]/gi, '');
|
|
10
|
+
}
|
|
11
|
+
return res.slice(0, len);
|
|
12
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import test from 'node:test';
|
|
2
|
+
import assert from 'node:assert';
|
|
3
|
+
|
|
4
|
+
import { parse, serialize } from '../src/kirby-txt.js';
|
|
5
|
+
|
|
6
|
+
test('parse()', () => {
|
|
7
|
+
assert.deepEqual(parse('Hello: world'), {
|
|
8
|
+
hello: 'world'
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test('serialize()', () => {
|
|
13
|
+
assert.deepEqual(serialize({
|
|
14
|
+
hello: 'worldwide\nweb'
|
|
15
|
+
}), 'Hello:\n\nworldwide\nweb');
|
|
16
|
+
});
|