@tryghost/url-utils 5.0.0-rc.1 → 5.0.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/index.js +1 -0
- package/lib/UrlUtils.d.ts +3 -0
- package/lib/UrlUtils.js +45 -111
- package/lib/utils/absolute-to-relative.d.ts +13 -0
- package/lib/utils/absolute-to-relative.js +5 -13
- package/lib/utils/absolute-to-transform-ready.d.ts +4 -0
- package/lib/utils/absolute-to-transform-ready.js +31 -91
- package/lib/utils/build-early-exit-match.d.ts +14 -0
- package/lib/utils/build-early-exit-match.js +39 -0
- package/lib/utils/deduplicate-double-slashes.d.ts +1 -0
- package/lib/utils/deduplicate-double-slashes.js +6 -2
- package/lib/utils/deduplicate-subdirectory.d.ts +9 -0
- package/lib/utils/deduplicate-subdirectory.js +3 -7
- package/lib/utils/html-absolute-to-relative.d.ts +3 -0
- package/lib/utils/html-absolute-to-relative.js +3 -6
- package/lib/utils/html-absolute-to-transform-ready.d.ts +4 -0
- package/lib/utils/html-absolute-to-transform-ready.js +4 -31
- package/lib/utils/html-relative-to-absolute.d.ts +3 -0
- package/lib/utils/html-relative-to-absolute.js +3 -5
- package/lib/utils/html-relative-to-transform-ready.d.ts +3 -0
- package/lib/utils/html-relative-to-transform-ready.js +2 -5
- package/lib/utils/html-to-transform-ready.d.ts +3 -0
- package/lib/utils/html-to-transform-ready.js +2 -2
- package/lib/utils/html-transform.d.ts +4 -0
- package/lib/utils/html-transform.js +10 -30
- package/lib/utils/index.d.ts +0 -0
- package/lib/utils/index.js +2 -0
- package/lib/utils/is-ssl.d.ts +2 -0
- package/lib/utils/is-ssl.js +4 -4
- package/lib/utils/lexical-absolute-to-relative.d.ts +2 -0
- package/lib/utils/lexical-absolute-to-relative.js +4 -6
- package/lib/utils/lexical-absolute-to-transform-ready.d.ts +2 -0
- package/lib/utils/lexical-absolute-to-transform-ready.js +4 -6
- package/lib/utils/lexical-relative-to-absolute.d.ts +3 -0
- package/lib/utils/lexical-relative-to-absolute.js +4 -5
- package/lib/utils/lexical-relative-to-transform-ready.d.ts +3 -0
- package/lib/utils/lexical-relative-to-transform-ready.js +4 -5
- package/lib/utils/lexical-to-transform-ready.d.ts +3 -0
- package/lib/utils/lexical-to-transform-ready.js +2 -2
- package/lib/utils/lexical-transform.d.ts +2 -0
- package/lib/utils/lexical-transform.js +6 -19
- package/lib/utils/markdown-absolute-to-relative.d.ts +4 -0
- package/lib/utils/markdown-absolute-to-relative.js +3 -6
- package/lib/utils/markdown-absolute-to-transform-ready.d.ts +5 -0
- package/lib/utils/markdown-absolute-to-transform-ready.js +5 -8
- package/lib/utils/markdown-relative-to-absolute.d.ts +4 -0
- package/lib/utils/markdown-relative-to-absolute.js +3 -6
- package/lib/utils/markdown-relative-to-transform-ready.d.ts +4 -0
- package/lib/utils/markdown-relative-to-transform-ready.js +3 -6
- package/lib/utils/markdown-to-transform-ready.d.ts +3 -0
- package/lib/utils/markdown-to-transform-ready.js +2 -2
- package/lib/utils/markdown-transform.d.ts +5 -0
- package/lib/utils/markdown-transform.js +7 -25
- package/lib/utils/mobiledoc-absolute-to-relative.d.ts +2 -0
- package/lib/utils/mobiledoc-absolute-to-relative.js +4 -6
- package/lib/utils/mobiledoc-absolute-to-transform-ready.d.ts +2 -0
- package/lib/utils/mobiledoc-absolute-to-transform-ready.js +4 -6
- package/lib/utils/mobiledoc-relative-to-absolute.d.ts +3 -0
- package/lib/utils/mobiledoc-relative-to-absolute.js +4 -5
- package/lib/utils/mobiledoc-relative-to-transform-ready.d.ts +3 -0
- package/lib/utils/mobiledoc-relative-to-transform-ready.js +4 -5
- package/lib/utils/mobiledoc-to-transform-ready.d.ts +3 -0
- package/lib/utils/mobiledoc-to-transform-ready.js +2 -2
- package/lib/utils/mobiledoc-transform.d.ts +1 -0
- package/lib/utils/mobiledoc-transform.js +4 -10
- package/lib/utils/plaintext-absolute-to-transform-ready.d.ts +4 -0
- package/lib/utils/plaintext-absolute-to-transform-ready.js +18 -10
- package/lib/utils/plaintext-relative-to-transform-ready.d.ts +2 -0
- package/lib/utils/plaintext-relative-to-transform-ready.js +2 -3
- package/lib/utils/plaintext-to-transform-ready.d.ts +3 -0
- package/lib/utils/plaintext-to-transform-ready.js +2 -2
- package/lib/utils/relative-to-absolute.d.ts +13 -0
- package/lib/utils/relative-to-absolute.js +6 -18
- package/lib/utils/relative-to-transform-ready.d.ts +2 -0
- package/lib/utils/relative-to-transform-ready.js +2 -9
- package/lib/utils/replace-permalink.d.ts +5 -0
- package/lib/utils/replace-permalink.js +2 -3
- package/lib/utils/strip-subdirectory-from-path.d.ts +9 -0
- package/lib/utils/strip-subdirectory-from-path.js +5 -9
- package/lib/utils/to-transform-ready.d.ts +3 -0
- package/lib/utils/to-transform-ready.js +2 -2
- package/lib/utils/transform-ready-to-absolute.d.ts +1 -0
- package/lib/utils/transform-ready-to-absolute.js +9 -27
- package/lib/utils/transform-ready-to-relative.d.ts +1 -0
- package/lib/utils/transform-ready-to-relative.js +2 -6
- package/lib/utils/url-join.d.ts +9 -0
- package/lib/utils/url-join.js +2 -8
- package/package.json +16 -7
package/index.js
CHANGED
package/lib/UrlUtils.js
CHANGED
|
@@ -1,25 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @ts-nocheck
|
|
1
3
|
// Contains all path information to be used throughout the codebase.
|
|
2
4
|
const _ = require('lodash');
|
|
3
5
|
const utils = require('./utils');
|
|
4
|
-
|
|
5
|
-
const STATIC_IMAGE_URL_PREFIX = 'content/images';
|
|
6
|
-
const STATIC_FILES_URL_PREFIX = 'content/files';
|
|
7
|
-
const STATIC_MEDIA_URL_PREFIX = 'content/media';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @typedef {Object} AssetBaseUrls
|
|
11
|
-
* @property {string} [image] Absolute base URL for images
|
|
12
|
-
* @property {string} [files] Absolute base URL for files
|
|
13
|
-
* @property {string} [media] Absolute base URL for media
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
function trimTrailingSlash(url) {
|
|
17
|
-
if (!url) {
|
|
18
|
-
return url;
|
|
19
|
-
}
|
|
20
|
-
return url.replace(/\/+$/, '');
|
|
21
|
-
}
|
|
22
|
-
|
|
23
6
|
// similar to Object.assign but will not override defaults if a source value is undefined
|
|
24
7
|
function assignOptions(target, ...sources) {
|
|
25
8
|
const options = sources.map((x) => {
|
|
@@ -40,68 +23,65 @@ module.exports = class UrlUtils {
|
|
|
40
23
|
* @param {('content' | 'admin')} [options.defaultApiType='content'] default API type to be used
|
|
41
24
|
* @param {Object} [options.slugs] object with 2 properties reserved and protected containing arrays of special case slugs
|
|
42
25
|
* @param {Number} [options.redirectCacheMaxAge]
|
|
43
|
-
* @param {
|
|
44
|
-
|
|
26
|
+
* @param {String} [options.staticImageUrlPrefix='content/images'] static prefix for serving images. Should not be passed in, unless customizing ghost instance image storage
|
|
27
|
+
* @param {String} [options.staticFilesUrlPrefix='content/files'] static prefix for serving files. Should not be passed in, unless customizing ghost instance file storage
|
|
28
|
+
* @param {String} [options.staticMediaUrlPrefix='content/media'] static prefix for serving media. Should not be passed in, unless customizing ghost instance media storage
|
|
29
|
+
* @param {object} [options.assetBaseUrls] asset CDN base URLs
|
|
30
|
+
* @param {string} [options.assetBaseUrls.image] image asset CDN base URL
|
|
31
|
+
* @param {string} [options.assetBaseUrls.files] files asset CDN base URL
|
|
32
|
+
* @param {string} [options.assetBaseUrls.media] media asset CDN base URL
|
|
33
|
+
*/
|
|
45
34
|
constructor(options = {}) {
|
|
46
35
|
const defaultOptions = {
|
|
47
36
|
slugs: null,
|
|
48
37
|
redirectCacheMaxAge: null,
|
|
49
38
|
baseApiPath: '/ghost/api',
|
|
50
|
-
defaultApiType: 'content'
|
|
39
|
+
defaultApiType: 'content',
|
|
40
|
+
staticImageUrlPrefix: 'content/images',
|
|
41
|
+
staticFilesUrlPrefix: 'content/files',
|
|
42
|
+
staticMediaUrlPrefix: 'content/media'
|
|
51
43
|
};
|
|
52
|
-
|
|
53
44
|
this._config = assignOptions({}, defaultOptions, options);
|
|
54
|
-
this._config.staticImageUrlPrefix = STATIC_IMAGE_URL_PREFIX;
|
|
55
|
-
this._config.staticFilesUrlPrefix = STATIC_FILES_URL_PREFIX;
|
|
56
|
-
this._config.staticMediaUrlPrefix = STATIC_MEDIA_URL_PREFIX;
|
|
57
|
-
|
|
58
45
|
const assetBaseUrls = options.assetBaseUrls || {};
|
|
59
46
|
this._assetBaseUrls = {
|
|
60
|
-
image:
|
|
61
|
-
files:
|
|
62
|
-
media:
|
|
47
|
+
image: assetBaseUrls.image || null,
|
|
48
|
+
files: assetBaseUrls.files || null,
|
|
49
|
+
media: assetBaseUrls.media || null
|
|
63
50
|
};
|
|
64
|
-
|
|
65
51
|
this.getSubdir = options.getSubdir;
|
|
66
52
|
this.getSiteUrl = options.getSiteUrl;
|
|
67
53
|
this.getAdminUrl = options.getAdminUrl;
|
|
68
54
|
}
|
|
69
|
-
|
|
70
55
|
_assetOptionDefaults() {
|
|
71
|
-
const siteBase = trimTrailingSlash(this.getSiteUrl ? this.getSiteUrl() : null);
|
|
72
56
|
return {
|
|
73
57
|
staticImageUrlPrefix: this._config.staticImageUrlPrefix,
|
|
74
58
|
staticFilesUrlPrefix: this._config.staticFilesUrlPrefix,
|
|
75
59
|
staticMediaUrlPrefix: this._config.staticMediaUrlPrefix,
|
|
76
|
-
imageBaseUrl: this._assetBaseUrls.image ||
|
|
60
|
+
imageBaseUrl: this._assetBaseUrls.image || null,
|
|
77
61
|
filesBaseUrl: this._assetBaseUrls.files || null,
|
|
78
62
|
mediaBaseUrl: this._assetBaseUrls.media || null
|
|
79
63
|
};
|
|
80
64
|
}
|
|
81
|
-
|
|
82
65
|
_buildAssetOptions(additionalDefaults = {}, options) {
|
|
83
66
|
return assignOptions({}, this._assetOptionDefaults(), additionalDefaults, options || {});
|
|
84
67
|
}
|
|
85
|
-
|
|
86
68
|
getProtectedSlugs() {
|
|
87
69
|
let subDir = this.getSubdir();
|
|
88
|
-
|
|
89
70
|
if (!_.isEmpty(subDir)) {
|
|
90
71
|
return this._config.slugs.concat([subDir.split('/').pop()]);
|
|
91
|
-
}
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
92
74
|
return this._config.slugs;
|
|
93
75
|
}
|
|
94
76
|
}
|
|
95
|
-
|
|
96
77
|
/** urlJoin
|
|
97
78
|
* Returns a URL/path for internal use in Ghost.
|
|
98
79
|
* @param {string} arguments takes arguments and concats those to a valid path/URL.
|
|
99
80
|
* @return {string} URL concatinated URL/path of arguments.
|
|
100
81
|
*/
|
|
101
82
|
urlJoin(...parts) {
|
|
102
|
-
return utils.urlJoin(parts, {rootUrl: this.getSiteUrl()});
|
|
83
|
+
return utils.urlJoin(parts, { rootUrl: this.getSiteUrl() });
|
|
103
84
|
}
|
|
104
|
-
|
|
105
85
|
// ## createUrl
|
|
106
86
|
// Simple url creation from a given path
|
|
107
87
|
// Ensures that our urls contain the subdirectory if there is one
|
|
@@ -117,23 +97,20 @@ module.exports = class UrlUtils {
|
|
|
117
97
|
// - a URL which always ends with a slash
|
|
118
98
|
createUrl(urlPath = '/', absolute = false, trailingSlash) {
|
|
119
99
|
let base;
|
|
120
|
-
|
|
121
100
|
// create base of url, always ends without a slash
|
|
122
101
|
if (absolute) {
|
|
123
102
|
base = this.getSiteUrl();
|
|
124
|
-
}
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
125
105
|
base = this.getSubdir();
|
|
126
106
|
}
|
|
127
|
-
|
|
128
107
|
if (trailingSlash) {
|
|
129
108
|
if (!urlPath.match(/\/$/)) {
|
|
130
109
|
urlPath += '/';
|
|
131
110
|
}
|
|
132
111
|
}
|
|
133
|
-
|
|
134
112
|
return this.urlJoin(base, urlPath);
|
|
135
113
|
}
|
|
136
|
-
|
|
137
114
|
// ## urlFor
|
|
138
115
|
// Synchronous url creation for a given context
|
|
139
116
|
// Can generate a url for a named path and given path.
|
|
@@ -155,40 +132,36 @@ module.exports = class UrlUtils {
|
|
|
155
132
|
let knownObjects = ['image', 'nav'];
|
|
156
133
|
let baseUrl;
|
|
157
134
|
let hostname;
|
|
158
|
-
|
|
159
135
|
// this will become really big
|
|
160
136
|
let knownPaths = {
|
|
161
137
|
home: '/',
|
|
162
138
|
sitemap_xsl: '/sitemap.xsl'
|
|
163
139
|
};
|
|
164
|
-
|
|
165
140
|
// Make data properly optional
|
|
166
141
|
if (_.isBoolean(data)) {
|
|
167
142
|
absolute = data;
|
|
168
143
|
data = null;
|
|
169
144
|
}
|
|
170
|
-
|
|
171
145
|
if (_.isObject(context) && context.relativeUrl) {
|
|
172
146
|
urlPath = context.relativeUrl;
|
|
173
|
-
}
|
|
147
|
+
}
|
|
148
|
+
else if (_.isString(context) && _.indexOf(knownObjects, context) !== -1) {
|
|
174
149
|
if (context === 'image' && data.image) {
|
|
175
150
|
urlPath = data.image;
|
|
176
151
|
imagePathRe = new RegExp('^' + this.getSubdir() + '/' + this._config.staticImageUrlPrefix);
|
|
177
152
|
absolute = imagePathRe.test(data.image) ? absolute : false;
|
|
178
|
-
|
|
179
153
|
if (absolute) {
|
|
180
154
|
// Remove the sub-directory from the URL because ghostConfig will add it back.
|
|
181
155
|
urlPath = urlPath.replace(new RegExp('^' + this.getSubdir()), '');
|
|
182
156
|
baseUrl = this.getSiteUrl().replace(/\/$/, '');
|
|
183
157
|
urlPath = baseUrl + urlPath;
|
|
184
158
|
}
|
|
185
|
-
|
|
186
159
|
return urlPath;
|
|
187
|
-
}
|
|
160
|
+
}
|
|
161
|
+
else if (context === 'nav' && data.nav) {
|
|
188
162
|
urlPath = data.nav.url;
|
|
189
163
|
baseUrl = this.getSiteUrl();
|
|
190
164
|
hostname = baseUrl.split('//')[1];
|
|
191
|
-
|
|
192
165
|
// If the hostname is present in the url
|
|
193
166
|
if (urlPath.indexOf(hostname) > -1
|
|
194
167
|
// do no not apply, if there is a subdomain, or a mailto link
|
|
@@ -201,77 +174,71 @@ module.exports = class UrlUtils {
|
|
|
201
174
|
absolute = true;
|
|
202
175
|
}
|
|
203
176
|
}
|
|
204
|
-
}
|
|
177
|
+
}
|
|
178
|
+
else if (context === 'home' && absolute) {
|
|
205
179
|
urlPath = this.getSiteUrl();
|
|
206
|
-
|
|
207
180
|
// CASE: there are cases where urlFor('home') needs to be returned without trailing
|
|
208
181
|
// slash e. g. the `{{@site.url}}` helper. See https://github.com/TryGhost/Ghost/issues/8569
|
|
209
182
|
if (data && data.trailingSlash === false) {
|
|
210
183
|
urlPath = urlPath.replace(/\/$/, '');
|
|
211
184
|
}
|
|
212
|
-
}
|
|
185
|
+
}
|
|
186
|
+
else if (context === 'admin') {
|
|
213
187
|
let adminUrl = this.getAdminUrl() || this.getSiteUrl();
|
|
214
188
|
let adminPath = '/ghost/';
|
|
215
|
-
|
|
216
189
|
if (absolute) {
|
|
217
190
|
urlPath = this.urlJoin(adminUrl, adminPath);
|
|
218
|
-
}
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
219
193
|
urlPath = adminPath;
|
|
220
194
|
}
|
|
221
|
-
}
|
|
195
|
+
}
|
|
196
|
+
else if (context === 'api') {
|
|
222
197
|
let adminUrl = this.getAdminUrl() || this.getSiteUrl();
|
|
223
198
|
let apiPath = this._config.baseApiPath + '/';
|
|
224
|
-
|
|
225
199
|
if (data.type && ['admin', 'content'].includes(data.type)) {
|
|
226
200
|
apiPath += data.type;
|
|
227
|
-
}
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
228
203
|
apiPath += this._config.defaultApiType;
|
|
229
204
|
}
|
|
230
|
-
|
|
231
205
|
// Ensure we end with a trailing slash
|
|
232
206
|
apiPath += '/';
|
|
233
|
-
|
|
234
207
|
if (absolute) {
|
|
235
208
|
urlPath = this.urlJoin(adminUrl, apiPath);
|
|
236
|
-
}
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
237
211
|
urlPath = apiPath;
|
|
238
212
|
}
|
|
239
|
-
}
|
|
213
|
+
}
|
|
214
|
+
else if (_.isString(context) && _.indexOf(_.keys(knownPaths), context) !== -1) {
|
|
240
215
|
// trying to create a url for a named path
|
|
241
216
|
urlPath = knownPaths[context];
|
|
242
217
|
}
|
|
243
|
-
|
|
244
218
|
// This url already has a protocol so is likely an external url to be returned
|
|
245
219
|
// or it is an alternative scheme, protocol-less, or an anchor-only path
|
|
246
220
|
if (urlPath && (urlPath.indexOf('://') !== -1 || urlPath.match(/^(\/\/|#|[a-zA-Z0-9-]+:)/))) {
|
|
247
221
|
return urlPath;
|
|
248
222
|
}
|
|
249
|
-
|
|
250
223
|
return this.createUrl(urlPath, absolute);
|
|
251
224
|
}
|
|
252
|
-
|
|
253
225
|
redirect301(res, redirectUrl) {
|
|
254
|
-
res.set({'Cache-Control': 'public, max-age=' + this._config.redirectCacheMaxAge});
|
|
226
|
+
res.set({ 'Cache-Control': 'public, max-age=' + this._config.redirectCacheMaxAge });
|
|
255
227
|
return res.redirect(301, redirectUrl);
|
|
256
228
|
}
|
|
257
|
-
|
|
258
229
|
redirectToAdmin(status, res, adminPath) {
|
|
259
230
|
let redirectUrl = this.urlJoin(this.urlFor('admin', true), adminPath, '/');
|
|
260
|
-
|
|
261
231
|
if (status === 301) {
|
|
262
232
|
return this.redirect301(res, redirectUrl);
|
|
263
233
|
}
|
|
264
234
|
return res.redirect(redirectUrl);
|
|
265
235
|
}
|
|
266
|
-
|
|
267
236
|
absoluteToRelative(url, options) {
|
|
268
237
|
return utils.absoluteToRelative(url, this.getSiteUrl(), options);
|
|
269
238
|
}
|
|
270
|
-
|
|
271
239
|
relativeToAbsolute(url, options) {
|
|
272
240
|
return utils.relativeToAbsolute(url, this.getSiteUrl(), options);
|
|
273
241
|
}
|
|
274
|
-
|
|
275
242
|
toTransformReady(url, itemPath, options) {
|
|
276
243
|
if (typeof itemPath === 'object' && !options) {
|
|
277
244
|
options = itemPath;
|
|
@@ -280,27 +247,22 @@ module.exports = class UrlUtils {
|
|
|
280
247
|
const _options = this._buildAssetOptions({}, options);
|
|
281
248
|
return utils.toTransformReady(url, this.getSiteUrl(), itemPath, _options);
|
|
282
249
|
}
|
|
283
|
-
|
|
284
250
|
absoluteToTransformReady(url, options) {
|
|
285
251
|
const _options = this._buildAssetOptions({}, options);
|
|
286
252
|
return utils.absoluteToTransformReady(url, this.getSiteUrl(), _options);
|
|
287
253
|
}
|
|
288
|
-
|
|
289
254
|
relativeToTransformReady(url, options) {
|
|
290
255
|
const _options = this._buildAssetOptions({}, options);
|
|
291
256
|
return utils.relativeToTransformReady(url, this.getSiteUrl(), _options);
|
|
292
257
|
}
|
|
293
|
-
|
|
294
258
|
transformReadyToAbsolute(url, options) {
|
|
295
259
|
const _options = this._buildAssetOptions({}, options);
|
|
296
260
|
return utils.transformReadyToAbsolute(url, this.getSiteUrl(), _options);
|
|
297
261
|
}
|
|
298
|
-
|
|
299
262
|
transformReadyToRelative(url, options) {
|
|
300
263
|
const _options = this._buildAssetOptions({}, options);
|
|
301
264
|
return utils.transformReadyToRelative(url, this.getSiteUrl(), _options);
|
|
302
265
|
}
|
|
303
|
-
|
|
304
266
|
htmlToTransformReady(html, itemPath, options) {
|
|
305
267
|
if (typeof itemPath === 'object' && !options) {
|
|
306
268
|
options = itemPath;
|
|
@@ -309,7 +271,6 @@ module.exports = class UrlUtils {
|
|
|
309
271
|
const _options = this._buildAssetOptions({}, options);
|
|
310
272
|
return utils.htmlToTransformReady(html, this.getSiteUrl(), itemPath, _options);
|
|
311
273
|
}
|
|
312
|
-
|
|
313
274
|
/**
|
|
314
275
|
* Convert relative URLs in html into absolute URLs
|
|
315
276
|
* @param {string} html
|
|
@@ -330,7 +291,6 @@ module.exports = class UrlUtils {
|
|
|
330
291
|
}, options);
|
|
331
292
|
return utils.htmlRelativeToAbsolute(html, this.getSiteUrl(), itemPath, _options);
|
|
332
293
|
}
|
|
333
|
-
|
|
334
294
|
htmlRelativeToTransformReady(html, itemPath, options) {
|
|
335
295
|
if (typeof itemPath === 'object' && !options) {
|
|
336
296
|
options = itemPath;
|
|
@@ -341,21 +301,18 @@ module.exports = class UrlUtils {
|
|
|
341
301
|
}, options);
|
|
342
302
|
return utils.htmlRelativeToTransformReady(html, this.getSiteUrl(), itemPath, _options);
|
|
343
303
|
}
|
|
344
|
-
|
|
345
304
|
htmlAbsoluteToRelative(html, options = {}) {
|
|
346
305
|
const _options = this._buildAssetOptions({
|
|
347
306
|
assetsOnly: false
|
|
348
307
|
}, options);
|
|
349
308
|
return utils.htmlAbsoluteToRelative(html, this.getSiteUrl(), _options);
|
|
350
309
|
}
|
|
351
|
-
|
|
352
310
|
htmlAbsoluteToTransformReady(html, options = {}) {
|
|
353
311
|
const _options = this._buildAssetOptions({
|
|
354
312
|
assetsOnly: false
|
|
355
313
|
}, options);
|
|
356
314
|
return utils.htmlAbsoluteToTransformReady(html, this.getSiteUrl(), _options);
|
|
357
315
|
}
|
|
358
|
-
|
|
359
316
|
markdownToTransformReady(markdown, itemPath, options) {
|
|
360
317
|
if (typeof itemPath === 'object' && !options) {
|
|
361
318
|
options = itemPath;
|
|
@@ -364,7 +321,6 @@ module.exports = class UrlUtils {
|
|
|
364
321
|
const _options = this._buildAssetOptions({}, options);
|
|
365
322
|
return utils.markdownToTransformReady(markdown, this.getSiteUrl(), itemPath, _options);
|
|
366
323
|
}
|
|
367
|
-
|
|
368
324
|
markdownRelativeToAbsolute(markdown, itemPath, options) {
|
|
369
325
|
if (typeof itemPath === 'object' && !options) {
|
|
370
326
|
options = itemPath;
|
|
@@ -375,7 +331,6 @@ module.exports = class UrlUtils {
|
|
|
375
331
|
}, options);
|
|
376
332
|
return utils.markdownRelativeToAbsolute(markdown, this.getSiteUrl(), itemPath, _options);
|
|
377
333
|
}
|
|
378
|
-
|
|
379
334
|
markdownRelativeToTransformReady(markdown, itemPath, options) {
|
|
380
335
|
if (typeof itemPath === 'object' && !options) {
|
|
381
336
|
options = itemPath;
|
|
@@ -386,21 +341,18 @@ module.exports = class UrlUtils {
|
|
|
386
341
|
}, options);
|
|
387
342
|
return utils.markdownRelativeToTransformReady(markdown, this.getSiteUrl(), itemPath, _options);
|
|
388
343
|
}
|
|
389
|
-
|
|
390
344
|
markdownAbsoluteToRelative(markdown, options = {}) {
|
|
391
345
|
const _options = this._buildAssetOptions({
|
|
392
346
|
assetsOnly: false
|
|
393
347
|
}, options);
|
|
394
348
|
return utils.markdownAbsoluteToRelative(markdown, this.getSiteUrl(), _options);
|
|
395
349
|
}
|
|
396
|
-
|
|
397
350
|
markdownAbsoluteToTransformReady(markdown, options) {
|
|
398
351
|
const _options = this._buildAssetOptions({
|
|
399
352
|
assetsOnly: false
|
|
400
353
|
}, options);
|
|
401
354
|
return utils.markdownAbsoluteToTransformReady(markdown, this.getSiteUrl(), _options);
|
|
402
355
|
}
|
|
403
|
-
|
|
404
356
|
mobiledocToTransformReady(serializedMobiledoc, itemPath, options) {
|
|
405
357
|
if (typeof itemPath === 'object' && !options) {
|
|
406
358
|
options = itemPath;
|
|
@@ -411,7 +363,6 @@ module.exports = class UrlUtils {
|
|
|
411
363
|
}, options);
|
|
412
364
|
return utils.mobiledocToTransformReady(serializedMobiledoc, this.getSiteUrl(), itemPath, _options);
|
|
413
365
|
}
|
|
414
|
-
|
|
415
366
|
mobiledocRelativeToAbsolute(serializedMobiledoc, itemPath, options) {
|
|
416
367
|
if (typeof itemPath === 'object' && !options) {
|
|
417
368
|
options = itemPath;
|
|
@@ -423,7 +374,6 @@ module.exports = class UrlUtils {
|
|
|
423
374
|
}, options);
|
|
424
375
|
return utils.mobiledocRelativeToAbsolute(serializedMobiledoc, this.getSiteUrl(), itemPath, _options);
|
|
425
376
|
}
|
|
426
|
-
|
|
427
377
|
mobiledocRelativeToTransformReady(serializedMobiledoc, itemPath, options) {
|
|
428
378
|
if (typeof itemPath === 'object' && !options) {
|
|
429
379
|
options = itemPath;
|
|
@@ -435,7 +385,6 @@ module.exports = class UrlUtils {
|
|
|
435
385
|
}, options);
|
|
436
386
|
return utils.mobiledocRelativeToTransformReady(serializedMobiledoc, this.getSiteUrl(), itemPath, _options);
|
|
437
387
|
}
|
|
438
|
-
|
|
439
388
|
mobiledocAbsoluteToRelative(serializedMobiledoc, options = {}) {
|
|
440
389
|
const _options = this._buildAssetOptions({
|
|
441
390
|
assetsOnly: false,
|
|
@@ -443,7 +392,6 @@ module.exports = class UrlUtils {
|
|
|
443
392
|
}, options);
|
|
444
393
|
return utils.mobiledocAbsoluteToRelative(serializedMobiledoc, this.getSiteUrl(), _options);
|
|
445
394
|
}
|
|
446
|
-
|
|
447
395
|
mobiledocAbsoluteToTransformReady(serializedMobiledoc, options = {}) {
|
|
448
396
|
const _options = this._buildAssetOptions({
|
|
449
397
|
assetsOnly: false,
|
|
@@ -451,7 +399,6 @@ module.exports = class UrlUtils {
|
|
|
451
399
|
}, options);
|
|
452
400
|
return utils.mobiledocAbsoluteToTransformReady(serializedMobiledoc, this.getSiteUrl(), _options);
|
|
453
401
|
}
|
|
454
|
-
|
|
455
402
|
lexicalToTransformReady(serializedLexical, itemPath, options) {
|
|
456
403
|
if (typeof itemPath === 'object' && !options) {
|
|
457
404
|
options = itemPath;
|
|
@@ -462,7 +409,6 @@ module.exports = class UrlUtils {
|
|
|
462
409
|
}, options);
|
|
463
410
|
return utils.lexicalToTransformReady(serializedLexical, this.getSiteUrl(), itemPath, _options);
|
|
464
411
|
}
|
|
465
|
-
|
|
466
412
|
lexicalRelativeToAbsolute(serializedLexical, itemPath, options) {
|
|
467
413
|
if (typeof itemPath === 'object' && !options) {
|
|
468
414
|
options = itemPath;
|
|
@@ -474,7 +420,6 @@ module.exports = class UrlUtils {
|
|
|
474
420
|
}, options);
|
|
475
421
|
return utils.lexicalRelativeToAbsolute(serializedLexical, this.getSiteUrl(), itemPath, _options);
|
|
476
422
|
}
|
|
477
|
-
|
|
478
423
|
lexicalRelativeToTransformReady(serializedLexical, itemPath, options) {
|
|
479
424
|
if (typeof itemPath === 'object' && !options) {
|
|
480
425
|
options = itemPath;
|
|
@@ -486,7 +431,6 @@ module.exports = class UrlUtils {
|
|
|
486
431
|
}, options);
|
|
487
432
|
return utils.lexicalRelativeToTransformReady(serializedLexical, this.getSiteUrl(), itemPath, _options);
|
|
488
433
|
}
|
|
489
|
-
|
|
490
434
|
lexicalAbsoluteToRelative(serializedLexical, options = {}) {
|
|
491
435
|
const _options = this._buildAssetOptions({
|
|
492
436
|
assetsOnly: false,
|
|
@@ -494,7 +438,6 @@ module.exports = class UrlUtils {
|
|
|
494
438
|
}, options);
|
|
495
439
|
return utils.lexicalAbsoluteToRelative(serializedLexical, this.getSiteUrl(), _options);
|
|
496
440
|
}
|
|
497
|
-
|
|
498
441
|
lexicalAbsoluteToTransformReady(serializedLexical, options = {}) {
|
|
499
442
|
const _options = this._buildAssetOptions({
|
|
500
443
|
assetsOnly: false,
|
|
@@ -502,12 +445,10 @@ module.exports = class UrlUtils {
|
|
|
502
445
|
}, options);
|
|
503
446
|
return utils.lexicalAbsoluteToTransformReady(serializedLexical, this.getSiteUrl(), _options);
|
|
504
447
|
}
|
|
505
|
-
|
|
506
448
|
plaintextToTransformReady(plaintext, options = {}) {
|
|
507
449
|
const _options = this._buildAssetOptions({}, options);
|
|
508
450
|
return utils.plaintextToTransformReady(plaintext, this.getSiteUrl(), _options);
|
|
509
451
|
}
|
|
510
|
-
|
|
511
452
|
/**
|
|
512
453
|
* Return whether the provided URL is part of the site (checks if same domain and within subdirectory)
|
|
513
454
|
* @param {URL} url
|
|
@@ -524,19 +465,15 @@ module.exports = class UrlUtils {
|
|
|
524
465
|
}
|
|
525
466
|
return false;
|
|
526
467
|
}
|
|
527
|
-
|
|
528
468
|
get isSSL() {
|
|
529
469
|
return utils.isSSL;
|
|
530
470
|
}
|
|
531
|
-
|
|
532
471
|
get replacePermalink() {
|
|
533
472
|
return utils.replacePermalink;
|
|
534
473
|
}
|
|
535
|
-
|
|
536
474
|
get deduplicateDoubleSlashes() {
|
|
537
475
|
return utils.deduplicateDoubleSlashes;
|
|
538
476
|
}
|
|
539
|
-
|
|
540
477
|
/**
|
|
541
478
|
* If you request **any** image in Ghost, it get's served via
|
|
542
479
|
* http://your-blog.com/content/images/2017/01/02/author.png
|
|
@@ -547,17 +484,14 @@ module.exports = class UrlUtils {
|
|
|
547
484
|
* my-content/another-dir/images/2017/01/02/author.png
|
|
548
485
|
*/
|
|
549
486
|
get STATIC_IMAGE_URL_PREFIX() {
|
|
550
|
-
return
|
|
487
|
+
return this._config.staticImageUrlPrefix;
|
|
551
488
|
}
|
|
552
|
-
|
|
553
489
|
get STATIC_FILES_URL_PREFIX() {
|
|
554
|
-
return
|
|
490
|
+
return this._config.staticFilesUrlPrefix;
|
|
555
491
|
}
|
|
556
|
-
|
|
557
492
|
get STATIC_MEDIA_URL_PREFIX() {
|
|
558
|
-
return
|
|
493
|
+
return this._config.staticMediaUrlPrefix;
|
|
559
494
|
}
|
|
560
|
-
|
|
561
495
|
// expose underlying functions to ease testing
|
|
562
496
|
get _utils() {
|
|
563
497
|
return utils;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
declare const URL: any;
|
|
2
|
+
declare const stripSubdirectoryFromPath: any;
|
|
3
|
+
/**
|
|
4
|
+
* Convert an absolute URL to a root-relative path if it matches the supplied root domain.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} url Absolute URL to convert to relative if possible
|
|
7
|
+
* @param {string} rootUrl Absolute URL to which the returned relative URL will match the domain root
|
|
8
|
+
* @param {Object} [options] Options that affect the conversion
|
|
9
|
+
* @param {boolean} [options.ignoreProtocol=true] Ignore protocol when matching url to root
|
|
10
|
+
* @param {boolean} [options.withoutSubdirectory=false] Strip the root subdirectory from the returned path
|
|
11
|
+
* @returns {string} The passed-in url or a relative path
|
|
12
|
+
*/
|
|
13
|
+
declare const absoluteToRelative: (url: any, rootUrl: any, _options?: {}) => any;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @ts-nocheck
|
|
1
3
|
// require the whatwg compatible URL library (same behaviour in node and browser)
|
|
2
|
-
const {URL} = require('url');
|
|
4
|
+
const { URL } = require('url');
|
|
3
5
|
const stripSubdirectoryFromPath = require('./strip-subdirectory-from-path');
|
|
4
|
-
|
|
5
6
|
/**
|
|
6
7
|
* Convert an absolute URL to a root-relative path if it matches the supplied root domain.
|
|
7
8
|
*
|
|
@@ -20,44 +21,35 @@ const absoluteToRelative = function absoluteToRelative(url, rootUrl, _options =
|
|
|
20
21
|
staticImageUrlPrefix: 'content/images'
|
|
21
22
|
};
|
|
22
23
|
const options = Object.assign({}, defaultOptions, _options);
|
|
23
|
-
|
|
24
24
|
if (options.assetsOnly) {
|
|
25
25
|
const staticImageUrlPrefixRegex = new RegExp(options.staticImageUrlPrefix);
|
|
26
26
|
if (!url.match(staticImageUrlPrefixRegex)) {
|
|
27
27
|
return url;
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
|
-
|
|
31
30
|
let parsedUrl;
|
|
32
31
|
let parsedRoot;
|
|
33
|
-
|
|
34
32
|
try {
|
|
35
33
|
parsedUrl = new URL(url, 'http://relative');
|
|
36
34
|
parsedRoot = parsedUrl.origin === 'null' ? undefined : new URL(rootUrl || parsedUrl.origin);
|
|
37
|
-
|
|
38
35
|
// return the url as-is if it was relative or non-http
|
|
39
36
|
if (parsedUrl.origin === 'null' || parsedUrl.origin === 'http://relative') {
|
|
40
37
|
return url;
|
|
41
38
|
}
|
|
42
|
-
}
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
43
41
|
return url;
|
|
44
42
|
}
|
|
45
|
-
|
|
46
43
|
const matchesHost = parsedUrl.host === parsedRoot.host;
|
|
47
44
|
const matchesProtocol = parsedUrl.protocol === parsedRoot.protocol;
|
|
48
45
|
const matchesPath = parsedUrl.pathname.indexOf(parsedRoot.pathname) === 0;
|
|
49
|
-
|
|
50
46
|
if (matchesHost && (options.ignoreProtocol || matchesProtocol) && matchesPath) {
|
|
51
47
|
let path = parsedUrl.href.replace(parsedUrl.origin, '');
|
|
52
|
-
|
|
53
48
|
if (options.withoutSubdirectory) {
|
|
54
49
|
path = stripSubdirectoryFromPath(path, rootUrl);
|
|
55
50
|
}
|
|
56
|
-
|
|
57
51
|
return path;
|
|
58
52
|
}
|
|
59
|
-
|
|
60
53
|
return url;
|
|
61
54
|
};
|
|
62
|
-
|
|
63
55
|
module.exports = absoluteToRelative;
|