@tryghost/mg-json 0.22.1 → 0.22.3
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 +2 -2
- package/index.js +1 -4
- package/lib/to-ghost-json/base-template.js +1 -1
- package/lib/to-ghost-json/hydrate.js +1 -1
- package/lib/to-ghost-json/meta-to-ghost.js +4 -6
- package/lib/to-ghost-json/process-post-relations.js +14 -12
- package/lib/to-ghost-json/remove-empty.js +4 -8
- package/lib/to-ghost-json/reorder-tags.js +3 -3
- package/lib/to-ghost-json/validate.js +12 -6
- package/lib/utils/schema.js +2 -9
- package/package.json +4 -8
package/README.md
CHANGED
|
@@ -30,8 +30,8 @@ Follow the instructions for the top-level repo.
|
|
|
30
30
|
|
|
31
31
|
## Test
|
|
32
32
|
|
|
33
|
-
- `pnpm
|
|
34
|
-
-
|
|
33
|
+
- `pnpm test` runs the package tests
|
|
34
|
+
- Linting and formatting are run from the repo root (`pnpm lint`, `pnpm format`)
|
|
35
35
|
|
|
36
36
|
|
|
37
37
|
# Copyright & License
|
package/index.js
CHANGED
|
@@ -16,7 +16,7 @@ import hydrate from './hydrate.js';
|
|
|
16
16
|
* We also need to ensure that each object has at least the bare minimum properties required for an import
|
|
17
17
|
*
|
|
18
18
|
*/
|
|
19
|
-
const removeMeta =
|
|
19
|
+
const removeMeta = resource => {
|
|
20
20
|
return resource.data || resource;
|
|
21
21
|
};
|
|
22
22
|
|
|
@@ -39,9 +39,7 @@ const deduplicateSlugs = (obj, type) => {
|
|
|
39
39
|
// @TODO: log some sort of warning for things like this?
|
|
40
40
|
const objectID = new ObjectID();
|
|
41
41
|
const maxBaseLength = GHOST_SLUG_MAX - DEDUP_SUFFIX_LENGTH;
|
|
42
|
-
const baseSlug = obj.slug.length > maxBaseLength
|
|
43
|
-
? obj.slug.substring(0, maxBaseLength).trim()
|
|
44
|
-
: obj.slug;
|
|
42
|
+
const baseSlug = obj.slug.length > maxBaseLength ? obj.slug.substring(0, maxBaseLength).trim() : obj.slug;
|
|
45
43
|
obj.slug = `${baseSlug}-${objectID}`;
|
|
46
44
|
}
|
|
47
45
|
|
|
@@ -64,7 +62,7 @@ const ensureValid = (resource, type, options) => {
|
|
|
64
62
|
return obj;
|
|
65
63
|
};
|
|
66
64
|
|
|
67
|
-
const normalizeKey =
|
|
65
|
+
const normalizeKey = key => {
|
|
68
66
|
let outputKey = null;
|
|
69
67
|
|
|
70
68
|
if (schema.RESOURCES.includes(key)) {
|
|
@@ -77,7 +75,7 @@ const normalizeKey = (key) => {
|
|
|
77
75
|
return outputKey;
|
|
78
76
|
};
|
|
79
77
|
|
|
80
|
-
const normalizeValue =
|
|
78
|
+
const normalizeValue = value => {
|
|
81
79
|
if (!Array.isArray(value)) {
|
|
82
80
|
value = [value];
|
|
83
81
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import ObjectID from 'bson-objectid';
|
|
2
2
|
import schema from '../utils/schema.js';
|
|
3
3
|
|
|
4
|
-
export default
|
|
4
|
+
export default json => {
|
|
5
5
|
json.users = json.users || [];
|
|
6
6
|
json.tags = json.tags || []; // expected to be empty
|
|
7
7
|
|
|
@@ -33,7 +33,8 @@ export default (json) => {
|
|
|
33
33
|
return;
|
|
34
34
|
}
|
|
35
35
|
const matchData = match.data || match;
|
|
36
|
-
return json[location].find(
|
|
36
|
+
return json[location].find(item => {
|
|
37
|
+
// eslint-disable-line array-callback-return
|
|
37
38
|
const itemData = item.data || item;
|
|
38
39
|
// @TODO: need to scrape, or post-process scrape for user and tag slugs
|
|
39
40
|
if (itemData.id && matchData.id && itemData.id === matchData.id) {
|
|
@@ -42,14 +43,15 @@ export default (json) => {
|
|
|
42
43
|
return item;
|
|
43
44
|
} else if (itemData.name && matchData.name && itemData.name === matchData.name) {
|
|
44
45
|
return item;
|
|
45
|
-
} else if ((item.url ?? match.url) != null && item.url === match.url) {
|
|
46
|
+
} else if ((item.url ?? match.url) != null && item.url === match.url) {
|
|
47
|
+
// eslint-disable-line eqeqeq
|
|
46
48
|
return item;
|
|
47
49
|
}
|
|
48
50
|
});
|
|
49
51
|
};
|
|
50
52
|
|
|
51
53
|
// Normalise author to { url, data } shape so matching and IDs work
|
|
52
|
-
const normaliseAuthor =
|
|
54
|
+
const normaliseAuthor = author => {
|
|
53
55
|
if (!author) {
|
|
54
56
|
return author;
|
|
55
57
|
}
|
|
@@ -62,10 +64,10 @@ export default (json) => {
|
|
|
62
64
|
};
|
|
63
65
|
};
|
|
64
66
|
|
|
65
|
-
const processPostAuthors =
|
|
67
|
+
const processPostAuthors = postData => {
|
|
66
68
|
let postAuthors = findPostRelations(postData, 'author');
|
|
67
69
|
|
|
68
|
-
postAuthors.forEach(
|
|
70
|
+
postAuthors.forEach(author => {
|
|
69
71
|
author = normaliseAuthor(author);
|
|
70
72
|
if (!author || !(author.data || author)) {
|
|
71
73
|
return;
|
|
@@ -94,15 +96,15 @@ export default (json) => {
|
|
|
94
96
|
});
|
|
95
97
|
|
|
96
98
|
// Ensure no author-like keys remain on the post (Ghost expects users + posts_authors only)
|
|
97
|
-
schema.AUTHOR_ALIASES.forEach(
|
|
99
|
+
schema.AUTHOR_ALIASES.forEach(key => {
|
|
98
100
|
delete postData[key];
|
|
99
101
|
});
|
|
100
102
|
};
|
|
101
103
|
|
|
102
|
-
const processPostTags =
|
|
104
|
+
const processPostTags = postData => {
|
|
103
105
|
let postTags = findPostRelations(postData, 'tag');
|
|
104
106
|
|
|
105
|
-
postTags.forEach(
|
|
107
|
+
postTags.forEach(postTag => {
|
|
106
108
|
let tag = findMatchingItem(postTag, 'tags');
|
|
107
109
|
|
|
108
110
|
if (!tag) {
|
|
@@ -122,7 +124,7 @@ export default (json) => {
|
|
|
122
124
|
});
|
|
123
125
|
};
|
|
124
126
|
|
|
125
|
-
const processPostMeta =
|
|
127
|
+
const processPostMeta = postData => {
|
|
126
128
|
let postMeta = {
|
|
127
129
|
post_id: postData.id
|
|
128
130
|
};
|
|
@@ -144,7 +146,7 @@ export default (json) => {
|
|
|
144
146
|
'email_only'
|
|
145
147
|
];
|
|
146
148
|
|
|
147
|
-
metaKeys.forEach(
|
|
149
|
+
metaKeys.forEach(item => {
|
|
148
150
|
if (postData[item]) {
|
|
149
151
|
postMeta[item] = postData[item];
|
|
150
152
|
delete postData[item];
|
|
@@ -156,7 +158,7 @@ export default (json) => {
|
|
|
156
158
|
return postData;
|
|
157
159
|
};
|
|
158
160
|
|
|
159
|
-
const processPostRelations =
|
|
161
|
+
const processPostRelations = post => {
|
|
160
162
|
try {
|
|
161
163
|
// Support both wrapped ({ url, data: { ... } }) and flat post shapes
|
|
162
164
|
const postData = post.data !== undefined ? post.data : post;
|
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
const removeEmptyUsers =
|
|
1
|
+
const removeEmptyUsers = data => {
|
|
2
2
|
// Assign temp IDs to users without IDs
|
|
3
|
-
data.users.forEach(
|
|
3
|
+
data.users.forEach(user => {
|
|
4
4
|
if (!user.id || user.id === '') {
|
|
5
5
|
user.id = `temp-id-${Math.random().toString(36).substring(2, 15)}`;
|
|
6
6
|
}
|
|
7
7
|
});
|
|
8
8
|
|
|
9
9
|
// Remove users without posts and their role associations
|
|
10
|
-
const userIdsWithPosts = new Set(
|
|
11
|
-
data.posts_authors.map(pa => pa.author_id)
|
|
12
|
-
);
|
|
10
|
+
const userIdsWithPosts = new Set(data.posts_authors.map(pa => pa.author_id));
|
|
13
11
|
|
|
14
12
|
data.users = data?.users?.filter(user => userIdsWithPosts.has(user.id)) || [];
|
|
15
13
|
if (data?.roles_users) {
|
|
@@ -19,6 +17,4 @@ const removeEmptyUsers = (data) => {
|
|
|
19
17
|
return data;
|
|
20
18
|
};
|
|
21
19
|
|
|
22
|
-
export {
|
|
23
|
-
removeEmptyUsers
|
|
24
|
-
};
|
|
20
|
+
export {removeEmptyUsers};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export default
|
|
1
|
+
export default json => {
|
|
2
2
|
if (!json.posts) {
|
|
3
3
|
return json;
|
|
4
4
|
}
|
|
5
5
|
|
|
6
|
-
json.posts.forEach(
|
|
6
|
+
json.posts.forEach(post => {
|
|
7
7
|
let tags = post.data.tags || [];
|
|
8
8
|
let publicTags = [];
|
|
9
9
|
let internalTags = [];
|
|
@@ -11,7 +11,7 @@ export default (json) => {
|
|
|
11
11
|
// Filter out empty tags
|
|
12
12
|
tags = tags.filter(tag => tag.data.name.length);
|
|
13
13
|
|
|
14
|
-
tags.forEach(
|
|
14
|
+
tags.forEach(tag => {
|
|
15
15
|
if (tag.data.name.includes('#')) {
|
|
16
16
|
internalTags.push(tag);
|
|
17
17
|
} else {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {slugify} from '@tryghost/string';
|
|
2
2
|
import emailValidator from 'node-email-verifier';
|
|
3
3
|
|
|
4
|
-
export default async
|
|
4
|
+
export default async json => {
|
|
5
5
|
if (!json.posts) {
|
|
6
6
|
return json;
|
|
7
7
|
}
|
|
@@ -34,7 +34,9 @@ export default async (json) => {
|
|
|
34
34
|
let truncated = input[propKey].substring(0, propValue).trim();
|
|
35
35
|
|
|
36
36
|
// eslint-disable-next-line no-console
|
|
37
|
-
console.warn(
|
|
37
|
+
console.warn(
|
|
38
|
+
`${propKey} for post slug "${item.data.slug}" is too long.\nOriginal: "${input[propKey]}"\nTruncated to: "${truncated}"`
|
|
39
|
+
);
|
|
38
40
|
|
|
39
41
|
input[propKey] = truncated;
|
|
40
42
|
}
|
|
@@ -44,10 +46,12 @@ export default async (json) => {
|
|
|
44
46
|
for (const [propKey, propValue] of Object.entries(tagProperties)) {
|
|
45
47
|
if (tag.data[propKey] && tag.data[propKey].length > propValue) {
|
|
46
48
|
let truncated = tag.data[propKey].substring(0, propValue).trim();
|
|
47
|
-
|
|
49
|
+
|
|
48
50
|
// eslint-disable-next-line no-console
|
|
49
|
-
console.warn(
|
|
50
|
-
|
|
51
|
+
console.warn(
|
|
52
|
+
`${propKey} for tag slug "${input.tags[iindex].data.slug}" is too long.\nOriginal: "${input.tags[iindex].data[propKey]}"\nTruncated to: "${truncated}"`
|
|
53
|
+
);
|
|
54
|
+
|
|
51
55
|
input.tags[iindex].data[propKey] = truncated;
|
|
52
56
|
}
|
|
53
57
|
}
|
|
@@ -69,7 +73,9 @@ export default async (json) => {
|
|
|
69
73
|
|
|
70
74
|
if (!isEmailValid) {
|
|
71
75
|
// The local part (before the @) must be 50 chars or fewer to pass Ghost's email validation
|
|
72
|
-
item.data.email = `${slugify(item?.data?.slug || item?.data?.name || 'author')
|
|
76
|
+
item.data.email = `${slugify(item?.data?.slug || item?.data?.name || 'author')
|
|
77
|
+
.slice(0, 50)
|
|
78
|
+
.replace(/-+$/, '')}@example.com`;
|
|
73
79
|
}
|
|
74
80
|
|
|
75
81
|
json.users[index] = item;
|
package/lib/utils/schema.js
CHANGED
|
@@ -3,9 +3,7 @@
|
|
|
3
3
|
// From Ghost core, along with a bunch of utility classes
|
|
4
4
|
|
|
5
5
|
// @TODO extend this to all importable Ghost resources
|
|
6
|
-
const RESOURCES = [
|
|
7
|
-
'posts', 'posts_authors', 'posts_meta', 'posts_tags', 'tags', 'users'
|
|
8
|
-
];
|
|
6
|
+
const RESOURCES = ['posts', 'posts_authors', 'posts_meta', 'posts_tags', 'tags', 'users'];
|
|
9
7
|
|
|
10
8
|
const RESOURCE_SINGULAR_TO_PLURAL = {
|
|
11
9
|
user: 'users',
|
|
@@ -14,12 +12,7 @@ const RESOURCE_SINGULAR_TO_PLURAL = {
|
|
|
14
12
|
tag: 'tags'
|
|
15
13
|
};
|
|
16
14
|
|
|
17
|
-
const AUTHOR_ALIASES = [
|
|
18
|
-
'author',
|
|
19
|
-
'authors',
|
|
20
|
-
'primary_author',
|
|
21
|
-
'author_id'
|
|
22
|
-
];
|
|
15
|
+
const AUTHOR_ALIASES = ['author', 'authors', 'primary_author', 'author_id'];
|
|
23
16
|
|
|
24
17
|
export default {
|
|
25
18
|
RESOURCES,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mg-json",
|
|
3
|
-
"version": "0.22.
|
|
3
|
+
"version": "0.22.3",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/TryGhost/migrate.git",
|
|
@@ -17,20 +17,16 @@
|
|
|
17
17
|
"publishConfig": {
|
|
18
18
|
"access": "public"
|
|
19
19
|
},
|
|
20
|
-
"devDependencies": {
|
|
21
|
-
"eslint": "8.57.1"
|
|
22
|
-
},
|
|
20
|
+
"devDependencies": {},
|
|
23
21
|
"dependencies": {
|
|
24
22
|
"@tryghost/string": "0.3.5",
|
|
25
23
|
"bson-objectid": "2.0.4",
|
|
26
24
|
"html-entities": "2.6.0",
|
|
27
25
|
"node-email-verifier": "4.0.0",
|
|
28
|
-
"@tryghost/mg-utils": "0.11.
|
|
26
|
+
"@tryghost/mg-utils": "0.11.3"
|
|
29
27
|
},
|
|
30
28
|
"scripts": {
|
|
31
29
|
"dev": "echo \"Implement me!\"",
|
|
32
|
-
"test": "node --test 'test/**/*.test.js'"
|
|
33
|
-
"posttest": "pnpm lint",
|
|
34
|
-
"lint": "eslint . --ext .js --cache"
|
|
30
|
+
"test": "node --test 'test/**/*.test.js'"
|
|
35
31
|
}
|
|
36
32
|
}
|