Skip to content

Commit

Permalink
✨ Added content_api_key helper (#21151)
Browse files Browse the repository at this point in the history
no ref

This very small helper adds {{content_api_key}} to the collection of handlebars helpers available to theme creators. This will make it easier for themes to access the content API key, without either requiring the user to get it from the integrations page and input it on the theme setting page or resorting to JavaScript to read it from one of the built-in script tag attributes -- both ugly workarounds.
  • Loading branch information
cathysarisky authored Oct 1, 2024
1 parent 86d6130 commit d8c4dfe
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
19 changes: 19 additions & 0 deletions ghost/core/core/frontend/helpers/content_api_key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const {SafeString} = require('../services/handlebars');
const logging = require('@tryghost/logging');
const {getFrontendKey} = require('../services/proxy');

module.exports = async function content_api_key() { // eslint-disable-line camelcase
try {
const frontendKey = await getFrontendKey();

if (!frontendKey) {
logging.warn('contentkey: No content key found');
return '';
}
return new SafeString(frontendKey);
} catch (error) {
logging.error(error);
return '';
}
};
module.exports.async = true;
19 changes: 19 additions & 0 deletions ghost/core/test/unit/frontend/helpers/content_api_key.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* eslint-disable no-regex-spaces */
const proxy = require('../../../../core/frontend/services/proxy');
const {getFrontendKey} = proxy;
const should = require('should');

// Stuff we are testing
const content_api_key = require('../../../../core/frontend/helpers/content_api_key');

describe('{{content_api_key}} helper', function () {
describe('compare to settings', function () {
it('returns the content API key', async function () {
const result = await content_api_key();
const expected = await getFrontendKey();
should.exist(result);
String(result).should.equal(expected);
});
});
});

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const helpers = require('../../../../../../core/frontend/services/helpers');
describe('Helpers', function () {
const hbsHelpers = ['each', 'if', 'unless', 'with', 'helperMissing', 'blockHelperMissing', 'log', 'lookup', 'block', 'contentFor'];
const ghostHelpers = [
'asset', 'authors', 'body_class', 'cancel_link', 'concat', 'content', 'date', 'encode', 'excerpt', 'facebook_url', 'foreach', 'get',
'asset', 'authors', 'body_class', 'cancel_link', 'concat', 'content', 'content_api_key', 'date', 'encode', 'excerpt', 'facebook_url', 'foreach', 'get',
'ghost_foot', 'ghost_head', 'has', 'img_url', 'is', 'link', 'link_class', 'meta_description', 'meta_title', 'navigation',
'next_post', 'page_url', 'pagination', 'plural', 'post_class', 'prev_post', 'price', 'raw', 'reading_time', 't', 'tags', 'title','total_members', 'total_paid_members', 'twitter_url',
'url', 'comment_count', 'collection', 'recommendations', 'readable_url'
Expand Down

0 comments on commit d8c4dfe

Please sign in to comment.