Skip to content

Commit

Permalink
Add post preview via uuid (/p/:uuid)
Browse files Browse the repository at this point in the history
Refs TryGhost#5097

- All drafts will show a preview link (this needs real css)
- Published posts will redirect
- Probably should go behind labs?
- Powered by ~10 pints between the two of us (@ErisDS, @novaugust)
  • Loading branch information
novaugust committed Apr 28, 2015
1 parent 18638d3 commit cf77b30
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 24 deletions.
7 changes: 7 additions & 0 deletions core/client/app/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ var Post = DS.Model.extend(NProgressSaveMixin, ValidationEngine, {
return this.get('ghostPaths.url').join(blogUrl, postUrl);
}),

previewUrl: Ember.computed('uuid', 'ghostPaths.url', 'config.blogUrl', function () {
var blogUrl = this.get('config.blogUrl'),
uuid = this.get('uuid'),
previewKeyword = this.get('config.routeKeywords.preview');
return this.get('ghostPaths.url').join(blogUrl, previewKeyword, uuid);
}),

scratch: null,
titleScratch: null,

Expand Down
7 changes: 7 additions & 0 deletions core/client/app/styles/layouts/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,13 @@ body.zen {
}
}//.post-settings-menu

.post-preview-link {
position: absolute;
top: 0;
right: 0;
font-size: 1.3rem;
}


