Ghost/core/frontend/helpers/asset.js
Hannah Wolfe c902d91c81
Renamed rendering service to handlebars
- This fits more closely, as this service is to so with rendering helpers and small parts
- Whereas we want to use "rendering" for things concerned with rendering pages
2022-04-05 20:10:33 +01:00

38 lines
1.1 KiB
JavaScript

// # Asset helper
// Usage: `{{asset "css/screen.css"}}`
//
// Returns the path to the specified asset.
const {metaData, urlUtils} = require('../services/proxy');
const {SafeString} = require('../services/handlebars');
const errors = require('@tryghost/errors');
const tpl = require('@tryghost/tpl');
const get = require('lodash/get');
const {getAssetUrl} = metaData;
const messages = {
pathIsRequired: 'The {{asset}} helper must be passed a path'
};
module.exports = function asset(path, options) {
const hasMinFile = get(options, 'hash.hasMinFile');
if (!path) {
throw new errors.IncorrectUsageError({
message: tpl(messages.pathIsRequired)
});
}
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
);
}
return new SafeString(
getAssetUrl(path, hasMinFile)
);
};