@tryghost/url-utils 5.0.0-rc.1 → 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 -0
  2. package/lib/UrlUtils.js +270 -220
  3. package/lib/index.d.ts +2 -0
  4. package/lib/index.js +6 -0
  5. package/lib/utils/absolute-to-relative.d.ts +19 -0
  6. package/lib/utils/absolute-to-relative.js +16 -19
  7. package/lib/utils/absolute-to-transform-ready.d.ts +13 -0
  8. package/lib/utils/absolute-to-transform-ready.js +39 -94
  9. package/lib/utils/build-early-exit-match.d.ts +24 -0
  10. package/lib/utils/build-early-exit-match.js +39 -0
  11. package/lib/utils/deduplicate-double-slashes.d.ts +2 -0
  12. package/lib/utils/deduplicate-double-slashes.js +7 -3
  13. package/lib/utils/deduplicate-subdirectory.d.ts +9 -0
  14. package/lib/utils/deduplicate-subdirectory.js +5 -9
  15. package/lib/utils/html-absolute-to-relative.d.ts +3 -0
  16. package/lib/utils/html-absolute-to-relative.js +12 -12
  17. package/lib/utils/html-absolute-to-transform-ready.d.ts +3 -0
  18. package/lib/utils/html-absolute-to-transform-ready.js +18 -38
  19. package/lib/utils/html-relative-to-absolute.d.ts +3 -0
  20. package/lib/utils/html-relative-to-absolute.js +11 -10
  21. package/lib/utils/html-relative-to-transform-ready.d.ts +3 -0
  22. package/lib/utils/html-relative-to-transform-ready.js +18 -13
  23. package/lib/utils/html-to-transform-ready.d.ts +3 -0
  24. package/lib/utils/html-to-transform-ready.js +18 -10
  25. package/lib/utils/html-transform.d.ts +3 -0
  26. package/lib/utils/html-transform.js +26 -47
  27. package/lib/utils/index.d.ts +76 -0
  28. package/lib/utils/index.js +116 -37
  29. package/lib/utils/is-ssl.d.ts +2 -0
  30. package/lib/utils/is-ssl.js +5 -5
  31. package/lib/utils/lexical-absolute-to-relative.d.ts +3 -0
  32. package/lib/utils/lexical-absolute-to-relative.js +12 -11
  33. package/lib/utils/lexical-absolute-to-transform-ready.d.ts +3 -0
  34. package/lib/utils/lexical-absolute-to-transform-ready.js +13 -12
  35. package/lib/utils/lexical-relative-to-absolute.d.ts +3 -0
  36. package/lib/utils/lexical-relative-to-absolute.js +11 -9
  37. package/lib/utils/lexical-relative-to-transform-ready.d.ts +3 -0
  38. package/lib/utils/lexical-relative-to-transform-ready.js +11 -9
  39. package/lib/utils/lexical-to-transform-ready.d.ts +3 -0
  40. package/lib/utils/lexical-to-transform-ready.js +18 -10
  41. package/lib/utils/lexical-transform.d.ts +3 -0
  42. package/lib/utils/lexical-transform.js +25 -36
  43. package/lib/utils/markdown-absolute-to-relative.d.ts +3 -0
  44. package/lib/utils/markdown-absolute-to-relative.js +13 -13
  45. package/lib/utils/markdown-absolute-to-transform-ready.d.ts +3 -0
  46. package/lib/utils/markdown-absolute-to-transform-ready.js +19 -15
  47. package/lib/utils/markdown-relative-to-absolute.d.ts +3 -0
  48. package/lib/utils/markdown-relative-to-absolute.js +13 -13
  49. package/lib/utils/markdown-relative-to-transform-ready.d.ts +3 -0
  50. package/lib/utils/markdown-relative-to-transform-ready.js +13 -13
  51. package/lib/utils/markdown-to-transform-ready.d.ts +3 -0
  52. package/lib/utils/markdown-to-transform-ready.js +18 -10
  53. package/lib/utils/markdown-transform.d.ts +3 -0
  54. package/lib/utils/markdown-transform.js +15 -30
  55. package/lib/utils/mobiledoc-absolute-to-relative.d.ts +3 -0
  56. package/lib/utils/mobiledoc-absolute-to-relative.js +12 -11
  57. package/lib/utils/mobiledoc-absolute-to-transform-ready.d.ts +3 -0
  58. package/lib/utils/mobiledoc-absolute-to-transform-ready.js +13 -12
  59. package/lib/utils/mobiledoc-relative-to-absolute.d.ts +3 -0
  60. package/lib/utils/mobiledoc-relative-to-absolute.js +11 -9
  61. package/lib/utils/mobiledoc-relative-to-transform-ready.d.ts +3 -0
  62. package/lib/utils/mobiledoc-relative-to-transform-ready.js +11 -9
  63. package/lib/utils/mobiledoc-to-transform-ready.d.ts +3 -0
  64. package/lib/utils/mobiledoc-to-transform-ready.js +18 -10
  65. package/lib/utils/mobiledoc-transform.d.ts +3 -0
  66. package/lib/utils/mobiledoc-transform.js +8 -13
  67. package/lib/utils/plaintext-absolute-to-transform-ready.d.ts +5 -0
  68. package/lib/utils/plaintext-absolute-to-transform-ready.js +29 -16
  69. package/lib/utils/plaintext-relative-to-transform-ready.d.ts +3 -0
  70. package/lib/utils/plaintext-relative-to-transform-ready.js +16 -9
  71. package/lib/utils/plaintext-to-transform-ready.d.ts +5 -0
  72. package/lib/utils/plaintext-to-transform-ready.js +18 -10
  73. package/lib/utils/relative-to-absolute.d.ts +15 -0
  74. package/lib/utils/relative-to-absolute.js +28 -33
  75. package/lib/utils/relative-to-transform-ready.d.ts +8 -0
  76. package/lib/utils/relative-to-transform-ready.js +23 -18
  77. package/lib/utils/replace-permalink.d.ts +16 -0
  78. package/lib/utils/replace-permalink.js +45 -10
  79. package/lib/utils/strip-subdirectory-from-path.d.ts +9 -0
  80. package/lib/utils/strip-subdirectory-from-path.js +7 -11
  81. package/lib/utils/to-transform-ready.d.ts +5 -0
  82. package/lib/utils/to-transform-ready.js +18 -10
  83. package/lib/utils/transform-ready-to-absolute.d.ts +9 -0
  84. package/lib/utils/transform-ready-to-absolute.js +10 -28
  85. package/lib/utils/transform-ready-to-relative.d.ts +3 -0
  86. package/lib/utils/transform-ready-to-relative.js +5 -8
  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 +12 -0
  90. package/lib/utils/url-join.js +12 -12
  91. package/package.json +20 -12
  92. package/index.js +0 -1
