Ghost/core/frontend/helpers/url.js

37 lines
1.2 KiB
JavaScript
Raw Normal View History

// # 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
const {metaData} = require('../services/proxy');
const {SafeString} = require('../services/rendering');
const logging = require('@tryghost/logging');
const sentry = require('../../shared/sentry');
const errors = require('@tryghost/errors');
const {getMetaDataUrl} = metaData;
module.exports = function url(options) {
const absolute = options && options.hash.absolute && options.hash.absolute !== 'false';
let outputUrl = getMetaDataUrl(this, absolute);
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('');
}
return new SafeString(outputUrl);
};