@tryghost/mg-linkfixer 0.3.0 → 0.4.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.
package/index.js CHANGED
@@ -1 +1,3 @@
1
- module.exports = require('./lib/LinkFixer');
1
+ import LinkFixer from './lib/LinkFixer.js';
2
+
3
+ export default LinkFixer;
package/lib/LinkFixer.js CHANGED
@@ -1,5 +1,6 @@
1
- const _ = require('lodash');
2
- const cheerio = require('cheerio');
1
+ import path from 'node:path';
2
+ import _ from 'lodash';
3
+ import cheerio from 'cheerio';
3
4
 
4
5
  // @TODO: expand this list
5
6
  const htmlFields = ['html'];
@@ -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
 
@@ -107,4 +133,4 @@ class LinkFixer {
107
133
  }
108
134
  }
109
135
 
110
- module.exports = LinkFixer;
136
+ export default LinkFixer;
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@tryghost/mg-linkfixer",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-linkfixer",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
7
7
  "main": "index.js",
8
+ "type": "module",
8
9
  "scripts": {
9
10
  "dev": "echo \"Implement me!\"",
10
- "test": "NODE_ENV=testing mocha './test/**/*.test.js'",
11
+ "test": "node --experimental-vm-modules $(yarn bin jest)",
11
12
  "lint": "eslint . --ext .js --cache",
12
13
  "posttest": "yarn lint"
13
14
  },
@@ -19,13 +20,15 @@
19
20
  "access": "public"
20
21
  },
21
22
  "devDependencies": {
22
- "mocha": "10.0.0",
23
- "should": "13.2.3",
24
- "sinon": "14.0.0"
23
+ "@tryghost/listr-smart-renderer": "^0.3.0",
24
+ "eslint": "8.25.0",
25
+ "jest": "29.1.2",
26
+ "jest-extended": "3.1.0",
27
+ "listr": "0.14.3"
25
28
  },
26
29
  "dependencies": {
27
30
  "cheerio": "^1.0.0-rc.3",
28
31
  "lodash": "4.17.21"
29
32
  },
30
- "gitHead": "7f4422a89070611f2500548c14d286cccfd4c861"
33
+ "gitHead": "35cb5c1ca6a826e1e596c4f2e209ab690fa06fa0"
31
34
  }