@tryghost/mg-linkfixer 0.3.0 → 0.3.1

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 +34 -8
  2. package/package.json +2 -2
package/lib/LinkFixer.js CHANGED
@@ -1,3 +1,4 @@
1
+ const path = require('path');
1
2
  const _ = require('lodash');
2
3
  const cheerio = require('cheerio');
3
4
 
@@ -11,8 +12,29 @@ class LinkFixer {
11
12
  this.linkMap = {};
12
13
  }
13
14
 
14
- removeProtocol(url) {
15
- return url.replace(/^https?:\/\//, '');
15
+ /**
16
+ * Clean the URL
17
+ * @param {String} url The given URL
18
+ * @returns {String} A cleaned URL with no protocol or parameters
19
+ *
20
+ * @example
21
+ * cleanURL('https://exampleurl.com/sample-page/child-sample-page/?dolor=simet');
22
+ * => exampleurl.com/sample-page/child-sample-page/
23
+ *
24
+ * @example
25
+ * cleanURL('https://exampleurl.com/sample-page/');
26
+ * => exampleurl.com/sample-page/
27
+ */
28
+ cleanURL(url) {
29
+ try {
30
+ const urlParts = new URL(url);
31
+ const cleanedURL = path.join(urlParts.host, urlParts.pathname);
32
+
33
+ return cleanedURL;
34
+ } catch (error) {
35
+ // If it's an invalid URL, return the original string
36
+ return url;
37
+ }
16
38
  }
17
39
 
18
40
  buildMap(ctx) {
@@ -48,8 +70,8 @@ class LinkFixer {
48
70
 
49
71
  // Remove the protocol, ensuring we treat `http` and `https` sites in the same way
50
72
  Object.keys(this.linkMap).forEach((key) => {
51
- let noProtocolKey = this.removeProtocol(key);
52
- this.linkMap[noProtocolKey] = this.linkMap[key];
73
+ let updatedURL = this.cleanURL(key);
74
+ this.linkMap[updatedURL] = this.linkMap[key];
53
75
  });
54
76
  }
55
77
 
@@ -59,11 +81,15 @@ class LinkFixer {
59
81
  let links = $('a').map(async (i, el) => {
60
82
  let href = $(el).attr('href');
61
83
 
62
- // Remove protocol, matching the protocol links stored in the linkMap
63
- let noProtocolHref = this.removeProtocol(href);
84
+ if (!href) {
85
+ return;
86
+ }
87
+
88
+ // Clean the URL, matching the links stored in the linkMap
89
+ let updatedURL = this.cleanURL(href);
64
90
 
65
- if (this.linkMap[noProtocolHref]) {
66
- $(el).attr('href', this.linkMap[noProtocolHref]);
91
+ if (this.linkMap[updatedURL]) {
92
+ $(el).attr('href', this.linkMap[updatedURL]);
67
93
  }
68
94
  }).get();
69
95
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/mg-linkfixer",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
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": "7f4422a89070611f2500548c14d286cccfd4c861"
30
+ "gitHead": "370c7f9973e7518cfb6433e694c8fbbf022c32cf"
31
31
  }