mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
commit
86e3835e4d
4 changed files with 295 additions and 0 deletions
|
@ -37,6 +37,8 @@ coreHelpers.tags = require('./tags');
|
||||||
coreHelpers.title = require('./title');
|
coreHelpers.title = require('./title');
|
||||||
coreHelpers.url = require('./url');
|
coreHelpers.url = require('./url');
|
||||||
coreHelpers.image = require('./image');
|
coreHelpers.image = require('./image');
|
||||||
|
coreHelpers.prev_post = require('./prev_next');
|
||||||
|
coreHelpers.next_post = require('./prev_next');
|
||||||
|
|
||||||
coreHelpers.helperMissing = function (arg) {
|
coreHelpers.helperMissing = function (arg) {
|
||||||
if (arguments.length === 2) {
|
if (arguments.length === 2) {
|
||||||
|
@ -109,6 +111,8 @@ registerHelpers = function (adminHbs) {
|
||||||
registerAsyncThemeHelper('meta_description', coreHelpers.meta_description);
|
registerAsyncThemeHelper('meta_description', coreHelpers.meta_description);
|
||||||
registerAsyncThemeHelper('meta_title', coreHelpers.meta_title);
|
registerAsyncThemeHelper('meta_title', coreHelpers.meta_title);
|
||||||
registerAsyncThemeHelper('post_class', coreHelpers.post_class);
|
registerAsyncThemeHelper('post_class', coreHelpers.post_class);
|
||||||
|
registerAsyncThemeHelper('next_post', coreHelpers.next_post);
|
||||||
|
registerAsyncThemeHelper('prev_post', coreHelpers.prev_post);
|
||||||
|
|
||||||
// Register admin helpers
|
// Register admin helpers
|
||||||
registerAdminHelper('asset', coreHelpers.asset);
|
registerAdminHelper('asset', coreHelpers.asset);
|
||||||
|
|
38
core/server/helpers/prev_next.js
Normal file
38
core/server/helpers/prev_next.js
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
// ### prevNext helper exposes methods for prev_post and next_post - separately defined in helpers index.
|
||||||
|
// Example usages
|
||||||
|
// `{{#prev_post}}<a href ="{{url}}>previous post</a>{{/prev_post}}'
|
||||||
|
// `{{#next_post}}<a href ="{{url absolute="true">next post</a>{{/next_post}}'
|
||||||
|
|
||||||
|
var api = require('../api'),
|
||||||
|
schema = require('../data/schema').checks,
|
||||||
|
Promise = require('bluebird'),
|
||||||
|
fetch, prevNext;
|
||||||
|
|
||||||
|
fetch = function (options) {
|
||||||
|
return api.posts.read(options).then(function (result) {
|
||||||
|
var related = result.posts[0];
|
||||||
|
if (related.previous) {
|
||||||
|
return options.fn(related.previous);
|
||||||
|
} else if (related.next) {
|
||||||
|
return options.fn(related.next);
|
||||||
|
} else {
|
||||||
|
return options.inverse(this);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// If prevNext method is called without valid post data then we must return a promise, if there is valid post data
|
||||||
|
// then the promise is handled in the api call.
|
||||||
|
|
||||||
|
prevNext = function (options) {
|
||||||
|
options = options || {};
|
||||||
|
options.include = options.name === 'prev_post' ? 'previous' : 'next';
|
||||||
|
if (schema.isPost(this)) {
|
||||||
|
options.slug = this.slug;
|
||||||
|
return fetch(options);
|
||||||
|
} else {
|
||||||
|
return Promise.resolve(options.inverse(this));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = prevNext;
|
126
core/test/unit/server_helpers/next_post_spec.js
Normal file
126
core/test/unit/server_helpers/next_post_spec.js
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
/*globals describe, beforeEach, afterEach, it*/
|
||||||
|
/*jshint expr:true*/
|
||||||
|
var should = require('should'),
|
||||||
|
sinon = require('sinon'),
|
||||||
|
Promise = require('bluebird'),
|
||||||
|
hbs = require('express-hbs'),
|
||||||
|
utils = require('./utils'),
|
||||||
|
|
||||||
|
// Stuff we are testing
|
||||||
|
handlebars = hbs.handlebars,
|
||||||
|
helpers = require('../../../server/helpers'),
|
||||||
|
api = require('../../../server/api');
|
||||||
|
|
||||||
|
describe('{{next_post}} helper', function () {
|
||||||
|
describe('with valid post data - ', function () {
|
||||||
|
var sandbox;
|
||||||
|
beforeEach(function () {
|
||||||
|
sandbox = sinon.sandbox.create();
|
||||||
|
utils.loadHelpers();
|
||||||
|
sandbox.stub(api.posts, 'read', function (options) {
|
||||||
|
if (options.include === 'next') {
|
||||||
|
return Promise.resolve({
|
||||||
|
posts: [{slug: '/current/', title: 'post 2', next: {slug: '/next/', title: 'post 3'}}]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function () {
|
||||||
|
sandbox.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('has loaded next_post helper', function () {
|
||||||
|
should.exist(handlebars.helpers.prev_post);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('shows \'if\' template with next post data', function (done) {
|
||||||
|
var fn = sinon.spy(),
|
||||||
|
inverse = sinon.spy(),
|
||||||
|
optionsData = {name: 'next_post', fn: fn, inverse: inverse};
|
||||||
|
|
||||||
|
helpers.prev_post.call({html: 'content',
|
||||||
|
markdown: 'ff',
|
||||||
|
title: 'post2',
|
||||||
|
slug: 'current',
|
||||||
|
created_at: new Date(0),
|
||||||
|
url: '/current/'}, optionsData).then(function () {
|
||||||
|
fn.called.should.be.true;
|
||||||
|
inverse.called.should.be.false;
|
||||||
|
done();
|
||||||
|
}).catch(function (err) {
|
||||||
|
console.log('err ', err);
|
||||||
|
done(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('for valid post with no next post', function () {
|
||||||
|
var sandbox;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
sandbox = sinon.sandbox.create();
|
||||||
|
utils.loadHelpers();
|
||||||
|
sandbox.stub(api.posts, 'read', function (options) {
|
||||||
|
if (options.include === 'next') {
|
||||||
|
return Promise.resolve({posts: [{slug: '/current/', title: 'post 2'}]});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function () {
|
||||||
|
sandbox.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('shows \'else\' template', function (done) {
|
||||||
|
var fn = sinon.spy(),
|
||||||
|
inverse = sinon.spy(),
|
||||||
|
optionsData = {name: 'next_post', fn: fn, inverse: inverse};
|
||||||
|
|
||||||
|
helpers.prev_post.call({html: 'content',
|
||||||
|
markdown: 'ff',
|
||||||
|
title: 'post2',
|
||||||
|
slug: 'current',
|
||||||
|
created_at: new Date(0),
|
||||||
|
url: '/current/'}, optionsData).then(function () {
|
||||||
|
fn.called.should.be.false;
|
||||||
|
inverse.called.should.be.true;
|
||||||
|
done();
|
||||||
|
}).catch(function (err) {
|
||||||
|
done(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('for invalid post data', function () {
|
||||||
|
var sandbox;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
sandbox = sinon.sandbox.create();
|
||||||
|
utils.loadHelpers();
|
||||||
|
sandbox.stub(api.posts, 'read', function (options) {
|
||||||
|
if (options.include === 'previous') {
|
||||||
|
return Promise.resolve({});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function () {
|
||||||
|
sandbox.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('shows \'else\' template', function (done) {
|
||||||
|
var fn = sinon.spy(),
|
||||||
|
inverse = sinon.spy(),
|
||||||
|
optionsData = {name: 'next_post', fn: fn, inverse: inverse};
|
||||||
|
|
||||||
|
helpers.prev_post.call({}, optionsData).then(function () {
|
||||||
|
fn.called.should.be.false;
|
||||||
|
inverse.called.should.be.true;
|
||||||
|
done();
|
||||||
|
}).catch(function (err) {
|
||||||
|
done(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
127
core/test/unit/server_helpers/prev_post_spec.js
Normal file
127
core/test/unit/server_helpers/prev_post_spec.js
Normal file
|
@ -0,0 +1,127 @@
|
||||||
|
/*globals describe, beforeEach, afterEach, it*/
|
||||||
|
/*jshint expr:true*/
|
||||||
|
var should = require('should'),
|
||||||
|
sinon = require('sinon'),
|
||||||
|
Promise = require('bluebird'),
|
||||||
|
hbs = require('express-hbs'),
|
||||||
|
utils = require('./utils'),
|
||||||
|
|
||||||
|
// Stuff we are testing
|
||||||
|
handlebars = hbs.handlebars,
|
||||||
|
helpers = require('../../../server/helpers'),
|
||||||
|
api = require('../../../server/api');
|
||||||
|
|
||||||
|
describe('{{prev_post}} helper', function () {
|
||||||
|
describe('with valid post data - ', function () {
|
||||||
|
var sandbox;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
sandbox = sinon.sandbox.create();
|
||||||
|
utils.loadHelpers();
|
||||||
|
sandbox.stub(api.posts, 'read', function (options) {
|
||||||
|
if (options.include === 'previous') {
|
||||||
|
return Promise.resolve({
|
||||||
|
posts: [{slug: '/current/', title: 'post 2', previous: {slug: '/previous/', title: 'post 1'}}]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function () {
|
||||||
|
sandbox.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('has loaded prev_post helper', function () {
|
||||||
|
should.exist(handlebars.helpers.prev_post);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('shows \'if\' template with previous post data', function (done) {
|
||||||
|
var fn = sinon.spy(),
|
||||||
|
inverse = sinon.spy(),
|
||||||
|
optionsData = {name: 'prev_post', fn: fn, inverse: inverse};
|
||||||
|
|
||||||
|
helpers.prev_post.call({html: 'content',
|
||||||
|
markdown: 'ff',
|
||||||
|
title: 'post2',
|
||||||
|
slug: 'current',
|
||||||
|
created_at: new Date(0),
|
||||||
|
url: '/current/'}, optionsData).then(function () {
|
||||||
|
fn.called.should.be.true;
|
||||||
|
inverse.called.should.be.false;
|
||||||
|
done();
|
||||||
|
}).catch(function (err) {
|
||||||
|
console.log('err ', err);
|
||||||
|
done(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('for valid post with no previous post', function () {
|
||||||
|
var sandbox;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
sandbox = sinon.sandbox.create();
|
||||||
|
utils.loadHelpers();
|
||||||
|
sandbox.stub(api.posts, 'read', function (options) {
|
||||||
|
if (options.include === 'previous') {
|
||||||
|
return Promise.resolve({posts: [{slug: '/current/', title: 'post 2'}]});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function () {
|
||||||
|
sandbox.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('shows \'else\' template', function (done) {
|
||||||
|
var fn = sinon.spy(),
|
||||||
|
inverse = sinon.spy(),
|
||||||
|
optionsData = {name: 'prev_post', fn: fn, inverse: inverse};
|
||||||
|
|
||||||
|
helpers.prev_post.call({html: 'content',
|
||||||
|
markdown: 'ff',
|
||||||
|
title: 'post2',
|
||||||
|
slug: 'current',
|
||||||
|
created_at: new Date(0),
|
||||||
|
url: '/current/'}, optionsData).then(function () {
|
||||||
|
fn.called.should.be.false;
|
||||||
|
inverse.called.should.be.true;
|
||||||
|
done();
|
||||||
|
}).catch(function (err) {
|
||||||
|
done(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('for invalid post data', function () {
|
||||||
|
var sandbox;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
sandbox = sinon.sandbox.create();
|
||||||
|
utils.loadHelpers();
|
||||||
|
sandbox.stub(api.posts, 'read', function (options) {
|
||||||
|
if (options.include === 'previous') {
|
||||||
|
return Promise.resolve({});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function () {
|
||||||
|
sandbox.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('shows \'else\' template', function (done) {
|
||||||
|
var fn = sinon.spy(),
|
||||||
|
inverse = sinon.spy(),
|
||||||
|
optionsData = {name: 'prev_post', fn: fn, inverse: inverse};
|
||||||
|
|
||||||
|
helpers.prev_post.call({}, optionsData).then(function () {
|
||||||
|
fn.called.should.be.false;
|
||||||
|
inverse.called.should.be.true;
|
||||||
|
done();
|
||||||
|
}).catch(function (err) {
|
||||||
|
done(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Reference in a new issue