2013-05-11 20:44:25 +04:00
|
|
|
/**
|
|
|
|
* Main controller for Ghost frontend
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*global require, module */
|
|
|
|
|
2014-01-01 19:27:39 +04:00
|
|
|
var moment = require('moment'),
|
|
|
|
RSS = require('rss'),
|
2014-02-05 12:40:30 +04:00
|
|
|
_ = require('lodash'),
|
2014-01-01 19:27:39 +04:00
|
|
|
url = require('url'),
|
|
|
|
when = require('when'),
|
2014-02-02 09:29:07 +04:00
|
|
|
Route = require('express').Route,
|
2014-01-01 19:27:39 +04:00
|
|
|
|
|
|
|
api = require('../api'),
|
|
|
|
config = require('../config'),
|
|
|
|
filters = require('../../server/filters'),
|
2014-02-23 06:16:07 +04:00
|
|
|
template = require('../helpers/template'),
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2014-02-02 09:29:07 +04:00
|
|
|
frontendControllers,
|
|
|
|
// Cache static post permalink regex
|
|
|
|
staticPostPermalink = new Route(null, '/:slug/:edit?');
|
|
|
|
|
2014-02-13 07:26:56 +04:00
|
|
|
function getPostPage(options) {
|
|
|
|
return api.settings.read('postsPerPage').then(function (postPP) {
|
|
|
|
var postsPerPage = parseInt(postPP.value, 10);
|
|
|
|
|
|
|
|
// No negative posts per page, must be number
|
|
|
|
if (!isNaN(postsPerPage) && postsPerPage > 0) {
|
|
|
|
options.limit = postsPerPage;
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.posts.browse(options);
|
|
|
|
}).then(function (page) {
|
|
|
|
|
|
|
|
// A bit of a hack for situations with no content.
|
|
|
|
if (page.pages === 0) {
|
|
|
|
page.pages = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return page;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatPageResponse(posts, page) {
|
|
|
|
return {
|
|
|
|
posts: posts,
|
|
|
|
pagination: {
|
|
|
|
page: page.page,
|
|
|
|
prev: page.prev,
|
|
|
|
next: page.next,
|
|
|
|
limit: page.limit,
|
|
|
|
total: page.total,
|
|
|
|
pages: page.pages
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleError(next) {
|
|
|
|
return function (err) {
|
|
|
|
var e = new Error(err.message);
|
2014-02-26 21:51:01 +04:00
|
|
|
e.status = err.code;
|
2014-02-13 07:26:56 +04:00
|
|
|
return next(e);
|
|
|
|
};
|
|
|
|
}
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
frontendControllers = {
|
2013-09-17 04:54:36 +04:00
|
|
|
'homepage': function (req, res, next) {
|
2013-12-06 12:51:35 +04:00
|
|
|
// Parse the page number
|
|
|
|
var pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1,
|
2014-02-13 07:26:56 +04:00
|
|
|
options = {
|
|
|
|
page: pageParam
|
|
|
|
};
|
2013-08-20 06:18:39 +04:00
|
|
|
|
2014-01-03 04:37:21 +04:00
|
|
|
// No negative pages, or page 1
|
|
|
|
if (isNaN(pageParam) || pageParam < 1 || (pageParam === 1 && req.route.path === '/page/:page/')) {
|
2014-01-05 10:40:53 +04:00
|
|
|
return res.redirect(config().paths.subdir + '/');
|
2014-01-03 04:37:21 +04:00
|
|
|
}
|
|
|
|
|
2014-02-13 07:26:56 +04:00
|
|
|
return getPostPage(options).then(function (page) {
|
2013-09-06 06:09:47 +04:00
|
|
|
|
2014-02-13 07:26:56 +04:00
|
|
|
// If page is greater than number of pages we have, redirect to last page
|
|
|
|
if (pageParam > page.pages) {
|
|
|
|
return res.redirect(page.pages === 1 ? config().paths.subdir + '/' : (config().paths.subdir + '/page/' + page.pages + '/'));
|
2013-12-06 12:51:35 +04:00
|
|
|
}
|
2014-02-13 07:26:56 +04:00
|
|
|
|
|
|
|
// Render the page of posts
|
|
|
|
filters.doFilter('prePostsRender', page.posts).then(function (posts) {
|
|
|
|
res.render('index', formatPageResponse(posts, page));
|
|
|
|
});
|
|
|
|
}).otherwise(handleError(next));
|
|
|
|
},
|
|
|
|
'tag': function (req, res, next) {
|
|
|
|
// Parse the page number
|
|
|
|
var pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1,
|
|
|
|
options = {
|
|
|
|
page: pageParam,
|
|
|
|
tag: req.params.slug
|
|
|
|
};
|
|
|
|
|
|
|
|
// Get url for tag page
|
|
|
|
function tagUrl(tag, page) {
|
|
|
|
var url = config().paths.subdir + '/tag/' + tag + '/';
|
|
|
|
|
|
|
|
if (page && page > 1) {
|
|
|
|
url += 'page/' + page + '/';
|
2013-08-21 18:05:17 +04:00
|
|
|
}
|
|
|
|
|
2014-02-13 07:26:56 +04:00
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No negative pages, or page 1
|
|
|
|
if (isNaN(pageParam) || pageParam < 1 || (req.params.page !== undefined && pageParam === 1)) {
|
|
|
|
return res.redirect(tagUrl(options.tag));
|
|
|
|
}
|
|
|
|
|
|
|
|
return getPostPage(options).then(function (page) {
|
|
|
|
|
2013-08-18 21:41:55 +04:00
|
|
|
// If page is greater than number of pages we have, redirect to last page
|
2014-02-13 07:26:56 +04:00
|
|
|
if (pageParam > page.pages) {
|
|
|
|
return res.redirect(tagUrl(options.tag, page.pages));
|
2013-08-18 21:41:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Render the page of posts
|
2013-11-28 06:45:01 +04:00
|
|
|
filters.doFilter('prePostsRender', page.posts).then(function (posts) {
|
2014-02-13 07:26:56 +04:00
|
|
|
api.settings.read('activeTheme').then(function (activeTheme) {
|
|
|
|
var paths = config().paths.availableThemes[activeTheme.value],
|
2014-03-03 09:41:37 +04:00
|
|
|
view = paths.hasOwnProperty('tag.hbs') ? 'tag' : 'index',
|
2014-02-13 07:26:56 +04:00
|
|
|
|
|
|
|
// Format data for template
|
|
|
|
response = _.extend(formatPageResponse(posts, page), {
|
|
|
|
tag: page.aspect.tag
|
|
|
|
});
|
|
|
|
|
|
|
|
res.render(view, response);
|
|
|
|
});
|
2013-05-11 20:44:25 +04:00
|
|
|
});
|
2014-02-13 07:26:56 +04:00
|
|
|
}).otherwise(handleError(next));
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
2013-09-17 04:54:36 +04:00
|
|
|
'single': function (req, res, next) {
|
2014-02-02 09:29:07 +04:00
|
|
|
var path = req.path,
|
|
|
|
params,
|
|
|
|
editFormat,
|
|
|
|
usingStaticPermalink = false;
|
|
|
|
|
|
|
|
api.settings.read('permalinks').then(function (permalink) {
|
|
|
|
editFormat = permalink.value[permalink.value.length - 1] === '/' ? ':edit?' : '/:edit?';
|
|
|
|
|
|
|
|
// Convert saved permalink into an express Route object
|
|
|
|
permalink = new Route(null, permalink.value + editFormat);
|
|
|
|
|
|
|
|
// Check if the path matches the permalink structure.
|
|
|
|
//
|
|
|
|
// If there are no matches found we then
|
|
|
|
// need to verify it's not a static post,
|
|
|
|
// and test against that permalink structure.
|
|
|
|
if (permalink.match(path) === false) {
|
|
|
|
// If there are still no matches then return.
|
|
|
|
if (staticPostPermalink.match(path) === false) {
|
|
|
|
// Throw specific error
|
|
|
|
// to break out of the promise chain.
|
|
|
|
throw new Error('no match');
|
|
|
|
}
|
|
|
|
|
|
|
|
permalink = staticPostPermalink;
|
|
|
|
usingStaticPermalink = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
params = permalink.params;
|
|
|
|
|
|
|
|
// Sanitize params we're going to use to lookup the post.
|
|
|
|
var postLookup = _.pick(permalink.params, 'slug', 'id');
|
|
|
|
|
|
|
|
// Query database to find post
|
|
|
|
return api.posts.read(postLookup);
|
|
|
|
}).then(function (post) {
|
|
|
|
|
|
|
|
if (!post) {
|
|
|
|
return next();
|
|
|
|
}
|
2013-12-30 11:03:29 +04:00
|
|
|
|
|
|
|
function render() {
|
2014-01-20 01:08:39 +04:00
|
|
|
// If we're ready to render the page but the last param is 'edit' then we'll send you to the edit page.
|
2014-02-02 09:29:07 +04:00
|
|
|
if (params.edit !== undefined) {
|
2014-01-05 10:40:53 +04:00
|
|
|
return res.redirect(config().paths.subdir + '/ghost/editor/' + post.id + '/');
|
2014-01-03 08:32:31 +04:00
|
|
|
}
|
2013-11-28 06:45:01 +04:00
|
|
|
filters.doFilter('prePostsRender', post).then(function (post) {
|
2013-12-06 12:51:35 +04:00
|
|
|
api.settings.read('activeTheme').then(function (activeTheme) {
|
2014-01-05 10:40:53 +04:00
|
|
|
var paths = config().paths.availableThemes[activeTheme.value],
|
2014-02-23 06:16:07 +04:00
|
|
|
view = template.getThemeViewForPost(paths, post);
|
|
|
|
|
2013-12-30 11:03:29 +04:00
|
|
|
res.render(view, {post: post});
|
2013-12-06 12:51:35 +04:00
|
|
|
});
|
2013-09-17 04:54:36 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-02-02 09:29:07 +04:00
|
|
|
// If we've checked the path with the static permalink structure
|
|
|
|
// then the post must be a static post.
|
|
|
|
// If it is not then we must return.
|
|
|
|
if (usingStaticPermalink) {
|
|
|
|
if (post.page === 1) {
|
|
|
|
return render();
|
|
|
|
}
|
2013-12-30 11:03:29 +04:00
|
|
|
|
2014-01-01 19:27:39 +04:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2014-02-02 09:29:07 +04:00
|
|
|
// If there is any date based paramter in the slug
|
|
|
|
// we will check it against the post published date
|
|
|
|
// to verify it's correct.
|
|
|
|
if (params.year || params.month || params.day) {
|
|
|
|
var slugDate = [],
|
|
|
|
slugFormat = [];
|
|
|
|
|
|
|
|
if (params.year) {
|
|
|
|
slugDate.push(params.year);
|
|
|
|
slugFormat.push('YYYY');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params.month) {
|
|
|
|
slugDate.push(params.month);
|
|
|
|
slugFormat.push('MM');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params.day) {
|
|
|
|
slugDate.push(params.day);
|
|
|
|
slugFormat.push('DD');
|
|
|
|
}
|
2013-12-30 11:03:29 +04:00
|
|
|
|
2014-02-02 09:29:07 +04:00
|
|
|
slugDate = slugDate.join('/');
|
|
|
|
slugFormat = slugFormat.join('/');
|
|
|
|
|
|
|
|
if (slugDate === moment(post.published_at).format(slugFormat)) {
|
2013-12-30 11:03:29 +04:00
|
|
|
return render();
|
|
|
|
}
|
|
|
|
|
2014-02-02 09:29:07 +04:00
|
|
|
return next();
|
|
|
|
}
|
2013-12-30 11:03:29 +04:00
|
|
|
|
2014-02-02 09:29:07 +04:00
|
|
|
render();
|
2013-12-30 11:03:29 +04:00
|
|
|
|
2013-09-17 04:54:36 +04:00
|
|
|
}).otherwise(function (err) {
|
2014-02-02 09:29:07 +04:00
|
|
|
// If we've thrown an error message
|
|
|
|
// of 'no match' then we found
|
|
|
|
// no path match.
|
|
|
|
if (err.message === 'no match') {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2014-02-13 07:26:56 +04:00
|
|
|
return handleError(next)(err);
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-08-28 02:31:43 +04:00
|
|
|
},
|
2013-09-17 04:54:36 +04:00
|
|
|
'rss': function (req, res, next) {
|
2013-08-28 02:31:43 +04:00
|
|
|
// Initialize RSS
|
2014-01-03 04:37:21 +04:00
|
|
|
var pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1,
|
2014-03-06 02:03:39 +04:00
|
|
|
tagParam = req.params.slug;
|
2014-01-03 04:37:21 +04:00
|
|
|
|
|
|
|
// No negative pages, or page 1
|
2014-03-06 02:03:39 +04:00
|
|
|
if (isNaN(pageParam) || pageParam < 1 ||
|
|
|
|
(pageParam === 1 && (req.route.path === '/rss/:page/' || req.route.path === '/tag/:slug/rss/:page/'))) {
|
|
|
|
if (tagParam !== undefined) {
|
|
|
|
return res.redirect(config().paths.subdir + '/tag/' + tagParam + '/rss/');
|
|
|
|
} else {
|
|
|
|
return res.redirect(config().paths.subdir + '/rss/');
|
|
|
|
}
|
2014-01-03 04:37:21 +04:00
|
|
|
}
|
|
|
|
|
2014-01-20 01:08:39 +04:00
|
|
|
// TODO: needs refactor for multi user to not use first user as default
|
2014-01-03 04:37:21 +04:00
|
|
|
return when.settle([
|
2013-12-06 12:51:35 +04:00
|
|
|
api.users.read({id : 1}),
|
|
|
|
api.settings.read('title'),
|
2014-01-03 04:37:21 +04:00
|
|
|
api.settings.read('description'),
|
|
|
|
api.settings.read('permalinks')
|
2013-12-30 05:03:30 +04:00
|
|
|
]).then(function (result) {
|
2013-08-28 02:31:43 +04:00
|
|
|
|
2014-03-06 02:03:39 +04:00
|
|
|
var options = {};
|
|
|
|
if (pageParam) { options.page = pageParam; }
|
|
|
|
if (tagParam) { options.tag = tagParam; }
|
|
|
|
|
|
|
|
return api.posts.browse(options).then(function (page) {
|
|
|
|
|
|
|
|
var user = result[0].value,
|
|
|
|
title = result[1].value.value,
|
|
|
|
description = result[2].value.value,
|
|
|
|
permalinks = result[3].value,
|
|
|
|
siteUrl = config.urlFor('home', null, true),
|
|
|
|
feedUrl = config.urlFor('rss', null, true),
|
|
|
|
maxPage = page.pages,
|
|
|
|
feedItems = [],
|
|
|
|
feed;
|
|
|
|
|
|
|
|
if (tagParam) {
|
|
|
|
title = page.aspect.tag.name + ' - ' + title;
|
|
|
|
feedUrl = feedUrl + 'tag/' + page.aspect.tag.slug + '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
feed = new RSS({
|
|
|
|
title: title,
|
|
|
|
description: description,
|
|
|
|
generator: 'Ghost v' + res.locals.version,
|
|
|
|
feed_url: feedUrl,
|
|
|
|
site_url: siteUrl,
|
|
|
|
ttl: '60'
|
|
|
|
});
|
|
|
|
|
2013-09-16 15:11:17 +04:00
|
|
|
|
|
|
|
// A bit of a hack for situations with no content.
|
|
|
|
if (maxPage === 0) {
|
|
|
|
maxPage = 1;
|
|
|
|
page.pages = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If page is greater than number of pages we have, redirect to last page
|
|
|
|
if (pageParam > maxPage) {
|
2014-03-06 02:03:39 +04:00
|
|
|
if (tagParam) {
|
|
|
|
return res.redirect(config().paths.subdir + '/tag/' + tagParam + '/rss/' + maxPage + '/');
|
|
|
|
} else {
|
|
|
|
return res.redirect(config().paths.subdir + '/rss/' + maxPage + '/');
|
|
|
|
}
|
2013-09-16 15:11:17 +04:00
|
|
|
}
|
|
|
|
|
2013-11-28 06:45:01 +04:00
|
|
|
filters.doFilter('prePostsRender', page.posts).then(function (posts) {
|
2013-09-16 15:11:17 +04:00
|
|
|
posts.forEach(function (post) {
|
2014-01-03 04:37:21 +04:00
|
|
|
var deferred = when.defer(),
|
|
|
|
item = {
|
2014-03-10 19:42:38 +04:00
|
|
|
title: post.title,
|
2014-01-03 04:37:21 +04:00
|
|
|
guid: post.uuid,
|
2014-01-05 10:40:53 +04:00
|
|
|
url: config.urlFor('post', {post: post, permalinks: permalinks}, true),
|
2014-01-03 04:37:21 +04:00
|
|
|
date: post.published_at,
|
2014-02-05 09:59:07 +04:00
|
|
|
categories: _.pluck(post.tags, 'name'),
|
|
|
|
author: user ? user.name : null
|
2014-01-03 04:37:21 +04:00
|
|
|
},
|
|
|
|
content = post.html;
|
|
|
|
|
|
|
|
//set img src to absolute url
|
|
|
|
content = content.replace(/src=["|'|\s]?([\w\/\?\$\.\+\-;%:@&=,_]+)["|'|\s]?/gi, function (match, p1) {
|
|
|
|
/*jslint unparam:true*/
|
|
|
|
p1 = url.resolve(siteUrl, p1);
|
|
|
|
return "src='" + p1 + "' ";
|
|
|
|
});
|
|
|
|
//set a href to absolute url
|
|
|
|
content = content.replace(/href=["|'|\s]?([\w\/\?\$\.\+\-;%:@&=,_]+)["|'|\s]?/gi, function (match, p1) {
|
|
|
|
/*jslint unparam:true*/
|
|
|
|
p1 = url.resolve(siteUrl, p1);
|
|
|
|
return "href='" + p1 + "' ";
|
2013-09-26 16:52:53 +04:00
|
|
|
});
|
2014-01-03 04:37:21 +04:00
|
|
|
item.description = content;
|
|
|
|
feed.item(item);
|
|
|
|
deferred.resolve();
|
2013-12-29 04:15:14 +04:00
|
|
|
feedItems.push(deferred.promise);
|
2013-09-16 15:11:17 +04:00
|
|
|
});
|
2013-12-29 04:15:14 +04:00
|
|
|
});
|
2014-01-03 04:37:21 +04:00
|
|
|
|
2013-12-29 04:15:14 +04:00
|
|
|
when.all(feedItems).then(function () {
|
2013-09-16 15:11:17 +04:00
|
|
|
res.set('Content-Type', 'text/xml');
|
|
|
|
res.send(feed.xml());
|
2013-08-28 02:31:43 +04:00
|
|
|
});
|
|
|
|
});
|
2014-02-13 07:26:56 +04:00
|
|
|
}).otherwise(handleError(next));
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
|
|
|
};
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-10-18 21:18:49 +04:00
|
|
|
module.exports = frontendControllers;
|