@tryghost/mg-linkfixer 0.4.8 → 0.5.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 +20 -6
  2. package/package.json +6 -6
package/lib/LinkFixer.js CHANGED
@@ -1,4 +1,4 @@
1
- import path from 'node:path';
1
+ import {join} from 'node:path';
2
2
  import _ from 'lodash';
3
3
  import cheerio from 'cheerio';
4
4
 
@@ -28,7 +28,7 @@ class LinkFixer {
28
28
  cleanURL(url) {
29
29
  try {
30
30
  const urlParts = new URL(url);
31
- const cleanedURL = path.join(urlParts.host, urlParts.pathname);
31
+ const cleanedURL = join(urlParts.host, urlParts.pathname);
32
32
 
33
33
  return cleanedURL;
34
34
  } catch (error) {
@@ -44,16 +44,30 @@ class LinkFixer {
44
44
 
45
45
  // @TODO: support for custom taxonomies
46
46
  ctx.result.posts.forEach(({url, data}) => {
47
- const RegexSlugYYYYMMDD = new RegExp(`^${ctx.options.url}/[a-zA-Z0-9-]+/([0-9]{4}/[0-9]{2}/[0-9]{2})/([a-zA-Z0-9-_]*)(/)?`);
47
+ // Parse the current post URL, so we can use its domain as a fallback
48
+ const postURLDomain = new URL(url);
49
+
50
+ // We need to handle the domain with http: and https:, so build part of the regexp pattern that accounts for both
51
+ const siteURL = new URL(ctx.options.url || postURLDomain.origin);
52
+
53
+ siteURL.protocol = 'http:';
54
+ const siteURLHttp = siteURL.toString().replace(/\/$/, ''); // Trim trailing slashes
55
+
56
+ siteURL.protocol = 'https:';
57
+ const siteURLHttps = siteURL.toString().replace(/\/$/, ''); // Trim trailing slashes
58
+
59
+ const siteURLBothProtocols = `(?:${siteURLHttp}|${siteURLHttps})`;
60
+
61
+ const RegexSlugYYYYMMDD = new RegExp(`^${siteURLBothProtocols}/[a-zA-Z0-9-]+/([0-9]{4}/[0-9]{2}/[0-9]{2})/([a-zA-Z0-9-_]*)(/)?`);
48
62
  const isSlugYYYYMMDDDatedPermalink = url.match(RegexSlugYYYYMMDD);
49
63
 
50
- const RegexYYYYMMDD = new RegExp(`^${ctx.options.url}/([0-9]{4}/[0-9]{2}/[0-9]{2})/([a-zA-Z0-9-_]*)(/)?`);
64
+ const RegexYYYYMMDD = new RegExp(`^${siteURLBothProtocols}/([0-9]{4}/[0-9]{2}/[0-9]{2})/([a-zA-Z0-9-_]*)(/)?`);
51
65
  const isYYYYMMDDDatedPermalink = url.match(RegexYYYYMMDD);
52
66
 
53
- const RegexSlugYYYYMM = new RegExp(`^${ctx.options.url}/[a-zA-Z0-9-]+/([0-9]{4}/[0-9]{2})/([a-zA-Z0-9-_]*)(/)?`);
67
+ const RegexSlugYYYYMM = new RegExp(`^${siteURLBothProtocols}/[a-zA-Z0-9-]+/([0-9]{4}/[0-9]{2})/([a-zA-Z0-9-_]*)(/)?`);
54
68
  const isSlugYYYYMMDatedPermalink = url.match(RegexSlugYYYYMM);
55
69
 
56
- const RegexYYYYMM = new RegExp(`^${ctx.options.url}/([0-9]{4}/[0-9]{2})/([a-zA-Z0-9-_]*)(/)?`);
70
+ const RegexYYYYMM = new RegExp(`^${siteURLBothProtocols}/([0-9]{4}/[0-9]{2})/([a-zA-Z0-9-_]*)(/)?`);
57
71
  const isYYYYMMDatedPermalink = url.match(RegexYYYYMM);
58
72
 
59
73
  if (ctx.options.datedPermalinks === '/yyyy/mm/dd/' && isYYYYMMDDDatedPermalink) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/mg-linkfixer",
3
- "version": "0.4.8",
3
+ "version": "0.5.0",
4
4
  "repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-linkfixer",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -20,15 +20,15 @@
20
20
  "access": "public"
21
21
  },
22
22
  "devDependencies": {
23
- "@tryghost/listr-smart-renderer": "^0.4.7",
24
- "eslint": "8.31.0",
25
- "jest": "29.3.1",
26
- "jest-extended": "3.2.2",
23
+ "@tryghost/listr-smart-renderer": "^0.5.0",
24
+ "eslint": "8.34.0",
25
+ "jest": "29.4.3",
26
+ "jest-extended": "3.2.3",
27
27
  "listr": "0.14.3"
28
28
  },
29
29
  "dependencies": {
30
30
  "cheerio": "^1.0.0-rc.3",
31
31
  "lodash": "4.17.21"
32
32
  },
33
- "gitHead": "ac01636f55ec4bcf15b1fed2e5efe58327009d0a"
33
+ "gitHead": "c4270f6229caa350155a03a0646b3c0c21eaff5e"
34
34
  }