2019-07-29 20:54:15 +03:00
|
|
|
// # link_class helper
|
2021-09-26 23:01:13 +03:00
|
|
|
const {config, SafeString, localUtils} = require('../services/proxy');
|
|
|
|
|
2019-07-29 20:54:15 +03:00
|
|
|
const _ = require('lodash');
|
2021-09-26 23:01:13 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
const tpl = require('@tryghost/tpl');
|
|
|
|
|
2020-04-08 21:02:31 +03:00
|
|
|
const {buildLinkClasses} = localUtils;
|
2019-07-29 20:54:15 +03:00
|
|
|
|
2021-09-23 22:46:22 +03:00
|
|
|
const messages = {
|
|
|
|
forIsRequired: 'The {{link_class}} helper requires a for="" attribute.'
|
|
|
|
};
|
|
|
|
|
2019-07-29 20:54:15 +03:00
|
|
|
module.exports = function link_class(options) { // eslint-disable-line camelcase
|
|
|
|
options = options || {};
|
|
|
|
options.hash = options.hash || {};
|
|
|
|
options.data = options.data || {};
|
|
|
|
|
|
|
|
// If there is no for provided, this is theme dev error, so we throw an error to make this clear.
|
|
|
|
if (!_.has(options.hash, 'for')) {
|
|
|
|
throw new errors.IncorrectUsageError({
|
2021-09-23 22:46:22 +03:00
|
|
|
message: tpl(messages.forIsRequired)
|
2019-07-29 20:54:15 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the for attribute is present but empty, this is probably a dynamic data problem, hard for theme devs to track down
|
|
|
|
// E.g. {{link_class for=slug}} in a context where slug returns an empty string
|
|
|
|
// Error's here aren't useful (same as with empty get helper filters) so we fallback gracefully
|
2021-03-24 12:43:43 +03:00
|
|
|
if (!options.hash.for || options.hash.for.string === '') {
|
2019-07-29 20:54:15 +03:00
|
|
|
options.hash.for = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
let href = options.hash.for.string || options.hash.for;
|
|
|
|
let classes = buildLinkClasses(config.get('url'), href, options);
|
|
|
|
|
|
|
|
return new SafeString(classes.join(' '));
|
|
|
|
};
|