@tryghost/mg-linkfixer 0.5.21 → 0.5.23

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2013-2023 Ghost Foundation
3
+ Copyright (c) 2013-2025 Ghost Foundation
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -36,4 +36,4 @@ Follow the instructions for the top-level repo.
36
36
 
37
37
  # Copyright & License
38
38
 
39
- Copyright (c) 2013-2023 Ghost Foundation - Released under the [MIT license](LICENSE).
39
+ Copyright (c) 2013-2025 Ghost Foundation - Released under the [MIT license](LICENSE).
package/lib/LinkFixer.js CHANGED
@@ -4,8 +4,22 @@ import cheerio from 'cheerio';
4
4
 
5
5
  // @TODO: expand this list
6
6
  const htmlFields = ['html'];
7
+ const lexicalFields = ['lexical'];
7
8
 
8
9
  const isHTMLField = field => _.includes(htmlFields, field);
10
+ const isLexicalField = field => _.includes(lexicalFields, field);
11
+
12
+ function mapObject(obj, fn) {
13
+ return Object.fromEntries(
14
+ Object.entries(obj).map(([key, value]) => {
15
+ if (typeof value === 'object' &&
16
+ value !== null) {
17
+ return [key, mapObject(value, fn)];
18
+ }
19
+ return [key, fn(key, value)];
20
+ })
21
+ );
22
+ }
9
23
 
10
24
  export default class LinkFixer {
11
25
  constructor() {
@@ -38,12 +52,14 @@ export default class LinkFixer {
38
52
  }
39
53
 
40
54
  buildMap(ctx) {
41
- if (!ctx.result.posts) {
55
+ if (!ctx?.result?.posts && !ctx?.data?.posts) {
42
56
  return;
43
57
  }
44
58
 
59
+ const theDataToParse = ctx?.result?.posts ?? ctx?.data?.posts;
60
+
45
61
  // @TODO: support for custom taxonomies
46
- ctx.result.posts.forEach(({url, data}) => {
62
+ theDataToParse.forEach(({url, data}) => {
47
63
  // Exit if no url or is empty string
48
64
  if (!url || url.length === 0) {
49
65
  return;
@@ -136,6 +152,22 @@ export default class LinkFixer {
136
152
  return $.html();
137
153
  }
138
154
 
155
+ async processLexical(lexical) {
156
+ const parsedLexical = JSON.parse(lexical);
157
+
158
+ const mappedObject = mapObject(parsedLexical, (key, value) => {
159
+ if (key === 'url') {
160
+ let updatedURL = this.cleanURL(value);
161
+
162
+ return this.linkMap[updatedURL];
163
+ } else {
164
+ return value;
165
+ }
166
+ });
167
+
168
+ return JSON.stringify(mappedObject);
169
+ }
170
+
139
171
  fix(ctx, task) {
140
172
  let tasks = [];
141
173
  let json = ctx.result;
@@ -151,6 +183,19 @@ export default class LinkFixer {
151
183
  _.forEach(resources, (resource) => {
152
184
  // For each field
153
185
  _.forEach(resource, (value, field) => {
186
+ if (isLexicalField(field)) {
187
+ tasks.push({
188
+ title: `${type}: ${resource.slug} ${field}`,
189
+ task: async () => {
190
+ try {
191
+ resource[field] = await this.processLexical(value);
192
+ } catch (error) {
193
+ ctx.errors.push(error);
194
+ throw error;
195
+ }
196
+ }
197
+ });
198
+ }
154
199
  if (isHTMLField(field)) {
155
200
  tasks.push({
156
201
  title: `${type}: ${resource.slug} ${field}`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/mg-linkfixer",
3
- "version": "0.5.21",
3
+ "version": "0.5.23",
4
4
  "repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-linkfixer",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -20,7 +20,7 @@
20
20
  "access": "public"
21
21
  },
22
22
  "devDependencies": {
23
- "@tryghost/listr-smart-renderer": "0.5.15",
23
+ "@tryghost/listr-smart-renderer": "0.5.16",
24
24
  "eslint": "8.57.0",
25
25
  "jest": "29.7.0",
26
26
  "jest-extended": "4.0.2",
@@ -30,5 +30,5 @@
30
30
  "cheerio": "1.0.0-rc.12",
31
31
  "lodash": "4.17.21"
32
32
  },
33
- "gitHead": "4995b3fa8393ec37c7181e08b9cd76a49a056cb1"
33
+ "gitHead": "fd2fdb1542c90de5caac0feaa76e8ea599ef202f"
34
34
  }