@tryghost/url-utils 4.0.3 → 4.2.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/url-utils.js +77 -0
- package/lib/utils/_lexical-transform.js +36 -0
- package/lib/utils/index.js +5 -0
- package/lib/utils/lexical-absolute-to-relative.js +16 -0
- package/lib/utils/lexical-absolute-to-transform-ready.js +16 -0
- package/lib/utils/lexical-relative-to-absolute.js +12 -0
- package/lib/utils/lexical-relative-to-transform-ready.js +12 -0
- package/lib/utils/lexical-to-transform-ready.js +13 -0
- package/package.json +2 -2
package/lib/url-utils.js
CHANGED
|
@@ -427,6 +427,66 @@ module.exports = class UrlUtils {
|
|
|
427
427
|
return utils.mobiledocAbsoluteToTransformReady(serializedMobiledoc, this.getSiteUrl(), _options);
|
|
428
428
|
}
|
|
429
429
|
|
|
430
|
+
lexicalToTransformReady(serializedLexical, itemPath, options) {
|
|
431
|
+
if (typeof itemPath === 'object' && !options) {
|
|
432
|
+
options = itemPath;
|
|
433
|
+
itemPath = null;
|
|
434
|
+
}
|
|
435
|
+
const defaultOptions = {
|
|
436
|
+
cardTransformers: this._config.cardTransformers
|
|
437
|
+
};
|
|
438
|
+
const _options = assignOptions({}, defaultOptions, options || {});
|
|
439
|
+
return utils.lexicalToTransformReady(serializedLexical, this.getSiteUrl(), itemPath, _options);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
lexicalRelativeToAbsolute(serializedLexical, itemPath, options) {
|
|
443
|
+
if (typeof itemPath === 'object' && !options) {
|
|
444
|
+
options = itemPath;
|
|
445
|
+
itemPath = null;
|
|
446
|
+
}
|
|
447
|
+
const defaultOptions = {
|
|
448
|
+
assetsOnly: false,
|
|
449
|
+
staticImageUrlPrefix: this._config.staticImageUrlPrefix,
|
|
450
|
+
cardTransformers: this._config.cardTransformers
|
|
451
|
+
};
|
|
452
|
+
const _options = assignOptions({}, defaultOptions, options || {});
|
|
453
|
+
return utils.lexicalRelativeToAbsolute(serializedLexical, this.getSiteUrl(), itemPath, _options);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
lexicalRelativeToTransformReady(serializedLexical, itemPath, options) {
|
|
457
|
+
if (typeof itemPath === 'object' && !options) {
|
|
458
|
+
options = itemPath;
|
|
459
|
+
itemPath = null;
|
|
460
|
+
}
|
|
461
|
+
const defaultOptions = {
|
|
462
|
+
assetsOnly: false,
|
|
463
|
+
staticImageUrlPrefix: this._config.staticImageUrlPrefix,
|
|
464
|
+
cardTransformers: this._config.cardTransformers
|
|
465
|
+
};
|
|
466
|
+
const _options = assignOptions({}, defaultOptions, options || {});
|
|
467
|
+
return utils.lexicalRelativeToTransformReady(serializedLexical, this.getSiteUrl(), itemPath, _options);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
lexicalAbsoluteToRelative(serializedLexical, options = {}) {
|
|
471
|
+
const defaultOptions = {
|
|
472
|
+
assetsOnly: false,
|
|
473
|
+
staticImageUrlPrefix: this._config.staticImageUrlPrefix,
|
|
474
|
+
cardTransformers: this._config.cardTransformers
|
|
475
|
+
};
|
|
476
|
+
const _options = assignOptions({}, defaultOptions, options);
|
|
477
|
+
return utils.lexicalAbsoluteToRelative(serializedLexical, this.getSiteUrl(), _options);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
lexicalAbsoluteToTransformReady(serializedLexical, options = {}) {
|
|
481
|
+
const defaultOptions = {
|
|
482
|
+
assetsOnly: false,
|
|
483
|
+
staticImageUrlPrefix: this._config.staticImageUrlPrefix,
|
|
484
|
+
cardTransformers: this._config.cardTransformers
|
|
485
|
+
};
|
|
486
|
+
const _options = assignOptions({}, defaultOptions, options);
|
|
487
|
+
return utils.lexicalAbsoluteToTransformReady(serializedLexical, this.getSiteUrl(), _options);
|
|
488
|
+
}
|
|
489
|
+
|
|
430
490
|
plaintextToTransformReady(plaintext, options = {}) {
|
|
431
491
|
const defaultOptions = {
|
|
432
492
|
staticImageUrlPrefix: this._config.staticImageUrlPrefix
|
|
@@ -435,6 +495,23 @@ module.exports = class UrlUtils {
|
|
|
435
495
|
return utils.plaintextToTransformReady(plaintext, this.getSiteUrl(), _options);
|
|
436
496
|
}
|
|
437
497
|
|
|
498
|
+
/**
|
|
499
|
+
* Return whether the provided URL is part of the site (checks if same domain and within subdirectory)
|
|
500
|
+
* @param {URL} url
|
|
501
|
+
* @param {string} [context] describing the context for which you need to check a url
|
|
502
|
+
* @returns {boolean}
|
|
503
|
+
*/
|
|
504
|
+
isSiteUrl(url, context = 'home') {
|
|
505
|
+
const siteUrl = new URL(this.urlFor(context, true));
|
|
506
|
+
if (siteUrl.host === url.host) {
|
|
507
|
+
if (url.pathname.startsWith(siteUrl.pathname)) {
|
|
508
|
+
return true;
|
|
509
|
+
}
|
|
510
|
+
return false;
|
|
511
|
+
}
|
|
512
|
+
return false;
|
|
513
|
+
}
|
|
514
|
+
|
|
438
515
|
get isSSL() {
|
|
439
516
|
return utils.isSSL;
|
|
440
517
|
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
function lexicalTransform(serializedLexical, siteUrl, transformFunction, itemPath, _options = {}) {
|
|
2
|
+
const defaultOptions = {assetsOnly: false, secure: false};
|
|
3
|
+
const options = Object.assign({}, defaultOptions, _options, {siteUrl, itemPath});
|
|
4
|
+
|
|
5
|
+
if (!serializedLexical) {
|
|
6
|
+
return serializedLexical;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// function only accepts serialized lexical so there's no chance of accidentally
|
|
10
|
+
// modifying pass-by-reference objects
|
|
11
|
+
const lexical = JSON.parse(serializedLexical);
|
|
12
|
+
|
|
13
|
+
if (!lexical?.root?.children) {
|
|
14
|
+
return serializedLexical;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// any lexical links will be a child object with a `url` attribute,
|
|
18
|
+
// recursively walk the tree transforming any `.url`s
|
|
19
|
+
const transformChildren = function (children) {
|
|
20
|
+
for (const child of children) {
|
|
21
|
+
if (child.url) {
|
|
22
|
+
child.url = transformFunction(child.url, siteUrl, itemPath, options);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (child.children) {
|
|
26
|
+
transformChildren(child.children);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
transformChildren(lexical.root.children);
|
|
32
|
+
|
|
33
|
+
return JSON.stringify(lexical);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = lexicalTransform;
|
package/lib/utils/index.js
CHANGED
|
@@ -19,6 +19,11 @@ module.exports = {
|
|
|
19
19
|
mobiledocAbsoluteToTransformReady: require('./mobiledoc-absolute-to-transform-ready'),
|
|
20
20
|
mobiledocRelativeToTransformReady: require('./mobiledoc-relative-to-transform-ready'),
|
|
21
21
|
mobiledocToTransformReady: require('./mobiledoc-to-transform-ready'),
|
|
22
|
+
lexicalAbsoluteToRelative: require('./lexical-absolute-to-relative'),
|
|
23
|
+
lexicalRelativeToAbsolute: require('./lexical-relative-to-absolute'),
|
|
24
|
+
lexicalAbsoluteToTransformReady: require('./lexical-absolute-to-transform-ready'),
|
|
25
|
+
lexicalRelativeToTransformReady: require('./lexical-relative-to-transform-ready'),
|
|
26
|
+
lexicalToTransformReady: require('./lexical-to-transform-ready'),
|
|
22
27
|
plaintextAbsoluteToTransformReady: require('./plaintext-absolute-to-transform-ready'),
|
|
23
28
|
plaintextRelativeToTransformReady: require('./plaintext-relative-to-transform-ready'),
|
|
24
29
|
plaintextToTransformReady: require('./plaintext-to-transform-ready'),
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const absoluteToRelative = require('./absolute-to-relative');
|
|
2
|
+
const lexicalTransform = require('./_lexical-transform');
|
|
3
|
+
|
|
4
|
+
function lexicalAbsoluteToRelative(serializedLexical, siteUrl, _options = {}) {
|
|
5
|
+
const defaultOptions = {assetsOnly: false, secure: false, cardTransformers: []};
|
|
6
|
+
const overrideOptions = {siteUrl, transformType: 'absoluteToRelative'};
|
|
7
|
+
const options = Object.assign({}, defaultOptions, _options, overrideOptions);
|
|
8
|
+
|
|
9
|
+
const transformFunction = function (_url, _siteUrl, _itemPath, __options) {
|
|
10
|
+
return absoluteToRelative(_url, _siteUrl, __options);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
return lexicalTransform(serializedLexical, siteUrl, transformFunction, '', options);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = lexicalAbsoluteToRelative;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const absoluteToTransformReady = require('./absolute-to-transform-ready');
|
|
2
|
+
const lexicalTransform = require('./_lexical-transform');
|
|
3
|
+
|
|
4
|
+
function lexicalAbsoluteToRelative(serializedLexical, siteUrl, _options = {}) {
|
|
5
|
+
const defaultOptions = {assetsOnly: false, secure: false, cardTransformers: []};
|
|
6
|
+
const overrideOptions = {siteUrl, transformType: 'toTransformReady'};
|
|
7
|
+
const options = Object.assign({}, defaultOptions, _options, overrideOptions);
|
|
8
|
+
|
|
9
|
+
const transformFunction = function (_url, _siteUrl, _itemPath, __options) {
|
|
10
|
+
return absoluteToTransformReady(_url, _siteUrl, __options);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
return lexicalTransform(serializedLexical, siteUrl, transformFunction, '', options);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = lexicalAbsoluteToRelative;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const relativeToAbsolute = require('./relative-to-absolute');
|
|
2
|
+
const lexicalTransform = require('./_lexical-transform');
|
|
3
|
+
|
|
4
|
+
function lexicalRelativeToAbsolute(serializedLexical, siteUrl, itemPath, _options = {}) {
|
|
5
|
+
const defaultOptions = {assetsOnly: false, secure: false, cardTransformers: []};
|
|
6
|
+
const overrideOptions = {siteUrl, itemPath, transformType: 'relativeToAbsolute'};
|
|
7
|
+
const options = Object.assign({}, defaultOptions, _options, overrideOptions);
|
|
8
|
+
|
|
9
|
+
return lexicalTransform(serializedLexical, siteUrl, relativeToAbsolute, itemPath, options);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
module.exports = lexicalRelativeToAbsolute;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const relativeToTransformReady = require('./relative-to-transform-ready');
|
|
2
|
+
const lexicalTransform = require('./_lexical-transform');
|
|
3
|
+
|
|
4
|
+
function lexicalRelativeToTransformReady(serializedLexical, siteUrl, itemPath, _options = {}) {
|
|
5
|
+
const defaultOptions = {assetsOnly: false, secure: false, cardTransformers: []};
|
|
6
|
+
const overrideOptions = {siteUrl, transformType: 'toTransformReady'};
|
|
7
|
+
const options = Object.assign({}, defaultOptions, _options, overrideOptions);
|
|
8
|
+
|
|
9
|
+
return lexicalTransform(serializedLexical, siteUrl, relativeToTransformReady, itemPath, options);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
module.exports = lexicalRelativeToTransformReady;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const lexicalRelativeToAbsolute = require('./lexical-relative-to-absolute');
|
|
2
|
+
const lexicalAbsoluteToTransformReady = require('./lexical-absolute-to-transform-ready');
|
|
3
|
+
|
|
4
|
+
function lexicalToTransformReady(lexical, siteUrl, itemPath, options) {
|
|
5
|
+
if (typeof itemPath === 'object' && !options) {
|
|
6
|
+
options = itemPath;
|
|
7
|
+
itemPath = null;
|
|
8
|
+
}
|
|
9
|
+
const absolute = lexicalRelativeToAbsolute(lexical, siteUrl, itemPath, options);
|
|
10
|
+
return lexicalAbsoluteToTransformReady(absolute, siteUrl, options);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module.exports = lexicalToTransformReady;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/url-utils",
|
|
3
|
-
"version": "4.0
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"repository": "https://github.com/TryGhost/SDK/tree/master/packages/url-utils",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -34,5 +34,5 @@
|
|
|
34
34
|
"remark-footnotes": "^1.0.0",
|
|
35
35
|
"unist-util-visit": "^2.0.0"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "7e8faa4260a758bca46ef7f6ff7ed0bd7ec21a13"
|
|
38
38
|
}
|