@tryghost/url-utils 5.0.0 → 5.1.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 (92) hide show
  1. package/lib/UrlUtils.d.ts +147 -3
  2. package/lib/UrlUtils.js +228 -112
  3. package/lib/index.d.ts +2 -0
  4. package/lib/index.js +6 -0
  5. package/lib/utils/absolute-to-relative.d.ts +9 -3
  6. package/lib/utils/absolute-to-relative.js +14 -9
  7. package/lib/utils/absolute-to-transform-ready.d.ts +13 -4
  8. package/lib/utils/absolute-to-transform-ready.js +16 -11
  9. package/lib/utils/build-early-exit-match.d.ts +11 -1
  10. package/lib/utils/build-early-exit-match.js +4 -4
  11. package/lib/utils/deduplicate-double-slashes.d.ts +2 -1
  12. package/lib/utils/deduplicate-double-slashes.js +2 -2
  13. package/lib/utils/deduplicate-subdirectory.d.ts +2 -2
  14. package/lib/utils/deduplicate-subdirectory.js +4 -4
  15. package/lib/utils/html-absolute-to-relative.d.ts +3 -3
  16. package/lib/utils/html-absolute-to-relative.js +10 -7
  17. package/lib/utils/html-absolute-to-transform-ready.d.ts +3 -4
  18. package/lib/utils/html-absolute-to-transform-ready.js +16 -9
  19. package/lib/utils/html-relative-to-absolute.d.ts +3 -3
  20. package/lib/utils/html-relative-to-absolute.js +43 -8
  21. package/lib/utils/html-relative-to-transform-ready.d.ts +3 -3
  22. package/lib/utils/html-relative-to-transform-ready.js +51 -11
  23. package/lib/utils/html-to-transform-ready.d.ts +3 -3
  24. package/lib/utils/html-to-transform-ready.js +17 -9
  25. package/lib/utils/html-transform.d.ts +5 -4
  26. package/lib/utils/html-transform.js +25 -21
  27. package/lib/utils/index.d.ts +76 -0
  28. package/lib/utils/index.js +115 -38
  29. package/lib/utils/is-ssl.d.ts +2 -2
  30. package/lib/utils/is-ssl.js +4 -4
  31. package/lib/utils/lexical-absolute-to-relative.d.ts +3 -2
  32. package/lib/utils/lexical-absolute-to-relative.js +9 -6
  33. package/lib/utils/lexical-absolute-to-transform-ready.d.ts +3 -2
  34. package/lib/utils/lexical-absolute-to-transform-ready.js +10 -7
  35. package/lib/utils/lexical-relative-to-absolute.d.ts +3 -3
  36. package/lib/utils/lexical-relative-to-absolute.js +8 -5
  37. package/lib/utils/lexical-relative-to-transform-ready.d.ts +3 -3
  38. package/lib/utils/lexical-relative-to-transform-ready.js +8 -5
  39. package/lib/utils/lexical-to-transform-ready.d.ts +3 -3
  40. package/lib/utils/lexical-to-transform-ready.js +17 -9
  41. package/lib/utils/lexical-transform.d.ts +3 -2
  42. package/lib/utils/lexical-transform.js +22 -19
  43. package/lib/utils/markdown-absolute-to-relative.d.ts +3 -4
  44. package/lib/utils/markdown-absolute-to-relative.js +11 -8
  45. package/lib/utils/markdown-absolute-to-transform-ready.d.ts +3 -5
  46. package/lib/utils/markdown-absolute-to-transform-ready.js +17 -10
  47. package/lib/utils/markdown-relative-to-absolute.d.ts +3 -4
  48. package/lib/utils/markdown-relative-to-absolute.js +11 -8
  49. package/lib/utils/markdown-relative-to-transform-ready.d.ts +3 -4
  50. package/lib/utils/markdown-relative-to-transform-ready.js +11 -8
  51. package/lib/utils/markdown-to-transform-ready.d.ts +3 -3
  52. package/lib/utils/markdown-to-transform-ready.js +17 -9
  53. package/lib/utils/markdown-transform.d.ts +3 -5
  54. package/lib/utils/markdown-transform.js +12 -6
  55. package/lib/utils/mobiledoc-absolute-to-relative.d.ts +3 -2
  56. package/lib/utils/mobiledoc-absolute-to-relative.js +9 -6
  57. package/lib/utils/mobiledoc-absolute-to-transform-ready.d.ts +3 -2
  58. package/lib/utils/mobiledoc-absolute-to-transform-ready.js +10 -7
  59. package/lib/utils/mobiledoc-relative-to-absolute.d.ts +3 -3
  60. package/lib/utils/mobiledoc-relative-to-absolute.js +8 -5
  61. package/lib/utils/mobiledoc-relative-to-transform-ready.d.ts +3 -3
  62. package/lib/utils/mobiledoc-relative-to-transform-ready.js +8 -5
  63. package/lib/utils/mobiledoc-to-transform-ready.d.ts +3 -3
  64. package/lib/utils/mobiledoc-to-transform-ready.js +17 -9
  65. package/lib/utils/mobiledoc-transform.d.ts +3 -1
  66. package/lib/utils/mobiledoc-transform.js +5 -4
  67. package/lib/utils/plaintext-absolute-to-transform-ready.d.ts +5 -4
  68. package/lib/utils/plaintext-absolute-to-transform-ready.js +16 -11
  69. package/lib/utils/plaintext-relative-to-transform-ready.d.ts +3 -2
  70. package/lib/utils/plaintext-relative-to-transform-ready.js +15 -7
  71. package/lib/utils/plaintext-to-transform-ready.d.ts +5 -3
  72. package/lib/utils/plaintext-to-transform-ready.js +17 -9
  73. package/lib/utils/relative-to-absolute.d.ts +5 -3
  74. package/lib/utils/relative-to-absolute.js +26 -19
  75. package/lib/utils/relative-to-transform-ready.d.ts +8 -2
  76. package/lib/utils/relative-to-transform-ready.js +22 -10
  77. package/lib/utils/replace-permalink.d.ts +13 -2
  78. package/lib/utils/replace-permalink.js +44 -8
  79. package/lib/utils/strip-subdirectory-from-path.d.ts +1 -1
  80. package/lib/utils/strip-subdirectory-from-path.js +5 -5
  81. package/lib/utils/to-transform-ready.d.ts +5 -3
  82. package/lib/utils/to-transform-ready.js +17 -9
  83. package/lib/utils/transform-ready-to-absolute.d.ts +9 -1
  84. package/lib/utils/transform-ready-to-absolute.js +2 -2
  85. package/lib/utils/transform-ready-to-relative.d.ts +3 -1
  86. package/lib/utils/transform-ready-to-relative.js +4 -3
  87. package/lib/utils/types.d.ts +92 -0
  88. package/lib/utils/types.js +2 -0
  89. package/lib/utils/url-join.d.ts +5 -2
  90. package/lib/utils/url-join.js +11 -5
  91. package/package.json +10 -11
  92. package/index.js +0 -2
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
- // @ts-nocheck
3
- // require the whatwg compatible URL library (same behaviour in node and browser)
4
- const { URL } = require('url');
5
- const urlJoin = require('./url-join');
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const url_1 = require("url");
7
+ const url_join_1 = __importDefault(require("./url-join"));
6
8
  // NOTE: Ghost's relative->absolute handling is a little strange when the rootUrl
