mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
Add RSS Feed
Issue #366 - adds node-rss dependency - adds /rss/ and /rss/:page/ routes which return XML RSS 2.0 feed
This commit is contained in:
parent
012ee8358e
commit
8030946095
3 changed files with 59 additions and 1 deletions
|
@ -6,6 +6,7 @@
|
|||
|
||||
var Ghost = require('../../ghost'),
|
||||
api = require('../api'),
|
||||
RSS = require('rss'),
|
||||
|
||||
ghost = new Ghost(),
|
||||
frontendControllers;
|
||||
|
@ -46,6 +47,60 @@ frontendControllers = {
|
|||
res.render('post', {post: post});
|
||||
});
|
||||
});
|
||||
},
|
||||
'rss': function (req, res) {
|
||||
// Initialize RSS
|
||||
var feed = new RSS({
|
||||
title: ghost.settings().title,
|
||||
description: ghost.settings().description,
|
||||
generator: 'Ghost v' + ghost.settings().currentVersion,
|
||||
author: ghost.settings().author,
|
||||
feed_url: ghost.settings().url + '/rss/',
|
||||
site_url: ghost.settings().url,
|
||||
ttl: '60'
|
||||
}),
|
||||
|
||||
// Parse the page number
|
||||
pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1;
|
||||
|
||||
// No negative pages
|
||||
if (pageParam < 1) {
|
||||
return res.redirect("/rss/1/");
|
||||
}
|
||||
|
||||
api.posts.browse({page: pageParam}).then(function (page) {
|
||||
var maxPage = page.pages;
|
||||
|
||||
// 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) {
|
||||
return res.redirect("/rss/" + maxPage + "/");
|
||||
}
|
||||
|
||||
ghost.doFilter('prePostsRender', page.posts, function (posts) {
|
||||
posts.forEach(function (post) {
|
||||
var item = {
|
||||
title: post.title,
|
||||
guid: post.uuid,
|
||||
url: ghost.settings().url + '/' + post.slug + '/',
|
||||
date: post.published_at
|
||||
};
|
||||
|
||||
if (post.meta_description !== null) {
|
||||
item.push({ description: post.meta_description });
|
||||
}
|
||||
|
||||
feed.item(item);
|
||||
});
|
||||
res.set('Content-Type', 'text/xml');
|
||||
res.send(feed.xml());
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
|
2
index.js
2
index.js
|
@ -238,6 +238,8 @@ when.all([ghost.init(), filters.loadCoreFilters(ghost), helpers.loadCoreHelpers(
|
|||
|
||||
// ### Frontend routes
|
||||
/* TODO: dynamic routing, homepage generator, filters ETC ETC */
|
||||
ghost.app().get('/rss/', frontend.rss);
|
||||
ghost.app().get('/rss/:page/', frontend.rss);
|
||||
ghost.app().get('/:slug', frontend.single);
|
||||
ghost.app().get('/', frontend.homepage);
|
||||
ghost.app().get('/page/:page/', frontend.homepage);
|
||||
|
|
|
@ -27,7 +27,8 @@
|
|||
"semver": "2.1.0",
|
||||
"fs-extra": "0.6.3",
|
||||
"downsize": "0.0.2",
|
||||
"validator": "1.4.0"
|
||||
"validator": "1.4.0",
|
||||
"rss": "0.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"grunt": "~0.4.1",
|
||||
|
|
Loading…
Add table
Reference in a new issue