wordpress-to-kirby 0.1.0 → 0.3.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.
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" width="401.42" height="207.25" viewBox="0 0 401.42 207.25"><path d="M98.43,5.2C44.07,5.2,0,49.26,0,103.63s44.07,98.43,98.43,98.43,98.43-44.07,98.43-98.43S152.79,5.2,98.43,5.2ZM98.43,187.06c-46,0-83.43-37.43-83.43-83.43S52.43,20.2,98.43,20.2s83.43,37.43,83.43,83.43-37.43,83.43-83.43,83.43ZM311.67,0l-89.74,51.81v103.63l89.74,51.81,89.74-51.81V51.81L311.67,0ZM386.42,146.78l-74.74,43.15-74.74-43.15V60.47l74.74-43.15,74.74,43.15v86.31ZM339.73,117.54l15.64-10.65v35.93l-15.64-9.09h-57.78l-15.3,9.65v-35.49l15.3,9.65h57.78ZM310.64,111.91l-44-24.64v-18.96c2.15,1.7,4.82,3.94,8.04,6.71,3.22,2.77,6.65,5.82,10.31,9.15,3.66,3.33,7.28,6.75,10.87,10.26,2.72,2.67,5.26,5.26,7.62,7.77,8.64-5.37,16.97-11.24,24.98-17.64,8.47-6.76,16.62-13.66,24.45-20.68.81,1.63,1.65,3.31,2.5,5.05.85,1.74,1.27,3.75,1.27,6.04,0,3.55-1.02,6.99-3.05,10.31-2.03,3.33-4.97,6.29-8.82,8.87l-31.16,20.85-3.03-3.08ZM158.26,68.75l-8.87,15.42-28.39,73.3h-3.77l-19.53-49.53-17.07,49.53h-3.77l-28.83-73.42-9.43-15.3h27.95l.22,15.3,14.88,39.99,15.15-18.39-8.52-21.6-7.32-15.3h20.52l21.53,58.61,13.18-43.3-2.33-15.3h24.4Z"/></svg>
@@ -0,0 +1,13 @@
1
+ # Contributing
2
+
3
+ We welcome contributions from the community. Please open an issue before committing significant work.
4
+
5
+ All contributions to this project, in code or in prose, must be human-authored.
6
+
7
+ ## Local development
8
+
9
+ Running tests:
10
+
11
+ ```bash
12
+ npm run test
13
+ ```
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Dan Burzo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,24 +1,174 @@
1
1
  # wordpress-to-kirby
2
2
 
3
+ <img src="./.github/wordpress-to-kirby.svg" height='60'>
4
+
5
+ <a href="https://www.npmjs.org/package/wordpress-to-kirby"><img src="https://img.shields.io/npm/v/wordpress-to-kirby.svg?style=flat-square&labelColor=EEEEEE&color=black" alt="npm version"></a>
6
+
3
7
  A Node.js-based command-line utility to convert a WordPress XML export to a directory of Kirby CMS files.
4
8
 
5
9
  ## Getting started
6
10
 
