@tryghost/mg-linkfixer 0.2.6 → 0.3.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.
Files changed (2) hide show
  1. package/lib/LinkFixer.js +16 -2
  2. package/package.json +2 -2
package/lib/LinkFixer.js CHANGED
@@ -11,6 +11,10 @@ class LinkFixer {
11
11
  this.linkMap = {};
12
12
  }
13
13
 
14
+ removeProtocol(url) {
15
+ return url.replace(/^https?:\/\//, '');
16
+ }
17
+
14
18
  buildMap(ctx) {
15
19
  if (!ctx.result.posts) {
16
20
  return;
@@ -41,6 +45,12 @@ class LinkFixer {
41
45
  this.linkMap[data.author.url] = `/author/${data.author.data.slug}/`;
42
46
  }
43
47
  });
48
+
49
+ // Remove the protocol, ensuring we treat `http` and `https` sites in the same way
50
+ Object.keys(this.linkMap).forEach((key) => {
51
+ let noProtocolKey = this.removeProtocol(key);
52
+ this.linkMap[noProtocolKey] = this.linkMap[key];
53
+ });
44
54
  }
45
55
 
46
56
  async processHTML(html) {
@@ -48,8 +58,12 @@ class LinkFixer {
48
58
 
49
59
  let links = $('a').map(async (i, el) => {
50
60
  let href = $(el).attr('href');
51
- if (this.linkMap[href]) {
52
- $(el).attr('href', this.linkMap[href]);
61
+
62
+ // Remove protocol, matching the protocol links stored in the linkMap
63
+ let noProtocolHref = this.removeProtocol(href);
64
+
65
+ if (this.linkMap[noProtocolHref]) {
66
+ $(el).attr('href', this.linkMap[noProtocolHref]);
53
67
  }
54
68
  }).get();
55
69
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/mg-linkfixer",
3
- "version": "0.2.6",
3
+ "version": "0.3.0",
4
4
  "repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-linkfixer",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -27,5 +27,5 @@
27
27
  "cheerio": "^1.0.0-rc.3",
28
28
  "lodash": "4.17.21"
29
29
  },
30
- "gitHead": "59b4fceeb91ef34ff1de054a9006f6f2d465fc2b"
30
+ "gitHead": "7f4422a89070611f2500548c14d286cccfd4c861"
31
31
  }