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