7
- Install from npm:
11
+ Install [`wordpress-to-kirby`](https://npmjs.com/package/wordpress-to-kirby/) from npm:
8
12
 
9
13
  ```bash
10
14
  npm install wordpress-to-kirby
11
15
  ```
12
16
 
13
- Or run with npx without installing:
17
+ Or run it with `npx` without installing:
14
18
 
15
19
  ```bash
16
20
  npx wordpress-to-kirby
17
21
  ```
18
22
 
23
+ Use `wordpress-to-kirby --help` for information on usage and available commands and options.
24
+
25
+ ## Commands
26
+
27
+ ### `wordpress-to-kirby generate`
28
+
29
+ Generate a Kirby CMS file structure from the provided XML file. Usage:
30
+
31
+ ```bash
32
+ wordpress-to-kirby generate [options] <wordpress-xml-file>
33
+ ```
34
+
35
+ ### `wordpress-to-kirby download`
36
+
37
+ Download the attachments found in the provided XML file. Usage:
38
+
39
+ ```bash
40
+ wordpress-to-kirby download [options] <wordpress-xml-file>
41
+ ```
42
+
19
43
  ## Options
20
44
 
21
- Use `wordpress-to-kirby --help` for a list of options.
45
+ ### `-h, --help`
46
+
47
+ Prints out this help information.
48
+
49
+ ```bash
50
+ wordpress-to-kirby --help
51
+ ```
52
+
53
+ ### `-v, --version`
54
+
55
+ Shows the program version.
56
+
57
+ ```bash
58
+ wordpress-to-kirby --version
59
+ ```
60
+
61
+ ### `-o, --outdir=<path>`
62
+
63
+ Root directory to which to write the output, relative to the current working directory. Default: `wordpress-to-kirby`.
64
+
65
+ ```bash
66
+ wordpress-to-kirby generate my-blog.xml -o my-blog
67
+ ```
68
+
69
+ ### `--contentdir=<path>`
70
+
71
+ For the `download` command, point to the location of a `wp-content` directory from which to download attachments.
72
+
73
+ The path can be:
74
+
75
+ * an absolute or relative local path;
76
+ * a fully-qualified URL, including the `wp-content` segment, e.g. `https://mywebsite.local/wp-content/`
77
+
78
+ ```bash
79
+ wordpress-to-kirby download my-blog.xml -o my-blog --contentdir=path/to/downloaded-wp-content
80
+ ```
81
+
82
+ ### `--parse=<rules>`
83
+
84
+ Instructions to parse some fields, specified as `<key>=<parser>,<key>=<parser>,…`.
85
+
86
+ The `--parse` option can be used multiple times.
87
+
88
+ Supported parsers:
89
+
90
+ * `php`: deserialize PHP object;
91
+ * `yaml`: parse as YAML;
92
+ * `json`: parse as JSON;
93
+ * `html`: parse as HTML.
94
+
95
+ ```bash
96
+ wordpress-to-kirby generate my-blog.xml --parse=content=html
97
+ ```
98
+
99
+ ### `--serialize=<rules>`
100
+
101
+ Instructions to serialize some fields, specified as `<key>=<serializer>,<key>=<serializer>,…`.
102
+
103
+ The `--serialize` option can be used multiple times.
104
+
105
+ Supported serializers:
106
+
107
+ * `php`: serialize as PHP object;
108
+ * `yaml`: serialize as YAML;
109
+ * `json`: serialize as JSON;
110
+ * `html`: serialize as HTML.
111
+
112
+ ```bash
113
+ wordpress-to-kirby generate my-blog.xml --serialize=content=html
114
+ ```
115
+
116
+ ### `--exclude-fields=<fields>`
117
+
118
+ Exclude certain fields from the output, specified as a comma-separated list of fields.
119
+
120
+ The `--exclude-fields` option can be used multiple times.
121
+
122
+ ```bash
123
+ wordpress-to-kirby generate my-blog.xml --exclude-fields=guid_isPermalink,comment_status,ping_status
124
+ ```
125
+
126
+ ### `--include-fields=<fields>`
127
+
128
+ Only include certain fields from the output, specified as a comma-separated list of fields. When `--include-fields` is specified, the `--exclude-fields` option is ignored.
129
+
130
+ The `--include-fields` option can be used multiple times.
131
+
132
+ ```bash
133
+ wordpress-to-kirby generate my-blog.xml --include-fields=title,content,uuid
134
+ ```
135
+
136
+ ### `--exclude-types=<types>`
137
+
138
+ Exclude certain post types from the output, specified as a comma-separated list of post types.
139
+
140
+ The `--exclude-types` option can be used multiple times.
141
+
142
+ ```bash
143
+ wordpress-to-kirby generate my-blog.xml --exclude-types=nav_menu_item
144
+ ```
145
+
146
+ ### `--include-types=<types>`
147
+
148
+ Only include certain post types from the output, specified as a comma-separated list of post types. When `--include-types` is specified, the `--exclude-types` option is ignored.
149
+
150
+ The `--include-types` option can be used multiple times.
151
+
152
+ ```bash
153
+ wordpress-to-kirby generate my-blog.xml --include-types=post,page,film,venue
154
+ ```
155
+
156
+ ### `--only-metadata`
157
+
158
+ For the `generate` command, only generate the `metadata.json` file.
159
+
160
+ ## Keys extracted from the XML
161
+
162
+ The following keys are extracted from the top level of the WordPress XML export:
163
+
164
+ `title`, `link`, `pubDate`, `creator`, `guid`, `guid_isPermaLink`, `description`, `content`, `excerpt`, `post_id`, `post_date`, `post_date_gmt`, `post_modified`, `post_modified_gmt`, `comment_status`, `ping_status`, `post_name`, `status`, `post_parent`, `menu_order`, `post_type`, `post_password`, `is_sticky`, `attachment_url` (only populated for the `attachment` post type).
165
+
166
+ Fields from `<wp:postmeta>` are included in the output with the `postmeta*` prefix.
167
+
168
+ ## Keys generated from scratch
169
+
170
+ * `uuid: <string>`, containing 16 random alphanumeric characters.
171
+ * `template: 'attachment'` is added to attachments.
22
172
 
23
173
  ## Prior art
24
174
 
package/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env node
2
- import { readFile, writeFile, mkdir } from 'node:fs/promises';
3
- import { join, dirname, extname, relative } from 'node:path';
2
+ import { readFile, writeFile, copyFile, mkdir } from 'node:fs/promises';
3
+ import { join, dirname, extname, relative, sep } from 'node:path';
4
+ import { parseArgs } from 'node:util';
4
5
 
5
- import opsh from 'opsh';
6
6
  import { JSDOM } from 'jsdom';
7
7
  import { load, dump } from 'js-yaml';
8
8
  import { serialize, unserialize } from 'php-serialize';
@@ -16,17 +16,61 @@ async function getPackage() {
16
16
  );
17
17
  }
