@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
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;
@@ -0,0 +1,19 @@
1
+ export interface AbsoluteToRelativeOptions {
2
+ ignoreProtocol: boolean;
3
+ withoutSubdirectory: boolean;
4
+ assetsOnly: boolean;
5
+ staticImageUrlPrefix: string;
6
+ }
7
+ export type AbsoluteToRelativeOptionsInput = Partial<AbsoluteToRelativeOptions>;
8
+ /**
9
+ * Convert an absolute URL to a root-relative path if it matches the supplied root domain.
10
+ *
11
+ * @param {string} url Absolute URL to convert to relative if possible
12
+ * @param {string} rootUrl Absolute URL to which the returned relative URL will match the domain root
13
+ * @param {Object} [options] Options that affect the conversion
14
+ * @param {boolean} [options.ignoreProtocol=true] Ignore protocol when matching url to root
15
+ * @param {boolean} [options.withoutSubdirectory=false] Strip the root subdirectory from the returned path
16
+ * @returns {string} The passed-in url or a relative path
17
+ */
18
+ declare const absoluteToRelative: (url: string, rootUrl?: string, _options?: AbsoluteToRelativeOptionsInput) => string;
19
+ export default absoluteToRelative;
@@ -1,7 +1,10 @@
1
- // require the whatwg compatible URL library (same behaviour in node and browser)
2
- const {URL} = require('url');
3
- const stripSubdirectoryFromPath = require('./strip-subdirectory-from-path');
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 strip_subdirectory_from_path_1 = __importDefault(require("./strip-subdirectory-from-path"));
5
8
  /**
6
9
  * Convert an absolute URL to a root-relative path if it matches the supplied root domain.
7
10
  *
@@ -20,44 +23,38 @@ const absoluteToRelative = function absoluteToRelative(url, rootUrl, _options =
20
23
  staticImageUrlPrefix: 'content/images'
21
24
  };
22
25
  const options = Object.assign({}, defaultOptions, _options);
23
-
24
26
  if (options.assetsOnly) {
25
27
  const staticImageUrlPrefixRegex = new RegExp(options.staticImageUrlPrefix);
26
28
  if (!url.match(staticImageUrlPrefixRegex)) {
27
29
  return url;
28
30
  }
29
31
  }
30
-
31
32
  let parsedUrl;
32
33
  let parsedRoot;
33
-
34
34
  try {
35
- parsedUrl = new URL(url, 'http://relative');
36
- parsedRoot = parsedUrl.origin === 'null' ? undefined : new URL(rootUrl || parsedUrl.origin);
37
-
35
+ parsedUrl = new url_1.URL(url, 'http://relative');
36
+ parsedRoot = parsedUrl.origin === 'null' ? undefined : new url_1.URL(rootUrl || parsedUrl.origin);
38
37
  // return the url as-is if it was relative or non-http
39
38
  if (parsedUrl.origin === 'null' || parsedUrl.origin === 'http://relative') {
40
39
  return url;
41
40
  }
42
- } catch (e) {
41
+ }
42
+ catch (e) {
43
+ return url;
44
+ }
45
+ if (!parsedRoot) {
43
46
  return url;
44
47
  }
45
-
46
48
  const matchesHost = parsedUrl.host === parsedRoot.host;
47
49
  const matchesProtocol = parsedUrl.protocol === parsedRoot.protocol;
48
50
  const matchesPath = parsedUrl.pathname.indexOf(parsedRoot.pathname) === 0;
49
-
50
51
  if (matchesHost && (options.ignoreProtocol || matchesProtocol) && matchesPath) {
51
52
  let path = parsedUrl.href.replace(parsedUrl.origin, '');
52
-
53
53
  if (options.withoutSubdirectory) {
54
- path = stripSubdirectoryFromPath(path, rootUrl);
54
+ path = (0, strip_subdirectory_from_path_1.default)(path, rootUrl);
55
55
  }
56
-
57
56
  return path;
58
57
  }
59
-
60
58
  return url;
61
59
  };
62
-
63
- module.exports = absoluteToRelative;
60
+ exports.default = absoluteToRelative;
@@ -0,0 +1,13 @@
1
+ import type { TransformReadyReplacementOptions } from './types';
2
+ import { type AbsoluteToRelativeOptions } from './absolute-to-relative';
3
+ export interface AbsoluteToTransformReadyOptions extends TransformReadyReplacementOptions, AbsoluteToRelativeOptions {
4
+ withoutSubdirectory: boolean;
5
+ staticFilesUrlPrefix?: string;
6
+ staticMediaUrlPrefix?: string;
7
+ imageBaseUrl?: string | null;
8
+ filesBaseUrl?: string | null;
9
+ mediaBaseUrl?: string | null;
10
+ }
11
+ export type AbsoluteToTransformReadyOptionsInput = Partial<AbsoluteToTransformReadyOptions>;
12
+ declare const absoluteToTransformReady: (url: string, root: string, _options?: AbsoluteToTransformReadyOptionsInput) => string;
13
+ export default absoluteToTransformReady;
@@ -1,69 +1,21 @@
1
- const {URL} = require('url');
2
- const absoluteToRelative = require('./absolute-to-relative');
3
-
4
- function normalizeBaseUrl(baseUrl) {
5
- if (!baseUrl) {
6
- return null;
7
- }
8
-
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_relative_1 = __importDefault(require("./absolute-to-relative"));
7
+ const url_1 = require("url");
8
+ function isRelative(url) {
9
+ let parsedInput;
9
10
  try {
10
- const parsed = new URL(baseUrl);
11
- let pathname = parsed.pathname.replace(/\/$/, '');
12
- if (pathname === '/') {
13
- pathname = '';
14
- }
15
-
16
- return {
17
- origin: parsed.origin,
18
- pathname
19
- };
20
- } catch (e) {
21
- return null;
11
+ parsedInput = new url_1.URL(url, 'http://relative');
22
12
  }
23
- }
24
-
25
- function matchCdnBase(parsedUrl, options) {
26
- const candidates = [
27
- {base: normalizeBaseUrl(options.mediaBaseUrl), prefix: options.staticMediaUrlPrefix},
28
- {base: normalizeBaseUrl(options.filesBaseUrl), prefix: options.staticFilesUrlPrefix}
29
- ];
30
-
31
- for (const candidate of candidates) {
32
- if (!candidate.base || !candidate.prefix) {
33
- continue;
34
- }
35
-
36
- if (parsedUrl.origin !== candidate.base.origin) {
37
- continue;
38
- }
39
-
40
- let pathname = parsedUrl.pathname;
41
- const basePath = candidate.base.pathname;
42
-
43
- if (basePath) {
44
- if (pathname === basePath) {
45
- pathname = '/';
46
- } else if (pathname.startsWith(basePath + '/')) {
47
- pathname = pathname.slice(basePath.length);
48
- } else {
49
- continue;
50
- }
51
- }
52
-
53
- if (!pathname.startsWith('/')) {
54
- pathname = `/${pathname}`;
55
- }
56
-
57
- if (!pathname.startsWith(`/${candidate.prefix}`)) {
58
- continue;
59
- }
60
-
61
- return `${pathname}${parsedUrl.search}${parsedUrl.hash}`;
13
+ catch (e) {
14
+ // url was unparseable
15
+ return false;
62
16
  }
63
-
64
- return null;
17
+ return parsedInput.origin === 'http://relative';
65
18
  }
66
-
67
19
  const absoluteToTransformReady = function (url, root, _options = {}) {
68
20
  const defaultOptions = {
69
21
  replacementStr: '__GHOST_URL__',
@@ -73,45 +25,38 @@ const absoluteToTransformReady = function (url, root, _options = {}) {
73
25
  staticMediaUrlPrefix: 'content/media',
74
26
  imageBaseUrl: null,
75
27
  filesBaseUrl: null,
76
- mediaBaseUrl: null
28
+ mediaBaseUrl: null,
29
+ ignoreProtocol: true,
30
+ assetsOnly: false
77
31
  };
78
32
  const options = Object.assign({}, defaultOptions, _options);
79
-
80
- let parsedInput;
81
- try {
82
- parsedInput = new URL(url, 'http://relative');
83
- } catch (e) {
84
- // url was unparseable
33
+ if (isRelative(url)) {
85
34
  return url;
86
35
  }
87
-
88
- if (parsedInput.origin !== 'http://relative') {
89
- const cdnMatch = matchCdnBase(parsedInput, options);
90
- if (cdnMatch) {
91
- return `${options.replacementStr}${cdnMatch}`;
36
+ // convert to relative with stripped subdir
37
+ // always returns root-relative starting with forward slash
38
+ const rootRelativeUrl = (0, absolute_to_relative_1.default)(url, root, options);
39
+ if (isRelative(rootRelativeUrl)) {
40
+ return `${options.replacementStr}${rootRelativeUrl}`;
41
+ }
42
+ if (options.mediaBaseUrl) {
43
+ const mediaRelativeUrl = (0, absolute_to_relative_1.default)(url, options.mediaBaseUrl, options);
44
+ if (isRelative(mediaRelativeUrl)) {
45
+ return `${options.replacementStr}${mediaRelativeUrl}`;
92
46
  }
93
47
  }
94
-
95
- if (parsedInput.origin === 'http://relative') {
96
- return url;
48
+ if (options.filesBaseUrl) {
49
+ const filesRelativeUrl = (0, absolute_to_relative_1.default)(url, options.filesBaseUrl, options);
50
+ if (isRelative(filesRelativeUrl)) {
51
+ return `${options.replacementStr}${filesRelativeUrl}`;
52
+ }
97
53
  }
98
-
99
- // convert to relative with stripped subdir
100
- // always returns root-relative starting with forward slash
101
- const relativeUrl = absoluteToRelative(url, root, options);
102
-
103
- // return still absolute urls as-is (eg. external site, mailto, etc)
104
- try {
105
- const parsedURL = new URL(relativeUrl, 'http://relative');
106
- if (parsedURL.origin !== 'http://relative') {
107
- return url;
54
+ if (options.imageBaseUrl) {
55
+ const imageRelativeUrl = (0, absolute_to_relative_1.default)(url, options.imageBaseUrl, options);
56
+ if (isRelative(imageRelativeUrl)) {
57
+ return `${options.replacementStr}${imageRelativeUrl}`;
108
58
  }
109
- } catch (e) {
110
- // url was unparseable
111
- return url;
112
59
  }
113
-
114
- return `${options.replacementStr}${relativeUrl}`;
60
+ return url;
115
61
  };
116
-
117
- module.exports = absoluteToTransformReady;
62
+ exports.default = absoluteToTransformReady;
@@ -0,0 +1,24 @@
1
+ import type { BaseUrlOptionsInput } from './types';
2
+ declare function escapeRegExp(string: string): string;
3
+ type BuildEarlyExitMatchOptions = BaseUrlOptionsInput & {
4
+ ignoreProtocol?: boolean;
5
+ };
6
+ /**
7
+ * Build a regex pattern that matches any of the configured base URLs (site URL + CDN URLs).
8
+ * This is used for early exit optimizations - if content doesn't contain any of these URLs,
9
+ * we can skip expensive parsing.
10
+ *
11
+ * @param {string} siteUrl - The site's base URL
12
+ * @param {Object} options - Options containing CDN base URLs
13
+ * @param {string} [options.imageBaseUrl] - CDN base URL for images
14
+ * @param {string} [options.filesBaseUrl] - CDN base URL for files
15
+ * @param {string} [options.mediaBaseUrl] - CDN base URL for media
16
+ * @param {boolean} [options.ignoreProtocol=true] - Whether to strip protocol from URLs
17
+ * @returns {string|null} Regex pattern matching any configured base URL, or null if none configured
18
+ */
19
+ declare function buildEarlyExitMatch(siteUrl: string, options?: BuildEarlyExitMatchOptions): string | null;
20
+ declare const _default: {
21
+ buildEarlyExitMatch: typeof buildEarlyExitMatch;
22
+ escapeRegExp: typeof escapeRegExp;
23
+ };
24
+ export default _default;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function escapeRegExp(string) {
4
+ return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
5
+ }
6
+ /**
7
+ * Build a regex pattern that matches any of the configured base URLs (site URL + CDN URLs).
8
+ * This is used for early exit optimizations - if content doesn't contain any of these URLs,
9
+ * we can skip expensive parsing.
10
+ *
11
+ * @param {string} siteUrl - The site's base URL
12
+ * @param {Object} options - Options containing CDN base URLs
13
+ * @param {string} [options.imageBaseUrl] - CDN base URL for images
14
+ * @param {string} [options.filesBaseUrl] - CDN base URL for files
15
+ * @param {string} [options.mediaBaseUrl] - CDN base URL for media
16
+ * @param {boolean} [options.ignoreProtocol=true] - Whether to strip protocol from URLs
17
+ * @returns {string|null} Regex pattern matching any configured base URL, or null if none configured
18
+ */
19
+ function buildEarlyExitMatch(siteUrl, options = {}) {
20
+ const candidates = [siteUrl, options.imageBaseUrl, options.filesBaseUrl, options.mediaBaseUrl]
21
+ .filter((value) => typeof value === 'string' && value.length > 0)
22
+ .map((value) => {
23
+ let normalized = options.ignoreProtocol ? value.replace(/http:|https:/, '') : value;
24
+ return normalized.replace(/\/$/, '');
25
+ })
26
+ .filter((value) => Boolean(value))
27
+ .map(escapeRegExp);
28
+ if (!candidates.length) {
29
+ return null;
30
+ }
31
+ if (candidates.length === 1) {
32
+ return candidates[0];
33
+ }
34
+ return `(?:${candidates.join('|')})`;
35
+ }
36
+ exports.default = {
37
+ buildEarlyExitMatch,
38
+ escapeRegExp
39
+ };
@@ -0,0 +1,2 @@
1
+ declare function deduplicateDoubleSlashes(url: string): string;
2
+ export default deduplicateDoubleSlashes;
@@ -1,5 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
1
3
  function deduplicateDoubleSlashes(url) {
2
- return url.replace(/\/\//g, '/');
4
+ // Preserve protocol slashes (e.g., http://, https://) and only deduplicate
5
+ // slashes in the path portion. The pattern (^|[^:])\/\/+ matches double slashes
6
+ // that are either at the start of the string or not preceded by a colon.
7
+ return url.replace(/(^|[^:])\/\/+/g, '$1/');
3
8
  }
4
-
5
- module.exports = deduplicateDoubleSlashes;
9
+ exports.default = deduplicateDoubleSlashes;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Remove duplicated directories from the start of a path or url's path
3
+ *
4
+ * @param {string} url URL or pathname with possible duplicate subdirectory
5
+ * @param {string} rootUrl Root URL with an optional subdirectory
6
+ * @returns {string} URL or pathname with any duplicated subdirectory removed
7
+ */
8
+ declare const deduplicateSubdirectory: (url: string, rootUrl: string) => string;
9
+ export default deduplicateSubdirectory;
@@ -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
  * Remove duplicated directories from the start of a path or url's path
5
6
  *
@@ -12,20 +13,15 @@ const deduplicateSubdirectory = function deduplicateSubdirectory(url, rootUrl) {
12
13
  if (!rootUrl.endsWith('/')) {
13
14
  rootUrl = `${rootUrl}/`;
14
15
  }
15
-
16
- const parsedRoot = new URL(rootUrl);
17
-
16
+ const parsedRoot = new url_1.URL(rootUrl);
18
17
  // do nothing if rootUrl does not have a subdirectory
19
18
  if (parsedRoot.pathname === '/') {
20
19
  return url;
21
20
  }
22
-
23
21
  const subdir = parsedRoot.pathname.replace(/(^\/|\/$)+/g, '');
24
22
  // we can have subdirs that match TLDs so we need to restrict matches to
25
23
  // duplicates that start with a / or the beginning of the url
26
24
  const subdirRegex = new RegExp(`(^|/)${subdir}/${subdir}(/|$)`);
27
-
28
25
  return url.replace(subdirRegex, `$1${subdir}/`);
29
26
  };
30
-
31
- module.exports = deduplicateSubdirectory;
27
+ exports.default = deduplicateSubdirectory;
@@ -0,0 +1,3 @@
1
+ import type { AbsoluteToRelativeOptionsInput } from './absolute-to-relative';
2
+ declare function htmlAbsoluteToRelative(html: string | undefined, siteUrl: string, _options?: AbsoluteToRelativeOptionsInput): string;
3
+ export default htmlAbsoluteToRelative;
@@ -1,20 +1,20 @@
1
- const htmlTransform = require('./html-transform');
2
- const absoluteToRelative = require('./absolute-to-relative');
3
-
4
- function htmlAbsoluteToRelative(html = '', siteUrl, _options) {
5
- const defaultOptions = {assetsOnly: false, ignoreProtocol: true};
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 html_transform_1 = __importDefault(require("./html-transform"));
7
+ const absolute_to_relative_1 = __importDefault(require("./absolute-to-relative"));
8
+ function htmlAbsoluteToRelative(html = '', siteUrl, _options = {}) {
9
+ const defaultOptions = { assetsOnly: false, ignoreProtocol: true };
6
10
  const options = Object.assign({}, defaultOptions, _options || {});
7
-
8
11
  // exit early and avoid parsing if the content does not contain the siteUrl
9
12
  options.earlyExitMatchStr = options.ignoreProtocol ? siteUrl.replace(/http:|https:/, '') : siteUrl;
10
13
  options.earlyExitMatchStr = options.earlyExitMatchStr.replace(/\/$/, '');
11
-
12
14
  // need to ignore itemPath because absoluteToRelative doesn't take that option
13
15
  const transformFunction = function (_url, _siteUrl, _itemPath, __options) {
14
- return absoluteToRelative(_url, _siteUrl, __options);
16
+ return (0, absolute_to_relative_1.default)(_url, _siteUrl, __options);
15
17
  };
16
-
17
- return htmlTransform(html, siteUrl, transformFunction, '', options);
18
+ return (0, html_transform_1.default)(html, siteUrl, transformFunction, '', options);
18
19
  }
19
-
20
- module.exports = htmlAbsoluteToRelative;
20
+ exports.default = htmlAbsoluteToRelative;
@@ -0,0 +1,3 @@
1
+ import type { AbsoluteToTransformReadyOptionsInput } from './types';
2
+ declare const htmlAbsoluteToTransformReady: (html: string | undefined, siteUrl: string, _options?: AbsoluteToTransformReadyOptionsInput) => string;
3
+ export default htmlAbsoluteToTransformReady;
@@ -1,44 +1,24 @@
1
- const htmlTransform = require('./html-transform');
2
- const absoluteToTransformReady = require('./absolute-to-transform-ready');
3
-
4
- function escapeRegExp(string) {
5
- return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
6
- }
7
-
8
- function buildEarlyExitMatch(siteUrl, options) {
9
- const candidates = [siteUrl, options.imageBaseUrl, options.filesBaseUrl, options.mediaBaseUrl]
10
- .filter(Boolean)
11
- .map((value) => {
12
- let normalized = options.ignoreProtocol ? value.replace(/http:|https:/, '') : value;
13
- return normalized.replace(/\/$/, '');
14
- })
15
- .filter(Boolean)
16
- .map(escapeRegExp);
17
-
18
- if (!candidates.length) {
19
- return null;
20
- }
21
-
22
- if (candidates.length === 1) {
23
- return candidates[0];
24
- }
25
-
26
- return `(?:${candidates.join('|')})`;
27
- }
28
-
29
- const htmlAbsoluteToTransformReady = function (html = '', siteUrl, _options) {
30
- const defaultOptions = {assetsOnly: false, ignoreProtocol: true};
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 html_transform_1 = __importDefault(require("./html-transform"));
7
+ const absolute_to_transform_ready_1 = __importDefault(require("./absolute-to-transform-ready"));
8
+ const build_early_exit_match_1 = __importDefault(require("./build-early-exit-match"));
9
+ const { buildEarlyExitMatch } = build_early_exit_match_1.default;
10
+ const htmlAbsoluteToTransformReady = function (html = '', siteUrl, _options = {}) {
11
+ const defaultOptions = { assetsOnly: false, ignoreProtocol: true };
31
12
  const options = Object.assign({}, defaultOptions, _options || {});
32
-
33
13
  // exit early and avoid parsing if the content does not contain the siteUrl or configured asset bases
34
- options.earlyExitMatchStr = buildEarlyExitMatch(siteUrl, options);
35
-
14
+ const earlyExitMatch = buildEarlyExitMatch(siteUrl, options);
15
+ if (earlyExitMatch) {
16
+ options.earlyExitMatchStr = earlyExitMatch;
17
+ }
36
18
  // need to ignore itemPath because absoluteToRelative doesn't take that option
37
19
  const transformFunction = function (_url, _siteUrl, _itemPath, __options) {
38
- return absoluteToTransformReady(_url, _siteUrl, __options);
20
+ return (0, absolute_to_transform_ready_1.default)(_url, _siteUrl, __options);
39
21
  };
40
-
41
- return htmlTransform(html, siteUrl, transformFunction, '', options);
22
+ return (0, html_transform_1.default)(html, siteUrl, transformFunction, '', options);
42
23
  };
43
-
44
- module.exports = htmlAbsoluteToTransformReady;
24
+ exports.default = htmlAbsoluteToTransformReady;
@@ -0,0 +1,3 @@
1
+ import type { SecureOptionsInput } from './types';
2
+ declare function htmlRelativeToAbsolute(html: string | undefined, siteUrl: string, itemPath: string | null, _options?: SecureOptionsInput): string;
3
+ export default htmlRelativeToAbsolute;
@@ -1,17 +1,18 @@
1
- const htmlTransform = require('./html-transform');
2
- const relativeToAbsolute = require('./relative-to-absolute');
3
-
4
- function htmlRelativeToAbsolute(html = '', siteUrl, itemPath, _options) {
5
- const defaultOptions = {assetsOnly: false, secure: false};
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 html_transform_1 = __importDefault(require("./html-transform"));
7
+ const relative_to_absolute_1 = __importDefault(require("./relative-to-absolute"));
8
+ function htmlRelativeToAbsolute(html = '', siteUrl, itemPath, _options = {}) {
9
+ const defaultOptions = { assetsOnly: false, secure: false };
6
10
  const options = Object.assign({}, defaultOptions, _options || {});
7
-
8
11
  // exit early and avoid parsing if the content does not contain an attribute we might transform
9
12
  options.earlyExitMatchStr = 'href=|src=|srcset=';
10
13
  if (options.assetsOnly) {
11
14
  options.earlyExitMatchStr = options.staticImageUrlPrefix;
12
15
  }
13
-
14
- return htmlTransform(html, siteUrl, relativeToAbsolute, itemPath, options);
16
+ return (0, html_transform_1.default)(html, siteUrl, relative_to_absolute_1.default, itemPath, options);
15
17
  }
16
-
17
- module.exports = htmlRelativeToAbsolute;
18
+ exports.default = htmlRelativeToAbsolute;
@@ -0,0 +1,3 @@
1
+ import { type RelativeToTransformReadyOptionsInput as RelativeToTransformReadyOptionsInputType } from './relative-to-transform-ready';
2
+ declare const htmlRelativeToTransformReady: (html: string | undefined, root: string, itemPath: string | null | RelativeToTransformReadyOptionsInputType, _options?: RelativeToTransformReadyOptionsInputType) => string;
3
+ export default htmlRelativeToTransformReady;
@@ -1,28 +1,33 @@
1
- const htmlTransform = require('./html-transform');
2
- const relativeToTransformReady = require('./relative-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 html_transform_1 = __importDefault(require("./html-transform"));
7
+ const relative_to_transform_ready_1 = __importDefault(require("./relative-to-transform-ready"));
4
8
  const htmlRelativeToTransformReady = function (html = '', 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
-
11
19
  const defaultOptions = {
12
20
  replacementStr: '__GHOST_URL__'
13
21
  };
14
22
  const overrideOptions = {
15
23
  secure: false
16
24
  };
17
- const options = Object.assign({}, defaultOptions, _options, overrideOptions);
18
-
25
+ const options = Object.assign({}, defaultOptions, finalOptions, overrideOptions);
19
26
  // exit early and avoid parsing if the content does not contain an attribute we might transform
20
27
  options.earlyExitMatchStr = 'href=|src=|srcset=';
21
28
  if (options.assetsOnly) {
22
29
  options.earlyExitMatchStr = options.staticImageUrlPrefix;
23
30
  }
24
-
25
- return htmlTransform(html, root, relativeToTransformReady, itemPath, options);
31
+ return (0, html_transform_1.default)(html, root, relative_to_transform_ready_1.default, finalItemPath, options);
26
32
  };
27
-
28
- module.exports = htmlRelativeToTransformReady;
33
+ exports.default = htmlRelativeToTransformReady;
@@ -0,0 +1,3 @@
1
+ import type { HtmlTransformOptionsInput } from './types';
2
+ declare function htmlToTransformReady(html: string, siteUrl: string, itemPath: string | null | HtmlTransformOptionsInput, options?: HtmlTransformOptionsInput): string;
3
+ export default htmlToTransformReady;
@@ -1,13 +1,21 @@
1
- const htmlRelativeToAbsolute = require('./html-relative-to-absolute');
2
- const htmlAbsoluteToTransformReady = require('./html-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 html_relative_to_absolute_1 = __importDefault(require("./html-relative-to-absolute"));
7
+ const html_absolute_to_transform_ready_1 = __importDefault(require("./html-absolute-to-transform-ready"));
4
8
  function htmlToTransformReady(html, 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 absolute = htmlRelativeToAbsolute(html, siteUrl, itemPath, options);
10
- return htmlAbsoluteToTransformReady(absolute, siteUrl, options);
15
+ else if (typeof itemPath === 'string') {
16
+ finalItemPath = itemPath;
17
+ }
18
+ const absolute = (0, html_relative_to_absolute_1.default)(html, siteUrl, finalItemPath, finalOptions);
19
+ return (0, html_absolute_to_transform_ready_1.default)(absolute, siteUrl, finalOptions);
11
20
  }
12
-
13
- module.exports = htmlToTransformReady;
21
+ exports.default = htmlToTransformReady;
@@ -0,0 +1,3 @@
1
+ import type { HtmlTransformOptionsInput, UrlTransformFunction } from './types';
2
+ declare function htmlTransform(html: string | undefined, siteUrl: string, transformFunction: UrlTransformFunction, itemPath: string | null, _options?: HtmlTransformOptionsInput): string;
3
+ export default htmlTransform;