2014-10-10 18:54:07 +04:00
|
|
|
// # Asset helper
|
2019-03-26 07:36:41 +03:00
|
|
|
// Usage: `{{asset "css/screen.css"}}`
|
2014-10-10 18:54:07 +04:00
|
|
|
//
|
2019-03-26 07:36:41 +03:00
|
|
|
// Returns the path to the specified asset.
|
2021-11-03 13:19:56 +03:00
|
|
|
const {metaData, urlUtils} = require('../services/proxy');
|
2021-09-28 17:06:33 +03:00
|
|
|
const {SafeString} = require('../services/rendering');
|
2021-09-26 23:01:13 +03:00
|
|
|
|
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
const tpl = require('@tryghost/tpl');
|
2019-03-26 07:36:41 +03:00
|
|
|
const get = require('lodash/get');
|
2019-09-09 14:52:55 +03:00
|
|
|
const {getAssetUrl} = metaData;
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2021-09-23 22:46:22 +03:00
|
|
|
const messages = {
|
|
|
|
pathIsRequired: 'The {{asset}} helper must be passed a path'
|
|
|
|
};
|
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
module.exports = function asset(path, options) {
|
2018-06-04 19:30:21 +03:00
|
|
|
const hasMinFile = get(options, 'hash.hasMinFile');
|
2017-02-03 21:25:39 +03:00
|
|
|
|
2019-09-09 14:52:55 +03:00
|
|
|
if (!path) {
|
|
|
|
throw new errors.IncorrectUsageError({
|
2021-09-23 22:46:22 +03:00
|
|
|
message: tpl(messages.pathIsRequired)
|
2019-09-09 14:52:55 +03:00
|
|
|
});
|
|
|
|
}
|
2021-11-03 13:19:56 +03:00
|
|
|
if (typeof urlUtils.getSiteUrl() !== 'undefined'
|
|
|
|
&& typeof urlUtils.getAdminUrl() !== 'undefined'
|
|
|
|
&& urlUtils.getSiteUrl() !== urlUtils.getAdminUrl()) {
|
|
|
|
const target = new URL(getAssetUrl(path, hasMinFile), urlUtils.getSiteUrl());
|
|
|
|
return new SafeString(
|
|
|
|
target.href
|
|
|
|
);
|
|
|
|
}
|
2019-09-09 14:52:55 +03:00
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
return new SafeString(
|
2017-04-10 12:30:21 +03:00
|
|
|
getAssetUrl(path, hasMinFile)
|
2016-01-17 13:07:52 +03:00
|
|
|
);
|
2017-04-04 19:07:35 +03:00
|
|
|
};
|