18
18
 
19
- const { operands, options } = opsh(
20
- process.argv.slice(2),
21
- ['help', 'h', 'version', 'v']
22
- );
19
+ const { positionals: operands, values: options } = parseArgs({
20
+ options: {
21
+ help: {
22
+ type: 'boolean',
23
+ short: 'h'
24
+ },
25
+ version: {
26
+ type: 'boolean',
27
+ short: 'v'
28
+ },
29
+ parse: {
30
+ type: 'string',
31
+ multiple: true
32
+ },
33
+ stringify: {
34
+ type: 'string',
35
+ multiple: true
36
+ },
37
+ outdir: {
38
+ type: 'string',
39
+ short: 'o',
40
+ default: 'wordpress-to-kirby'
41
+ },
42
+ contentdir: {
43
+ type: 'string'
44
+ },
45
+ 'include-fields': {
46
+ type: 'string',
47
+ multiple: true
48
+ },
49
+ 'exclude-fields': {
50
+ type: 'string',
51
+ multiple: true
52
+ },
53
+ 'include-types': {
54
+ type: 'string',
55
+ multiple: true
56
+ },
57
+ 'exclude-types': {
58
+ type: 'string',
59
+ multiple: true
60
+ },
61
+ 'only-metadata': {
62
+ type: 'boolean'
63
+ }
64
+ },
65
+ allowPositionals: true
66
+ });
23
67
 
