mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
💄 Misc cleanup in middleware, helpers & apps (#7479)
no issue - unsued code: - there are no public assets anymore, might need to use this instead of shared in future, but for now lets remove it to reduce confusion - the `input password` box was incorrectly registered as an admin helper, thinking that was needed in order to render the default template. This isn't needed. - apps: - small structure & comment update to amp app - moving input_password helper into private blogging app - refactor helpers in subscribers app
This commit is contained in:
parent
763305a74d
commit
7dba7b52f8
12 changed files with 128 additions and 110 deletions
|
@ -1,12 +1,12 @@
|
|||
var router = require('./lib/router'),
|
||||
registerAmpHelpers = require('./lib/helpers'),
|
||||
registerHelpers = require('./lib/helpers'),
|
||||
|
||||
// Dirty requires
|
||||
config = require('../../config');
|
||||
|
||||
module.exports = {
|
||||
activate: function activate(ghost) {
|
||||
registerAmpHelpers(ghost);
|
||||
registerHelpers(ghost);
|
||||
},
|
||||
|
||||
setupRoutes: function setupRoutes(blogRouter) {
|
||||
|
|
|
@ -1,16 +1,11 @@
|
|||
var ampContentHelper = require('./amp_content'),
|
||||
ampComponentsHelper = require('./amp_components'),
|
||||
registerAsyncThemeHelper = require('../../../../helpers').registerAsyncThemeHelper,
|
||||
ghostHead = require('../../../../helpers/ghost_head'),
|
||||
registerAmpHelpers;
|
||||
// Dirty require!
|
||||
var ghostHead = require('../../../../helpers/ghost_head');
|
||||
|
||||
registerAmpHelpers = function (ghost) {
|
||||
ghost.helpers.registerAsync('amp_content', ampContentHelper);
|
||||
module.exports = function registerAmpHelpers(ghost) {
|
||||
ghost.helpers.registerAsync('amp_content', require('./amp_content'));
|
||||
|
||||
ghost.helpers.register('amp_components', ampComponentsHelper);
|
||||
ghost.helpers.register('amp_components', require('./amp_components'));
|
||||
|
||||
// we use the {{ghost_head}} helper, but call it {{amp_ghost_head}}, so it's consistent
|
||||
registerAsyncThemeHelper('amp_ghost_head', ghostHead);
|
||||
ghost.helpers.registerAsync('amp_ghost_head', ghostHead);
|
||||
};
|
||||
|
||||
module.exports = registerAmpHelpers;
|
||||
|
|
|
@ -3,10 +3,11 @@ var config = require('../../config'),
|
|||
logging = require('../../logging'),
|
||||
i18n = require('../../i18n'),
|
||||
middleware = require('./lib/middleware'),
|
||||
router = require('./lib/router');
|
||||
router = require('./lib/router'),
|
||||
registerHelpers = require('./lib/helpers');
|
||||
|
||||
module.exports = {
|
||||
activate: function activate() {
|
||||
activate: function activate(ghost) {
|
||||
var err, paths;
|
||||
|
||||
if (utils.url.getSubdir()) {
|
||||
|
@ -23,6 +24,8 @@ module.exports = {
|
|||
process.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
registerHelpers(ghost);
|
||||
},
|
||||
|
||||
setupMiddleware: function setupMiddleware(blogApp) {
|
||||
|
|
3
core/server/apps/private-blogging/lib/helpers/index.js
Normal file
3
core/server/apps/private-blogging/lib/helpers/index.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
module.exports = function registerHelpers(ghost) {
|
||||
ghost.helpers.register('input_password', require('./input_password'));
|
||||
};
|
|
@ -3,11 +3,12 @@
|
|||
//
|
||||
// Password input used on private.hbs for password-protected blogs
|
||||
//
|
||||
// We use the name meta_title to match the helper for consistency:
|
||||
// We use the name input_password to match the helper for consistency:
|
||||
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
|
||||
|
||||
var hbs = require('express-hbs'),
|
||||
utils = require('./utils'),
|
||||
// Dirty requires
|
||||
var hbs = require('express-hbs'),
|
||||
utils = require('../../../../helpers/utils'),
|
||||
input_password;
|
||||
|
||||
input_password = function (options) {
|
|
@ -1,23 +1,18 @@
|
|||
// We use the name input_password to match the helper for consistency:
|
||||
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
|
||||
var should = require('should'),
|
||||
hbs = require('express-hbs'),
|
||||
utils = require('./utils'),
|
||||
|
||||
// Stuff we are testing
|
||||
handlebars = hbs.handlebars,
|
||||
helpers = require('../../../server/helpers');
|
||||
input_password = require('../lib/helpers/input_password');
|
||||
|
||||
describe('{{input_password}} helper', function () {
|
||||
before(function () {
|
||||
utils.loadHelpers();
|
||||
});
|
||||
|
||||
it('has loaded input_password helper', function () {
|
||||
should.exist(handlebars.helpers.input_password);
|
||||
it('has input_password helper', function () {
|
||||
should.exist(input_password);
|
||||
});
|
||||
|
||||
it('returns the correct input when no custom options are specified', function () {
|
||||
var markup = '<input class="private-login-password" type="password" name="password" autofocus="autofocus" />',
|
||||
rendered = helpers.input_password();
|
||||
rendered = input_password();
|
||||
should.exist(rendered);
|
||||
|
||||
String(rendered).should.equal(markup);
|
||||
|
@ -30,7 +25,7 @@ describe('{{input_password}} helper', function () {
|
|||
class: 'test-class'
|
||||
}
|
||||
},
|
||||
rendered = helpers.input_password(options);
|
||||
rendered = input_password(options);
|
||||
|
||||
should.exist(rendered);
|
||||
|
||||
|
@ -44,7 +39,7 @@ describe('{{input_password}} helper', function () {
|
|||
placeholder: 'Test'
|
||||
}
|
||||
},
|
||||
rendered = helpers.input_password(options);
|
||||
rendered = input_password(options);
|
||||
|
||||
should.exist(rendered);
|
||||
|
|
@ -1,74 +1,13 @@
|
|||
var _ = require('lodash'),
|
||||
path = require('path'),
|
||||
hbs = require('express-hbs'),
|
||||
router = require('./lib/router'),
|
||||
var router = require('./lib/router'),
|
||||
registerHelpers = require('./lib/helpers'),
|
||||
|
||||
// Dirty requires
|
||||
config = require('../../config'),
|
||||
logging = require('../../logging'),
|
||||
i18n = require('../../i18n'),
|
||||
labs = require('../../utils/labs'),
|
||||
template = require('../../helpers/template'),
|
||||
utils = require('../../helpers/utils'),
|
||||
globalUtils = require('../../utils'),
|
||||
|
||||
params = ['error', 'success', 'email'],
|
||||
|
||||
/**
|
||||
* This helper script sets the referrer and current location if not existent.
|
||||
*
|
||||
* document.querySelector['.location']['value'] = document.querySelector('.location')['value'] || window.location.href;
|
||||
*/
|
||||
subscribeScript =
|
||||
'<script type="text/javascript">' +
|
||||
'(function(g,h,o,s,t){' +
|
||||
'h[o](\'.location\')[s]=h[o](\'.location\')[s] || g.location.href;' +
|
||||
'h[o](\'.referrer\')[s]=h[o](\'.referrer\')[s] || h.referrer;' +
|
||||
'})(window,document,\'querySelector\',\'value\');' +
|
||||
'</script>';
|
||||
|
||||
function makeHidden(name, extras) {
|
||||
return utils.inputTemplate({
|
||||
type: 'hidden',
|
||||
name: name,
|
||||
className: name,
|
||||
extras: extras
|
||||
});
|
||||
}
|
||||
|
||||
function subscribeFormHelper(options) {
|
||||
var root = options.data.root,
|
||||
data = _.merge({}, options.hash, _.pick(root, params), {
|
||||
action: path.join('/', globalUtils.url.getSubdir(), config.get('routeKeywords').subscribe, '/'),
|
||||
script: new hbs.handlebars.SafeString(subscribeScript),
|
||||
hidden: new hbs.handlebars.SafeString(
|
||||
makeHidden('confirm') +
|
||||
makeHidden('location', root.subscribed_url ? 'value=' + root.subscribed_url : '') +
|
||||
makeHidden('referrer', root.subscribed_referrer ? 'value=' + root.subscribed_referrer : '')
|
||||
)
|
||||
});
|
||||
|
||||
return template.execute('subscribe_form', data, options);
|
||||
}
|
||||
config = require('../../config'),
|
||||
labs = require('../../utils/labs');
|
||||
|
||||
module.exports = {
|
||||
activate: function activate(ghost) {
|
||||
var err;
|
||||
|
||||
// Correct way to register a helper from an app
|
||||
ghost.helpers.register('subscribe_form', function labsEnabledHelper() {
|
||||
if (labs.isSet('subscribers') === true) {
|
||||
return subscribeFormHelper.apply(this, arguments);
|
||||
}
|
||||
|
||||
err = new Error();
|
||||
err.message = i18n.t('warnings.helpers.helperNotAvailable', {helperName: 'subscribe_form'});
|
||||
err.context = i18n.t('warnings.helpers.apiMustBeEnabled', {helperName: 'subscribe_form', flagName: 'subscribers'});
|
||||
err.help = i18n.t('warnings.helpers.seeLink', {url: 'http://support.ghost.org/subscribers-beta/'});
|
||||
logging.error(err);
|
||||
|
||||
return new hbs.handlebars.SafeString('<script>console.error(' + JSON.stringify(err) + ');</script>');
|
||||
});
|
||||
registerHelpers(ghost);
|
||||
},
|
||||
|
||||
setupRoutes: function setupRoutes(blogRouter) {
|
||||
|
|
32
core/server/apps/subscribers/lib/helpers/index.js
Normal file
32
core/server/apps/subscribers/lib/helpers/index.js
Normal file
|
@ -0,0 +1,32 @@
|
|||
// Dirty requires!
|
||||
var hbs = require('express-hbs'),
|
||||
logging = require('../../../../logging'),
|
||||
i18n = require('../../../../i18n'),
|
||||
labs = require('../../../../utils/labs'),
|
||||
|
||||
errorMessages = [
|
||||
i18n.t('warnings.helpers.helperNotAvailable', {helperName: 'subscribe_form'}),
|
||||
i18n.t('warnings.helpers.apiMustBeEnabled', {helperName: 'subscribe_form', flagName: 'subscribers'}),
|
||||
i18n.t('warnings.helpers.seeLink', {url: 'http://support.ghost.org/subscribers-beta/'})
|
||||
];
|
||||
|
||||
module.exports = function registerHelpers(ghost) {
|
||||
var err;
|
||||
|
||||
ghost.helpers.register('input_email', require('./input_email'));
|
||||
|
||||
ghost.helpers.register('subscribe_form', function labsEnabledHelper() {
|
||||
if (labs.isSet('subscribers') === true) {
|
||||
return require('./subscribe_form').apply(this, arguments);
|
||||
}
|
||||
|
||||
err = new Error();
|
||||
err.message = i18n.t('warnings.helpers.helperNotAvailable', {helperName: 'subscribe_form'});
|
||||
err.context = i18n.t('warnings.helpers.apiMustBeEnabled', {helperName: 'subscribe_form', flagName: 'subscribers'});
|
||||
err.help = i18n.t('warnings.helpers.seeLink', {url: 'http://support.ghost.org/subscribers-beta/'});
|
||||
|
||||
logging.error(err);
|
||||
|
||||
return new hbs.handlebars.SafeString('<script>console.error("' + errorMessages.join(' ') + '");</script>');
|
||||
});
|
||||
};
|
|
@ -1,13 +1,14 @@
|
|||
// # Input Email Helper
|
||||
// Usage: `{{input_email}}`
|
||||
//
|
||||
// Password input used on private.hbs for password-protected blogs
|
||||
// Used by `{{subscribe_form}}`
|
||||
//
|
||||
// We use the name meta_title to match the helper for consistency:
|
||||
// We use the name input_email to match the helper for consistency:
|
||||
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
|
||||
|
||||
var hbs = require('express-hbs'),
|
||||
utils = require('./utils'),
|
||||
// Dirty requires
|
||||
var hbs = require('express-hbs'),
|
||||
utils = require('../../../../helpers/utils'),
|
||||
input_email;
|
||||
|
||||
input_email = function (options) {
|
58
core/server/apps/subscribers/lib/helpers/subscribe_form.js
Normal file
58
core/server/apps/subscribers/lib/helpers/subscribe_form.js
Normal file
|
@ -0,0 +1,58 @@
|
|||
// # Subscribe Form Helper
|
||||
// Usage: `{{subscribe_form}}`
|
||||
//
|
||||
// We use the name subscribe_form to match the helper for consistency:
|
||||
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
|
||||
var _ = require('lodash'),
|
||||
path = require('path'),
|
||||
|
||||
// Dirty requires
|
||||
hbs = require('express-hbs'),
|
||||
config = require('../../../../config'),
|
||||
template = require('../../../../helpers/template'),
|
||||
utils = require('../../../../helpers/utils'),
|
||||
globalUtils = require('../../../../utils'),
|
||||
|
||||
params = ['error', 'success', 'email'],
|
||||
|
||||
subscribe_form,
|
||||
subscribeScript;
|
||||
|
||||
function makeHidden(name, extras) {
|
||||
return utils.inputTemplate({
|
||||
type: 'hidden',
|
||||
name: name,
|
||||
className: name,
|
||||
extras: extras
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* This helper script sets the referrer and current location if not existent.
|
||||
*
|
||||
* document.querySelector['.location']['value'] = document.querySelector('.location')['value'] || window.location.href;
|
||||
*/
|
||||
subscribeScript =
|
||||
'<script type="text/javascript">' +
|
||||
'(function(g,h,o,s,t){' +
|
||||
'h[o](\'.location\')[s]=h[o](\'.location\')[s] || g.location.href;' +
|
||||
'h[o](\'.referrer\')[s]=h[o](\'.referrer\')[s] || h.referrer;' +
|
||||
'})(window,document,\'querySelector\',\'value\');' +
|
||||
'</script>';
|
||||
|
||||
subscribe_form = function (options) {
|
||||
var root = options.data.root,
|
||||
data = _.merge({}, options.hash, _.pick(root, params), {
|
||||
action: path.join('/', globalUtils.url.getSubdir(), config.get('routeKeywords').subscribe, '/'),
|
||||
script: new hbs.handlebars.SafeString(subscribeScript),
|
||||
hidden: new hbs.handlebars.SafeString(
|
||||
makeHidden('confirm') +
|
||||
makeHidden('location', root.subscribed_url ? 'value=' + root.subscribed_url : '') +
|
||||
makeHidden('referrer', root.subscribed_referrer ? 'value=' + root.subscribed_referrer : '')
|
||||
)
|
||||
});
|
||||
|
||||
return template.execute('subscribe_form', data, options);
|
||||
};
|
||||
|
||||
module.exports = subscribe_form;
|
|
@ -40,8 +40,6 @@ coreHelpers.twitter_url = require('./twitter_url');
|
|||
coreHelpers.url = require('./url');
|
||||
|
||||
// Specialist helpers for certain templates
|
||||
coreHelpers.input_password = require('./input_password');
|
||||
coreHelpers.input_email = require('./input_email');
|
||||
coreHelpers.page_url = require('./page_url');
|
||||
coreHelpers.pageUrl = require('./page_url').deprecated;
|
||||
|
||||
|
@ -103,8 +101,6 @@ registerHelpers = function (adminHbs) {
|
|||
registerThemeHelper('has', coreHelpers.has);
|
||||
registerThemeHelper('is', coreHelpers.is);
|
||||
registerThemeHelper('image', coreHelpers.image);
|
||||
registerThemeHelper('input_email', coreHelpers.input_email);
|
||||
registerThemeHelper('input_password', coreHelpers.input_password);
|
||||
registerThemeHelper('meta_description', coreHelpers.meta_description);
|
||||
registerThemeHelper('meta_title', coreHelpers.meta_title);
|
||||
registerThemeHelper('navigation', coreHelpers.navigation);
|
||||
|
@ -128,7 +124,6 @@ registerHelpers = function (adminHbs) {
|
|||
|
||||
// Register admin helpers
|
||||
registerAdminHelper('asset', coreHelpers.asset);
|
||||
registerAdminHelper('input_password', coreHelpers.input_password);
|
||||
};
|
||||
|
||||
module.exports = coreHelpers;
|
||||
|
|
|
@ -124,10 +124,6 @@ setupMiddleware = function setupMiddleware(blogApp) {
|
|||
{maxAge: utils.ONE_HOUR_MS, fallthrough: false}
|
||||
));
|
||||
blogApp.use('/content/images', storage.getStorage().serve());
|
||||
blogApp.use('/public', serveStatic(
|
||||
path.join(corePath, '/built/public'),
|
||||
{maxAge: utils.ONE_YEAR_MS, fallthrough: false}
|
||||
));
|
||||
|
||||
debug('Static content done');
|
||||
// First determine whether we're serving admin or theme content
|
||||
|
|
Loading…
Add table
Reference in a new issue