@tryghost/url-utils 5.0.0 → 5.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/lib/UrlUtils.d.ts +147 -3
  2. package/lib/UrlUtils.js +228 -112
  3. package/lib/index.d.ts +2 -0
  4. package/lib/index.js +6 -0
  5. package/lib/utils/absolute-to-relative.d.ts +9 -3
  6. package/lib/utils/absolute-to-relative.js +14 -9
  7. package/lib/utils/absolute-to-transform-ready.d.ts +13 -4
  8. package/lib/utils/absolute-to-transform-ready.js +16 -11
  9. package/lib/utils/build-early-exit-match.d.ts +11 -1
  10. package/lib/utils/build-early-exit-match.js +4 -4
  11. package/lib/utils/deduplicate-double-slashes.d.ts +2 -1
  12. package/lib/utils/deduplicate-double-slashes.js +2 -2
  13. package/lib/utils/deduplicate-subdirectory.d.ts +2 -2
  14. package/lib/utils/deduplicate-subdirectory.js +4 -4
  15. package/lib/utils/html-absolute-to-relative.d.ts +3 -3
  16. package/lib/utils/html-absolute-to-relative.js +10 -7
  17. package/lib/utils/html-absolute-to-transform-ready.d.ts +3 -4
  18. package/lib/utils/html-absolute-to-transform-ready.js +16 -9
  19. package/lib/utils/html-relative-to-absolute.d.ts +3 -3
  20. package/lib/utils/html-relative-to-absolute.js +43 -8
  21. package/lib/utils/html-relative-to-transform-ready.d.ts +3 -3
  22. package/lib/utils/html-relative-to-transform-ready.js +51 -11
  23. package/lib/utils/html-to-transform-ready.d.ts +3 -3
  24. package/lib/utils/html-to-transform-ready.js +17 -9
  25. package/lib/utils/html-transform.d.ts +5 -4
  26. package/lib/utils/html-transform.js +25 -21
  27. package/lib/utils/index.d.ts +76 -0
  28. package/lib/utils/index.js +115 -38
  29. package/lib/utils/is-ssl.d.ts +2 -2
  30. package/lib/utils/is-ssl.js +4 -4
  31. package/lib/utils/lexical-absolute-to-relative.d.ts +3 -2
  32. package/lib/utils/lexical-absolute-to-relative.js +9 -6
  33. package/lib/utils/lexical-absolute-to-transform-ready.d.ts +3 -2
  34. package/lib/utils/lexical-absolute-to-transform-ready.js +10 -7
  35. package/lib/utils/lexical-relative-to-absolute.d.ts +3 -3
  36. package/lib/utils/lexical-relative-to-absolute.js +8 -5
  37. package/lib/utils/lexical-relative-to-transform-ready.d.ts +3 -3
  38. package/lib/utils/lexical-relative-to-transform-ready.js +8 -5
  39. package/lib/utils/lexical-to-transform-ready.d.ts +3 -3
  40. package/lib/utils/lexical-to-transform-ready.js +17 -9
  41. package/lib/utils/lexical-transform.d.ts +3 -2
  42. package/lib/utils/lexical-transform.js +22 -19
  43. package/lib/utils/markdown-absolute-to-relative.d.ts +3 -4
  44. package/lib/utils/markdown-absolute-to-relative.js +11 -8
  45. package/lib/utils/markdown-absolute-to-transform-ready.d.ts +3 -5
  46. package/lib/utils/markdown-absolute-to-transform-ready.js +17 -10
  47. package/lib/utils/markdown-relative-to-absolute.d.ts +3 -4
  48. package/lib/utils/markdown-relative-to-absolute.js +11 -8
  49. package/lib/utils/markdown-relative-to-transform-ready.d.ts +3 -4
  50. package/lib/utils/markdown-relative-to-transform-ready.js +11 -8
  51. package/lib/utils/markdown-to-transform-ready.d.ts +3 -3
  52. package/lib/utils/markdown-to-transform-ready.js +17 -9
  53. package/lib/utils/markdown-transform.d.ts +3 -5
  54. package/lib/utils/markdown-transform.js +12 -6
  55. package/lib/utils/mobiledoc-absolute-to-relative.d.ts +3 -2
  56. package/lib/utils/mobiledoc-absolute-to-relative.js +9 -6
  57. package/lib/utils/mobiledoc-absolute-to-transform-ready.d.ts +3 -2
  58. package/lib/utils/mobiledoc-absolute-to-transform-ready.js +10 -7
  59. package/lib/utils/mobiledoc-relative-to-absolute.d.ts +3 -3
  60. package/lib/utils/mobiledoc-relative-to-absolute.js +8 -5
  61. package/lib/utils/mobiledoc-relative-to-transform-ready.d.ts +3 -3
  62. package/lib/utils/mobiledoc-relative-to-transform-ready.js +8 -5
  63. package/lib/utils/mobiledoc-to-transform-ready.d.ts +3 -3
  64. package/lib/utils/mobiledoc-to-transform-ready.js +17 -9
  65. package/lib/utils/mobiledoc-transform.d.ts +3 -1
  66. package/lib/utils/mobiledoc-transform.js +5 -4
  67. package/lib/utils/plaintext-absolute-to-transform-ready.d.ts +5 -4
  68. package/lib/utils/plaintext-absolute-to-transform-ready.js +16 -11
  69. package/lib/utils/plaintext-relative-to-transform-ready.d.ts +3 -2
  70. package/lib/utils/plaintext-relative-to-transform-ready.js +15 -7
  71. package/lib/utils/plaintext-to-transform-ready.d.ts +5 -3
  72. package/lib/utils/plaintext-to-transform-ready.js +17 -9
  73. package/lib/utils/relative-to-absolute.d.ts +5 -3
  74. package/lib/utils/relative-to-absolute.js +26 -19
  75. package/lib/utils/relative-to-transform-ready.d.ts +8 -2
  76. package/lib/utils/relative-to-transform-ready.js +22 -10
  77. package/lib/utils/replace-permalink.d.ts +13 -2
  78. package/lib/utils/replace-permalink.js +44 -8
  79. package/lib/utils/strip-subdirectory-from-path.d.ts +1 -1
  80. package/lib/utils/strip-subdirectory-from-path.js +5 -5
  81. package/lib/utils/to-transform-ready.d.ts +5 -3
  82. package/lib/utils/to-transform-ready.js +17 -9
  83. package/lib/utils/transform-ready-to-absolute.d.ts +9 -1
  84. package/lib/utils/transform-ready-to-absolute.js +2 -2
  85. package/lib/utils/transform-ready-to-relative.d.ts +3 -1
  86. package/lib/utils/transform-ready-to-relative.js +4 -3
  87. package/lib/utils/types.d.ts +92 -0
  88. package/lib/utils/types.js +2 -0
  89. package/lib/utils/url-join.d.ts +5 -2
  90. package/lib/utils/url-join.js +11 -5
  91. package/package.json +10 -11
  92. package/index.js +0 -2
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
- // @ts-nocheck
3
- // require the whatwg compatible URL library (same behaviour in node and browser)
4
- const { URL } = require('url');
5
- const stripSubdirectoryFromPath = require('./strip-subdirectory-from-path');
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"));
6
8
  /**
7
9
  * Convert an absolute URL to a root-relative path if it matches the supplied root domain.
8
10
  *
@@ -30,14 +32,17 @@ const absoluteToRelative = function absoluteToRelative(url, rootUrl, _options =
30
32
  let parsedUrl;
31
33
  let parsedRoot;
32
34
  try {
33
- parsedUrl = new URL(url, 'http://relative');
34
- parsedRoot = parsedUrl.origin === 'null' ? undefined : new URL(rootUrl || parsedUrl.origin);
35
+ parsedUrl = new url_1.URL(url, 'http://relative');
36
+ parsedRoot = parsedUrl.origin === 'null' ? undefined : new url_1.URL(rootUrl || parsedUrl.origin);
35
37
  // return the url as-is if it was relative or non-http
36
38
  if (parsedUrl.origin === 'null' || parsedUrl.origin === 'http://relative') {
37
39
  return url;
38
40
  }
39
41
  }
40
- catch (e) {
42
+ catch {
43
+ return url;
44
+ }
45
+ if (!parsedRoot) {
41
46
  return url;
42
47
  }
43
48
  const matchesHost = parsedUrl.host === parsedRoot.host;
@@ -46,10 +51,10 @@ const absoluteToRelative = function absoluteToRelative(url, rootUrl, _options =
46
51
  if (matchesHost && (options.ignoreProtocol || matchesProtocol) && matchesPath) {
47
52
  let path = parsedUrl.href.replace(parsedUrl.origin, '');
48
53
  if (options.withoutSubdirectory) {
49
- path = stripSubdirectoryFromPath(path, rootUrl);
54
+ path = (0, strip_subdirectory_from_path_1.default)(path, rootUrl);
50
55
  }
51
56
  return path;
52
57
  }
53
58
  return url;
54
59
  };
55
- module.exports = absoluteToRelative;
60
+ exports.default = absoluteToRelative;
@@ -1,4 +1,13 @@
1
- declare const URL: any;
2
- declare const absoluteToRelative: any;
3
- declare function isRelative(url: any): boolean;
4
- declare const absoluteToTransformReady: (url: any, root: any, _options?: {}) => any;
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,13 +1,16 @@
1
1
  "use strict";
2
- // @ts-nocheck
3
- const { URL } = require('url');
4
- const absoluteToRelative = require('./absolute-to-relative');
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");
5
8
  function isRelative(url) {
6
9
  let parsedInput;
7
10
  try {
8
- parsedInput = new URL(url, 'http://relative');
11
+ parsedInput = new url_1.URL(url, 'http://relative');
9
12
  }
10
- catch (e) {
13
+ catch {
11
14
  // url was unparseable
12
15
  return false;
13
16
  }
@@ -22,7 +25,9 @@ const absoluteToTransformReady = function (url, root, _options = {}) {
22
25
  staticMediaUrlPrefix: 'content/media',
23
26
  imageBaseUrl: null,
24
27
  filesBaseUrl: null,
25
- mediaBaseUrl: null
28
+ mediaBaseUrl: null,
29
+ ignoreProtocol: true,
30
+ assetsOnly: false
26
31
  };
27
32
  const options = Object.assign({}, defaultOptions, _options);
28
33
  if (isRelative(url)) {
@@ -30,28 +35,28 @@ const absoluteToTransformReady = function (url, root, _options = {}) {
30
35
  }
31
36
  // convert to relative with stripped subdir
32
37
  // always returns root-relative starting with forward slash
33
- const rootRelativeUrl = absoluteToRelative(url, root, options);
38
+ const rootRelativeUrl = (0, absolute_to_relative_1.default)(url, root, options);
34
39
  if (isRelative(rootRelativeUrl)) {
35
40
  return `${options.replacementStr}${rootRelativeUrl}`;
36
41
  }
37
42
  if (options.mediaBaseUrl) {
38
- const mediaRelativeUrl = absoluteToRelative(url, options.mediaBaseUrl, options);
43
+ const mediaRelativeUrl = (0, absolute_to_relative_1.default)(url, options.mediaBaseUrl, options);
39
44
  if (isRelative(mediaRelativeUrl)) {
40
45
  return `${options.replacementStr}${mediaRelativeUrl}`;
41
46
  }
42
47
  }
43
48
  if (options.filesBaseUrl) {
44
- const filesRelativeUrl = absoluteToRelative(url, options.filesBaseUrl, options);
49
+ const filesRelativeUrl = (0, absolute_to_relative_1.default)(url, options.filesBaseUrl, options);
45
50
  if (isRelative(filesRelativeUrl)) {
46
51
  return `${options.replacementStr}${filesRelativeUrl}`;
47
52
  }
48
53
  }
49
54
  if (options.imageBaseUrl) {
50
- const imageRelativeUrl = absoluteToRelative(url, options.imageBaseUrl, options);
55
+ const imageRelativeUrl = (0, absolute_to_relative_1.default)(url, options.imageBaseUrl, options);
51
56
  if (isRelative(imageRelativeUrl)) {
52
57
  return `${options.replacementStr}${imageRelativeUrl}`;
53
58
  }
54
59
  }
55
60
  return url;
56
61
  };
57
- module.exports = absoluteToTransformReady;
62
+ exports.default = absoluteToTransformReady;
@@ -1,3 +1,8 @@
1
+ import type { BaseUrlOptionsInput } from './types';
2
+ declare function escapeRegExp(string: string): string;
3
+ type BuildEarlyExitMatchOptions = BaseUrlOptionsInput & {
4
+ ignoreProtocol?: boolean;
5
+ };
1
6
  /**
2
7
  * Build a regex pattern that matches any of the configured base URLs (site URL + CDN URLs).
3
8
  * This is used for early exit optimizations - if content doesn't contain any of these URLs,
@@ -11,4 +16,9 @@
11
16
  * @param {boolean} [options.ignoreProtocol=true] - Whether to strip protocol from URLs
12
17
  * @returns {string|null} Regex pattern matching any configured base URL, or null if none configured
13
18
  */