24
- if (options.h || options.help) {
25
- await printHelp();
68
+ if (options.help) {
69
+ printHelp();
26
70
  process.exit();
27
71
  }
28
72
 
29
- if (options.v || options.version) {
73
+ if (options.version) {
30
74
  const pkg = await getPackage();
31
75
  console.log(pkg.version);
32
76
  process.exit();
@@ -51,8 +95,11 @@ const serializers = {
51
95
  'postmeta_wp_attachment_metadata': 'json'
52
96
  };
53
97
 
54
- function commasep(val) {
55
- return (val || '').split(/\s*,\s*/).filter(Boolean);
98
+ function commasep(arr = []) {
99
+ return arr
100
+ .map(val => (val || '').split(/\s*,\s*/))
101
+ .flat()
102
+ .filter(Boolean);
56
103
  }
57
104
 
58
105
  commasep(options.parse).forEach(pair => {
@@ -75,28 +122,38 @@ commasep(options.serialize).forEach(pair => {
75
122
  serializers[key] = val;
76
123
  });
77
124
 
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']));
125
+ const fields_to_include = new Set(commasep(options['include-fields']));
126
+ const fields_to_exclude = new Set(options['include-fields'] ? [] : commasep(options['exclude-fields']));
80
127
 
81
128
  const types_to_include = new Set(commasep(options['include-types']));
82
129
  const types_to_exclude = new Set(options['include-types'] ? [] : commasep(options['exclude-types']));
83
130
 
84
131
  function process_item(item) {
85
132
  let entries = Object.entries(item);
86
- if (keys_to_include.size) {
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]));
133
+ if (fields_to_include.size) {
134
+ entries = entries.filter(entry => fields_to_include.has(entry[0]));
135
+ } else if (fields_to_exclude.size) {
136
+ entries = entries.filter(entry => !fields_to_exclude.has(entry[0]));
90
137
  }
91
138
  return Object.fromEntries(
92
139
  entries.map(entry => [
93
140
  entry[0],
94
- serialize_value(entry[0], entry[1])
141
+ serialize_value(entry[0], entry[1], item.post_id)
95
142
  ])
96
143
  );
97
144
  }
98
145
 
99
- function parse_value(key, value) {
146
+ function abbr(value, len = 64) {
147
+ if (typeof value !== 'string') {
148
+ return value;
149
+ }
150
+ if (value.length <= len) {
151
+ return value;
152
+ }
153
+ return value.slice(0, len) + '…';
154
+ }
155
+
156
+ function parse_value(key, value, id) {
100
157
  if (!value) {
101
158
  return value;
102
159
  }
@@ -105,21 +162,21 @@ function parse_value(key, value) {
105
162
  try {
106
163
  return unserialize(value);
107
164
  } catch(err) {
108
- console.error(`Warn: Can’t serialize PHP value, skipping: ${value}`);
165
+ console.error(`Warn: Can’t parse PHP value in post ${id}, skipping: ${abbr(value)}`);
109
166
  return value;
110
167
  }
111
168
  case 'yaml':
112
169
  try {
113
170
  return load(value);
114
171
  } catch (err) {
115
- console.error(`Warn: Can’t serialize YAML value, skipping: ${value}`);
172
+ console.error(`Warn: Can’t parse YAML value in post ${id}, skipping: ${abbr(value)}`);
116
173
  return value;
117
174
  }
118
175
  case 'json':
119
176
  try {
120
177
  return JSON.parse(value);
121
178
  } catch(err) {
122
- console.error(`Warn: Can’t serialize JSON value, skipping: ${value}`);
179
+ console.error(`Warn: Can’t parse JSON value in post ${id}, skipping: ${abbr(value)}`);
123
180
  return value;
124
181
  }
125
182
  case 'html':
@@ -128,7 +185,7 @@ function parse_value(key, value) {
128
185
  `<!doctype html><html><head></head><body>${value}</body></html>`
129
186
  ).window.document;
130
187
  } catch(err) {
131
- console.error(`Warn: Can’t serialize JSON value, skipping: ${value}`);
188
+ console.error(`Warn: Can’t parse HTML value in post ${id}, skipping: ${abbr(value)}`);
132
189
  return value;
133
190
  }
134
191
  default:
@@ -136,7 +193,7 @@ function parse_value(key, value) {
136
193
  }
137
194
  }
138
195
 
139
- function serialize_value(key, value) {
196
+ function serialize_value(key, value, id) {
140
197
  if (!value) {
141
198
  return value;
142
199
  }
@@ -145,28 +202,28 @@ function serialize_value(key, value) {
145
202
  try {
146
203
  return serialize(value);
147
204
  } catch(err) {
148
- console.error(`Warn: Can’t stringify PHP value, skipping: ${value}`);
205
+ console.error(`Warn: Can’t serialize PHP value in post ${id}, skipping: ${abbr(value)}`);
149
206
  return value;
150
207
  }
151
208
  case 'yaml':
152
209
  try {
153
210
  return dump(value);
154
211
  } catch (err) {
155
- console.error(`Warn: Can’t stringify YAML value, skipping: ${value}`);
212
+ console.error(`Warn: Can’t serialize YAML value in post ${id}, skipping: ${abbr(value)}`);
156
213
  return value;
157
214
  }
158
215
  case 'json':
159
216
  try {
160
217
  return JSON.stringify(value);
161
218
  } catch(err) {
162
- console.error(`Warn: Can’t stringify JSON value, skipping: ${value}`);
219
+ console.error(`Warn: Can’t serialize JSON value in post ${id}, skipping: ${abbr(value)}`);
163
220
  return value;
164
221
  }
165
222
  case 'html':
166
223
  try {
167
224
  return value.body.innerHTML;
168
225
  } catch(err) {
169
- console.error(`Warn: Can’t stringify HTML value, skipping: ${value}`);
226
+ console.error(`Warn: Can’t serialize HTML value in post ${id}, skipping: ${abbr(value)}`);
170
227
  return value;
171
228
  }
172
229
  default:
@@ -201,42 +258,48 @@ function extract(item) {
201
258
  }
202
259
  return (el ?? item).querySelector(sel);
203
260
  }
261
+
262
+ const post_id = parse_value('post_id', select('post_id', 'wp').textContent);
263
+
264
+ const post_type = parse_value('post_type', select('post_type', 'wp').textContent, post_id);
265
+
204
266
  return {
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),
267
+ post_id,
268
+ post_type,
269
+ title: parse_value('title', select('title').textContent, post_id),
270
+ link: parse_value('link', select('link').textContent, post_id),
271
+ pubDate: parse_value('pubDate', select('pubDate').textContent, post_id),
272
+ creator: parse_value('creator', select('creator', 'dc').textContent, post_id),
273
+ guid: parse_value('guid', select('guid').textContent, post_id),
274
+ guid_isPermaLink: parse_value('guid_isPermaLink', select('guid').getAttribute('isPermaLink'), post_id),
275
+ description: parse_value('description', select('description').textContent, post_id),
276
+ content: parse_value('content', select('encoded', 'content').textContent, post_id),
277
+ excerpt: parse_value('excerpt', select('encoded', 'excerpt').textContent, post_id),
278
+ post_date: parse_value('post_date', select('post_date', 'wp').textContent, post_id),
279
+ post_date_gmt: parse_value('post_date_gmt', select('post_date_gmt', 'wp').textContent, post_id),
280
+ post_modified: parse_value('post_modified', select('post_modified', 'wp').textContent, post_id),
281
+ post_modified_gmt: parse_value('post_modified_gmt', select('post_modified_gmt', 'wp').textContent, post_id),
282
+ comment_status: parse_value('comment_status', select('comment_status', 'wp').textContent, post_id),
283
+ ping_status: parse_value('ping_status', select('ping_status', 'wp').textContent, post_id),
284
+ post_name: parse_value('post_name', select('post_name', 'wp').textContent, post_id),
285
+ status: parse_value('status', select('status', 'wp').textContent, post_id),
286
+ post_parent: parse_value('post_parent', select('post_parent', 'wp').textContent, post_id),
287
+ menu_order: parse_value('menu_order', select('menu_order', 'wp').textContent, post_id),
288
+ post_password: parse_value('post_password', select('post_password', 'wp').textContent, post_id),
289
+ attachment_url: parse_value('attachment_url', select('attachment_url', 'wp')?.textContent, post_id),
290
+ is_sticky: parse_value('is_sticky', select('is_sticky', 'wp').textContent, post_id),
229
291
  ...select('postmeta', 'wp', true).reduce((acc, it) => {
230
292
  const key = select('meta_key', 'wp', false, it).textContent;
231
293
  const value = select('meta_value', 'wp', false, it).textContent;
232
- acc['postmeta' + key] = parse_value('postmeta' + key, value);
294
+ acc['postmeta' + key] = parse_value('postmeta' + key, value, post_id);
233
295
  return acc;
234
296
  }, {}),
235
- uuid: uuid()
297
+ uuid: uuid(),
298
+ template: post_type === 'attachment' ? 'attachment' : undefined
236
299
  }
237
300
  }
238
301
 
239
- const outdir = options.o || options.outdir || 'wordpress-to-kirby';
302
+ const outdir = options.outdir;
240
303
 
241
304
  function outpath(item) {
242
305
  let parts = (new URL(item.link)).pathname.split('/').filter(Boolean);
@@ -251,52 +314,113 @@ function outpath(item) {
251
314
  }
252
315
 
253
316
  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
- };
317
+
318
+ const processed_items = items.map(process_item);
259
319
 
260
320
  const metadata = {
261
321
  output: [],
262
322
  types: {}
263
323
  };
264
324
 
265
- items.forEach(item => {
325
+ processed_items.forEach((item, idx) => {
326
+ /*
327
+ Some of these fields may have been omitted from the object,
328
+ but we still need them for metadata purposes.
329
+ */
330
+ const full_item = items[idx];
266
331
  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))
332
+ post_id: +full_item.post_id,
333
+ post_type: full_item.post_type,
334
+ url: full_item.attachment_url ?? full_item.link,
335
+ uuid: full_item.uuid,
336
+ path: relative(outdir, outpath(full_item))
272
337
  });
273
- metadata.types[item.post_type] = (metadata.types[item.post_type] || [])
274
- .concat(Object.keys(item));
338
+ metadata.types[full_item.post_type] = [].concat(
339
+ metadata.types[full_item.post_type],
340
+ Object.keys(item)
341
+ );
275
342
  });
276
343
 
277
- const blueprints_path = join(outdir, 'site', 'blueprints');
278
- await mkdir(join(blueprints_path, 'pages'), { recursive: true });
279
-
280
344
  Object.keys(metadata.types).forEach(key => {
281
345
  metadata.types[key] = Array.from(new Set(metadata.types[key])).sort();
346
+ });
347
+
348
+ await mkdir(outdir, { recursive: true });
349
+ await writeFile(join(outdir, 'metadata.json'), JSON.stringify(metadata, null, '\t'));
350
+
351
+ if (!options['only-metadata']) {
352
+
353
+ for (let item of processed_items) {
354
+ const destination = outpath(item);
355
+ await mkdir(dirname(destination), { recursive: true });
356
+ await writeFile(destination, serialize_item(item));
357
+ };
358
+
359
+ const blueprints_path = join(outdir, 'site', 'blueprints');
360
+ await mkdir(join(blueprints_path, 'pages'), { recursive: true });
361
+ await mkdir(join(blueprints_path, 'files'), { recursive: true });
362
+
282
363
  // 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: {
364
+ Object.keys(metadata.types).forEach(key => {
365
+ const blueprint_content = metadata.types[key].reduce((acc, curr) => {
366
+ acc[curr] = {
367
+ type: 'text'
368
+ };
369
+ return acc;
370
+ }, {});
371
+
372
+ if (key === 'attachment') {
373
+ const blueprint_dest = join(
374
+ blueprints_path,
375
+ 'files',
376
+ key + '.yml'
377
+ );
378
+
379
+ writeFile(blueprint_dest, dump({
380
+ fields: blueprint_content
381
+ }));
382
+
383
+ } else {
384
+ const blueprint_dest = join(
385
+ blueprints_path,
386
+ 'pages',
387
+ key + '.yml'
388
+ );
389
+
390
+ writeFile(blueprint_dest, dump({
391
+ tabs: {
294
392
  fields: {
295
- type: 'fields',
296
- fields: blueprint_content
297
- }
393
+ label: 'Fields',
394
+ sections: {
395
+ fields: {
396
+ type: 'fields',
397
+ fields: blueprint_content
398
+ }
399
+ }
400
+ },
401
+ pages: {
402
+ label: 'Pages',
403
+ sections: {
404
+ pages: {
405
+ type: 'pages'
406
+ }
407
+ }
408
+ },
409
+ files: {
410
+ label: 'Files',
411
+ sections: {
412
+ files: {
413
+ type: 'files'
414
+ }
415
+ }
416
+ }
298
417
  }
299
- },
418
+ }));
419
+ }
420
+ });
421
+
422
+ const default_blueprint = {
423
+ tabs: {
300
424
  pages: {
301
425
  label: 'Pages',
302
426
  sections: {
@@ -314,102 +438,169 @@ if (command === 'generate') {
314
438
  }
315
439
  }
316
440
  }
317
- }));
318
- });
319
-
441
+ };
442
+ await writeFile(join(blueprints_path, 'pages', 'default.yml'), dump(default_blueprint));
443
+ await writeFile(join(blueprints_path, 'site.yml'), dump(default_blueprint));
444
+ }
445
+ }
320
446
 
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
- }
447
+ if (command === 'download') {
448
+
449
+ const contentdir = options.contentdir ?
450
+ (URL.canParse(options.contentdir) ?
451
+ new URL(options.contentdir) :
452
+ options.contentdir
453
+ ) : null;
454
+
455
+ const fetched = new Set();
456
+
457
+ for (let item of items.filter(it => it.post_type === 'attachment')) {
458
+ const attachment = process_item(item);
459
+ const dest = outpath(attachment).replace(/\.txt$/, '');
460
+ if (contentdir) {
461
+ const pathname = new URL(attachment.attachment_url).pathname
462
+ .split('/')
463
+ .filter(p => p && p !== 'wp-content')
464
+ .join(sep);
465
+ if (typeof contentdir === 'string') {
466
+ await mkdir(dirname(dest), { recursive: true });
467
+ await copyFile(join(contentdir, pathname), dest);
468
+ } else {
469
+ const url = new URL(pathname, contentdir);
470
+ if (fetched.has(url)) {
471
+ console.log(`Already fetched, skipping: ${url}`);
472
+ } else {
473
+ console.log(`Fetching: ${url}`);
474
+ await mkdir(dirname(dest), { recursive: true });
475
+ await writeFile(
476
+ dest,
477
+ (await fetch(url)).body
478
+ );
479
+ console.log(`Fetched: ${url}`);
480
+ fetched.add(url);
337
481
  }
338
482
  }
483
+ } else {
484
+ if (fetched.has(attachment.attachment_url)) {
485
+ console.log(`Already fetched, skipping: ${attachment.attachment_url}`);
486
+ } else {
487
+ console.log(`Fetching: ${attachment.attachment_url}`);
488
+ await mkdir(dirname(dest), { recursive: true });
489
+ await writeFile(
490
+ dest,
491
+ (await fetch(attachment.attachment_url)).body
492
+ );
493
+ console.log(`Fetched: ${attachment.attachment_url}`);
494
+ fetched.add(attachment.attachment_url);
495
+ }
339
496
  }
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);
497
+
498
+ }
348
499
  }
