2014-10-10 18:54:07 +04:00
|
|
|
// # URL helper
|
|
|
|
// Usage: `{{url}}`, `{{url absolute="true"}}`
|
|
|
|
//
|
|
|
|
// Returns the URL for the current object scope i.e. If inside a post scope will return post permalink
|
|
|
|
// `absolute` flag outputs absolute URL, else URL is relative
|
|
|
|
|
2021-09-28 17:06:33 +03:00
|
|
|
const {metaData} = require('../services/proxy');
|
|
|
|
const {SafeString} = require('../services/rendering');
|
2021-12-02 12:34:15 +03:00
|
|
|
const logging = require('@tryghost/logging');
|
|
|
|
const sentry = require('../../shared/sentry');
|
|
|
|
const errors = require('@tryghost/errors');
|
2021-09-26 23:01:13 +03:00
|
|
|
|
2020-03-30 23:23:02 +03:00
|
|
|
const {getMetaDataUrl} = metaData;
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
module.exports = function url(options) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const absolute = options && options.hash.absolute && options.hash.absolute !== 'false';
|
|
|
|
let outputUrl = getMetaDataUrl(this, absolute);
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2021-12-02 12:34:15 +03:00
|
|
|
try {
|
|
|
|
outputUrl = encodeURI(decodeURI(outputUrl));
|
|
|
|
} catch (err) {
|
|
|
|
// Happens when the outputURL contains an invalid URI character like "%%" or "%80"
|
|
|
|
|
|
|
|
// Send the error not to be blind to these
|
|
|
|
const error = new errors.IncorrectUsageError({
|
|
|
|
message: `The url "${outputUrl}" couldn't be escaped correctly`,
|
|
|
|
err: err
|
|
|
|
});
|
|
|
|
sentry.captureException(error);
|
|
|
|
logging.error(error);
|
|
|
|
|
|
|
|
return new SafeString('');
|
|
|
|
}
|
2017-01-11 13:45:56 +03:00
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
return new SafeString(outputUrl);
|
|
|
|
};
|