14
- declare function buildEarlyExitMatch(siteUrl: any, options?: {}): any;
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;
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- // @ts-nocheck
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  function escapeRegExp(string) {
4
4
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
5
5
  }
@@ -18,12 +18,12 @@ function escapeRegExp(string) {
18
18
  */
19
19
  function buildEarlyExitMatch(siteUrl, options = {}) {
20
20
  const candidates = [siteUrl, options.imageBaseUrl, options.filesBaseUrl, options.mediaBaseUrl]
21
- .filter(Boolean)
21
+ .filter((value) => typeof value === 'string' && value.length > 0)
22
22
  .map((value) => {
23
23
  let normalized = options.ignoreProtocol ? value.replace(/http:|https:/, '') : value;
24
24
  return normalized.replace(/\/$/, '');
25
25
  })
26
- .filter(Boolean)
26
+ .filter((value) => Boolean(value))
27
27
  .map(escapeRegExp);
28
28
  if (!candidates.length) {
29
29
  return null;
@@ -33,7 +33,7 @@ function buildEarlyExitMatch(siteUrl, options = {}) {
33
33
  }
34
34
  return `(?:${candidates.join('|')})`;
35
35
  }
36
- module.exports = {
36
+ exports.default = {
37
37
  buildEarlyExitMatch,
38
38
  escapeRegExp
39
39
  };
@@ -1 +1,2 @@
1
- declare function deduplicateDoubleSlashes(url: any): any;
1
+ declare function deduplicateDoubleSlashes(url: string): string;
2
+ export default deduplicateDoubleSlashes;
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
- // @ts-nocheck
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  function deduplicateDoubleSlashes(url) {
4
4
  // Preserve protocol slashes (e.g., http://, https://) and only deduplicate
5
5
  // slashes in the path portion. The pattern (^|[^:])\/\/+ matches double slashes
6
6
  // that are either at the start of the string or not preceded by a colon.
7
7
  return url.replace(/(^|[^:])\/\/+/g, '$1/');
8
8
  }
9
- module.exports = deduplicateDoubleSlashes;
9
+ exports.default = deduplicateDoubleSlashes;
@@ -1,4 +1,3 @@
1
- declare const URL: any;
2
1
  /**
3
2
  * Remove duplicated directories from the start of a path or url's path
4
3
  *
@@ -6,4 +5,5 @@ declare const URL: any;
6
5
  * @param {string} rootUrl Root URL with an optional subdirectory
7
6
  * @returns {string} URL or pathname with any duplicated subdirectory removed
8
7
  */
9
- declare const deduplicateSubdirectory: (url: any, rootUrl: any) => any;
8
+ declare const deduplicateSubdirectory: (url: string, rootUrl: string) => string;
9
+ export default deduplicateSubdirectory;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
- // @ts-nocheck
3
- const { URL } = require('url');
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const url_1 = require("url");
4
4
  /**
5
5
  * Remove duplicated directories from the start of a path or url's path
6
6
  *
@@ -13,7 +13,7 @@ const deduplicateSubdirectory = function deduplicateSubdirectory(url, rootUrl) {
13
13
  if (!rootUrl.endsWith('/')) {
14
14
  rootUrl = `${rootUrl}/`;
15
15
  }
16
- const parsedRoot = new URL(rootUrl);
16
+ const parsedRoot = new url_1.URL(rootUrl);
17
17
  // do nothing if rootUrl does not have a subdirectory
18
18
  if (parsedRoot.pathname === '/') {
19
19
  return url;
@@ -24,4 +24,4 @@ const deduplicateSubdirectory = function deduplicateSubdirectory(url, rootUrl) {
24
24
  const subdirRegex = new RegExp(`(^|/)${subdir}/${subdir}(/|$)`);
25
25
  return url.replace(subdirRegex, `$1${subdir}/`);
26
26
  };
27
- module.exports = deduplicateSubdirectory;
27
+ exports.default = deduplicateSubdirectory;
@@ -1,3 +1,3 @@
1
- declare const htmlTransform: any;
2
- declare const absoluteToRelative: any;
3
- declare function htmlAbsoluteToRelative(html: string | undefined, siteUrl: any, _options: any): any;
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,8 +1,11 @@
1
1
  "use strict";
2
- // @ts-nocheck
3
- const htmlTransform = require('./html-transform');
4
- const absoluteToRelative = require('./absolute-to-relative');
5
- function htmlAbsoluteToRelative(html = '', siteUrl, _options) {
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 = {}) {
6
9
  const defaultOptions = { assetsOnly: false, ignoreProtocol: true };
7
10
  const options = Object.assign({}, defaultOptions, _options || {});
8
11
  // exit early and avoid parsing if the content does not contain the siteUrl
@@ -10,8 +13,8 @@ function htmlAbsoluteToRelative(html = '', siteUrl, _options) {
10
13
  options.earlyExitMatchStr = options.earlyExitMatchStr.replace(/\/$/, '');
11
14
  // need to ignore itemPath because absoluteToRelative doesn't take that option
12
15
  const transformFunction = function (_url, _siteUrl, _itemPath, __options) {
13
- return absoluteToRelative(_url, _siteUrl, __options);
16
+ return (0, absolute_to_relative_1.default)(_url, _siteUrl, __options);
14
17
  };
15
- return htmlTransform(html, siteUrl, transformFunction, '', options);
18
+ return (0, html_transform_1.default)(html, siteUrl, transformFunction, '', options);
16
19
  }
17
- module.exports = htmlAbsoluteToRelative;
20
+ exports.default = htmlAbsoluteToRelative;
@@ -1,4 +1,3 @@
1
- declare const htmlTransform: any;
2
- declare const absoluteToTransformReady: any;
3
- declare const buildEarlyExitMatch: any;
4
- declare const htmlAbsoluteToTransformReady: (html: string | undefined, siteUrl: any, _options: any) => any;
1
+ import type { AbsoluteToTransformReadyOptionsInput } from './types';
2
+ declare const htmlAbsoluteToTransformReady: (html: string | undefined, siteUrl: string, _options?: AbsoluteToTransformReadyOptionsInput) => string;
3
+ export default htmlAbsoluteToTransformReady;
@@ -1,17 +1,24 @@
1
1
  "use strict";
2
- // @ts-nocheck
3
- const htmlTransform = require('./html-transform');
4
- const absoluteToTransformReady = require('./absolute-to-transform-ready');
5
- const { buildEarlyExitMatch } = require('./build-early-exit-match');
6
- const htmlAbsoluteToTransformReady = function (html = '', siteUrl, _options) {
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 = {}) {
7
11
  const defaultOptions = { assetsOnly: false, ignoreProtocol: true };
8
12
  const options = Object.assign({}, defaultOptions, _options || {});
9
13
  // exit early and avoid parsing if the content does not contain the siteUrl or configured asset bases
10
- options.earlyExitMatchStr = buildEarlyExitMatch(siteUrl, options);
14
+ const earlyExitMatch = buildEarlyExitMatch(siteUrl, options);
15
+ if (earlyExitMatch) {
16
+ options.earlyExitMatchStr = earlyExitMatch;
17
+ }
11
18
  // need to ignore itemPath because absoluteToRelative doesn't take that option
12
19
  const transformFunction = function (_url, _siteUrl, _itemPath, __options) {
13
- return absoluteToTransformReady(_url, _siteUrl, __options);
20
+ return (0, absolute_to_transform_ready_1.default)(_url, _siteUrl, __options);
14
21
  };
15
- return htmlTransform(html, siteUrl, transformFunction, '', options);
22
+ return (0, html_transform_1.default)(html, siteUrl, transformFunction, '', options);
16
23
  };
17
- module.exports = htmlAbsoluteToTransformReady;
24
+ exports.default = htmlAbsoluteToTransformReady;
@@ -1,3 +1,3 @@
1
- declare const htmlTransform: any;
2
- declare const relativeToAbsolute: any;
3
- declare function htmlRelativeToAbsolute(html: string | undefined, siteUrl: any, itemPath: any, _options: any): any;
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,15 +1,50 @@
1
1
  "use strict";
2
- // @ts-nocheck
3
- const htmlTransform = require('./html-transform');
4
- const relativeToAbsolute = require('./relative-to-absolute');
5
- function htmlRelativeToAbsolute(html = '', siteUrl, itemPath, _options) {
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 });
39
+ const html_transform_1 = __importStar(require("./html-transform"));
40
+ const relative_to_absolute_1 = __importDefault(require("./relative-to-absolute"));
41
+ function htmlRelativeToAbsolute(html = '', siteUrl, itemPath, _options = {}) {
6
42
  const defaultOptions = { assetsOnly: false, secure: false };
7
43
  const options = Object.assign({}, defaultOptions, _options || {});
8
- // exit early and avoid parsing if the content does not contain an attribute we might transform
9
- options.earlyExitMatchStr = 'href=|src=|srcset=';
44
+ options.earlyExitMatchStr = html_transform_1.earlyExitMatchStr;
10
45
  if (options.assetsOnly) {
11
46
  options.earlyExitMatchStr = options.staticImageUrlPrefix;
12
47
  }
13
- return htmlTransform(html, siteUrl, relativeToAbsolute, itemPath, options);
48
+ return (0, html_transform_1.default)(html, siteUrl, relative_to_absolute_1.default, itemPath, options);
14
49
  }
15
- module.exports = htmlRelativeToAbsolute;
50
+ exports.default = htmlRelativeToAbsolute;
@@ -1,3 +1,3 @@
1
- declare const htmlTransform: any;
2
- declare const relativeToTransformReady: any;
3
- declare const htmlRelativeToTransformReady: (html: string | undefined, root: any, itemPath: any, _options: any) => any;
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,12 +1,53 @@
1
1
  "use strict";
2
- // @ts-nocheck
3
- const htmlTransform = require('./html-transform');
4
- const relativeToTransformReady = require('./relative-to-transform-ready');
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 });
39
+ const html_transform_1 = __importStar(require("./html-transform"));
40
+ const relative_to_transform_ready_1 = __importDefault(require("./relative-to-transform-ready"));
5
41
  const htmlRelativeToTransformReady = function (html = '', root, itemPath, _options) {
6
42
  // itemPath is optional, if it's an object may be the options param instead
7
- if (typeof itemPath === 'object' && !_options) {
8
- _options = itemPath;
9
- itemPath = null;
43
+ let finalItemPath = null;
44
+ let finalOptions = _options || {};
45
+ if (typeof itemPath === 'object' && itemPath !== null && !_options) {
46
+ finalOptions = itemPath;
47
+ finalItemPath = null;
48
+ }
49
+ else if (typeof itemPath === 'string') {
50
+ finalItemPath = itemPath;
10
51
  }
11
52
  const defaultOptions = {
12
53
  replacementStr: '__GHOST_URL__'
@@ -14,12 +55,11 @@ const htmlRelativeToTransformReady = function (html = '', root, itemPath, _optio
14
55
  const overrideOptions = {
15
56
  secure: false
16
57
  };
17
- const options = Object.assign({}, defaultOptions, _options, overrideOptions);
18
- // exit early and avoid parsing if the content does not contain an attribute we might transform
19
- options.earlyExitMatchStr = 'href=|src=|srcset=';
58
+ const options = Object.assign({}, defaultOptions, finalOptions, overrideOptions);
59
+ options.earlyExitMatchStr = html_transform_1.earlyExitMatchStr;
20
60
  if (options.assetsOnly) {
21
61
  options.earlyExitMatchStr = options.staticImageUrlPrefix;
22
62
  }
23
- return htmlTransform(html, root, relativeToTransformReady, itemPath, options);
63
+ return (0, html_transform_1.default)(html, root, relative_to_transform_ready_1.default, finalItemPath, options);
24
64
  };
25
- module.exports = htmlRelativeToTransformReady;
65
+ exports.default = htmlRelativeToTransformReady;
@@ -1,3 +1,3 @@
1
- declare const htmlRelativeToAbsolute: any;
2
- declare const htmlAbsoluteToTransformReady: any;
3
- declare function htmlToTransformReady(html: any, siteUrl: any, itemPath: any, options: any): any;
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
1
  "use strict";
2
- // @ts-nocheck
3
- const htmlRelativeToAbsolute = require('./html-relative-to-absolute');
4
- const htmlAbsoluteToTransformReady = require('./html-absolute-to-transform-ready');
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const 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"));
5
8
  function htmlToTransformReady(html, siteUrl, itemPath, options) {
6
- if (typeof itemPath === 'object' && !options) {
7
- options = itemPath;
8
- itemPath = null;
9
+ let finalItemPath = null;
10
+ let finalOptions = options || {};
11
+ if (typeof itemPath === 'object' && itemPath !== null && !options) {
12
+ finalOptions = itemPath;
13
+ finalItemPath = null;
9
14
  }
10
- const absolute = htmlRelativeToAbsolute(html, siteUrl, itemPath, options);
11
- 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);
12
20
  }
13
- module.exports = htmlToTransformReady;
21
+ exports.default = htmlToTransformReady;
@@ -1,4 +1,5 @@
1
- declare const cheerio: any;
2
- declare function extractSrcsetUrls(srcset?: string): string[];
3
- declare function extractStyleUrls(style?: string): string[];
4
- declare function htmlTransform(html: string | undefined, siteUrl: any, transformFunction: any, itemPath: any, _options: any): string;
1
+ import type { HtmlTransformOptionsInput, UrlTransformFunction } from './types';
2
+ export declare const transformAttributes: string[];
3
+ export declare const earlyExitMatchStr: string;
4
+ declare function htmlTransform(html: string | undefined, siteUrl: string, transformFunction: UrlTransformFunction, itemPath: string | null, _options?: HtmlTransformOptionsInput): string;
5
+ export default htmlTransform;
@@ -1,6 +1,12 @@
1
1
  "use strict";
2
- // @ts-nocheck
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.earlyExitMatchStr = exports.transformAttributes = void 0;
4
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
3
5
  const cheerio = require('cheerio');
6
+ exports.transformAttributes = ['href', 'src', 'srcset', 'style'];
7
+ exports.earlyExitMatchStr = exports.transformAttributes
8
+ .map(attr => `${attr}=`)
9
+ .join('|');
4
10
  function escapeRegExp(string) {
5
11
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
6
12
  }
@@ -18,23 +24,16 @@ function extractStyleUrls(style = '') {
18
24
  }
19
25
  return urls;
20
26
  }
21
- function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options) {
22
- const defaultOptions = { assetsOnly: false, secure: false };
27
+ function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options = {}) {
28
+ const defaultOptions = {
29
+ assetsOnly: false,
30
+ secure: false
31
+ };
23
32
  const options = Object.assign({}, defaultOptions, _options || {});
24
33
  if (!html || (options.earlyExitMatchStr && !html.match(new RegExp(options.earlyExitMatchStr)))) {
25
34
  return html;
26
35
  }
27
36
  const htmlContent = cheerio.load(html, { decodeEntities: false });
28
- // replacements is keyed with the attr name + original relative value so
29
- // that we can implement skips for untouchable urls
30
- //
31
- // replacements = {
32
- // 'href="/test"': [
33
- // {name: 'href', originalValue: '/test', absoluteValue: '.../test'},
34
- // {name: 'href', originalValue: '/test', skip: true}, // found inside a <code> element
35
- // {name: 'href', originalValue: '/test', absoluteValue: '.../test'},
36
- // ]
37
- // }
38
37
  const replacements = {};
39
38
  function addReplacement(replacement) {
40
39
  const key = `${replacement.name}="${replacement.originalValue}"`;
@@ -43,11 +42,11 @@ function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options
43
42
  }
44
43
  replacements[key].push(replacement);
45
44
  }
46
- // find all of the relative url attributes that we care about
47
- ['href', 'src', 'srcset', 'style'].forEach((attributeName) => {
45
+ exports.transformAttributes.forEach((attributeName) => {
48
46
  htmlContent('[' + attributeName + ']').each((ix, el) => {
49
47
  // ignore <stream> elems and html inside of <code> elements
50
- if (el.name === 'stream' || htmlContent(el).closest('code').length) {
48
+ const elementName = 'name' in el ? el.name : null;
49
+ if (elementName === 'stream' || htmlContent(el).closest('code').length) {
51
50
  addReplacement({
52
51
  name: attributeName,
53
52
  originalValue: htmlContent(el).attr(attributeName),
@@ -55,8 +54,8 @@ function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options
55
54
  });
56
55
  return;
57
56
  }
58
- el = htmlContent(el);
59
- const originalValue = el.attr(attributeName);
57
+ const elWrapper = htmlContent(el);
58
+ const originalValue = elWrapper.attr(attributeName);
60
59
  if (attributeName === 'srcset' || attributeName === 'style') {
61
60
  let urls;
62
61
  if (attributeName === 'srcset') {
@@ -65,11 +64,11 @@ function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options
65
64
  else {
66
65
  urls = extractStyleUrls(originalValue);
67
66
  }
68
- const absoluteUrls = urls.map(url => transformFunction(url, siteUrl, itemPath, options));
67
+ const absoluteUrls = urls.map((url) => transformFunction(url, siteUrl, itemPath, options));
69
68
  let transformedValue = originalValue;
70
69
  urls.forEach((url, i) => {
71
70
  if (absoluteUrls[i]) {
72
- let regex = new RegExp(escapeRegExp(url), 'g');
71
+ const regex = new RegExp(escapeRegExp(url), 'g');
73
72
  transformedValue = transformedValue.replace(regex, absoluteUrls[i]);
74
73
  }
75
74
  });
@@ -102,6 +101,11 @@ function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options
102
101
  skipCount += 1;
103
102
  return;
104
103
  }
104
+ // transformedValue is guaranteed to exist here because we only add replacements
105
+ // when transformedValue !== originalValue, and we've already skipped entries with skip: true
106
+ if (!transformedValue) {
107
+ return;
108
+ }
105
109
  // this regex avoids matching unrelated plain text by checking that the attribute/value pair
106
110
  // is surrounded by <> - that should be sufficient because if the plain text had that wrapper
107
111
  // it would be parsed as a tag
@@ -120,4 +124,4 @@ function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options
120
124
  }
121
125
  return html;
122
126
  }
123
- module.exports = htmlTransform;
127
+ exports.default = htmlTransform;