349
500
 
350
501
  function printHelp() {
351
502
  console.log(`wordpress-to-kirby:
352
503
 
353
- transform a WordPress XML export
354
- into a directory of Kirby CMS files.
504
+ transform a WordPress XML export into a directory of Kirby CMS files.
355
505
 
356
506
  Usage:
357
507
 
358
- wordpress-to-kirby <command> <xml-file> <options>
508
+ wordpress-to-kirby generate [options] <wordpress-xml-file>
509
+ wordpress-to-kirby download [options] <wordpress-xml-file>
359
510
 
360
- Commands:
511
+ Commands available:
361
512
 
362
513
  generate
514
+
363
515
  Generate a Kirby CMS file structure from the provided XML file.
364
516
 
365
517
  download
366
- Download attachments. Not implemented.
518
+
519
+ Download attachments.
367
520
 
368
- Options:
521
+ Options available:
369
522
 
370
523
  -h, --help
371
- Print out this help information.
524
+
525
+ Prints out this help information.
372
526
 
373
527
  -v, --version
374
- Show the program version.
375
528
 
376
- -o, --outdir
377
- Root directory to output to, relative to CWD.
529
+ Shows the program version.
530
+
531
+ -o <path>, --outdir=<path>
532
+
533
+ Root directory to which to write the output,
534
+ relative to the current working directory.
535
+
378
536
  Default: 'wordpress-to-kirby'
379
537
 
538
+ --contentdir=<path>
539
+
540
+ For the 'download' command, point to the location
541
+ of a 'wp-content' directory from which to download attachments.
542
+
543
+ The path can be:
544
+
545
+ * an absolute or relative local path;
546
+ * a fully-qualified URL, including the 'wp-content' segment,
547
+ e.g. https://mywebsite.local/wp-content/
548
+
380
549
  --parse=<rules>
381
- Instructions to parse some fields, in the form of:
382
- "<key>=<parser>,<key>=<parser>,…"
550
+
551
+ Instructions to parse some fields, specified as:
552
+ "<key>=<parser>,<key>=<parser>,…".
553
+
554
+ The option can be used multiple times.
555
+
383
556
  Supported parsers:
384
- php: deserialize PHP object.
385
- yaml: parse as YAML.
386
- json: parse as JSON.
387
- html: parse as HTML.
388
- Prefix post meta fields with "postmeta".
557
+ * php: deserialize PHP object.
558
+ * yaml: parse as YAML.
559
+ * json: parse as JSON.
560
+ * html: parse as HTML.
389
561
 
390
562
  --serialize=<rules>
391
- Instructions to serialize some fields, in the form of:
563
+
564
+ Instructions to serialize some fields, specified as:
392
565
  "<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".
399
566
 
400
- --exclude-keys=<keys>
401
- Exclude certain keys from the output. Comma-separated.
567
+ The option can be used multiple times.
568
+
569
+ Supported serializers:
570
+ * php: serialize as PHP object.
571
+ * yaml: serialize as YAML.
572
+ * json: serialize as JSON.
573
+ * html: serialize as HTML.
574
+
575
+ --exclude-fields=<fields>
402
576
 
403
- --include-keys=<keys>
404
- Only include certain keys from the output. Comma-separated.
405
- When '--include-keys' is specified, any '--exclude-keys' is ignored.
577
+ Exclude certain fields from the output, specified as a
578
+ comma-separated list of fields. Can be used multiple times.
579
+
580
+ --include-fields=<fields>
581
+
582
+ Only include certain fields from the output, specified as a
583
+ comma-separated list of fields. Can be used multiple times.
584
+
585
+ When '--include-fields' is specified, the '--exclude-fields'
586
+ option is ignored.
406
587
 
407
588
  --exclude-types=<types>
408
- Exclude certain post types from the output. Comma-separated.
589
+
590
+ Exclude certain post types from the output, specified as a
591
+ comma-separated list of post types. Can be used multiple times.
409
592
 
410
593
  --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.
594
+
595
+ Only include certain post types from the output, specified as a
596
+ comma-separated list of post types. Can be used multiple times.
597
+
598
+ When '--include-types' is specified, the '--exclude-types'
599
+ option is ignored.
600
+
601
+ --only-metadata
602
+
603
+ For the 'generate' command, only generate the 'metadata.json' file.
413
604
 
414
605
  Keys extracted from the XML:
415
606
 
@@ -424,11 +615,12 @@ Keys extracted from the XML:
424
615
 
425
616
  Post metadata:
426
617
 
427
- Fields from <wp:postmeta> not starting with an underscore
428
- are included in the output with the 'postmeta*' prefix.
618
+ Fields from '<wp:postmeta>' are included in the output
619
+ with the 'postmeta*' prefix.
429
620
 
430
621
  Keys generated from scratch:
431
622
 
432
- uuid — sixteen random alphanumeric characters.
623
+ uuid — sixteen random alphanumeric characters.
624
+ template: 'attachment' — added to attachments only.
433
625
  `);
434
626
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wordpress-to-kirby",
3
- "version": "0.1.0",
3
+ "version": "0.3.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": {
@@ -21,7 +21,6 @@
21
21
  "dependencies": {
22
22
  "js-yaml": "^5.5.0",
23
23
  "jsdom": "^31.0.1",
24
- "opsh": "^1.1.0",
25
24
  "php-serialize": "^5.1.3"
26
25
  }
27
26
  }