@tryghost/url-utils 5.0.0 → 5.1.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 (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 +13 -8
  7. package/lib/utils/absolute-to-transform-ready.d.ts +13 -4
  8. package/lib/utils/absolute-to-transform-ready.js +15 -10
  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 +9 -6
  21. package/lib/utils/html-relative-to-transform-ready.d.ts +3 -3
  22. package/lib/utils/html-relative-to-transform-ready.js +17 -9
  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 +3 -4
  26. package/lib/utils/html-transform.js +18 -19
  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 +21 -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 +9 -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 +25 -18
  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 +4 -4
  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 +8 -9
  92. package/index.js +0 -2
package/lib/UrlUtils.js CHANGED
@@ -1,8 +1,44 @@
1
1
  "use strict";
2
- // @ts-nocheck
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
3
39
  // Contains all path information to be used throughout the codebase.
4
- const _ = require('lodash');
5
- const utils = require('./utils');
40
+ const _ = __importStar(require("lodash"));
41
+ const utils_1 = __importDefault(require("./utils"));
6
42
  // similar to Object.assign but will not override defaults if a source value is undefined
7
43
  function assignOptions(target, ...sources) {
8
44
  const options = sources.map((x) => {
@@ -12,7 +48,12 @@ function assignOptions(target, ...sources) {
12
48
  });
13
49
  return Object.assign(target, ...options);
14
50
  }
15
- module.exports = class UrlUtils {
51
+ class UrlUtils {
52
+ _config;
53
+ _assetBaseUrls;
54
+ getSubdir;
55
+ getSiteUrl;
56
+ getAdminUrl;
16
57
  /**
17
58
  * Initialization method to pass in URL configurations
18
59
  * @param {Object} options
@@ -48,9 +89,9 @@ module.exports = class UrlUtils {
48
89
  files: assetBaseUrls.files || null,
49
90
  media: assetBaseUrls.media || null
50
91
  };
51
- this.getSubdir = options.getSubdir;
52
- this.getSiteUrl = options.getSiteUrl;
53
- this.getAdminUrl = options.getAdminUrl;
92
+ this.getSubdir = options.getSubdir || (() => '');
93
+ this.getSiteUrl = options.getSiteUrl || (() => '');
94
+ this.getAdminUrl = options.getAdminUrl || (() => '');
54
95
  }
55
96
  _assetOptionDefaults() {
56
97
  return {
@@ -66,12 +107,16 @@ module.exports = class UrlUtils {
66
107
  return assignOptions({}, this._assetOptionDefaults(), additionalDefaults, options || {});
67
108
  }
68
109
  getProtectedSlugs() {
69
- let subDir = this.getSubdir();
110
+ const subDir = this.getSubdir();
70
111
  if (!_.isEmpty(subDir)) {
71
- return this._config.slugs.concat([subDir.split('/').pop()]);
112
+ const lastSegment = subDir.split('/').pop();
113
+ if (this._config.slugs && lastSegment) {
114
+ return this._config.slugs.concat([lastSegment]);
115
+ }
116
+ return lastSegment ? [lastSegment] : [];
72
117
  }
73
118
  else {
74
- return this._config.slugs;
119
+ return this._config.slugs || [];
75
120
  }
76
121
  }
77
122
  /** urlJoin
@@ -80,7 +125,7 @@ module.exports = class UrlUtils {
80
125
  * @return {string} URL concatinated URL/path of arguments.
81
126
  */
82
127
  urlJoin(...parts) {
83
- return utils.urlJoin(parts, { rootUrl: this.getSiteUrl() });
128
+ return utils_1.default.urlJoin(parts, { rootUrl: this.getSiteUrl() });
84
129
  }
85
130
  // ## createUrl
86
131
  // Simple url creation from a given path
@@ -129,11 +174,11 @@ module.exports = class UrlUtils {
129
174
  urlFor(context, data, absolute) {
130
175
  let urlPath = '/';
131
176
  let imagePathRe;
132
- let knownObjects = ['image', 'nav'];
177
+ const knownObjects = ['image', 'nav'];
133
178
  let baseUrl;
134
179
  let hostname;
135
180
  // this will become really big
136
- let knownPaths = {
181
+ const knownPaths = {
137
182
  home: '/',
138
183
  sitemap_xsl: '/sitemap.xsl'
139
184
  };
@@ -142,14 +187,15 @@ module.exports = class UrlUtils {
142
187
  absolute = data;
143
188
  data = null;
144
189
  }
145
- if (_.isObject(context) && context.relativeUrl) {
146
- urlPath = context.relativeUrl;
190
+ if (_.isObject(context) && !_.isArray(context) && 'relativeUrl' in context && typeof context.relativeUrl === 'string') {
191
+ const relativeUrl = context.relativeUrl;
192
+ urlPath = relativeUrl || '/';
147
193
  }
148
194
  else if (_.isString(context) && _.indexOf(knownObjects, context) !== -1) {
149
- if (context === 'image' && data.image) {
195
+ if (context === 'image' && data && typeof data === 'object' && !_.isArray(data) && 'image' in data && typeof data.image === 'string') {
150
196
  urlPath = data.image;
151
197
  imagePathRe = new RegExp('^' + this.getSubdir() + '/' + this._config.staticImageUrlPrefix);
152
- absolute = imagePathRe.test(data.image) ? absolute : false;
198
+ absolute = imagePathRe.test(urlPath) ? (absolute || false) : false;
153
199
  if (absolute) {
154
200
  // Remove the sub-directory from the URL because ghostConfig will add it back.
155
201
  urlPath = urlPath.replace(new RegExp('^' + this.getSubdir()), '');
@@ -158,7 +204,7 @@ module.exports = class UrlUtils {
158
204
  }
159
205
  return urlPath;
160
206
  }
161
- else if (context === 'nav' && data.nav) {
207
+ else if (context === 'nav' && data && typeof data === 'object' && !_.isArray(data) && 'nav' in data && data.nav && typeof data.nav === 'object' && !_.isArray(data.nav) && 'url' in data.nav && typeof data.nav.url === 'string') {
162
208
  urlPath = data.nav.url;
163
209
  baseUrl = this.getSiteUrl();
164
210
  hostname = baseUrl.split('//')[1];
@@ -179,12 +225,12 @@ module.exports = class UrlUtils {
179
225
  urlPath = this.getSiteUrl();
180
226
  // CASE: there are cases where urlFor('home') needs to be returned without trailing
181
227
  // slash e. g. the `{{@site.url}}` helper. See https://github.com/TryGhost/Ghost/issues/8569
182
- if (data && data.trailingSlash === false) {
228
+ if (data && typeof data === 'object' && !_.isArray(data) && 'trailingSlash' in data && data.trailingSlash === false) {
183
229
  urlPath = urlPath.replace(/\/$/, '');
184
230
  }
185
231
  }
186
232
  else if (context === 'admin') {
187
- let adminUrl = this.getAdminUrl() || this.getSiteUrl();
233
+ const adminUrl = this.getAdminUrl() || this.getSiteUrl();
188
234
  let adminPath = '/ghost/';
189
235
  if (absolute) {
190
236
  urlPath = this.urlJoin(adminUrl, adminPath);
@@ -194,9 +240,9 @@ module.exports = class UrlUtils {
194
240
  }
195
241
  }
196
242
  else if (context === 'api') {
197
- let adminUrl = this.getAdminUrl() || this.getSiteUrl();
243
+ const adminUrl = this.getAdminUrl() || this.getSiteUrl();
198
244
  let apiPath = this._config.baseApiPath + '/';
199
- if (data.type && ['admin', 'content'].includes(data.type)) {
245
+ if (data && typeof data === 'object' && !_.isArray(data) && 'type' in data && typeof data.type === 'string' && ['admin', 'content'].includes(data.type)) {
200
246
  apiPath += data.type;
201
247
  }
202
248
  else {
@@ -224,52 +270,65 @@ module.exports = class UrlUtils {
224
270
  }
225
271
  redirect301(res, redirectUrl) {
226
272
  res.set({ 'Cache-Control': 'public, max-age=' + this._config.redirectCacheMaxAge });
227
- return res.redirect(301, redirectUrl);
273
+ res.redirect(301, redirectUrl);
228
274
  }
229
275
  redirectToAdmin(status, res, adminPath) {
230
- let redirectUrl = this.urlJoin(this.urlFor('admin', true), adminPath, '/');
276
+ const redirectUrl = this.urlJoin(this.urlFor('admin', true), adminPath, '/');
231
277
  if (status === 301) {
232
- return this.redirect301(res, redirectUrl);
278
+ this.redirect301(res, redirectUrl);
279
+ }
280
+ else {
281
+ res.redirect(redirectUrl);
233
282
  }
234
- return res.redirect(redirectUrl);
235
283
  }
236
284
  absoluteToRelative(url, options) {
237
- return utils.absoluteToRelative(url, this.getSiteUrl(), options);
285
+ return utils_1.default.absoluteToRelative(url, this.getSiteUrl(), options);
238
286
  }
239
287
  relativeToAbsolute(url, options) {
240
- return utils.relativeToAbsolute(url, this.getSiteUrl(), options);
288
+ // Original code passes options as third parameter (itemPath), preserving that behavior
289
+ return utils_1.default.relativeToAbsolute(url, this.getSiteUrl(), options || null, undefined);
241
290
  }
242
291
  toTransformReady(url, itemPath, options) {
243
- if (typeof itemPath === 'object' && !options) {
244
- options = itemPath;
245
- itemPath = null;
292
+ let finalItemPath = null;
293
+ let finalOptions = options;
294
+ if (typeof itemPath === 'object' && itemPath !== null && !options) {
295
+ finalOptions = itemPath;
296
+ finalItemPath = null;
246
297
  }
247
- const _options = this._buildAssetOptions({}, options);
248
- return utils.toTransformReady(url, this.getSiteUrl(), itemPath, _options);
298
+ else if (typeof itemPath === 'string') {
299
+ finalItemPath = itemPath;
300
+ }
301
+ const _options = this._buildAssetOptions({}, finalOptions);
302
+ return utils_1.default.toTransformReady(url, this.getSiteUrl(), finalItemPath, _options);
249
303
  }
250
304
  absoluteToTransformReady(url, options) {
251
305
  const _options = this._buildAssetOptions({}, options);
252
- return utils.absoluteToTransformReady(url, this.getSiteUrl(), _options);
306
+ return utils_1.default.absoluteToTransformReady(url, this.getSiteUrl(), _options);
253
307
  }
254
308
  relativeToTransformReady(url, options) {
255
309
  const _options = this._buildAssetOptions({}, options);
256
- return utils.relativeToTransformReady(url, this.getSiteUrl(), _options);
310
+ return utils_1.default.relativeToTransformReady(url, this.getSiteUrl(), _options);
257
311
  }
258
312
  transformReadyToAbsolute(url, options) {
259
313
  const _options = this._buildAssetOptions({}, options);
260
- return utils.transformReadyToAbsolute(url, this.getSiteUrl(), _options);
314
+ return utils_1.default.transformReadyToAbsolute(url, this.getSiteUrl(), _options);
261
315
  }
262
316
  transformReadyToRelative(url, options) {
263
317
  const _options = this._buildAssetOptions({}, options);
264
- return utils.transformReadyToRelative(url, this.getSiteUrl(), _options);
318
+ return utils_1.default.transformReadyToRelative(url, this.getSiteUrl(), _options);
265
319
  }
266
320
  htmlToTransformReady(html, itemPath, options) {
267
- if (typeof itemPath === 'object' && !options) {
268
- options = itemPath;
269
- itemPath = null;
321
+ let finalItemPath = null;
322
+ let finalOptions = options;
323
+ if (typeof itemPath === 'object' && itemPath !== null && !options) {
324
+ finalOptions = itemPath;
325
+ finalItemPath = null;
270
326
  }
271
- const _options = this._buildAssetOptions({}, options);
272
- return utils.htmlToTransformReady(html, this.getSiteUrl(), itemPath, _options);
327
+ else if (typeof itemPath === 'string') {
328
+ finalItemPath = itemPath;
329
+ }
330
+ const _options = this._buildAssetOptions({}, finalOptions);
331
+ return utils_1.default.htmlToTransformReady(html, this.getSiteUrl(), finalItemPath, _options);
273
332
  }
274
333
  /**
275
334
  * Convert relative URLs in html into absolute URLs
@@ -282,172 +341,227 @@ module.exports = class UrlUtils {
282
341
  * the variable that takes the result of this function
283
342
  */
284
343
  htmlRelativeToAbsolute(html, itemPath, options) {
285
- if (typeof itemPath === 'object' && !options) {
286
- options = itemPath;
287
- itemPath = null;
344
+ let finalItemPath = null;
345
+ let finalOptions = options;
346
+ if (typeof itemPath === 'object' && itemPath !== null && !options) {
347
+ finalOptions = itemPath;
348
+ finalItemPath = null;
349
+ }
350
+ else if (typeof itemPath === 'string') {
351
+ finalItemPath = itemPath;
288
352
  }
289
353
  const _options = this._buildAssetOptions({
290
354
  assetsOnly: false
291
- }, options);
292
- return utils.htmlRelativeToAbsolute(html, this.getSiteUrl(), itemPath, _options);
355
+ }, finalOptions);
356
+ return utils_1.default.htmlRelativeToAbsolute(html, this.getSiteUrl(), finalItemPath, _options);
293
357
  }
294
358
  htmlRelativeToTransformReady(html, itemPath, options) {
295
- if (typeof itemPath === 'object' && !options) {
296
- options = itemPath;
297
- itemPath = null;
359
+ let finalItemPath = null;
360
+ let finalOptions = options;
361
+ if (typeof itemPath === 'object' && itemPath !== null && !options) {
362
+ finalOptions = itemPath;
363
+ finalItemPath = null;
364
+ }
365
+ else if (typeof itemPath === 'string') {
366
+ finalItemPath = itemPath;
298
367
  }
299
368
  const _options = this._buildAssetOptions({
300
369
  assetsOnly: false
301
- }, options);
302
- return utils.htmlRelativeToTransformReady(html, this.getSiteUrl(), itemPath, _options);
370
+ }, finalOptions);
371
+ return utils_1.default.htmlRelativeToTransformReady(html, this.getSiteUrl(), finalItemPath, _options);
303
372
  }
304
373
  htmlAbsoluteToRelative(html, options = {}) {
305
374
  const _options = this._buildAssetOptions({
306
375
  assetsOnly: false
307
376
  }, options);
308
- return utils.htmlAbsoluteToRelative(html, this.getSiteUrl(), _options);
377
+ return utils_1.default.htmlAbsoluteToRelative(html, this.getSiteUrl(), _options);
309
378
  }
310
379
  htmlAbsoluteToTransformReady(html, options = {}) {
311
380
  const _options = this._buildAssetOptions({
312
381
  assetsOnly: false
313
382
  }, options);
314
- return utils.htmlAbsoluteToTransformReady(html, this.getSiteUrl(), _options);
383
+ return utils_1.default.htmlAbsoluteToTransformReady(html, this.getSiteUrl(), _options);
315
384
  }
316
385
  markdownToTransformReady(markdown, itemPath, options) {
317
- if (typeof itemPath === 'object' && !options) {
318
- options = itemPath;
319
- itemPath = null;
386
+ let finalItemPath = null;
387
+ let finalOptions = options;
388
+ if (typeof itemPath === 'object' && itemPath !== null && !options) {
389
+ finalOptions = itemPath;
390
+ finalItemPath = null;
320
391
  }
321
- const _options = this._buildAssetOptions({}, options);
322
- return utils.markdownToTransformReady(markdown, this.getSiteUrl(), itemPath, _options);
392
+ else if (typeof itemPath === 'string') {
393
+ finalItemPath = itemPath;
394
+ }
395
+ const _options = this._buildAssetOptions({}, finalOptions);
396
+ return utils_1.default.markdownToTransformReady(markdown, this.getSiteUrl(), finalItemPath, _options);
323
397
  }
324
398
  markdownRelativeToAbsolute(markdown, itemPath, options) {
325
- if (typeof itemPath === 'object' && !options) {
326
- options = itemPath;
327
- itemPath = null;
399
+ let finalItemPath = null;
400
+ let finalOptions = options;
401
+ if (typeof itemPath === 'object' && itemPath !== null && !options) {
402
+ finalOptions = itemPath;
403
+ finalItemPath = null;
404
+ }
405
+ else if (typeof itemPath === 'string') {
406
+ finalItemPath = itemPath;
328
407
  }
329
408
  const _options = this._buildAssetOptions({
330
409
  assetsOnly: false
331
- }, options);
332
- return utils.markdownRelativeToAbsolute(markdown, this.getSiteUrl(), itemPath, _options);
410
+ }, finalOptions);
411
+ return utils_1.default.markdownRelativeToAbsolute(markdown, this.getSiteUrl(), finalItemPath, _options);
333
412
  }
334
413
  markdownRelativeToTransformReady(markdown, itemPath, options) {
335
- if (typeof itemPath === 'object' && !options) {
336
- options = itemPath;
337
- itemPath = null;
414
+ let finalItemPath = null;
415
+ let finalOptions = options;
416
+ if (typeof itemPath === 'object' && itemPath !== null && !options) {
417
+ finalOptions = itemPath;
418
+ finalItemPath = null;
419
+ }
420
+ else if (typeof itemPath === 'string') {
421
+ finalItemPath = itemPath;
338
422
  }
339
423
  const _options = this._buildAssetOptions({
340
424
  assetsOnly: false
341
- }, options);
342
- return utils.markdownRelativeToTransformReady(markdown, this.getSiteUrl(), itemPath, _options);
425
+ }, finalOptions);
426
+ return utils_1.default.markdownRelativeToTransformReady(markdown, this.getSiteUrl(), finalItemPath, _options);
343
427
  }
344
428
  markdownAbsoluteToRelative(markdown, options = {}) {
345
429
  const _options = this._buildAssetOptions({
346
430
  assetsOnly: false
347
431
  }, options);
348
- return utils.markdownAbsoluteToRelative(markdown, this.getSiteUrl(), _options);
432
+ return utils_1.default.markdownAbsoluteToRelative(markdown, this.getSiteUrl(), _options);
349
433
  }
350
- markdownAbsoluteToTransformReady(markdown, options) {
434
+ markdownAbsoluteToTransformReady(markdown, options = {}) {
351
435
  const _options = this._buildAssetOptions({
352
436
  assetsOnly: false
353
437
  }, options);
354
- return utils.markdownAbsoluteToTransformReady(markdown, this.getSiteUrl(), _options);
438
+ return utils_1.default.markdownAbsoluteToTransformReady(markdown, this.getSiteUrl(), _options);
355
439
  }
356
440
  mobiledocToTransformReady(serializedMobiledoc, itemPath, options) {
357
- if (typeof itemPath === 'object' && !options) {
358
- options = itemPath;
359
- itemPath = null;
441
+ let finalItemPath = null;
442
+ let finalOptions = options;
443
+ if (typeof itemPath === 'object' && itemPath !== null && !options) {
444
+ finalOptions = itemPath;
445
+ finalItemPath = null;
446
+ }
447
+ else if (typeof itemPath === 'string') {
448
+ finalItemPath = itemPath;
360
449
  }
361
450
  const _options = this._buildAssetOptions({
362
451
  cardTransformers: this._config.cardTransformers
363
- }, options);
364
- return utils.mobiledocToTransformReady(serializedMobiledoc, this.getSiteUrl(), itemPath, _options);
452
+ }, finalOptions);
453
+ return utils_1.default.mobiledocToTransformReady(serializedMobiledoc, this.getSiteUrl(), finalItemPath, _options);
365
454
  }
366
455
  mobiledocRelativeToAbsolute(serializedMobiledoc, itemPath, options) {
367
- if (typeof itemPath === 'object' && !options) {
368
- options = itemPath;
369
- itemPath = null;
456
+ let finalItemPath = null;
457
+ let finalOptions = options;
458
+ if (typeof itemPath === 'object' && itemPath !== null && !options) {
459
+ finalOptions = itemPath;
460
+ finalItemPath = null;
461
+ }
462
+ else if (typeof itemPath === 'string') {
463
+ finalItemPath = itemPath;
370
464
  }
371
465
  const _options = this._buildAssetOptions({
372
466
  assetsOnly: false,
373
467
  cardTransformers: this._config.cardTransformers
374
- }, options);
375
- return utils.mobiledocRelativeToAbsolute(serializedMobiledoc, this.getSiteUrl(), itemPath, _options);
468
+ }, finalOptions);
469
+ return utils_1.default.mobiledocRelativeToAbsolute(serializedMobiledoc, this.getSiteUrl(), finalItemPath, _options);
376
470
  }
377
471
  mobiledocRelativeToTransformReady(serializedMobiledoc, itemPath, options) {
378
- if (typeof itemPath === 'object' && !options) {
379
- options = itemPath;
380
- itemPath = null;
472
+ let finalItemPath = null;
473
+ let finalOptions = options;
474
+ if (typeof itemPath === 'object' && itemPath !== null && !options) {
475
+ finalOptions = itemPath;
476
+ finalItemPath = null;
477
+ }
478
+ else if (typeof itemPath === 'string') {
479
+ finalItemPath = itemPath;
381
480
  }
382
481
  const _options = this._buildAssetOptions({
383
482
  assetsOnly: false,
384
483
  cardTransformers: this._config.cardTransformers
385
- }, options);
386
- return utils.mobiledocRelativeToTransformReady(serializedMobiledoc, this.getSiteUrl(), itemPath, _options);
484
+ }, finalOptions);
485
+ return utils_1.default.mobiledocRelativeToTransformReady(serializedMobiledoc, this.getSiteUrl(), finalItemPath, _options);
387
486
  }
388
487
  mobiledocAbsoluteToRelative(serializedMobiledoc, options = {}) {
389
488
  const _options = this._buildAssetOptions({
390
489
  assetsOnly: false,
391
490
  cardTransformers: this._config.cardTransformers
392
491
  }, options);
393
- return utils.mobiledocAbsoluteToRelative(serializedMobiledoc, this.getSiteUrl(), _options);
492
+ return utils_1.default.mobiledocAbsoluteToRelative(serializedMobiledoc, this.getSiteUrl(), _options);
394
493
  }
395
494
  mobiledocAbsoluteToTransformReady(serializedMobiledoc, options = {}) {
396
495
  const _options = this._buildAssetOptions({
397
496
  assetsOnly: false,
398
497
  cardTransformers: this._config.cardTransformers
399
498
  }, options);
400
- return utils.mobiledocAbsoluteToTransformReady(serializedMobiledoc, this.getSiteUrl(), _options);
499
+ return utils_1.default.mobiledocAbsoluteToTransformReady(serializedMobiledoc, this.getSiteUrl(), _options);
401
500
  }
402
501
  lexicalToTransformReady(serializedLexical, itemPath, options) {
403
- if (typeof itemPath === 'object' && !options) {
404
- options = itemPath;
405
- itemPath = null;
502
+ let finalItemPath = null;
503
+ let finalOptions = options;
504
+ if (typeof itemPath === 'object' && itemPath !== null && !options) {
505
+ finalOptions = itemPath;
506
+ finalItemPath = null;
507
+ }
508
+ else if (typeof itemPath === 'string') {
509
+ finalItemPath = itemPath;
406
510
  }
407
511
  const _options = this._buildAssetOptions({
408
512
  cardTransformers: this._config.cardTransformers
409
- }, options);
410
- return utils.lexicalToTransformReady(serializedLexical, this.getSiteUrl(), itemPath, _options);
513
+ }, finalOptions);
514
+ return utils_1.default.lexicalToTransformReady(serializedLexical, this.getSiteUrl(), finalItemPath, _options);
411
515
  }
412
516
  lexicalRelativeToAbsolute(serializedLexical, itemPath, options) {
413
- if (typeof itemPath === 'object' && !options) {
414
- options = itemPath;
415
- itemPath = null;
517
+ let finalItemPath = null;
518
+ let finalOptions = options;
519
+ if (typeof itemPath === 'object' && itemPath !== null && !options) {
520
+ finalOptions = itemPath;
521
+ finalItemPath = null;
522
+ }
523
+ else if (typeof itemPath === 'string') {
524
+ finalItemPath = itemPath;
416
525
  }
417
526
  const _options = this._buildAssetOptions({
418
527
  assetsOnly: false,
419
528
  cardTransformers: this._config.cardTransformers
420
- }, options);
421
- return utils.lexicalRelativeToAbsolute(serializedLexical, this.getSiteUrl(), itemPath, _options);
529
+ }, finalOptions);
530
+ return utils_1.default.lexicalRelativeToAbsolute(serializedLexical, this.getSiteUrl(), finalItemPath, _options);
422
531
  }
423
532
  lexicalRelativeToTransformReady(serializedLexical, itemPath, options) {
424
- if (typeof itemPath === 'object' && !options) {
425
- options = itemPath;
426
- itemPath = null;
533
+ let finalItemPath = null;
534
+ let finalOptions = options;
535
+ if (typeof itemPath === 'object' && itemPath !== null && !options) {
536
+ finalOptions = itemPath;
537
+ finalItemPath = null;
538
+ }
539
+ else if (typeof itemPath === 'string') {
540
+ finalItemPath = itemPath;
427
541
  }
428
542
  const _options = this._buildAssetOptions({
429
543
  assetsOnly: false,
430
544
  cardTransformers: this._config.cardTransformers
431
- }, options);
432
- return utils.lexicalRelativeToTransformReady(serializedLexical, this.getSiteUrl(), itemPath, _options);
545
+ }, finalOptions);
546
+ return utils_1.default.lexicalRelativeToTransformReady(serializedLexical, this.getSiteUrl(), finalItemPath, _options);
433
547
  }
434
548
  lexicalAbsoluteToRelative(serializedLexical, options = {}) {
435
549
  const _options = this._buildAssetOptions({
436
550
  assetsOnly: false,
437
551
  cardTransformers: this._config.cardTransformers
438
552
  }, options);
439
- return utils.lexicalAbsoluteToRelative(serializedLexical, this.getSiteUrl(), _options);
553
+ return utils_1.default.lexicalAbsoluteToRelative(serializedLexical, this.getSiteUrl(), _options);
440
554
  }
441
555
  lexicalAbsoluteToTransformReady(serializedLexical, options = {}) {
442
556
  const _options = this._buildAssetOptions({
443
557
  assetsOnly: false,
444
558
  cardTransformers: this._config.cardTransformers
445
559
  }, options);
446
- return utils.lexicalAbsoluteToTransformReady(serializedLexical, this.getSiteUrl(), _options);
560
+ return utils_1.default.lexicalAbsoluteToTransformReady(serializedLexical, this.getSiteUrl(), _options);
447
561
  }
448
562
  plaintextToTransformReady(plaintext, options = {}) {
449
563
  const _options = this._buildAssetOptions({}, options);
450
- return utils.plaintextToTransformReady(plaintext, this.getSiteUrl(), _options);
564
+ return utils_1.default.plaintextToTransformReady(plaintext, this.getSiteUrl(), null, _options);
451
565
  }
452
566
  /**
453
567
  * Return whether the provided URL is part of the site (checks if same domain and within subdirectory)
@@ -466,13 +580,13 @@ module.exports = class UrlUtils {
466
580
  return false;
467
581
  }
468
582
  get isSSL() {
469
- return utils.isSSL;
583
+ return utils_1.default.isSSL;
470
584
  }
471
585
  get replacePermalink() {
472
- return utils.replacePermalink;
586
+ return utils_1.default.replacePermalink;
473
587
  }
474
588
  get deduplicateDoubleSlashes() {
475
- return utils.deduplicateDoubleSlashes;
589
+ return utils_1.default.deduplicateDoubleSlashes;
476
590
  }
477
591
  /**
478
592
  * If you request **any** image in Ghost, it get's served via
@@ -494,6 +608,8 @@ module.exports = class UrlUtils {
494
608
  }
495
609
  // expose underlying functions to ease testing
496
610
  get _utils() {
497
- return utils;
611
+ return utils_1.default;
498
612
  }
499
- };
613
+ }
614
+ exports.default = UrlUtils;
615
+ ;
package/lib/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import UrlUtils from './UrlUtils';
2
+ export = UrlUtils;
package/lib/index.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ const UrlUtils_1 = __importDefault(require("./UrlUtils"));
6
+ module.exports = UrlUtils_1.default;
@@ -1,5 +1,10 @@
1
- declare const URL: any;
2
- declare const stripSubdirectoryFromPath: any;
1
+ export interface AbsoluteToRelativeOptions {
2
+ ignoreProtocol: boolean;
3
+ withoutSubdirectory: boolean;
4
+ assetsOnly: boolean;
5
+ staticImageUrlPrefix: string;
6
+ }
7
+ export type AbsoluteToRelativeOptionsInput = Partial<AbsoluteToRelativeOptions>;
3
8
  /**
4
9
  * Convert an absolute URL to a root-relative path if it matches the supplied root domain.
5
10
  *
@@ -10,4 +15,5 @@ declare const stripSubdirectoryFromPath: any;
10
15
  * @param {boolean} [options.withoutSubdirectory=false] Strip the root subdirectory from the returned path
11
16
  * @returns {string} The passed-in url or a relative path
12
17
  */
13
- declare const absoluteToRelative: (url: any, rootUrl: any, _options?: {}) => any;
18
+ declare const absoluteToRelative: (url: string, rootUrl?: string, _options?: AbsoluteToRelativeOptionsInput) => string;
19
+ export default absoluteToRelative;