//
// Post Settings Menu meta Data
Expand Down
5 changes: 5 additions & 0 deletions core/client/app/templates/post-settings-menu.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
<form>
<div class="form-group">
<label for="url">Post URL</label>
{{#if model.isDraft}}
<a class="post-preview-link" target="_blank" href="{{model.previewUrl}}">
Preview<span class="icon-external"></span>
</a>
{{/if}}
<span class="input-icon icon-link">
{{gh-input class="post-setting-slug" id="url" value=slugValue name="post-setting-slug" focus-out="updateSlug" selectOnClick="true" stopEnterKeyDownPropagation="true"}}
</span>
Expand Down
2 changes: 2 additions & 0 deletions core/client/app/utils/config-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var isNumeric = function (num) {
return false;
} else if (isNumeric(val)) {
return +val;
} else if (val.indexOf('{') === 0) {
return JSON.parse(val);
} else {
return val;
}
Expand Down
3 changes: 2 additions & 1 deletion core/server/api/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function getValidKeys() {
database: config.database.client,
mail: _.isObject(config.mail) ? config.mail.transport : '',
blogUrl: config.url.replace(/\/$/, ''),
blogTitle: config.theme.title
blogTitle: config.theme.title,
routeKeywords: JSON.stringify(config.routeKeywords)
};

return validKeys;
Expand Down
4 changes: 2 additions & 2 deletions core/server/api/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ posts = {
* @return {Promise(Post)} Post
*/
read: function read(options) {
var attrs = ['id', 'slug', 'status'],
var attrs = ['id', 'slug', 'status', 'uuid'],
data = _.pick(options, attrs);

options = _.omit(options, attrs);

// only published posts if no user is present
if (!(options.context && options.context.user)) {
if (!data.uuid && !(options.context && options.context.user)) {
data.status = 'published';
}

Expand Down
3 changes: 2 additions & 1 deletion core/server/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ ConfigManager.prototype.set = function (config) {
routeKeywords: {
tag: 'tag',
author: 'author',
page: 'page'
page: 'page',
preview: 'p'
},
slugs: {
// Used by generateSlug to generate slugs for posts, tags, users, ..
Expand Down
66 changes: 56 additions & 10 deletions core/server/controllers/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ function getActiveThemePaths() {
});
}

/*
* Sets the response context around a post and renders it
* with the current theme's post view. Used by post preview
* and single post methods.
* Returns a function that takes the post to be rendered.
*/
function renderPost(req, res) {
return function (post) {
return getActiveThemePaths().then(function (paths) {
var view = template.getThemeViewForPost(paths, post),
response = formatResponse(post);

setResponseContext(req, res, response);

res.render(view, response);
});
};
}

frontendControllers = {
homepage: function (req, res, next) {
// Parse the page number
Expand Down Expand Up @@ -271,6 +290,41 @@ frontendControllers = {
}).catch(handleError(next));
},

preview: function (req, res, next) {
var params = {
uuid: req.params.uuid,
status: 'all',
include: 'author,tags,fields'
};

// Query database to find post
api.posts.read(params).then(function (result) {
var post = result.posts[0];

if (!post) {
return next();
}

if (post.status === 'published') {
return res.redirect(301, config.urlFor('post', {post: post}));
}

setReqCtx(req, post);

filters.doFilter('prePostsRender', post, res.locals)
.then(renderPost(req, res, post));
}).catch(function (err) {
// If we've thrown an error message
// of type: 'NotFound' then we found
// no path match.
if (err.type === 'NotFoundError') {
return next();
}

return handleError(next)(err);
});
},

single: function (req, res, next) {
var path = req.path,
params,
Expand Down Expand Up @@ -336,16 +390,8 @@ frontendControllers = {

setReqCtx(req, post);

filters.doFilter('prePostsRender', post, res.locals).then(function (post) {
getActiveThemePaths().then(function (paths) {
var view = template.getThemeViewForPost(paths, post),
response = formatResponse(post);

setResponseContext(req, res, response);

res.render(view, response);
});
});
filters.doFilter('prePostsRender', post, res.locals)
.then(renderPost(req, res, post));
}

// If we've checked the path with the static permalink structure
Expand Down
24 changes: 14 additions & 10 deletions core/server/routes/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ var frontend = require('../controllers/frontend'),

frontendRoutes = function () {
var router = express.Router(),
subdir = config.paths.subdir;
subdir = config.paths.subdir,
routeKeywords = config.routeKeywords;

// ### Admin routes
router.get(/^\/(logout|signout)\/$/, function redirect(req, res) {
Expand Down Expand Up @@ -37,19 +38,22 @@ frontendRoutes = function () {
});

// Tags
router.get('/' + config.routeKeywords.tag + '/:slug/rss/', frontend.rss);
router.get('/' + config.routeKeywords.tag + '/:slug/rss/:page/', frontend.rss);
router.get('/' + config.routeKeywords.tag + '/:slug/' + config.routeKeywords.page + '/:page/', frontend.tag);
router.get('/' + config.routeKeywords.tag + '/:slug/', frontend.tag);
router.get('/' + routeKeywords.tag + '/:slug/rss/', frontend.rss);
router.get('/' + routeKeywords.tag + '/:slug/rss/:page/', frontend.rss);
router.get('/' + routeKeywords.tag + '/:slug/' + routeKeywords.page + '/:page/', frontend.tag);
router.get('/' + routeKeywords.tag + '/:slug/', frontend.tag);

// Authors
router.get('/' + config.routeKeywords.author + '/:slug/rss/', frontend.rss);
router.get('/' + config.routeKeywords.author + '/:slug/rss/:page/', frontend.rss);
router.get('/' + config.routeKeywords.author + '/:slug/' + config.routeKeywords.page + '/:page/', frontend.author);
router.get('/' + config.routeKeywords.author + '/:slug/', frontend.author);
router.get('/' + routeKeywords.author + '/:slug/rss/', frontend.rss);
router.get('/' + routeKeywords.author + '/:slug/rss/:page/', frontend.rss);
router.get('/' + routeKeywords.author + '/:slug/' + routeKeywords.page + '/:page/', frontend.author);
router.get('/' + routeKeywords.author + '/:slug/', frontend.author);

// Post Live Preview
router.get('/' + routeKeywords.preview + '/:uuid', frontend.preview);

// Default
router.get('/' + config.routeKeywords.page + '/:page/', frontend.homepage);
router.get('/' + routeKeywords.page + '/:page/', frontend.homepage);
router.get('/', frontend.homepage);
router.get('*', frontend.single);

Expand Down

0 comments on commit cf77b30

Please sign in to comment.