@tryghost/mg-linkfixer 0.5.21 → 0.5.22
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/lib/LinkFixer.js +47 -2
- package/package.json +2 -2
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
|
|
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
|
-
|
|
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.
|
|
3
|
+
"version": "0.5.22",
|
|
4
4
|
"repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-linkfixer",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -30,5 +30,5 @@
|
|
|
30
30
|
"cheerio": "1.0.0-rc.12",
|
|
31
31
|
"lodash": "4.17.21"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "9994cb30b1c2c72b2c66ab5b532a0fa7dcedec3c"
|
|
34
34
|
}
|