@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.
- package/lib/UrlUtils.d.ts +147 -3
- package/lib/UrlUtils.js +228 -112
- package/lib/index.d.ts +2 -0
- package/lib/index.js +6 -0
- package/lib/utils/absolute-to-relative.d.ts +9 -3
- package/lib/utils/absolute-to-relative.js +14 -9
- package/lib/utils/absolute-to-transform-ready.d.ts +13 -4
- package/lib/utils/absolute-to-transform-ready.js +16 -11
- package/lib/utils/build-early-exit-match.d.ts +11 -1
- package/lib/utils/build-early-exit-match.js +4 -4
- package/lib/utils/deduplicate-double-slashes.d.ts +2 -1
- package/lib/utils/deduplicate-double-slashes.js +2 -2
- package/lib/utils/deduplicate-subdirectory.d.ts +2 -2
- package/lib/utils/deduplicate-subdirectory.js +4 -4
- package/lib/utils/html-absolute-to-relative.d.ts +3 -3
- package/lib/utils/html-absolute-to-relative.js +10 -7
- package/lib/utils/html-absolute-to-transform-ready.d.ts +3 -4
- package/lib/utils/html-absolute-to-transform-ready.js +16 -9
- package/lib/utils/html-relative-to-absolute.d.ts +3 -3
- package/lib/utils/html-relative-to-absolute.js +43 -8
- package/lib/utils/html-relative-to-transform-ready.d.ts +3 -3
- package/lib/utils/html-relative-to-transform-ready.js +51 -11
- package/lib/utils/html-to-transform-ready.d.ts +3 -3
- package/lib/utils/html-to-transform-ready.js +17 -9
- package/lib/utils/html-transform.d.ts +5 -4
- package/lib/utils/html-transform.js +25 -21
- package/lib/utils/index.d.ts +76 -0
- package/lib/utils/index.js +115 -38
- package/lib/utils/is-ssl.d.ts +2 -2
- package/lib/utils/is-ssl.js +4 -4
- package/lib/utils/lexical-absolute-to-relative.d.ts +3 -2
- package/lib/utils/lexical-absolute-to-relative.js +9 -6
- package/lib/utils/lexical-absolute-to-transform-ready.d.ts +3 -2
- package/lib/utils/lexical-absolute-to-transform-ready.js +10 -7
- package/lib/utils/lexical-relative-to-absolute.d.ts +3 -3
- package/lib/utils/lexical-relative-to-absolute.js +8 -5
- package/lib/utils/lexical-relative-to-transform-ready.d.ts +3 -3
- package/lib/utils/lexical-relative-to-transform-ready.js +8 -5
- package/lib/utils/lexical-to-transform-ready.d.ts +3 -3
- package/lib/utils/lexical-to-transform-ready.js +17 -9
- package/lib/utils/lexical-transform.d.ts +3 -2
- package/lib/utils/lexical-transform.js +22 -19
- package/lib/utils/markdown-absolute-to-relative.d.ts +3 -4
- package/lib/utils/markdown-absolute-to-relative.js +11 -8
- package/lib/utils/markdown-absolute-to-transform-ready.d.ts +3 -5
- package/lib/utils/markdown-absolute-to-transform-ready.js +17 -10
- package/lib/utils/markdown-relative-to-absolute.d.ts +3 -4
- package/lib/utils/markdown-relative-to-absolute.js +11 -8
- package/lib/utils/markdown-relative-to-transform-ready.d.ts +3 -4
- package/lib/utils/markdown-relative-to-transform-ready.js +11 -8
- package/lib/utils/markdown-to-transform-ready.d.ts +3 -3
- package/lib/utils/markdown-to-transform-ready.js +17 -9
- package/lib/utils/markdown-transform.d.ts +3 -5
- package/lib/utils/markdown-transform.js +12 -6
- package/lib/utils/mobiledoc-absolute-to-relative.d.ts +3 -2
- package/lib/utils/mobiledoc-absolute-to-relative.js +9 -6
- package/lib/utils/mobiledoc-absolute-to-transform-ready.d.ts +3 -2
- package/lib/utils/mobiledoc-absolute-to-transform-ready.js +10 -7
- package/lib/utils/mobiledoc-relative-to-absolute.d.ts +3 -3
- package/lib/utils/mobiledoc-relative-to-absolute.js +8 -5
- package/lib/utils/mobiledoc-relative-to-transform-ready.d.ts +3 -3
- package/lib/utils/mobiledoc-relative-to-transform-ready.js +8 -5
- package/lib/utils/mobiledoc-to-transform-ready.d.ts +3 -3
- package/lib/utils/mobiledoc-to-transform-ready.js +17 -9
- package/lib/utils/mobiledoc-transform.d.ts +3 -1
- package/lib/utils/mobiledoc-transform.js +5 -4
- package/lib/utils/plaintext-absolute-to-transform-ready.d.ts +5 -4
- package/lib/utils/plaintext-absolute-to-transform-ready.js +16 -11
- package/lib/utils/plaintext-relative-to-transform-ready.d.ts +3 -2
- package/lib/utils/plaintext-relative-to-transform-ready.js +15 -7
- package/lib/utils/plaintext-to-transform-ready.d.ts +5 -3
- package/lib/utils/plaintext-to-transform-ready.js +17 -9
- package/lib/utils/relative-to-absolute.d.ts +5 -3
- package/lib/utils/relative-to-absolute.js +26 -19
- package/lib/utils/relative-to-transform-ready.d.ts +8 -2
- package/lib/utils/relative-to-transform-ready.js +22 -10
- package/lib/utils/replace-permalink.d.ts +13 -2
- package/lib/utils/replace-permalink.js +44 -8
- package/lib/utils/strip-subdirectory-from-path.d.ts +1 -1
- package/lib/utils/strip-subdirectory-from-path.js +5 -5
- package/lib/utils/to-transform-ready.d.ts +5 -3
- package/lib/utils/to-transform-ready.js +17 -9
- package/lib/utils/transform-ready-to-absolute.d.ts +9 -1
- package/lib/utils/transform-ready-to-absolute.js +2 -2
- package/lib/utils/transform-ready-to-relative.d.ts +3 -1
- package/lib/utils/transform-ready-to-relative.js +4 -3
- package/lib/utils/types.d.ts +92 -0
- package/lib/utils/types.js +2 -0
- package/lib/utils/url-join.d.ts +5 -2
- package/lib/utils/url-join.js +11 -5
- package/package.json +10 -11
- package/index.js +0 -2
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
|
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 =
|
|
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
|
-
|
|
60
|
+
exports.default = absoluteToRelative;
|
|
@@ -1,4 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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
|
-
|
|
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:
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
36
|
+
exports.default = {
|
|
37
37
|
buildEarlyExitMatch,
|
|
38
38
|
escapeRegExp
|
|
39
39
|
};
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
declare function deduplicateDoubleSlashes(url:
|
|
1
|
+
declare function deduplicateDoubleSlashes(url: string): string;
|
|
2
|
+
export default deduplicateDoubleSlashes;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
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
|
-
|
|
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:
|
|
8
|
+
declare const deduplicateSubdirectory: (url: string, rootUrl: string) => string;
|
|
9
|
+
export default deduplicateSubdirectory;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
const
|
|
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
|
-
|
|
27
|
+
exports.default = deduplicateSubdirectory;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
declare
|
|
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,8 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
|
16
|
+
return (0, absolute_to_relative_1.default)(_url, _siteUrl, __options);
|
|
14
17
|
};
|
|
15
|
-
return
|
|
18
|
+
return (0, html_transform_1.default)(html, siteUrl, transformFunction, '', options);
|
|
16
19
|
}
|
|
17
|
-
|
|
20
|
+
exports.default = htmlAbsoluteToRelative;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
declare const
|
|
3
|
-
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
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
|
-
|
|
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
|
|
20
|
+
return (0, absolute_to_transform_ready_1.default)(_url, _siteUrl, __options);
|
|
14
21
|
};
|
|
15
|
-
return
|
|
22
|
+
return (0, html_transform_1.default)(html, siteUrl, transformFunction, '', options);
|
|
16
23
|
};
|
|
17
|
-
|
|
24
|
+
exports.default = htmlAbsoluteToTransformReady;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
declare
|
|
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,15 +1,50 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
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
|
|
48
|
+
return (0, html_transform_1.default)(html, siteUrl, relative_to_absolute_1.default, itemPath, options);
|
|
14
49
|
}
|
|
15
|
-
|
|
50
|
+
exports.default = htmlRelativeToAbsolute;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
declare const
|
|
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,12 +1,53 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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,
|
|
18
|
-
|
|
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
|
|
63
|
+
return (0, html_transform_1.default)(html, root, relative_to_transform_ready_1.default, finalItemPath, options);
|
|
24
64
|
};
|
|
25
|
-
|
|
65
|
+
exports.default = htmlRelativeToTransformReady;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
declare
|
|
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
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
11
|
-
|
|
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
|
-
|
|
21
|
+
exports.default = htmlToTransformReady;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
declare
|
|
3
|
-
declare
|
|
4
|
-
declare function htmlTransform(html: string | undefined, siteUrl:
|
|
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
|
-
|
|
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 = {
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
59
|
-
const originalValue =
|
|
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
|
-
|
|
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
|
-
|
|
127
|
+
exports.default = htmlTransform;
|