@@ -1,7 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
1
3
  function mobiledocTransform(serializedMobiledoc, siteUrl, transformFunction, itemPath, _options = {}) {
2
- const defaultOptions = {assetsOnly: false, secure: false, cardTransformers: []};
3
- const options = Object.assign({}, defaultOptions, _options, {siteUrl, itemPath});
4
-
4
+ const defaultOptions = { assetsOnly: false, secure: false, cardTransformers: [] };
5
+ const options = Object.assign({}, defaultOptions, _options, { siteUrl, itemPath });
5
6
  // options.cardTransformers has an object for each card that has a name and multiple
6
7
  // transformer functions. By collecting the functions we need into a named object it
7
8
  // reduces the need to loop through and find the transformer for each card later on
@@ -10,25 +11,21 @@ function mobiledocTransform(serializedMobiledoc, siteUrl, transformFunction, ite
10
11
  cardTransformers[cardTransformer.name] = cardTransformer[options.transformType];
11
12
  });
12
13
  delete options.cardTransformers;
13
-
14
14
  // function only accepts serialized mobiledoc so there's no chance of accidentally
15
15
  // modifying pass-by-reference objects
16
16
  const mobiledoc = JSON.parse(serializedMobiledoc);
17
-
18
17
  // any mobiledoc links will have an 'a' markup with an 'href' attribute
