@tryghost/mg-json 0.17.0 → 0.19.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.
|
@@ -1,18 +1,11 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
|
-
|
|
3
|
-
const baseJSON = `{
|
|
4
|
-
"meta": {
|
|
5
|
-
"exported_on": <%= timestamp %>,
|
|
6
|
-
"version":"<%= version %>"
|
|
7
|
-
},
|
|
8
|
-
"data": {}
|
|
9
|
-
}`;
|
|
10
|
-
const baseTemplate = _.template(baseJSON);
|
|
11
1
|
const defaultVersion = '2.0.0';
|
|
12
2
|
|
|
13
3
|
export default (version) => {
|
|
14
|
-
return
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
4
|
+
return {
|
|
5
|
+
meta: {
|
|
6
|
+
exported_on: Date.now(),
|
|
7
|
+
version: version || defaultVersion
|
|
8
|
+
},
|
|
9
|
+
data: {}
|
|
10
|
+
};
|
|
18
11
|
};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
1
|
import {slugify} from '@tryghost/string';
|
|
3
2
|
import {decode} from 'html-entities';
|
|
3
|
+
import {stringUtils} from '@tryghost/mg-utils';
|
|
4
|
+
|
|
5
|
+
const {startCase, kebabCase} = stringUtils;
|
|
4
6
|
/**
|
|
5
7
|
* Hydrate Ghost objects
|
|
6
8
|
* Extend object with the minimum data needed for an import to succeed
|
|
@@ -22,7 +24,7 @@ const fakeEmail = (nameSlug, domain) => `${nameSlug}@${domain}`;
|
|
|
22
24
|
const hydrateUser = (input, options) => {
|
|
23
25
|
// Handle the case where we have a slug but no name
|
|
24
26
|
if (!input.name && input.slug) {
|
|
25
|
-
input.name =
|
|
27
|
+
input.name = startCase(input.slug);
|
|
26
28
|
} else if (!input.name) {
|
|
27
29
|
// Else if there's no name or slug, we'll have to use a fake
|
|
28
30
|
input.name = fakeName;
|
|
@@ -35,7 +37,7 @@ const hydrateUser = (input, options) => {
|
|
|
35
37
|
|
|
36
38
|
// Handle the case where there is no email by generating one based on slug or name
|
|
37
39
|
if (!input.email) {
|
|
38
|
-
input.email = fakeEmail(input.slug ||
|
|
40
|
+
input.email = fakeEmail(input.slug || kebabCase(input.name), options?.email || fakeEmailDomain);
|
|
39
41
|
}
|
|
40
42
|
|
|
41
43
|
// @TODO: log some sort of warning for things like this?
|
|
@@ -94,7 +96,7 @@ const hydratePost = (input, options) => {
|
|
|
94
96
|
input.title = decode(input.title);
|
|
95
97
|
|
|
96
98
|
// @TODO: log some sort of warning for things like this?
|
|
97
|
-
if (!
|
|
99
|
+
if (!['published', 'draft', 'scheduled'].includes(input.status)) {
|
|
98
100
|
input.status = 'draft';
|
|
99
101
|
}
|
|
100
102
|
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
1
|
import ObjectID from 'bson-objectid';
|
|
3
2
|
import schema from '../utils/schema.js';
|
|
4
3
|
import hydrate from './hydrate.js';
|
|
@@ -36,7 +35,7 @@ const deduplicateSlugs = (obj, type) => {
|
|
|
36
35
|
slugs[type] = [];
|
|
37
36
|
}
|
|
38
37
|
|
|
39
|
-
if (
|
|
38
|
+
if (slugs[type].includes(obj.slug)) {
|
|
40
39
|
// @TODO: log some sort of warning for things like this?
|
|
41
40
|
const objectID = new ObjectID();
|
|
42
41
|
const maxBaseLength = GHOST_SLUG_MAX - DEDUP_SUFFIX_LENGTH;
|
|
@@ -58,7 +57,7 @@ const ensureValid = (resource, type, options) => {
|
|
|
58
57
|
obj = deduplicateSlugs(obj, type);
|
|
59
58
|
}
|
|
60
59
|
|
|
61
|
-
if (
|
|
60
|
+
if (type in hydrate) {
|
|
62
61
|
obj = hydrate[type](obj, options);
|
|
63
62
|
}
|
|
64
63
|
|
|
@@ -68,10 +67,10 @@ const ensureValid = (resource, type, options) => {
|
|
|
68
67
|
const normalizeKey = (key) => {
|
|
69
68
|
let outputKey = null;
|
|
70
69
|
|
|
71
|
-
if (
|
|
70
|
+
if (schema.RESOURCES.includes(key)) {
|
|
72
71
|
outputKey = key;
|
|
73
72
|
// If this key is singular, convert to plural form
|
|
74
|
-
} else if (
|
|
73
|
+
} else if (key in schema.RESOURCE_SINGULAR_TO_PLURAL) {
|
|
75
74
|
outputKey = schema.RESOURCE_SINGULAR_TO_PLURAL[key];
|
|
76
75
|
}
|
|
77
76
|
|
|
@@ -79,7 +78,7 @@ const normalizeKey = (key) => {
|
|
|
79
78
|
};
|
|
80
79
|
|
|
81
80
|
const normalizeValue = (value) => {
|
|
82
|
-
if (!
|
|
81
|
+
if (!Array.isArray(value)) {
|
|
83
82
|
value = [value];
|
|
84
83
|
}
|
|
85
84
|
|
|
@@ -91,7 +90,7 @@ const normalizeValue = (value) => {
|
|
|
91
90
|
* Iterate over each key and return only ones that we recognise
|
|
92
91
|
*/
|
|
93
92
|
export default (input, options) => {
|
|
94
|
-
return
|
|
93
|
+
return Object.entries(input).reduce((data, [inputKey, inputValue]) => {
|
|
95
94
|
let key = normalizeKey(inputKey);
|
|
96
95
|
let entries = normalizeValue(inputValue);
|
|
97
96
|
|
|
@@ -100,7 +99,7 @@ export default (input, options) => {
|
|
|
100
99
|
return data;
|
|
101
100
|
}
|
|
102
101
|
|
|
103
|
-
data[key] =
|
|
102
|
+
data[key] = entries.map(entry => ensureValid(entry, key, options));
|
|
104
103
|
|
|
105
104
|
return data;
|
|
106
105
|
}, {});
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
1
|
import ObjectID from 'bson-objectid';
|
|
3
2
|
import schema from '../utils/schema.js';
|
|
4
3
|
|
|
@@ -18,10 +17,10 @@ export default (json) => {
|
|
|
18
17
|
// Only use plural key when it's actually an array (e.g. authors: [a, b]).
|
|
19
18
|
// When single-author Co-Authors Plus posts set authors: undefined and author: <person>,
|
|
20
19
|
// we must not take this branch or we'd process no one and orphan the post.
|
|
21
|
-
if (
|
|
20
|
+
if (pluralForm in postData && Array.isArray(postData[pluralForm])) {
|
|
22
21
|
relations = postData[pluralForm];
|
|
23
22
|
delete postData[pluralForm];
|
|
24
|
-
} else if (
|
|
23
|
+
} else if (relation in postData) {
|
|
25
24
|
relations.push(postData[relation]);
|
|
26
25
|
delete postData[relation];
|
|
27
26
|
}
|
|
@@ -30,7 +29,7 @@ export default (json) => {
|
|
|
30
29
|
};
|
|
31
30
|
|
|
32
31
|
const findMatchingItem = (match, location) => {
|
|
33
|
-
if (!
|
|
32
|
+
if (!(location in json)) {
|
|
34
33
|
return;
|
|
35
34
|
}
|
|
36
35
|
const matchData = match.data || match;
|
|
@@ -43,7 +42,7 @@ export default (json) => {
|
|
|
43
42
|
return item;
|
|
44
43
|
} else if (itemData.name && matchData.name && itemData.name === matchData.name) {
|
|
45
44
|
return item;
|
|
46
|
-
} else if (
|
|
45
|
+
} else if ((item.url ?? match.url) != null && item.url === match.url) { // eslint-disable-line eqeqeq
|
|
47
46
|
return item;
|
|
48
47
|
}
|
|
49
48
|
});
|
|
@@ -54,19 +53,19 @@ export default (json) => {
|
|
|
54
53
|
if (!author) {
|
|
55
54
|
return author;
|
|
56
55
|
}
|
|
57
|
-
if (
|
|
56
|
+
if ('data' in author && typeof author.data === 'object' && author.data !== null) {
|
|
58
57
|
return author;
|
|
59
58
|
}
|
|
60
59
|
return {
|
|
61
60
|
url: author.url || author.slug || '',
|
|
62
|
-
data:
|
|
61
|
+
data: 'data' in author ? author.data : author
|
|
63
62
|
};
|
|
64
63
|
};
|
|
65
64
|
|
|
66
65
|
const processPostAuthors = (postData) => {
|
|
67
66
|
let postAuthors = findPostRelations(postData, 'author');
|
|
68
67
|
|
|
69
|
-
|
|
68
|
+
postAuthors.forEach((author) => {
|
|
70
69
|
author = normaliseAuthor(author);
|
|
71
70
|
if (!author || !(author.data || author)) {
|
|
72
71
|
return;
|
|
@@ -103,7 +102,7 @@ export default (json) => {
|
|
|
103
102
|
const processPostTags = (postData) => {
|
|
104
103
|
let postTags = findPostRelations(postData, 'tag');
|
|
105
104
|
|
|
106
|
-
|
|
105
|
+
postTags.forEach((postTag) => {
|
|
107
106
|
let tag = findMatchingItem(postTag, 'tags');
|
|
108
107
|
|
|
109
108
|
if (!tag) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mg-json",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/TryGhost/migrate.git",
|
|
@@ -24,8 +24,8 @@
|
|
|
24
24
|
"@tryghost/string": "0.3.1",
|
|
25
25
|
"bson-objectid": "2.0.4",
|
|
26
26
|
"html-entities": "2.6.0",
|
|
27
|
-
"
|
|
28
|
-
"
|
|
27
|
+
"node-email-verifier": "4.0.0",
|
|
28
|
+
"@tryghost/mg-utils": "0.8.0"
|
|
29
29
|
},
|
|
30
30
|
"scripts": {
|
|
31
31
|
"dev": "echo \"Implement me!\"",
|