@tryghost/url-utils 5.0.0 → 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.
- 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 +13 -8
- package/lib/utils/absolute-to-transform-ready.d.ts +13 -4
- package/lib/utils/absolute-to-transform-ready.js +15 -10
- 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 +9 -6
- package/lib/utils/html-relative-to-transform-ready.d.ts +3 -3
- package/lib/utils/html-relative-to-transform-ready.js +17 -9
- 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 +3 -4
- package/lib/utils/html-transform.js +18 -19
- 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 +21 -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 +9 -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 +25 -18
- 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 +4 -4
- 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 +8 -9
- 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,8 +32,8 @@ 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;
|
|
@@ -40,16 +42,19 @@ const absoluteToRelative = function absoluteToRelative(url, rootUrl, _options =
|
|
|
40
42
|
catch (e) {
|
|
41
43
|
return url;
|
|
42
44
|
}
|
|
45
|
+
if (!parsedRoot) {
|
|
46
|
+
return url;
|
|
47
|
+
}
|
|
43
48
|
const matchesHost = parsedUrl.host === parsedRoot.host;
|
|
44
49
|
const matchesProtocol = parsedUrl.protocol === parsedRoot.protocol;
|
|
45
50
|
const matchesPath = parsedUrl.pathname.indexOf(parsedRoot.pathname) === 0;
|
|
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,11 +1,14 @@
|
|
|
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
13
|
catch (e) {
|
|
11
14
|
// url was unparseable
|
|
@@ -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,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 relative_to_absolute_1 = __importDefault(require("./relative-to-absolute"));
|
|
8
|
+
function htmlRelativeToAbsolute(html = '', siteUrl, itemPath, _options = {}) {
|
|
6
9
|
const defaultOptions = { assetsOnly: false, secure: false };
|
|
7
10
|
const options = Object.assign({}, defaultOptions, _options || {});
|
|
8
11
|
// exit early and avoid parsing if the content does not contain an attribute we might transform
|
|
@@ -10,6 +13,6 @@ function htmlRelativeToAbsolute(html = '', siteUrl, itemPath, _options) {
|
|
|
10
13
|
if (options.assetsOnly) {
|
|
11
14
|
options.earlyExitMatchStr = options.staticImageUrlPrefix;
|
|
12
15
|
}
|
|
13
|
-
return
|
|
16
|
+
return (0, html_transform_1.default)(html, siteUrl, relative_to_absolute_1.default, itemPath, options);
|
|
14
17
|
}
|
|
15
|
-
|
|
18
|
+
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,20 @@
|
|
|
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_transform_1 = __importDefault(require("./html-transform"));
|
|
7
|
+
const relative_to_transform_ready_1 = __importDefault(require("./relative-to-transform-ready"));
|
|
5
8
|
const htmlRelativeToTransformReady = function (html = '', root, itemPath, _options) {
|
|
6
9
|
// itemPath is optional, if it's an object may be the options param instead
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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;
|
|
10
18
|
}
|
|
11
19
|
const defaultOptions = {
|
|
12
20
|
replacementStr: '__GHOST_URL__'
|
|
@@ -14,12 +22,12 @@ const htmlRelativeToTransformReady = function (html = '', root, itemPath, _optio
|
|
|
14
22
|
const overrideOptions = {
|
|
15
23
|
secure: false
|
|
16
24
|
};
|
|
17
|
-
const options = Object.assign({}, defaultOptions,
|
|
25
|
+
const options = Object.assign({}, defaultOptions, finalOptions, overrideOptions);
|
|
18
26
|
// exit early and avoid parsing if the content does not contain an attribute we might transform
|
|
19
27
|
options.earlyExitMatchStr = 'href=|src=|srcset=';
|
|
20
28
|
if (options.assetsOnly) {
|
|
21
29
|
options.earlyExitMatchStr = options.staticImageUrlPrefix;
|
|
22
30
|
}
|
|
23
|
-
return
|
|
31
|
+
return (0, html_transform_1.default)(html, root, relative_to_transform_ready_1.default, finalItemPath, options);
|
|
24
32
|
};
|
|
25
|
-
|
|
33
|
+
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,3 @@
|
|
|
1
|
-
|
|
2
|
-
declare function
|
|
3
|
-
|
|
4
|
-
declare function htmlTransform(html: string | undefined, siteUrl: any, transformFunction: any, itemPath: any, _options: any): string;
|
|
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;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const cheerio = require('cheerio');
|
|
4
4
|
function escapeRegExp(string) {
|
|
5
5
|
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
@@ -18,23 +18,16 @@ function extractStyleUrls(style = '') {
|
|
|
18
18
|
}
|
|
19
19
|
return urls;
|
|
20
20
|
}
|
|
21
|
-
function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options) {
|
|
22
|
-
const defaultOptions = {
|
|
21
|
+
function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options = {}) {
|
|
22
|
+
const defaultOptions = {
|
|
23
|
+
assetsOnly: false,
|
|
24
|
+
secure: false
|
|
25
|
+
};
|
|
23
26
|
const options = Object.assign({}, defaultOptions, _options || {});
|
|
24
27
|
if (!html || (options.earlyExitMatchStr && !html.match(new RegExp(options.earlyExitMatchStr)))) {
|
|
25
28
|
return html;
|
|
26
29
|
}
|
|
27
30
|
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
31
|
const replacements = {};
|
|
39
32
|
function addReplacement(replacement) {
|
|
40
33
|
const key = `${replacement.name}="${replacement.originalValue}"`;
|
|
@@ -47,7 +40,8 @@ function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options
|
|
|
47
40
|
['href', 'src', 'srcset', 'style'].forEach((attributeName) => {
|
|
48
41
|
htmlContent('[' + attributeName + ']').each((ix, el) => {
|
|
49
42
|
// ignore <stream> elems and html inside of <code> elements
|
|
50
|
-
|
|
43
|
+
const elementName = 'name' in el ? el.name : null;
|
|
44
|
+
if (elementName === 'stream' || htmlContent(el).closest('code').length) {
|
|
51
45
|
addReplacement({
|
|
52
46
|
name: attributeName,
|
|
53
47
|
originalValue: htmlContent(el).attr(attributeName),
|
|
@@ -55,8 +49,8 @@ function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options
|
|
|
55
49
|
});
|
|
56
50
|
return;
|
|
57
51
|
}
|
|
58
|
-
|
|
59
|
-
const originalValue =
|
|
52
|
+
const elWrapper = htmlContent(el);
|
|
53
|
+
const originalValue = elWrapper.attr(attributeName);
|
|
60
54
|
if (attributeName === 'srcset' || attributeName === 'style') {
|
|
61
55
|
let urls;
|
|
62
56
|
if (attributeName === 'srcset') {
|
|
@@ -65,11 +59,11 @@ function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options
|
|
|
65
59
|
else {
|
|
66
60
|
urls = extractStyleUrls(originalValue);
|
|
67
61
|
}
|
|
68
|
-
const absoluteUrls = urls.map(url => transformFunction(url, siteUrl, itemPath, options));
|
|
62
|
+
const absoluteUrls = urls.map((url) => transformFunction(url, siteUrl, itemPath, options));
|
|
69
63
|
let transformedValue = originalValue;
|
|
70
64
|
urls.forEach((url, i) => {
|
|
71
65
|
if (absoluteUrls[i]) {
|
|
72
|
-
|
|
66
|
+
const regex = new RegExp(escapeRegExp(url), 'g');
|
|
73
67
|
transformedValue = transformedValue.replace(regex, absoluteUrls[i]);
|
|
74
68
|
}
|
|
75
69
|
});
|
|
@@ -102,6 +96,11 @@ function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options
|
|
|
102
96
|
skipCount += 1;
|
|
103
97
|
return;
|
|
104
98
|
}
|
|
99
|
+
// transformedValue is guaranteed to exist here because we only add replacements
|
|
100
|
+
// when transformedValue !== originalValue, and we've already skipped entries with skip: true
|
|
101
|
+
if (!transformedValue) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
105
104
|
// this regex avoids matching unrelated plain text by checking that the attribute/value pair
|
|
106
105
|
// is surrounded by <> - that should be sufficient because if the plain text had that wrapper
|
|
107
106
|
// it would be parsed as a tag
|
|
@@ -120,4 +119,4 @@ function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options
|
|
|
120
119
|
}
|
|
121
120
|
return html;
|
|
122
121
|
}
|
|
123
|
-
|
|
122
|
+
exports.default = htmlTransform;
|
package/lib/utils/index.d.ts
CHANGED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import absoluteToRelative from './absolute-to-relative';
|
|
2
|
+
import absoluteToTransformReady from './absolute-to-transform-ready';
|
|
3
|
+
import deduplicateDoubleSlashes from './deduplicate-double-slashes';
|
|
4
|
+
import deduplicateSubdirectory from './deduplicate-subdirectory';
|
|
5
|
+
import htmlAbsoluteToRelative from './html-absolute-to-relative';
|
|
6
|
+
import htmlAbsoluteToTransformReady from './html-absolute-to-transform-ready';
|
|
7
|
+
import htmlRelativeToAbsolute from './html-relative-to-absolute';
|
|
8
|
+
import htmlRelativeToTransformReady from './html-relative-to-transform-ready';
|
|
9
|
+
import htmlToTransformReady from './html-to-transform-ready';
|
|
10
|
+
import isSSL from './is-ssl';
|
|
11
|
+
import lexicalAbsoluteToRelative from './lexical-absolute-to-relative';
|
|
12
|
+
import lexicalAbsoluteToTransformReady from './lexical-absolute-to-transform-ready';
|
|
13
|
+
import lexicalRelativeToAbsolute from './lexical-relative-to-absolute';
|
|
14
|
+
import lexicalRelativeToTransformReady from './lexical-relative-to-transform-ready';
|
|
15
|
+
import lexicalToTransformReady from './lexical-to-transform-ready';
|
|
16
|
+
import markdownAbsoluteToRelative from './markdown-absolute-to-relative';
|
|
17
|
+
import markdownRelativeToAbsolute from './markdown-relative-to-absolute';
|
|
18
|
+
import markdownAbsoluteToTransformReady from './markdown-absolute-to-transform-ready';
|
|
19
|
+
import markdownRelativeToTransformReady from './markdown-relative-to-transform-ready';
|
|
20
|
+
import markdownToTransformReady from './markdown-to-transform-ready';
|
|
21
|
+
import mobiledocAbsoluteToRelative from './mobiledoc-absolute-to-relative';
|
|
22
|
+
import mobiledocRelativeToAbsolute from './mobiledoc-relative-to-absolute';
|
|
23
|
+
import mobiledocAbsoluteToTransformReady from './mobiledoc-absolute-to-transform-ready';
|
|
24
|
+
import mobiledocRelativeToTransformReady from './mobiledoc-relative-to-transform-ready';
|
|
25
|
+
import mobiledocToTransformReady from './mobiledoc-to-transform-ready';
|
|
26
|
+
import plaintextAbsoluteToTransformReady from './plaintext-absolute-to-transform-ready';
|
|
27
|
+
import plaintextRelativeToTransformReady from './plaintext-relative-to-transform-ready';
|
|
28
|
+
import plaintextToTransformReady from './plaintext-to-transform-ready';
|
|
29
|
+
import relativeToAbsolute from './relative-to-absolute';
|
|
30
|
+
import relativeToTransformReady from './relative-to-transform-ready';
|
|
31
|
+
import replacePermalink from './replace-permalink';
|
|
32
|
+
import stripSubdirectoryFromPath from './strip-subdirectory-from-path';
|
|
33
|
+
import toTransformReady from './to-transform-ready';
|
|
34
|
+
import transformReadyToAbsolute from './transform-ready-to-absolute';
|
|
35
|
+
import transformReadyToRelative from './transform-ready-to-relative';
|
|
36
|
+
import urlJoin from './url-join';
|
|
37
|
+
export { absoluteToRelative, absoluteToTransformReady, deduplicateDoubleSlashes, deduplicateSubdirectory, htmlAbsoluteToRelative, htmlRelativeToAbsolute, htmlAbsoluteToTransformReady, htmlRelativeToTransformReady, htmlToTransformReady, isSSL, lexicalAbsoluteToRelative, lexicalAbsoluteToTransformReady, lexicalRelativeToAbsolute, lexicalRelativeToTransformReady, lexicalToTransformReady, markdownAbsoluteToRelative, markdownRelativeToAbsolute, markdownAbsoluteToTransformReady, markdownRelativeToTransformReady, markdownToTransformReady, mobiledocAbsoluteToRelative, mobiledocRelativeToAbsolute, mobiledocAbsoluteToTransformReady, mobiledocRelativeToTransformReady, mobiledocToTransformReady, plaintextAbsoluteToTransformReady, plaintextRelativeToTransformReady, plaintextToTransformReady, relativeToAbsolute, relativeToTransformReady, replacePermalink, stripSubdirectoryFromPath, toTransformReady, transformReadyToAbsolute, transformReadyToRelative, urlJoin };
|
|
38
|
+
declare const utils: {
|
|
39
|
+
absoluteToRelative: (url: string, rootUrl?: string, _options?: import("./absolute-to-relative").AbsoluteToRelativeOptionsInput) => string;
|
|
40
|
+
absoluteToTransformReady: (url: string, root: string, _options?: import("./absolute-to-transform-ready").AbsoluteToTransformReadyOptionsInput) => string;
|
|
41
|
+
deduplicateDoubleSlashes: typeof deduplicateDoubleSlashes;
|
|
42
|
+
deduplicateSubdirectory: (url: string, rootUrl: string) => string;
|
|
43
|
+
htmlAbsoluteToRelative: typeof htmlAbsoluteToRelative;
|
|
44
|
+
htmlRelativeToAbsolute: typeof htmlRelativeToAbsolute;
|
|
45
|
+
htmlAbsoluteToTransformReady: (html: string | undefined, siteUrl: string, _options?: import("./types").AbsoluteToTransformReadyOptionsInput) => string;
|
|
46
|
+
htmlRelativeToTransformReady: (html: string | undefined, root: string, itemPath: string | null | import("./relative-to-transform-ready").RelativeToTransformReadyOptionsInput, _options?: import("./relative-to-transform-ready").RelativeToTransformReadyOptionsInput) => string;
|
|
47
|
+
htmlToTransformReady: typeof htmlToTransformReady;
|
|
48
|
+
isSSL: typeof isSSL;
|
|
49
|
+
lexicalAbsoluteToRelative: typeof lexicalAbsoluteToRelative;
|
|
50
|
+
lexicalRelativeToAbsolute: typeof lexicalRelativeToAbsolute;
|
|
51
|
+
lexicalAbsoluteToTransformReady: typeof lexicalAbsoluteToTransformReady;
|
|
52
|
+
lexicalRelativeToTransformReady: typeof lexicalRelativeToTransformReady;
|
|
53
|
+
lexicalToTransformReady: typeof lexicalToTransformReady;
|
|
54
|
+
markdownAbsoluteToRelative: typeof markdownAbsoluteToRelative;
|
|
55
|
+
markdownAbsoluteToTransformReady: typeof markdownAbsoluteToTransformReady;
|
|
56
|
+
markdownRelativeToAbsolute: typeof markdownRelativeToAbsolute;
|
|
57
|
+
markdownRelativeToTransformReady: typeof markdownRelativeToTransformReady;
|
|
58
|
+
markdownToTransformReady: typeof markdownToTransformReady;
|
|
59
|
+
mobiledocAbsoluteToRelative: typeof mobiledocAbsoluteToRelative;
|
|
60
|
+
mobiledocAbsoluteToTransformReady: typeof mobiledocAbsoluteToTransformReady;
|
|
61
|
+
mobiledocRelativeToAbsolute: typeof mobiledocRelativeToAbsolute;
|
|
62
|
+
mobiledocRelativeToTransformReady: typeof mobiledocRelativeToTransformReady;
|
|
63
|
+
mobiledocToTransformReady: typeof mobiledocToTransformReady;
|
|
64
|
+
plaintextAbsoluteToTransformReady: (plaintext: string, rootUrl: string, itemPath: string | null | Partial<Partial<import("./types").AbsoluteToTransformReadyOptions> & Partial<import("./types").BaseUrlOptions>>, options?: Partial<Partial<import("./types").AbsoluteToTransformReadyOptions> & Partial<import("./types").BaseUrlOptions>>) => string;
|
|
65
|
+
plaintextRelativeToTransformReady: (plaintext: string, rootUrl: string, itemPath: string | null | import("./relative-to-transform-ready").RelativeToTransformReadyOptionsInput, options?: import("./relative-to-transform-ready").RelativeToTransformReadyOptionsInput) => string;
|
|
66
|
+
plaintextToTransformReady: typeof plaintextToTransformReady;
|
|
67
|
+
relativeToAbsolute: (path: string, rootUrl: string, itemPath: string | null | import("./relative-to-absolute").RelativeToAbsoluteOptionsInput, _options?: import("./relative-to-absolute").RelativeToAbsoluteOptionsInput) => string;
|
|
68
|
+
relativeToTransformReady: (url: string, root: string, itemPath: string | null | import("./relative-to-transform-ready").RelativeToTransformReadyOptionsInput, _options?: import("./relative-to-transform-ready").RelativeToTransformReadyOptionsInput) => string;
|
|
69
|
+
replacePermalink: typeof replacePermalink;
|
|
70
|
+
stripSubdirectoryFromPath: (path?: string, rootUrl?: string) => string;
|
|
71
|
+
toTransformReady: typeof toTransformReady;
|
|
72
|
+
transformReadyToAbsolute: (str: string | undefined, root: string, _options?: import("./transform-ready-to-absolute").TransformReadyToAbsoluteOptionsInput) => string;
|
|
73
|
+
transformReadyToRelative: (str: string | undefined, root: string, _options?: import("./types").TransformReadyReplacementOptionsInput) => string;
|
|
74
|
+
urlJoin: typeof urlJoin;
|
|
75
|
+
};
|
|
76
|
+
export default utils;
|