19
18
  (mobiledoc.markups || []).forEach((markup) => {
20
19
  if (markup[0] === 'a' && markup[1]) {
21
20
  // mobiledoc markup attrs are in an array like ['key', 'value', 'key2', 'value2']
22
21
  // we only care about the href attr so loop through and find it so we can get the idx of it's value
23
22
  let hrefIndex = -1;
24
-
25
23
  markup[1].forEach((attr, index) => {
26
24
  if (attr === 'href') {
27
25
  hrefIndex = index + 1;
28
26
  }
29
27
  });
30
-
31
- if (hrefIndex !== -1) {
28
+ if (hrefIndex !== -1 && typeof markup[1][hrefIndex] === 'string') {
32
29
  const transformedUrl = transformFunction(markup[1][hrefIndex], siteUrl, itemPath, options);
33
30
  if (transformedUrl) {
34
31
  markup[1][hrefIndex] = transformedUrl;
@@ -36,20 +33,18 @@ function mobiledocTransform(serializedMobiledoc, siteUrl, transformFunction, ite
36
33
  }
37
34
  }
38
35
  });
39
-
40
36
  // any other urls will be within card payloads. We can't know what format
41
37
  // cards may contain so we sub out to card-specific transform functions that
42
38
  // are passed in as options from the consuming application.
43
39
  (mobiledoc.cards || []).forEach((card) => {
44
- const [name, payload] = card;
40
+ const name = card[0];
41
+ const payload = card[1];
45
42
  if (cardTransformers[name]) {
46
43
  // transformers take a payload and return a transformed payload
47
44
  const transformedPayload = cardTransformers[name](payload, options);
48
45
  card[1] = transformedPayload;
49
46
  }
50
47
  });
51
-
52
48
  return JSON.stringify(mobiledoc);
53
49
  }
54
-
55
- module.exports = mobiledocTransform;
50
+ exports.default = mobiledocTransform;
@@ -0,0 +1,5 @@
1
+ import type { AbsoluteToTransformReadyOptionsInput, BaseUrlOptionsInput } from './types';
2
+ type PlaintextAbsoluteToTransformReadyOptions = AbsoluteToTransformReadyOptionsInput & BaseUrlOptionsInput;
3
+ type PlaintextAbsoluteToTransformReadyOptionsInput = Partial<PlaintextAbsoluteToTransformReadyOptions>;
4
+ declare const plaintextAbsoluteToTransformReady: (plaintext: string, rootUrl: string, itemPath: string | null | PlaintextAbsoluteToTransformReadyOptionsInput, options?: PlaintextAbsoluteToTransformReadyOptionsInput) => string;
5
+ export default plaintextAbsoluteToTransformReady;
@@ -1,26 +1,39 @@
1
- const absoluteToTransformReady = require('./absolute-to-transform-ready');
2
-
3
- function escapeRegExp(string) {
4
- return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
1
+ "use strict";
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 absolute_to_transform_ready_1 = __importDefault(require("./absolute-to-transform-ready"));
7
+ const build_early_exit_match_1 = __importDefault(require("./build-early-exit-match"));
8
+ const { escapeRegExp } = build_early_exit_match_1.default;
9
+ const url_1 = require("url");
10
+ function buildLinkRegex(rootUrl, options = {}) {
11
+ // Build a regex that matches links from ANY configured base URL (site + CDNs)
12
+ const baseUrls = [rootUrl, options.imageBaseUrl, options.filesBaseUrl, options.mediaBaseUrl]
13
+ .filter((value) => Boolean(value));
14
+ const patterns = baseUrls.map((baseUrl) => {
15
+ const parsed = new url_1.URL(baseUrl);
16
+ const escapedUrl = escapeRegExp(`${parsed.hostname}${parsed.pathname.replace(/\/$/, '')}`);
17
+ return escapedUrl;
18
+ });
19
+ if (!patterns.length) {
20
+ return null;
21
+ }
22
+ const pattern = patterns.length === 1 ? patterns[0] : `(?:${patterns.join('|')})`;
23
+ return new RegExp(` \\[(https?://${pattern}.*?)\\]`, 'g');
5
24
  }
6
-
7
25
  const plaintextAbsoluteToTransformReady = function plaintextAbsoluteToTransformReady(plaintext, rootUrl, itemPath, options) {
8
26
  // itemPath is optional, if it's an object may be the options param instead
9
- if (typeof itemPath === 'object' && !options) {
10
- options = itemPath;
11
- itemPath = null;
27
+ let finalOptions = options || {};
28
+ if (typeof itemPath === 'object' && itemPath !== null && !options) {
29
+ finalOptions = itemPath;
12
30
  }
13
-
14
31
  // plaintext links look like "Link title [url]"
15
32
  // those links are all we care about so we can do a fast regex here
16
- const rootURL = new URL(rootUrl);
17
- const escapedRootUrl = escapeRegExp(`${rootURL.hostname}${rootURL.pathname.replace(/\/$/, '')}`);
18
- const linkRegex = new RegExp(` \\[(https?://${escapedRootUrl}.*?)\\]`, 'g');
19
-
33
+ const linkRegex = buildLinkRegex(rootUrl, finalOptions);
20
34
  return plaintext.replace(linkRegex, function (fullMatch, url) {
21
- const newUrl = absoluteToTransformReady(`${url}`, rootUrl, options);
35
+ const newUrl = (0, absolute_to_transform_ready_1.default)(`${url}`, rootUrl, finalOptions);
22
36
  return ` [${newUrl}]`;
23
37
  });
24
38
  };
25
-
26
- module.exports = plaintextAbsoluteToTransformReady;
39
+ exports.default = plaintextAbsoluteToTransformReady;
@@ -0,0 +1,3 @@
1
+ import type { RelativeToTransformReadyOptionsInput } from './relative-to-transform-ready';
2
+ declare const plaintextRelativeToTransformReady: (plaintext: string, rootUrl: string, itemPath: string | null | RelativeToTransformReadyOptionsInput, options?: RelativeToTransformReadyOptionsInput) => string;
3
+ export default plaintextRelativeToTransformReady;
@@ -1,18 +1,25 @@
1
- const relativeToTransformReady = require('./relative-to-transform-ready');
2
-
1
+ "use strict";
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_transform_ready_1 = __importDefault(require("./relative-to-transform-ready"));
3
7
  const plaintextRelativeToTransformReady = function plaintextRelativeToTransformReady(plaintext, rootUrl, itemPath, options) {
4
8
  // itemPath is optional, if it's an object may be the options param instead
5
- if (typeof itemPath === 'object' && !options) {
6
- options = itemPath;
7
- itemPath = null;
9
+ let finalItemPath = null;
10
+ let finalOptions = options || {};
11
+ if (typeof itemPath === 'object' && itemPath !== null && !options) {
12
+ finalOptions = itemPath;
13
+ finalItemPath = null;
14
+ }
15
+ else if (typeof itemPath === 'string') {
16
+ finalItemPath = itemPath;
8
17
  }
9
-
10
18
  // plaintext links look like "Link title [url]"
11
19
  // those are all we care about so we can do a fast regex here
12
20
  return plaintext.replace(/ \[(\/.*?)\]/g, function (fullMatch, path) {
13
- const newPath = relativeToTransformReady(`${path}`, rootUrl, itemPath, options);
21
+ const newPath = (0, relative_to_transform_ready_1.default)(`${path}`, rootUrl, finalItemPath, finalOptions);
14
22
  return ` [${newPath}]`;
15
23
  });
16
24
  };
17
-
18
- module.exports = plaintextRelativeToTransformReady;
25
+ exports.default = plaintextRelativeToTransformReady;
@@ -0,0 +1,5 @@
1
+ import type { UnknownRecord } from './types';
2
+ type PlaintextTransformOptions = UnknownRecord;
3
+ type PlaintextTransformOptionsInput = Partial<PlaintextTransformOptions>;
4
+ declare function plaintextToTransformReady(plaintext: string, siteUrl: string, itemPath: string | null | PlaintextTransformOptionsInput, options?: PlaintextTransformOptionsInput): string;
5
+ export default plaintextToTransformReady;
@@ -1,13 +1,21 @@
1
- const plaintextRelativeToTransformReady = require('./plaintext-relative-to-transform-ready');
2
- const plaintextAbsoluteToTransformReady = require('./plaintext-absolute-to-transform-ready');
3
-
1
+ "use strict";
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 plaintext_relative_to_transform_ready_1 = __importDefault(require("./plaintext-relative-to-transform-ready"));
7
+ const plaintext_absolute_to_transform_ready_1 = __importDefault(require("./plaintext-absolute-to-transform-ready"));
4
8
  function plaintextToTransformReady(plaintext, siteUrl, itemPath, options) {
5
- if (typeof itemPath === 'object' && !options) {
6
- options = itemPath;
7
- itemPath = null;
9
+ let finalItemPath = null;
10
+ let finalOptions = options || {};
11
+ if (typeof itemPath === 'object' && itemPath !== null && !options) {
12
+ finalOptions = itemPath;
13
+ finalItemPath = null;
8
14
  }
9
- const relativeTransformed = plaintextRelativeToTransformReady(plaintext, siteUrl, itemPath, options);
10
- return plaintextAbsoluteToTransformReady(relativeTransformed, siteUrl, options);
15
+ else if (typeof itemPath === 'string') {
16
+ finalItemPath = itemPath;
17
+ }
18
+ const relativeTransformed = (0, plaintext_relative_to_transform_ready_1.default)(plaintext, siteUrl, finalItemPath, finalOptions);
19
+ return (0, plaintext_absolute_to_transform_ready_1.default)(relativeTransformed, siteUrl, finalOptions);
11
20
  }
12
-
13
- module.exports = plaintextToTransformReady;
21
+ exports.default = plaintextToTransformReady;
@@ -0,0 +1,15 @@
1
+ import type { SecureOptions, SecureOptionsInput } from './types';
2
+ export type RelativeToAbsoluteOptions = SecureOptions;
3
+ export type RelativeToAbsoluteOptionsInput = SecureOptionsInput;
4
+ /**
5
+ * Convert a root-relative path to an absolute URL based on the supplied root.
6
+ * Will _only_ convert root-relative urls (/some/path not some/path)
7
+ *
8
+ * @param {string} path
9
+ * @param {string} rootUrl
10
+ * @param {string} itemPath
11
+ * @param {object} options
12
+ * @returns {string} The passed in url or an absolute URL using
13
+ */
14
+ declare const relativeToAbsolute: (path: string, rootUrl: string, itemPath: string | null | RelativeToAbsoluteOptionsInput, _options?: RelativeToAbsoluteOptionsInput) => string;
15
+ export default relativeToAbsolute;
@@ -1,7 +1,10 @@
1
- // require the whatwg compatible URL library (same behaviour in node and browser)
2
- const {URL} = require('url');
3
- const urlJoin = require('./url-join');
4
-
1
+ "use strict";
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"));
5
8
  // NOTE: Ghost's relative->absolute handling is a little strange when the rootUrl
6
9
  // includes a subdirectory. Root-relative paths such as /content/image.jpg are
7
10
  // actually treated as subdirectory-relative. This means that it's possible to
@@ -10,7 +13,6 @@ const urlJoin = require('./url-join');
10
13
  // of path styles (/content/image.png and /subdir/content/image.png). To handle
11
14
  // this when all root-relative paths are treated as subdir-relative we have to
12
15
  // rely on subdirectory deduplication.
13
-
14
16
  /**
15
17
  * Convert a root-relative path to an absolute URL based on the supplied root.
16
18
  * Will _only_ convert root-relative urls (/some/path not some/path)
@@ -23,23 +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
-
31
37
  // itemPath could be sent as a full url in which case, extract the pathname
32
- if (itemPath && itemPath.match(/^http/)) {
33
- const itemUrl = new URL(itemPath);
34
- itemPath = itemUrl.pathname;
38
+ if (finalItemPath && finalItemPath.match(/^http/)) {
39
+ const itemUrl = new url_1.URL(finalItemPath);
40
+ finalItemPath = itemUrl.pathname;
35
41
  }
36
-
37
42
  const defaultOptions = {
38
43
  assetsOnly: false,
39
44
  staticImageUrlPrefix: 'content/images'
40
45
  };
41
- const options = Object.assign({}, defaultOptions, _options);
42
-
46
+ const options = Object.assign({}, defaultOptions, finalOptions);
43
47
  // return the path as-is if it's not an asset path and we're only modifying assets
44
48
  if (options.assetsOnly) {
45
49
  const staticImageUrlPrefixRegex = new RegExp(options.staticImageUrlPrefix);
@@ -47,48 +51,39 @@ const relativeToAbsolute = function relativeToAbsolute(path, rootUrl, itemPath,
47
51
  return path;
48
52
  }
49
53
  }
50
-
51
54
  // if URL is absolute return it as-is
52
55
  try {
53
- const parsed = new URL(path, 'http://relative');
54
-
56
+ const parsed = new url_1.URL(path, 'http://relative');
55
57
  if (parsed.origin !== 'http://relative') {
56
58
  return path;
57
59
  }
58
-
59
60
  // Do not convert protocol relative URLs
60
61
  if (path.lastIndexOf('//', 0) === 0) {
61
62
  return path;
62
63
  }
63
- } catch (e) {
64
+ }
65
+ catch (e) {
64
66
  return path;
65
67
  }
66
-
67
68
  // return the path as-is if it's a pure hash param
68
69
  if (path.startsWith('#')) {
69
70
  return path;
70
71
  }
71
-
72
72
  // return the path as-is if it's not root-relative and we have no itemPath
73
- if (!itemPath && !path.match(/^\//)) {
73
+ if (!finalItemPath && !path.match(/^\//)) {
74
74
  return path;
75
75
  }
76
-
77
76
  // force root to always have a trailing-slash for consistent behaviour
78
77
  if (!rootUrl.endsWith('/')) {
79
78
  rootUrl = `${rootUrl}/`;
80
79
  }
81
-
82
- const parsedRootUrl = new URL(rootUrl);
83
- const basePath = path.startsWith('/') ? '' : itemPath;
84
- const fullPath = urlJoin([parsedRootUrl.pathname, basePath, path], {rootUrl});
85
- const absoluteUrl = new URL(fullPath, rootUrl);
86
-
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);
87
84
  if (options.secure) {
88
85
  absoluteUrl.protocol = 'https:';
89
86
  }
90
-
91
87
  return absoluteUrl.toString();
92
88
  };
93
-
94
- module.exports = relativeToAbsolute;
89
+ exports.default = relativeToAbsolute;
@@ -0,0 +1,8 @@
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,12 +1,21 @@
1
- const relativeToAbsolute = require('./relative-to-absolute');
2
-
1
+ "use strict";
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");
3
8
  const relativeToTransformReady = function (url, root, itemPath, _options) {
4
9
  // itemPath is optional, if it's an object may be the options param instead
5
- if (typeof itemPath === 'object' && !_options) {
6
- _options = itemPath;
7
- 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;
8
18
  }
9
-
10
19
  const defaultOptions = {
11
20
  replacementStr: '__GHOST_URL__',
12
21
  staticImageUrlPrefix: 'content/images'
@@ -14,29 +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);
18
-
26
+ const options = Object.assign({}, defaultOptions, finalOptions, overrideOptions);
19
27
  // convert to absolute
20
- const absoluteUrl = relativeToAbsolute(url, root, itemPath, options);
21
-
28
+ const absoluteUrl = (0, relative_to_absolute_1.default)(url, root, finalItemPath, options);
22
29
  if (absoluteUrl === url) {
23
30
  return url;
24
31
  }
25
-
26
- const rootUrl = new URL(root);
32
+ const rootUrl = new url_1.URL(root);
27
33
  const rootPathname = rootUrl.pathname.replace(/\/$/, '');
28
-
29
34
  // only convert to transform-ready if root url has no subdirectory or the subdirectory matches
30
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(/\/$/, '');
31
39
  // replace root with replacement string
32
40
  const transformedUrl = absoluteUrl
33
- .replace(root, `${options.replacementStr}/`) // always have trailing slash after magic string
41
+ .replace(normalizedRoot, `${options.replacementStr}/`) // always have trailing slash after magic string
34
42
  .replace(/([^:])\/\//g, '$1/');
35
-
36
43
  return transformedUrl;
37
44
  }
38
-
39
45
  return url;
40
46
  };
41
-
42
- module.exports = relativeToTransformReady;
47
+ exports.default = relativeToTransformReady;
@@ -0,0 +1,16 @@
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
+ }
12
+ /**
13
+ * creates the url path for a post based on blog timezone and permalink pattern
14
+ */
15
+ declare function replacePermalink(permalink: string, resource: PermalinkResource, timezone?: string): string;
16
+ export default replacePermalink;
@@ -1,10 +1,43 @@
1
- const moment = require('moment-timezone');
2
-
1
+ "use strict";
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"));
3
37
  /**
4
38
  * creates the url path for a post based on blog timezone and permalink pattern
5
39
  */
6
40
  function replacePermalink(permalink, resource, timezone = 'UTC') {
7
- const output = permalink;
8
41
  const primaryTagFallback = 'all';
9
42
  const publishedAtMoment = moment.tz(resource.published_at || Date.now(), timezone);
10
43
  const permalinkLookUp = {
@@ -18,7 +51,7 @@ function replacePermalink(permalink, resource, timezone = 'UTC') {
18
51
  return publishedAtMoment.format('DD');
19
52
  },
20
53
  author: function () {
21
- return resource.primary_author.slug;
54
+ return resource.primary_author?.slug ?? 'undefined';
22
55
  },
23
56
  primary_author: function () {
24
57
  return resource.primary_author ? resource.primary_author.slug : primaryTagFallback;
@@ -33,14 +66,16 @@ function replacePermalink(permalink, resource, timezone = 'UTC') {
33
66
  return resource.id;
34
67
  }
35
68
  };
36
-
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
-
46
- module.exports = replacePermalink;
81
+ exports.default = replacePermalink;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Removes the directory in the root url from the relative path
3
+ *
4
+ * @param {string} path Relative path (eg, '/my/subdir/my/file.png)
5
+ * @param {string} rootUrl Root URL (eg, 'https://mysite.com/my/subdir/)
6
+ * @returns {string} Path relative to the rootUrl's path
7
+ */
8
+ declare const stripSubdirectoryFromPath: (path?: string, rootUrl?: string) => string;
9
+ export default stripSubdirectoryFromPath;
@@ -1,5 +1,6 @@
1
- const {URL} = require('url');
2
-
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const url_1 = require("url");
3
4
  /**
4
5
  * Removes the directory in the root url from the relative path
5
6
  *
@@ -12,25 +13,20 @@ const stripSubdirectoryFromPath = function stripSubdirectoryFromPath(path = '',
12
13
  if (!rootUrl.endsWith('/')) {
13
14
  rootUrl = `${rootUrl}/`;
14
15
  }
15
-
16
16
  let parsedRoot;
17
-
18
17
  try {
19
- parsedRoot = new URL(rootUrl);
20
- } catch (e) {
18
+ parsedRoot = new url_1.URL(rootUrl);
19
+ }
20
+ catch (e) {
21
21
  return path;
22
22
  }
23
-
24
23
  // do nothing if rootUrl does not have a subdirectory
25
24
  if (parsedRoot.pathname === '/') {
26
25
  return path;
27
26
  }
28
-
29
27
  if (path.startsWith(parsedRoot.pathname)) {
30
28
  return path.replace(parsedRoot.pathname, '/');
31
29
  }
32
-
33
30
  return path;
34
31
  };
35
-
36
- module.exports = stripSubdirectoryFromPath;
32
+ exports.default = stripSubdirectoryFromPath;
@@ -0,0 +1,5 @@
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
- const relativeToAbsolute = require('./relative-to-absolute');
2
- const absoluteToTransformReady = require('./absolute-to-transform-ready');
3
-
1
+ "use strict";
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"));
4
8
  function toTransformReady(url, siteUrl, itemPath, options) {
5
- if (typeof itemPath === 'object' && !options) {
6
- options = itemPath;
7
- itemPath = null;
9
+ let finalItemPath = null;
10
+ let finalOptions = options || {};
11
+ if (typeof itemPath === 'object' && itemPath !== null && !options) {
12
+ finalOptions = itemPath;
13
+ finalItemPath = null;
8
14
  }
9
- const absoluteUrl = relativeToAbsolute(url, siteUrl, itemPath, options);
10
- 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);
11
20
  }
12
-
13
- module.exports = toTransformReady;
21
+ exports.default = toTransformReady;
@@ -0,0 +1,9 @@
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;