@tryghost/url-utils 5.0.0-rc.0 → 5.0.0-rc.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.
@@ -1,13 +1,37 @@
1
1
  const htmlTransform = require('./html-transform');
2
2
  const absoluteToTransformReady = require('./absolute-to-transform-ready');
3
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
+
4
29
  const htmlAbsoluteToTransformReady = function (html = '', siteUrl, _options) {
5
30
  const defaultOptions = {assetsOnly: false, ignoreProtocol: true};
6
31
  const options = Object.assign({}, defaultOptions, _options || {});
7
32
 
8
- // exit early and avoid parsing if the content does not contain the siteUrl
9
- options.earlyExitMatchStr = options.ignoreProtocol ? siteUrl.replace(/http:|https:/, '') : siteUrl;
10
- options.earlyExitMatchStr = options.earlyExitMatchStr.replace(/\/$/, '');
33
+ // exit early and avoid parsing if the content does not contain the siteUrl or configured asset bases
34
+ options.earlyExitMatchStr = buildEarlyExitMatch(siteUrl, options);
11
35
 
12
36
  // need to ignore itemPath because absoluteToRelative doesn't take that option
13
37
  const transformFunction = function (_url, _siteUrl, _itemPath, __options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/url-utils",
3
- "version": "5.0.0-rc.0",
3
+ "version": "5.0.0-rc.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/TryGhost/SDK.git",