diff --git a/core/server/controllers/frontend.js b/core/server/controllers/frontend.js index 97dc2ff54c..9324119449 100644 --- a/core/server/controllers/frontend.js +++ b/core/server/controllers/frontend.js @@ -82,6 +82,13 @@ frontendControllers = { post = promises[1]; function render() { + // If we're ready to render the page + // but the last param is 'edit' then we'll + // actually kick you to the edit page. + if (req.params[2] && req.params[2] === 'edit') { + return res.redirect(config.paths().subdir + '/ghost/editor/' + post.id + '/'); + } + filters.doFilter('prePostsRender', post).then(function (post) { api.settings.read('activeTheme').then(function (activeTheme) { var paths = config.paths().availableThemes[activeTheme.value], @@ -125,6 +132,10 @@ frontendControllers = { return next(e); }); }, + 'edit': function (req, res, next) { + req.params[2] = 'edit'; + return frontendControllers.single(req, res, next); + }, 'rss': function (req, res, next) { // Initialize RSS var siteUrl = config().url, diff --git a/core/server/routes/frontend.js b/core/server/routes/frontend.js index 74748ab3bd..2a630c8a5c 100644 --- a/core/server/routes/frontend.js +++ b/core/server/routes/frontend.js @@ -13,6 +13,8 @@ module.exports = function (server) { // Examples: // Given `/plain-slug/` the req.params would be [undefined, 'plain-slug'] // Given `/2012/12/24/plain-slug/` the req.params would be ['2012/12/24/', 'plain-slug'] + // Given `/plain-slug/edit/` the req.params would be [undefined, 'plain-slug', 'edit'] server.get(/^\/([0-9]{4}\/[0-9]{2}\/[0-9]{2}\/)?([^\/.]*)\/$/, frontend.single); + server.get(/^\/([0-9]{4}\/[0-9]{2}\/[0-9]{2}\/)?([^\/.]*)\/edit\/$/, frontend.edit); server.get('/', frontend.homepage); }; \ No newline at end of file diff --git a/core/test/functional/routes/frontend_test.js b/core/test/functional/routes/frontend_test.js index 8a5f15af2d..652063d6e1 100644 --- a/core/test/functional/routes/frontend_test.js +++ b/core/test/functional/routes/frontend_test.js @@ -88,8 +88,6 @@ describe('Frontend Routing', function () { // get today's date var date = moment().format("YYYY/MM/DD"); - console.log('date', date); - request.get('/' + date + '/welcome-to-ghost/') .expect('Cache-Control', cacheRules.hour) .expect(404) diff --git a/core/test/unit/frontend_spec.js b/core/test/unit/frontend_spec.js index 6dc7a274e1..af35e17531 100644 --- a/core/test/unit/frontend_spec.js +++ b/core/test/unit/frontend_spec.js @@ -14,7 +14,8 @@ describe('Frontend Controller', function () { var ghost, sandbox, - apiSettingsStub; + apiSettingsStub, + adminEditPagePath = '/ghost/editor/'; beforeEach(function () { sandbox = sinon.sandbox.create(); @@ -36,7 +37,8 @@ describe('Frontend Controller', function () { 'title': 'Test static page', 'slug': 'test-static-page', 'markdown': 'Test static page content', - 'page': 1 + 'page': 1, + 'published_at': new Date('2013/12/30').getTime() }, mockPost = { 'status': 'published', @@ -44,7 +46,8 @@ describe('Frontend Controller', function () { 'title': 'Test normal post', 'slug': 'test-normal-post', 'markdown': 'The test normal post content', - 'page': 0 + 'page': 0, + 'published_at': new Date('2014/1/2').getTime() }; beforeEach(function () { @@ -61,6 +64,7 @@ describe('Frontend Controller', function () { sandbox.stub(config, 'paths', function () { return { + 'subdir': '', 'availableThemes': { 'casper': { 'assets': null, @@ -83,7 +87,7 @@ describe('Frontend Controller', function () { it('can render a static page', function (done) { var req = { - params: [undefined, 'test-static-page'] + params: [undefined, mockStaticPost.slug] }, res = { render: function (view, context) { @@ -98,7 +102,7 @@ describe('Frontend Controller', function () { it('will NOT render a static page accessed as a date url', function (done) { var req = { - params: ['2012/12/30/', 'test-static-page'] + params: ['2012/12/30/', mockStaticPost.slug] }, res = { render: sinon.spy() @@ -112,7 +116,7 @@ describe('Frontend Controller', function () { it('can render a normal post', function (done) { var req = { - params: [undefined, 'test-normal-post'] + params: [undefined, mockPost.slug] }, res = { render: function (view, context) { @@ -128,7 +132,7 @@ describe('Frontend Controller', function () { it('will NOT render a normal post accessed as a date url', function (done) { var req = { - params: ['2012/12/30/', 'test-normal-post'] + params: ['2012/12/30/', mockPost.slug] }, res = { render: sinon.spy() @@ -139,6 +143,71 @@ describe('Frontend Controller', function () { done(); }); }); + + // Handle Edit append + it('will redirect to admin edit page for a normal post', function (done) { + var req = { + params: [undefined, mockPost.slug, 'edit'] + }, + res = { + render: sinon.spy(), + redirect: function(arg) { + res.render.called.should.be.false; + arg.should.eql(adminEditPagePath + mockPost.id + '/'); + done(); + } + }; + + frontend.single(req, res, null); + }); + + it('will NOT redirect to admin edit page for a normal post accessed as a date url', function (done) { + var req = { + params: ['2012/12/30/', mockPost.slug, 'edit'] + }, + res = { + render: sinon.spy(), + redirect: sinon.spy() + }; + + frontend.single(req, res, function () { + res.render.called.should.be.false; + res.redirect.called.should.be.false; + done(); + }); + }); + + it('will redirect to admin edit page for a static page accessed as a slug', function (done) { + var req = { + params: [undefined, mockStaticPost.slug, 'edit'] + }, + res = { + render: sinon.spy(), + redirect: function(arg) { + res.render.called.should.be.false; + arg.should.eql(adminEditPagePath + mockStaticPost.id + '/'); + done(); + } + }; + + frontend.single(req, res, null); + }); + + it('will NOT redirect to admin edit page for a static page accessed as a date url', function (done) { + var req = { + params: ['2012/12/30/', mockStaticPost.slug, 'edit'] + }, + res = { + render: sinon.spy(), + redirect: sinon.spy() + }; + + frontend.single(req, res, function () { + res.render.called.should.be.false; + res.redirect.called.should.be.false; + done(); + }); + }); }); describe('permalink set to date', function () { @@ -150,7 +219,7 @@ describe('Frontend Controller', function () { it('can render a static page', function (done) { var req = { - params: [undefined, 'test-static-page'] + params: [undefined, mockStaticPost.slug] }, res = { render: function (view, context) { @@ -178,9 +247,9 @@ describe('Frontend Controller', function () { }); it('can render a normal post', function (done) { - var date = moment().format('YYYY/MM/DD/'), + var date = moment(mockPost.published_at).format('YYYY/MM/DD/'), req = { - params: [date, 'test-normal-post'] + params: [date, mockPost.slug] }, res = { render: function (view, context) { @@ -195,9 +264,9 @@ describe('Frontend Controller', function () { }); it('will NOT render a normal post with the wrong date', function (done) { - var date = moment().subtract('days', 1).format('YYYY/MM/DD/'), + var date = moment(mockPost.published_at).subtract('days', 1).format('YYYY/MM/DD/'), req = { - params: [date, 'test-normal-post'] + params: [date, mockPost.slug] }, res = { render: sinon.spy() @@ -211,7 +280,7 @@ describe('Frontend Controller', function () { it('will NOT render a normal post accessed as a slug url', function (done) { var req = { - params: [undefined, 'test-normal-post'] + params: [undefined, mockPost.slug] }, res = { render: sinon.spy() @@ -222,6 +291,71 @@ describe('Frontend Controller', function () { done(); }); }); + + // Handle Edit append + it('will redirect to admin edit page for a normal post', function (done) { + var req = { + params: [moment(mockPost.published_at).format('YYYY/MM/DD/'), mockPost.slug, 'edit'] + }, + res = { + render: sinon.spy(), + redirect: function (arg) { + res.render.called.should.be.false; + arg.should.eql(adminEditPagePath + mockPost.id + '/'); + done(); + } + }; + + frontend.single(req, res, null); + }); + + it('will NOT redirect to admin edit page for a normal post accessed as a slug url', function (done) { + var req = { + params: [undefined, mockPost.slug, 'edit'] + }, + res = { + render: sinon.spy(), + redirect: sinon.spy() + }; + + frontend.single(req, res, function () { + res.render.called.should.be.false; + res.redirect.called.should.be.false; + done(); + }); + }); + + it('will redirect to admin edit page for a static page accessed as a slug url', function (done) { + var req = { + params: [undefined, mockStaticPost.slug, 'edit'] + }, + res = { + render: sinon.spy(), + redirect: function (arg) { + res.render.called.should.be.false; + arg.should.eql(adminEditPagePath + mockStaticPost.id + '/'); + done(); + } + }; + + frontend.single(req, res, null); + }); + + it('will NOT redirect to admin edit page for a static page accessed as a date url', function (done) { + var req = { + params: ['2012/12/30/', mockStaticPost.slug, 'edit'] + }, + res = { + render: sinon.spy(), + redirect: sinon.spy() + }; + + frontend.single(req, res, function () { + res.render.called.should.be.false; + res.redirect.called.should.be.false; + done(); + }); + }); }); }); }); \ No newline at end of file