7
9
  // includes a subdirectory. Root-relative paths such as /content/image.jpg are
8
10
  // actually treated as subdirectory-relative. This means that it's possible to
@@ -23,20 +25,25 @@ const urlJoin = require('./url-join');
23
25
  */
24
26
  const relativeToAbsolute = function relativeToAbsolute(path, rootUrl, itemPath, _options) {
25
27
  // itemPath is optional, if it's an object it may be the options param instead
26
- if (typeof itemPath === 'object' && !_options) {
27
- _options = itemPath;
28
- itemPath = null;
28
+ let finalItemPath = null;
29
+ let finalOptions = _options || {};
30
+ if (typeof itemPath === 'object' && itemPath !== null && !_options) {
31
+ finalOptions = itemPath;
32
+ finalItemPath = null;
33
+ }
34
+ else if (typeof itemPath === 'string') {
35
+ finalItemPath = itemPath;
29
36
  }
30
37
  // itemPath could be sent as a full url in which case, extract the pathname
31
- if (itemPath && itemPath.match(/^http/)) {
32
- const itemUrl = new URL(itemPath);
33
- itemPath = itemUrl.pathname;
38
+ if (finalItemPath && finalItemPath.match(/^http/)) {
39
+ const itemUrl = new url_1.URL(finalItemPath);
40
+ finalItemPath = itemUrl.pathname;
34
41
  }
35
42
  const defaultOptions = {
36
43
  assetsOnly: false,
37
44
  staticImageUrlPrefix: 'content/images'
38
45
  };
39
- const options = Object.assign({}, defaultOptions, _options);
46
+ const options = Object.assign({}, defaultOptions, finalOptions);
40
47
  // return the path as-is if it's not an asset path and we're only modifying assets
41
48
  if (options.assetsOnly) {
42
49
  const staticImageUrlPrefixRegex = new RegExp(options.staticImageUrlPrefix);
@@ -46,7 +53,7 @@ const relativeToAbsolute = function relativeToAbsolute(path, rootUrl, itemPath,
46
53
  }
47
54
  // if URL is absolute return it as-is
48
55
  try {
49
- const parsed = new URL(path, 'http://relative');
56
+ const parsed = new url_1.URL(path, 'http://relative');
50
57
  if (parsed.origin !== 'http://relative') {
51
58
  return path;
52
59
  }
@@ -55,7 +62,7 @@ const relativeToAbsolute = function relativeToAbsolute(path, rootUrl, itemPath,
55
62
  return path;
56
63
  }
57
64
  }
58
- catch (e) {
65
+ catch {
59
66
  return path;
60
67
  }
61
68
  // return the path as-is if it's a pure hash param
@@ -63,20 +70,20 @@ const relativeToAbsolute = function relativeToAbsolute(path, rootUrl, itemPath,
63
70
  return path;
64
71
  }
65
72
  // return the path as-is if it's not root-relative and we have no itemPath
66
- if (!itemPath && !path.match(/^\//)) {
73
+ if (!finalItemPath && !path.match(/^\//)) {
67
74
  return path;
68
75
  }
69
76
  // force root to always have a trailing-slash for consistent behaviour
70
77
  if (!rootUrl.endsWith('/')) {
71
78
  rootUrl = `${rootUrl}/`;
72
79
  }
73
- const parsedRootUrl = new URL(rootUrl);
74
- const basePath = path.startsWith('/') ? '' : itemPath;
75
- const fullPath = urlJoin([parsedRootUrl.pathname, basePath, path], { rootUrl });
76
- const absoluteUrl = new URL(fullPath, rootUrl);
80
+ const parsedRootUrl = new url_1.URL(rootUrl);
81
+ const basePath = path.startsWith('/') ? '' : (finalItemPath || '');
82
+ const fullPath = (0, url_join_1.default)([parsedRootUrl.pathname, basePath, path], { rootUrl });
83
+ const absoluteUrl = new url_1.URL(fullPath, rootUrl);
77
84
  if (options.secure) {
78
85
  absoluteUrl.protocol = 'https:';
79
86
  }
80
87
  return absoluteUrl.toString();
81
88
  };
82
- module.exports = relativeToAbsolute;
89
+ exports.default = relativeToAbsolute;
@@ -1,2 +1,8 @@
1
- declare const relativeToAbsolute: any;
2
- declare const relativeToTransformReady: (url: any, root: any, itemPath: any, _options: any) => any;
1
+ import type { TransformReadyReplacementOptions } from './types';
2
+ export interface RelativeToTransformReadyOptions extends TransformReadyReplacementOptions {
3
+ staticImageUrlPrefix: string;
4
+ secure: boolean;
5
+ }
6
+ export type RelativeToTransformReadyOptionsInput = Partial<RelativeToTransformReadyOptions>;
7
+ declare const relativeToTransformReady: (url: string, root: string, itemPath: string | null | RelativeToTransformReadyOptionsInput, _options?: RelativeToTransformReadyOptionsInput) => string;
8
+ export default relativeToTransformReady;
@@ -1,11 +1,20 @@
1
1
  "use strict";
2
- // @ts-nocheck
3
- const relativeToAbsolute = require('./relative-to-absolute');
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const relative_to_absolute_1 = __importDefault(require("./relative-to-absolute"));
7
+ const url_1 = require("url");
4
8
  const relativeToTransformReady = function (url, root, itemPath, _options) {
5
9
  // itemPath is optional, if it's an object may be the options param instead
6
- if (typeof itemPath === 'object' && !_options) {
7
- _options = itemPath;
8
- itemPath = null;
10
+ let finalItemPath = null;
11
+ let finalOptions = _options || {};
12
+ if (typeof itemPath === 'object' && itemPath !== null && !_options) {
13
+ finalOptions = itemPath;
14
+ finalItemPath = null;
15
+ }
16
+ else if (typeof itemPath === 'string') {
17
+ finalItemPath = itemPath;
9
18
  }
10
19
  const defaultOptions = {
11
20
  replacementStr: '__GHOST_URL__',
@@ -14,22 +23,25 @@ const relativeToTransformReady = function (url, root, itemPath, _options) {
14
23
  const overrideOptions = {
15
24
  secure: false
16
25
  };
17
- const options = Object.assign({}, defaultOptions, _options, overrideOptions);
26
+ const options = Object.assign({}, defaultOptions, finalOptions, overrideOptions);
18
27
  // convert to absolute
19
- const absoluteUrl = relativeToAbsolute(url, root, itemPath, options);
28
+ const absoluteUrl = (0, relative_to_absolute_1.default)(url, root, finalItemPath, options);
20
29
  if (absoluteUrl === url) {
21
30
  return url;
22
31
  }
23
- const rootUrl = new URL(root);
32
+ const rootUrl = new url_1.URL(root);
24
33
  const rootPathname = rootUrl.pathname.replace(/\/$/, '');
25
34
  // only convert to transform-ready if root url has no subdirectory or the subdirectory matches
26
35
  if (!url.match(/^\//) || rootPathname === '' || url.indexOf(rootPathname) === 0 || url.indexOf(`/${options.staticImageUrlPrefix}`) === 0) {
36
+ // normalize root to match the protocol of absoluteUrl (in case secure option changed it)
37
+ const absoluteUrlParsed = new url_1.URL(absoluteUrl);
38
+ const normalizedRoot = `${absoluteUrlParsed.protocol}//${rootUrl.host}${rootUrl.pathname}`.replace(/\/$/, '');
27
39
  // replace root with replacement string
28
40
  const transformedUrl = absoluteUrl
29
- .replace(root, `${options.replacementStr}/`) // always have trailing slash after magic string
41
+ .replace(normalizedRoot, `${options.replacementStr}/`) // always have trailing slash after magic string
30
42
  .replace(/([^:])\/\//g, '$1/');
31
43
  return transformedUrl;
32
44
  }
33
45
  return url;
34
46
  };
35
- module.exports = relativeToTransformReady;
47
+ exports.default = relativeToTransformReady;
@@ -1,5 +1,16 @@
1
- declare const moment: any;
1
+ interface PermalinkResource {
2
+ published_at?: string | number | Date | null;
3
+ primary_author?: {
4
+ slug: string;
5
+ } | null;
6
+ primary_tag?: {
7
+ slug: string;
8
+ } | null;
9
+ slug: string;
10
+ id: string;
11
+ }
2
12
  /**
3
13
  * creates the url path for a post based on blog timezone and permalink pattern
4
14
  */
5
- declare function replacePermalink(permalink: any, resource: any, timezone?: string): any;
15
+ declare function replacePermalink(permalink: string, resource: PermalinkResource, timezone?: string): string;
16
+ export default replacePermalink;
@@ -1,11 +1,43 @@
1
1
  "use strict";
2
- // @ts-nocheck
3
- const moment = require('moment-timezone');
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ const moment = __importStar(require("moment-timezone"));
4
37
  /**
5
38
  * creates the url path for a post based on blog timezone and permalink pattern
6
39
  */
7
40
  function replacePermalink(permalink, resource, timezone = 'UTC') {
8
- const output = permalink;
9
41
  const primaryTagFallback = 'all';
10
42
  const publishedAtMoment = moment.tz(resource.published_at || Date.now(), timezone);
11
43
  const permalinkLookUp = {
@@ -19,7 +51,7 @@ function replacePermalink(permalink, resource, timezone = 'UTC') {
19
51
  return publishedAtMoment.format('DD');
20
52
  },
21
53
  author: function () {
22
- return resource.primary_author.slug;
54
+ return resource.primary_author?.slug ?? 'undefined';
23
55
  },
24
56
  primary_author: function () {
25
57
  return resource.primary_author ? resource.primary_author.slug : primaryTagFallback;
@@ -36,10 +68,14 @@ function replacePermalink(permalink, resource, timezone = 'UTC') {
36
68
  };
37
69
  // replace tags like :slug or :year with actual values
38
70
  const permalinkKeys = Object.keys(permalinkLookUp);
39
- return output.replace(/(:[a-z_]+)/g, function (match) {
40
- if (permalinkKeys.includes(match.substr(1))) {
41
- return permalinkLookUp[match.substr(1)]();
71
+ return permalink.replace(/(:[a-z_]+)/g, function (match) {
72
+ const key = match.slice(1);
73
+ if (permalinkKeys.includes(key)) {
74
+ // Known route segment - use the lookup function
75
+ return permalinkLookUp[key]();
42
76
  }
77
+ // Unknown route segment - return 'undefined' string
78
+ return 'undefined';
43
79
  });
44
80
  }
45
- module.exports = replacePermalink;
81
+ exports.default = replacePermalink;
@@ -1,4 +1,3 @@
1
- declare const URL: any;
2
1
  /**
3
2
  * Removes the directory in the root url from the relative path
4
3
  *
@@ -7,3 +6,4 @@ declare const URL: any;
7
6
  * @returns {string} Path relative to the rootUrl's path
8
7
  */
9
8
  declare const stripSubdirectoryFromPath: (path?: string, rootUrl?: string) => string;
9
+ export default stripSubdirectoryFromPath;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
- // @ts-nocheck
3
- const { URL } = require('url');
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const url_1 = require("url");
4
4
  /**
5
5
  * Removes the directory in the root url from the relative path
6
6
  *
@@ -15,9 +15,9 @@ const stripSubdirectoryFromPath = function stripSubdirectoryFromPath(path = '',
15
15
  }
16
16
  let parsedRoot;
17
17
  try {
18
- parsedRoot = new URL(rootUrl);
18
+ parsedRoot = new url_1.URL(rootUrl);
19
19
  }
20
- catch (e) {
20
+ catch {
21
21
  return path;
22
22
  }
23
23
  // do nothing if rootUrl does not have a subdirectory
@@ -29,4 +29,4 @@ const stripSubdirectoryFromPath = function stripSubdirectoryFromPath(path = '',
29
29
  }
30
30
  return path;
31
31
  };
32
- module.exports = stripSubdirectoryFromPath;
32
+ exports.default = stripSubdirectoryFromPath;
@@ -1,3 +1,5 @@
1
- declare const relativeToAbsolute: any;
2
- declare const absoluteToTransformReady: any;
3
- declare function toTransformReady(url: any, siteUrl: any, itemPath: any, options: any): any;
1
+ import type { AbsoluteToTransformReadyOptionsInput } from './absolute-to-transform-ready';
2
+ import { type RelativeToAbsoluteOptionsInput } from './relative-to-absolute';
3
+ export type ToTransformReadyOptions = RelativeToAbsoluteOptionsInput & AbsoluteToTransformReadyOptionsInput;
4
+ declare function toTransformReady(url: string, siteUrl: string, itemPath: string | null | ToTransformReadyOptions, options?: ToTransformReadyOptions): string;
5
+ export default toTransformReady;
@@ -1,13 +1,21 @@
1
1
  "use strict";
2
- // @ts-nocheck
3
- const relativeToAbsolute = require('./relative-to-absolute');
4
- const absoluteToTransformReady = require('./absolute-to-transform-ready');
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const relative_to_absolute_1 = __importDefault(require("./relative-to-absolute"));
7
+ const absolute_to_transform_ready_1 = __importDefault(require("./absolute-to-transform-ready"));
5
8
  function toTransformReady(url, siteUrl, itemPath, options) {
6
- if (typeof itemPath === 'object' && !options) {
7
- options = itemPath;
8
- itemPath = null;
9
+ let finalItemPath = null;
10
+ let finalOptions = options || {};
11
+ if (typeof itemPath === 'object' && itemPath !== null && !options) {
12
+ finalOptions = itemPath;
13
+ finalItemPath = null;
9
14
  }
10
- const absoluteUrl = relativeToAbsolute(url, siteUrl, itemPath, options);
11
- return absoluteToTransformReady(absoluteUrl, siteUrl, options);
15
+ else if (typeof itemPath === 'string') {
16
+ finalItemPath = itemPath;
17
+ }
18
+ const absoluteUrl = (0, relative_to_absolute_1.default)(url, siteUrl, finalItemPath, finalOptions);
19
+ return (0, absolute_to_transform_ready_1.default)(absoluteUrl, siteUrl, finalOptions);
12
20
  }
13
- module.exports = toTransformReady;
21
+ exports.default = toTransformReady;
@@ -1 +1,9 @@
1
- declare const transformReadyToAbsolute: (str: string | undefined, root: any, _options?: {}) => string;
1
+ import type { TransformReadyReplacementOptions, BaseUrlOptions } from './types';
2
+ export interface TransformReadyToAbsoluteOptions extends TransformReadyReplacementOptions, BaseUrlOptions {
3
+ staticImageUrlPrefix: string;
4
+ staticFilesUrlPrefix: string;
5
+ staticMediaUrlPrefix: string;
6
+ }
7
+ export type TransformReadyToAbsoluteOptionsInput = Partial<TransformReadyToAbsoluteOptions>;
8
+ declare const transformReadyToAbsolute: (str: string | undefined, root: string, _options?: TransformReadyToAbsoluteOptionsInput) => string;
9
+ export default transformReadyToAbsolute;
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- // @ts-nocheck
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  function escapeRegExp(string) {
4
4
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
5
5
  }
@@ -32,4 +32,4 @@ const transformReadyToAbsolute = function (str = '', root, _options = {}) {
32
32
  return root.replace(/\/$/, '');
33
33
  });
34
34
  };
35
- module.exports = transformReadyToAbsolute;
35
+ exports.default = transformReadyToAbsolute;
@@ -1 +1,3 @@
1
- declare const transformReadyToRelative: (str: string | undefined, root: any, _options?: {}) => string;
1
+ import type { TransformReadyReplacementOptionsInput } from './types';
2
+ declare const transformReadyToRelative: (str: string | undefined, root: string, _options?: TransformReadyReplacementOptionsInput) => string;
3
+ export default transformReadyToRelative;
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
- // @ts-nocheck
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const url_1 = require("url");
3
4
  function escapeRegExp(string) {
4
5
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
5
6
  }
@@ -11,10 +12,10 @@ const transformReadyToRelative = function (str = '', root, _options = {}) {
11
12
  if (!str || str.indexOf(options.replacementStr) === -1) {
12
13
  return str;
13
14
  }
14
- const rootURL = new URL(root);
15
+ const rootURL = new url_1.URL(root);
15
16
  // subdir with no trailing slash because we'll always have a trailing slash after the magic string
16
17
  const subdir = rootURL.pathname.replace(/\/$/, '');
17
18
  const replacementRegex = new RegExp(escapeRegExp(options.replacementStr), 'g');
18
19
  return str.replace(replacementRegex, subdir);
19
20
  };
20
- module.exports = transformReadyToRelative;
21
+ exports.default = transformReadyToRelative;
@@ -0,0 +1,92 @@
1
+ export type UnknownRecord = Record<string, unknown>;
2
+ export interface AssetAwareOptions extends UnknownRecord {
3
+ assetsOnly: boolean;
4
+ staticImageUrlPrefix?: string;
5
+ }
6
+ export type AssetAwareOptionsInput = Partial<AssetAwareOptions>;
7
+ export interface SecureOptions extends AssetAwareOptions {
8
+ secure?: boolean;
9
+ }
10
+ export type SecureOptionsInput = Partial<SecureOptions>;
11
+ export interface TransformReadyReplacementOptions extends UnknownRecord {
12
+ replacementStr: string;
13
+ withoutSubdirectory?: boolean;
14
+ }
15
+ export type TransformReadyReplacementOptionsInput = Partial<TransformReadyReplacementOptions>;
16
+ export interface BaseUrlOptions extends UnknownRecord {
17
+ imageBaseUrl?: string | null;
18
+ filesBaseUrl?: string | null;
19
+ mediaBaseUrl?: string | null;
20
+ }
21
+ export type BaseUrlOptionsInput = Partial<BaseUrlOptions>;
22
+ export interface EarlyExitOptions extends UnknownRecord {
23
+ earlyExitMatchStr?: string;
24
+ }
25
+ export type EarlyExitOptionsInput = Partial<EarlyExitOptions>;
26
+ export interface AbsoluteToTransformReadyOptions extends TransformReadyReplacementOptions, BaseUrlOptions, AssetAwareOptions {
27
+ staticFilesUrlPrefix?: string;
28
+ staticMediaUrlPrefix?: string;
29
+ }
30
+ export type AbsoluteToTransformReadyOptionsInput = Partial<AbsoluteToTransformReadyOptions>;
31
+ export type UrlTransformFunction = (url: string, siteUrl: string, itemPath: string | null, options: UnknownRecord) => string;
32
+ export type TransformType = 'relativeToAbsolute' | 'absoluteToRelative' | 'toTransformReady';
33
+ export interface HtmlTransformOptions extends SecureOptions {
34
+ earlyExitMatchStr?: string;
35
+ }
36
+ export type HtmlTransformOptionsInput = SecureOptionsInput & {
37
+ earlyExitMatchStr?: string;
38
+ };
39
+ export interface MarkdownTransformOptions extends AssetAwareOptions {
40
+ ignoreProtocol: boolean;
41
+ earlyExitMatchStr?: string;
42
+ }
43
+ export type MarkdownTransformOptionsInput = AssetAwareOptionsInput & {
44
+ ignoreProtocol?: boolean;
45
+ earlyExitMatchStr?: string;
46
+ };
47
+ export interface MarkdownTransformFunctions {
48
+ html: UrlTransformFunction;
49
+ url: UrlTransformFunction;
50
+ }
51
+ export type CardTransformer = (payload: unknown, options: UnknownRecord) => unknown;
52
+ export interface MobiledocCardTransformer {
53
+ name: string;
54
+ relativeToAbsolute?: CardTransformer;
55
+ absoluteToRelative?: CardTransformer;
56
+ toTransformReady?: CardTransformer;
57
+ }
58
+ export interface MobiledocTransformOptions extends SecureOptions {
59
+ cardTransformers: MobiledocCardTransformer[];
60
+ siteUrl: string;
61
+ itemPath: string | null;
62
+ transformType: TransformType;
63
+ }
64
+ export type MobiledocTransformOptionsInput = Partial<Omit<MobiledocTransformOptions, 'cardTransformers' | 'siteUrl' | 'itemPath' | 'transformType'>> & {
65
+ cardTransformers?: MobiledocCardTransformer[];
66
+ siteUrl?: string;
67
+ itemPath?: string | null;
68
+ transformType?: TransformType;
69
+ };
70
+ export type LexicalTransformFunction = (value: string) => string;
71
+ export interface LexicalUrlTransformMap {
72
+ [key: string]: string | LexicalUrlTransformMap;
73
+ }
74
+ export interface LexicalNodeConfig {
75
+ getType(): string;
76
+ urlTransformMap?: LexicalUrlTransformMap;
77
+ }
78
+ export type LexicalTransformRegistry = Partial<Record<TransformType, Record<string, LexicalTransformFunction>>>;
79
+ export interface LexicalTransformOptions extends SecureOptions {
80
+ nodes: LexicalNodeConfig[];
81
+ transformMap: LexicalTransformRegistry;
82
+ transformType: TransformType;
83
+ siteUrl: string;
84
+ itemPath: string | null;
85
+ }
86
+ export type LexicalTransformOptionsInput = Partial<Omit<LexicalTransformOptions, 'nodes' | 'transformMap' | 'siteUrl' | 'itemPath' | 'transformType'>> & {
87
+ nodes?: LexicalNodeConfig[];
88
+ transformMap?: LexicalTransformRegistry;
89
+ siteUrl?: string;
90
+ itemPath?: string | null;
91
+ transformType?: TransformType;
92
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,4 +1,6 @@
1
- declare const deduplicateSubdirectory: any;
1
+ interface UrlJoinOptions {
2
+ rootUrl: string;
3
+ }
2
4
  /** urlJoin
3
5
  * Returns a URL/path for internal use in Ghost.
4
6
  * @param {string[]} parts takes parts and concats those to a valid path/URL.
@@ -6,4 +8,5 @@ declare const deduplicateSubdirectory: any;
6
8
  * @param {string} options.rootUrl used for deduplicating any subdirectories
7
9
  * @return {string} URL concatinated URL/path of arguments.
8
10
  */
9
- declare function urlJoin(parts: any, options: any): any;
11
+ declare function urlJoin(parts: string[], options?: UrlJoinOptions): string;
12
+ export default urlJoin;
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
- // @ts-nocheck
3
- const deduplicateSubdirectory = require('./deduplicate-subdirectory');
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const deduplicate_subdirectory_1 = __importDefault(require("./deduplicate-subdirectory"));
4
7
  /** urlJoin
5
8
  * Returns a URL/path for internal use in Ghost.
6
9
  * @param {string[]} parts takes parts and concats those to a valid path/URL.
@@ -8,7 +11,7 @@ const deduplicateSubdirectory = require('./deduplicate-subdirectory');
8
11
  * @param {string} options.rootUrl used for deduplicating any subdirectories
9
12
  * @return {string} URL concatinated URL/path of arguments.
10
13
  */
11
- function urlJoin(parts, options) {
14
+ function urlJoin(parts, options = { rootUrl: '' }) {
12
15
  let prefixDoubleSlash = false;
13
16
  // Remove empty item at the beginning
14
17
  if (parts[0] === '') {
@@ -26,6 +29,9 @@ function urlJoin(parts, options) {
26
29
  if (prefixDoubleSlash) {
27
30
  url = url.replace(/^\//, '//');
28
31
  }
29
- return deduplicateSubdirectory(url, options.rootUrl);
32
+ if (!options.rootUrl) {
33
+ return url;
34
+ }
35
+ return (0, deduplicate_subdirectory_1.default)(url, options.rootUrl);
30
36
  }
31
- module.exports = urlJoin;
37
+ exports.default = urlJoin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/url-utils",
3
- "version": "5.0.0",
3
+ "version": "5.1.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/TryGhost/SDK.git",
@@ -8,26 +8,25 @@
8
8
  },
9
9
  "author": "Ghost Foundation",
10
10
  "license": "MIT",
11
- "main": "index.js",
12
- "types": "lib/UrlUtils.d.ts",
11
+ "main": "lib/index.js",
12
+ "types": "lib/index.d.ts",
13
13
  "scripts": {
14
14
  "dev": "echo \"Implement me!\"",
15
15
  "pretest": "yarn build",
16
16
  "test": "NODE_ENV=testing c8 --src lib --all --reporter text --reporter cobertura --reporter html mocha './test/**/*.test.js'",
17
17
  "build": "tsc -p tsconfig.json",
18
- "lint": "eslint src test index.js --ext .js,.ts --cache",
18
+ "lint": "eslint src test --ext .js,.ts --cache",
19
19
  "prepare": "NODE_ENV=production yarn build",
20
20
  "posttest": "yarn lint"
21
21
  },
22
22
  "files": [
23
- "lib/",
24
- "index.js"
23
+ "lib/"
25
24
  ],
26
25
  "publishConfig": {
27
26
  "access": "public"
28
27
  },
29
28
  "devDependencies": {
30
- "@tryghost/config-url-helpers": "^1.0.19",
29
+ "@tryghost/config-url-helpers": "^1.0.20",
31
30
  "@types/cheerio": "^0.22.31",
32
31
  "@types/lodash": "^4.14.202",
33
32
  "@types/node": "^20.0.0",
@@ -35,7 +34,7 @@
35
34
  "mocha": "11.7.5",
36
35
  "rewire": "9.0.1",
37
36
  "should": "13.2.3",
38
- "sinon": "21.0.0",
37
+ "sinon": "21.0.1",
39
38
  "typescript": "5.9.3"
40
39
  },
41
40
  "dependencies": {
@@ -43,9 +42,9 @@
43
42
  "lodash": "^4.17.21",
44
43
  "moment": "^2.27.0",
45
44
  "moment-timezone": "^0.5.31",
46
- "remark": "^11.0.2",
47
- "remark-footnotes": "^1.0.0",
45
+ "remark": "11.0.2",
46
+ "remark-footnotes": "1.0.0",
48
47
  "unist-util-visit": "^2.0.0"
49
48
  },
50
- "gitHead": "91913b7a8e8afc3ddc411eceacb3d5b271671bd6"
49
+ "gitHead": "a5b249c5514742350f74bcb30b332977d373598a"
51
50
  }
package/index.js DELETED
@@ -1,2 +0,0 @@
1
- module.exports = require('./lib/UrlUtils');
2
-