mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Cleans up HTML data attributes on body in default.hbs
closes #4485 - removes data attributes used on body in default.hbs - introduces new way to generate configuration through meta tags - config initializer consumes configurations from the meta tags using parser - moves blog_title helper/value to be a property in a configuration api
This commit is contained in:
parent
86cb690474
commit
df5a598718
8 changed files with 64 additions and 202 deletions
|
@ -835,7 +835,7 @@ var _ = require('lodash'),
|
|||
//
|
||||
// `NODE_ENV=testing mocha --timeout=15000 --ui=bdd --reporter=spec core/test/unit/config_spec.js`
|
||||
//
|
||||
// Unit tests are run with [mocha](http://visionmedia.github.io/mocha/) using
|
||||
// Unit tests are run with [mocha](http://mochajs.org/) using
|
||||
// [should](https://github.com/visionmedia/should.js) to describe the tests in a highly readable style.
|
||||
// Unit tests do **not** touch the database.
|
||||
// A coverage report can be generated for these tests using the `grunt test-coverage` task.
|
||||
|
@ -853,7 +853,7 @@ var _ = require('lodash'),
|
|||
//
|
||||
// `NODE_ENV=testing grunt mochacli:api`
|
||||
//
|
||||
// Integration tests are run with [mocha](http://visionmedia.github.io/mocha/) using
|
||||
// Integration tests are run with [mocha](http://mochajs.org/) using
|
||||
// [should](https://github.com/visionmedia/should.js) to describe the tests in a highly readable style.
|
||||
// Integration tests are different to the unit tests because they make requests to the database.
|
||||
//
|
||||
|
@ -879,7 +879,7 @@ var _ = require('lodash'),
|
|||
//
|
||||
// `NODE_ENV=testing mocha --timeout=15000 --ui=bdd --reporter=spec core/test/functional/routes/admin_test.js`
|
||||
//
|
||||
// Route tests are run with [mocha](http://visionmedia.github.io/mocha/) using
|
||||
// Route tests are run with [mocha](http://mochajs.org/) using
|
||||
// [should](https://github.com/visionmedia/should.js) and [supertest](https://github.com/visionmedia/supertest)
|
||||
// to describe and create the tests.
|
||||
//
|
||||
|
|
|
@ -1,16 +1,11 @@
|
|||
import getConfig from 'ghost/utils/config-parser';
|
||||
|
||||
var ConfigInitializer = {
|
||||
name: 'config',
|
||||
|
||||
initialize: function (container, application) {
|
||||
var apps = $('body').data('apps'),
|
||||
tagsUI = $('body').data('tagsui'),
|
||||
fileStorage = $('body').data('filestorage'),
|
||||
blogUrl = $('body').data('blogurl'),
|
||||
blogTitle = $('body').data('blogtitle');
|
||||
|
||||
application.register(
|
||||
'ghost:config', {apps: apps, fileStorage: fileStorage, blogUrl: blogUrl, tagsUI: tagsUI, blogTitle: blogTitle}, {instantiate: false}
|
||||
);
|
||||
var config = getConfig();
|
||||
application.register('ghost:config', config, {instantiate: false});
|
||||
|
||||
application.inject('route', 'config', 'ghost:config');
|
||||
application.inject('controller', 'config', 'ghost:config');
|
||||
|
|
36
core/client/utils/config-parser.js
Normal file
36
core/client/utils/config-parser.js
Normal file
|
@ -0,0 +1,36 @@
|
|||
var isNumeric = function (num) {
|
||||
return !isNaN(num);
|
||||
},
|
||||
|
||||
_mapType = function (val) {
|
||||
if (val === '') {
|
||||
return null;
|
||||
} else if (val === 'true') {
|
||||
return true;
|
||||
} else if (val === 'false') {
|
||||
return false;
|
||||
} else if (isNumeric(val)) {
|
||||
return +val;
|
||||
} else {
|
||||
return val;
|
||||
}
|
||||
},
|
||||
|
||||
parseConfiguration = function () {
|
||||
var metaConfigTags = $('meta[name^="env-"]'),
|
||||
propertyName,
|
||||
config = {},
|
||||
value,
|
||||
key,
|
||||
i;
|
||||
|
||||
for (i = 0; i < metaConfigTags.length; i += 1) {
|
||||
key = $(metaConfigTags[i]).prop('name');
|
||||
value = $(metaConfigTags[i]).prop('content');
|
||||
propertyName = key.substring(4); // produce config name ignoring the initial 'env-'.
|
||||
config[propertyName] = _mapType(value); // map string values to types if possible
|
||||
}
|
||||
return config;
|
||||
};
|
||||
|
||||
export default parseConfiguration;
|
|
@ -17,7 +17,8 @@ function getValidKeys() {
|
|||
environment: process.env.NODE_ENV,
|
||||
database: config.database.client,
|
||||
mail: _.isObject(config.mail) ? config.mail.transport : '',
|
||||
blogUrl: config.url
|
||||
blogUrl: config.url,
|
||||
blogTitle: config.theme.title
|
||||
};
|
||||
|
||||
return parsePackageJson('package.json').then(function (json) {
|
||||
|
|
|
@ -13,8 +13,15 @@ adminControllers = {
|
|||
/*jslint unparam:true*/
|
||||
|
||||
function renderIndex() {
|
||||
res.render('default', {
|
||||
skip_google_fonts: config.isPrivacyDisabled('useGoogleFonts')
|
||||
return api.configuration.browse().then(function (data) {
|
||||
var apiConfig = _.omit(data.configuration, function (value) {
|
||||
return _.contains(['environment', 'database', 'mail', 'version'], value.key);
|
||||
});
|
||||
|
||||
res.render('default', {
|
||||
skip_google_fonts: config.isPrivacyDisabled('useGoogleFonts'),
|
||||
configuration: apiConfig
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -1,12 +1,7 @@
|
|||
var hbs = require('express-hbs'),
|
||||
_ = require('lodash'),
|
||||
Promise = require('bluebird'),
|
||||
|
||||
config = require('../config'),
|
||||
errors = require('../errors'),
|
||||
|
||||
utils = require('./utils'),
|
||||
|
||||
coreHelpers = {},
|
||||
registerHelpers;
|
||||
|
||||
|
@ -44,70 +39,6 @@ coreHelpers.image = require('./image');
|
|||
|
||||
coreHelpers.ghost_script_tags = require('./ghost_script_tags');
|
||||
|
||||
// ### Filestorage helper
|
||||
//
|
||||
// *Usage example:*
|
||||
// `{{file_storage}}`
|
||||
//
|
||||
// Returns the config value for fileStorage.
|
||||
coreHelpers.file_storage = function (context, options) {
|
||||
/*jshint unused:false*/
|
||||
if (config.hasOwnProperty('fileStorage')) {
|
||||
return _.isObject(config.fileStorage) ? 'true' : config.fileStorage.toString();
|
||||
}
|
||||
return 'true';
|
||||
};
|
||||
|
||||
// ### Apps helper
|
||||
//
|
||||
// *Usage example:*
|
||||
// `{{apps}}`
|
||||
//
|
||||
// Returns the config value for apps.
|
||||
coreHelpers.apps = function (context, options) {
|
||||
/*jshint unused:false*/
|
||||
if (config.hasOwnProperty('apps')) {
|
||||
return config.apps.toString();
|
||||
}
|
||||
return 'false';
|
||||
};
|
||||
|
||||
// ### TagsUI helper
|
||||
//
|
||||
// *Usage example:*
|
||||
// `{{tags_ui}}`
|
||||
//
|
||||
// Returns the config value for tagsUI or false if no value present
|
||||
coreHelpers.tags_ui = function (context, options) {
|
||||
/*jshint unused:false*/
|
||||
if (config.hasOwnProperty('tagsUI')) {
|
||||
return config.tagsUI.toString();
|
||||
}
|
||||
return 'false';
|
||||
};
|
||||
|
||||
// ### Blog Url helper
|
||||
//
|
||||
// *Usage example:*
|
||||
// `{{blog_url}}`
|
||||
//
|
||||
// Returns the config value for url.
|
||||
coreHelpers.blog_url = function (context, options) {
|
||||
/*jshint unused:false*/
|
||||
return config.theme.url.toString();
|
||||
};
|
||||
|
||||
// ### Blog Title helper
|
||||
//
|
||||
// *Usage example:*
|
||||
// `{{blog_title}}`
|
||||
//
|
||||
// Returns the config value for url.
|
||||
coreHelpers.blog_title = function (context, options) {
|
||||
/*jshint unused:false*/
|
||||
return config.theme.title.toString();
|
||||
};
|
||||
|
||||
coreHelpers.helperMissing = function (arg) {
|
||||
if (arguments.length === 2) {
|
||||
return undefined;
|
||||
|
@ -177,12 +108,6 @@ registerHelpers = function (adminHbs) {
|
|||
// Register admin helpers
|
||||
registerAdminHelper('ghost_script_tags', coreHelpers.ghost_script_tags);
|
||||
registerAdminHelper('asset', coreHelpers.asset);
|
||||
registerAdminHelper('apps', coreHelpers.apps);
|
||||
registerAdminHelper('file_storage', coreHelpers.file_storage);
|
||||
registerAdminHelper('tags_ui', coreHelpers.tags_ui);
|
||||
|
||||
registerAdminHelper('blog_title', coreHelpers.blog_title);
|
||||
registerAdminHelper('blog_url', coreHelpers.blog_url);
|
||||
};
|
||||
|
||||
module.exports = coreHelpers;
|
||||
|
|
|
@ -29,14 +29,18 @@
|
|||
<meta name="msapplication-square150x150logo" content="{{asset "img/medium.png" ghost="true"}}" />
|
||||
<meta name="msapplication-square310x310logo" content="{{asset "img/large.png" ghost="true"}}" />
|
||||
|
||||
{{#each configuration}}
|
||||
<meta name="env-{{this.key}}" content="{{this.value}}" />
|
||||
{{/each}}
|
||||
|
||||
{{#unless skip_google_fonts}}
|
||||
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Open+Sans:400,300,700" />
|
||||
{{/unless}}
|
||||
<link rel="stylesheet" href="{{asset "css/ghost.min.css" ghost="true"}}" />
|
||||
</head>
|
||||
<body class="{{bodyClass}}" data-apps="{{apps}}" data-filestorage="{{file_storage}}" data-tagsui="{{tags_ui}}" data-blogurl="{{blog_url}}" data-blogtitle="{{blog_title}}">
|
||||
<body>
|
||||
|
||||
{{{ghost_script_tags}}}
|
||||
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
|
|
|
@ -1,33 +1,18 @@
|
|||
/*globals describe, beforeEach, afterEach, it*/
|
||||
/*globals describe, beforeEach, it*/
|
||||
/*jshint expr:true*/
|
||||
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
|
||||
var should = require('should'),
|
||||
_ = require('lodash'),
|
||||
rewire = require('rewire'),
|
||||
hbs = require('express-hbs'),
|
||||
|
||||
// Stuff we are testing
|
||||
helpers = rewire('../../server/helpers'),
|
||||
config = rewire('../../server/config');
|
||||
|
||||
helpers = rewire('../../server/helpers');
|
||||
|
||||
describe('Helpers', function () {
|
||||
var overrideConfig = function (newConfig) {
|
||||
var existingConfig = helpers.__get__('config');
|
||||
config.set(_.extend(existingConfig, newConfig));
|
||||
};
|
||||
|
||||
beforeEach(function () {
|
||||
var adminHbs = hbs.create();
|
||||
helpers = rewire('../../server/helpers');
|
||||
|
||||
overrideConfig({
|
||||
paths: {
|
||||
subdir: ''
|
||||
},
|
||||
theme: {
|
||||
url: 'http://testurl.com'
|
||||
}
|
||||
});
|
||||
|
||||
helpers.loadCoreHelpers(adminHbs);
|
||||
});
|
||||
|
||||
|
@ -48,95 +33,4 @@ describe('Helpers', function () {
|
|||
runHelper('test helper', 'second argument').should.not.throwError();
|
||||
});
|
||||
});
|
||||
|
||||
describe('file storage helper', function () {
|
||||
it('is loaded', function () {
|
||||
should.exist(helpers.file_storage);
|
||||
});
|
||||
|
||||
it('should return the string true when config() has no fileStorage property', function () {
|
||||
var fileStorage = helpers.file_storage();
|
||||
|
||||
should.exist(fileStorage);
|
||||
fileStorage.should.equal('true');
|
||||
});
|
||||
|
||||
it('should return the config.fileStorage value when it exists', function () {
|
||||
var setting = 'file storage value',
|
||||
cfg = helpers.__get__('config'),
|
||||
fileStorage;
|
||||
|
||||
_.extend(cfg, {
|
||||
fileStorage: setting
|
||||
});
|
||||
|
||||
fileStorage = helpers.file_storage();
|
||||
|
||||
should.exist(fileStorage);
|
||||
fileStorage.should.equal(setting);
|
||||
});
|
||||
|
||||
it('should just return true if config.fileStorage is an object', function () {
|
||||
var setting = {someKey: 'someValue'},
|
||||
cfg = helpers.__get__('config'),
|
||||
fileStorage;
|
||||
|
||||
_.extend(cfg, {
|
||||
fileStorage: setting
|
||||
});
|
||||
|
||||
fileStorage = helpers.file_storage();
|
||||
|
||||
should.exist(fileStorage);
|
||||
fileStorage.should.equal('true');
|
||||
});
|
||||
});
|
||||
|
||||
describe('apps helper', function () {
|
||||
it('is loaded', function () {
|
||||
should.exist(helpers.apps);
|
||||
});
|
||||
|
||||
it('should return the string false when config() has no apps property', function () {
|
||||
var apps = helpers.apps();
|
||||
|
||||
should.exist(apps);
|
||||
apps.should.equal('false');
|
||||
});
|
||||
|
||||
it('should return the config.apps value when it exists', function () {
|
||||
var setting = 'app value',
|
||||
cfg = helpers.__get__('config'),
|
||||
apps;
|
||||
|
||||
_.extend(cfg, {
|
||||
apps: setting
|
||||
});
|
||||
|
||||
apps = helpers.apps();
|
||||
|
||||
should.exist(apps);
|
||||
apps.should.equal(setting);
|
||||
});
|
||||
});
|
||||
|
||||
describe('blog_url helper', function () {
|
||||
var configUrl = config.url;
|
||||
|
||||
afterEach(function () {
|
||||
config.set({url: configUrl});
|
||||
});
|
||||
|
||||
it('is loaded', function () {
|
||||
should.exist(helpers.blog_url);
|
||||
});
|
||||
|
||||
it('should return the test url by default', function () {
|
||||
var blogUrl = helpers.blog_url();
|
||||
|
||||
should.exist(blogUrl);
|
||||
// this is set in another test == bad!
|
||||
blogUrl.should.equal('http://testurl.com');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue