@tryghost/mg-json 0.11.12 → 0.11.13

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.
@@ -23,6 +23,10 @@ const removeMeta = (resource) => {
23
23
 
24
24
  let slugs = {};
25
25
 
26
+ // Ghost's post/tag slug max length (MySQL utf8mb4 index limit)
27
+ const GHOST_SLUG_MAX = 191;
28
+ const DEDUP_SUFFIX_LENGTH = 25; // '-' + 24-char ObjectID
29
+
26
30
  const deduplicateSlugs = (obj, type) => {
27
31
  if (!slugs[type]) {
28
32
  slugs[type] = [];
@@ -31,7 +35,11 @@ const deduplicateSlugs = (obj, type) => {
31
35
  if (_.includes(slugs[type], obj.slug)) {
32
36
  // @TODO: log some sort of warning for things like this?
33
37
  const objectID = new ObjectID();
34
- obj.slug = `${obj.slug}-${objectID}`;
38
+ const maxBaseLength = GHOST_SLUG_MAX - DEDUP_SUFFIX_LENGTH;
39
+ const baseSlug = obj.slug.length > maxBaseLength
40
+ ? obj.slug.substring(0, maxBaseLength).trim()
41
+ : obj.slug;
42
+ obj.slug = `${baseSlug}-${objectID}`;
35
43
  }
36
44
 
37
45
  slugs[type].push(obj.slug);
@@ -15,7 +15,10 @@ export default (json) => {
15
15
  let relations = [];
16
16
  let pluralForm = schema.RESOURCE_SINGULAR_TO_PLURAL[relation];
17
17
 
18
- if (_.has(postData, pluralForm)) {
18
+ // Only use plural key when it's actually an array (e.g. authors: [a, b]).
19
+ // When single-author Co-Authors Plus posts set authors: undefined and author: <person>,
20
+ // we must not take this branch or we'd process no one and orphan the post.
21
+ if (_.has(postData, pluralForm) && Array.isArray(postData[pluralForm])) {
19
22
  relations = postData[pluralForm];
20
23
  delete postData[pluralForm];
21
24
  } else if (_.has(postData, relation)) {
@@ -30,13 +33,15 @@ export default (json) => {
30
33
  if (!_.has(json, location)) {
31
34
  return;
32
35
  }
36
+ const matchData = match.data || match;
33
37
  return json[location].find((item) => { // eslint-disable-line array-callback-return
38
+ const itemData = item.data || item;
34
39
  // @TODO: need to scrape, or post-process scrape for user and tag slugs
35
- if (item.data.id && item.data.id === match.data.id) {
40
+ if (itemData.id && matchData.id && itemData.id === matchData.id) {
36
41
  return item;
37
- } else if (item.data.slug && item.data.slug === match.data.slug) {
42
+ } else if (itemData.slug && matchData.slug && itemData.slug === matchData.slug) {
38
43
  return item;
39
- } else if (item.data.name && item.data.name === match.data.name) {
44
+ } else if (itemData.name && matchData.name && itemData.name === matchData.name) {
40
45
  return item;
41
46
  } else if (!_.isNil(item.url || match.url) && item.url === match.url) {
42
47
  return item;
@@ -44,27 +49,55 @@ export default (json) => {
44
49
  });
45
50
  };
46
51
 
52
+ // Normalise author to { url, data } shape so matching and IDs work
53
+ const normaliseAuthor = (author) => {
54
+ if (!author) {
55
+ return author;
56
+ }
57
+ if (_.has(author, 'data') && _.isObject(author.data)) {
58
+ return author;
59
+ }
60
+ return {
61
+ url: author.url || author.slug || '',
62
+ data: _.has(author, 'data') ? author.data : author
63
+ };
64
+ };
65
+
47
66
  const processPostAuthors = (postData) => {
48
67
  let postAuthors = findPostRelations(postData, 'author');
49
68
 
50
69
  _.each(postAuthors, (author) => {
70
+ author = normaliseAuthor(author);
71
+ if (!author || !(author.data || author)) {
72
+ return;
73
+ }
74
+ const authorData = author.data || author;
51
75
  let user = findMatchingItem(author, 'users');
52
76
 
53
77
  if (!user) {
54
78
  user = author;
55
- user.data.id = new ObjectID();
79
+ if (!user.data) {
80
+ user = {url: authorData.slug || authorData.name || '', data: authorData};
81
+ }
82
+ user.data.id = user.data.id || new ObjectID();
56
83
  json.users.push(user);
57
84
  }
58
85
 
59
- if (user && !user.data.id) {
60
- user.data.id = new ObjectID();
86
+ const userData = user.data || user;
87
+ if (!userData.id) {
88
+ userData.id = new ObjectID();
61
89
  }
62
90
 
63
91
  json.posts_authors.push({
64
92
  post_id: postData.id,
65
- author_id: user.data.id
93
+ author_id: userData.id
66
94
  });
67
95
  });
96
+
97
+ // Ensure no author-like keys remain on the post (Ghost expects users + posts_authors only)
98
+ schema.AUTHOR_ALIASES.forEach((key) => {
99
+ delete postData[key];
100
+ });
68
101
  };
69
102
 
70
103
  const processPostTags = (postData) => {
@@ -126,14 +159,18 @@ export default (json) => {
126
159
 
127
160
  const processPostRelations = (post) => {
128
161
  try {
129
- // Ensure we have a post ID
130
- if (!post.data.id) {
131
- post.data.id = new ObjectID();
162
+ // Support both wrapped ({ url, data: { ... } }) and flat post shapes
163
+ const postData = post.data !== undefined ? post.data : post;
164
+ if (!postData.id) {
165
+ postData.id = new ObjectID();
132
166
  }
133
167
 
134
- processPostAuthors(post.data);
135
- processPostTags(post.data);
136
- post.data = processPostMeta(post.data);
168
+ processPostAuthors(postData);
169
+ processPostTags(postData);
170
+ const metaProcessed = processPostMeta(postData);
171
+ if (post.data !== undefined) {
172
+ post.data = metaProcessed;
173
+ }
137
174
  } catch (error) {
138
175
  error.reference = post.url;
139
176
  throw error;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/mg-json",
3
- "version": "0.11.12",
3
+ "version": "0.11.13",
4
4
  "repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-json",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -21,15 +21,15 @@
21
21
  },
22
22
  "devDependencies": {
23
23
  "eslint": "8.57.0",
24
- "jest": "30.0.5",
25
- "jest-extended": "6.0.0"
24
+ "jest": "30.2.0",
25
+ "jest-extended": "7.0.0"
26
26
  },
27
27
  "dependencies": {
28
28
  "@tryghost/string": "0.2.21",
29
29
  "bson-objectid": "2.0.4",
30
30
  "html-entities": "2.6.0",
31
- "lodash": "4.17.21",
31
+ "lodash": "4.17.23",
32
32
  "node-email-verifier": "4.0.0"
33
33
  },
34
- "gitHead": "027225930710c0643603f6ae346dc76504d9bb7a"
34
+ "gitHead": "ac9a868976ef5963df7fc2426af9e298bd5d8666"
35
35
  }