Skip to content

Commit

Permalink
Pre & Next Links for Ghost_Head
Browse files Browse the repository at this point in the history
closes TryGhost#685
- Now that we have a ‘pagination’ meta object, we can implement
SEO-friendly `next` and `prev` ref links in `<head>`.
- This implementation works uniformly for anything that supports
pagination in the current schema (posts, tags, authors)
- Regex should make the implementation future-proof for additional
pagination
  • Loading branch information
felixrieseberg committed Aug 20, 2014
1 parent b92f201 commit 15e4dd1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
18 changes: 17 additions & 1 deletion core/server/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,9 @@ coreHelpers.ghost_head = function (options) {
blog = config.theme(),
head = [],
majorMinor = /^(\d+\.)?(\d+)/,
trimmedVersion = this.version;
trimmedVersion = this.version,
trimmedUrlpattern = /.+(?=\/page\/\d*\/)/,
trimmedUrl, next, prev;

trimmedVersion = trimmedVersion ? trimmedVersion.match(majorMinor)[0] : '?';

Expand All @@ -502,6 +504,20 @@ coreHelpers.ghost_head = function (options) {
return coreHelpers.url.call(self, {hash: {absolute: true}}).then(function (url) {
head.push('<link rel="canonical" href="' + url + '" />');

if (self.pagination) {
trimmedUrl = self.relativeUrl.match(trimmedUrlpattern);
if (self.pagination.prev) {
prev = (self.pagination.prev > 1 ? prev = '/page/' + self.pagination.prev + '/' : prev = '/');
prev = (trimmedUrl) ? '/' + trimmedUrl + prev : prev;
head.push('<link rel="prev" href="' + config.urlFor({relativeUrl: prev}, true) + '" />');
}
if (self.pagination.next) {
next = '/page/' + self.pagination.next + '/';
next = (trimmedUrl) ? '/' + trimmedUrl + next : next;
head.push('<link rel="next" href="' + config.urlFor({relativeUrl: next}, true) + '" />');
}
}

return filters.doFilter('ghost_head', head);
}).then(function (head) {
var headString = _.reduce(head, function (memo, item) { return memo + '\n' + item; }, '');
Expand Down
26 changes: 26 additions & 0 deletions core/test/unit/server_helpers_index_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,32 @@ describe('Core Helpers', function () {
done();
}).catch(done);
});

it('returns next & prev URL correctly for middle page', function (done) {
configUpdate({url: 'http://testurl.com'});
helpers.ghost_head.call({version: '0.3.0', relativeUrl: '/page/3/', pagination: {next: '4', prev: '2'}}).then(function (rendered) {
should.exist(rendered);
rendered.string.should.equal('<meta name="generator" content="Ghost 0.3" />\n' +
'<link rel="alternate" type="application/rss+xml" title="Ghost" href="/rss/">\n' +
'<link rel="canonical" href="http://testurl.com/page/3/" />\n' +
'<link rel="prev" href="http://testurl.com/page/2/" />\n' +
'<link rel="next" href="http://testurl.com/page/4/" />');
done();
}).catch(done);
});

it('returns next & prev URL correctly for second page', function (done) {
configUpdate({url: 'http://testurl.com'});
helpers.ghost_head.call({version: '0.3.0', relativeUrl: '/page/2/', pagination: {next: '3', prev: '1'}}).then(function (rendered) {
should.exist(rendered);
rendered.string.should.equal('<meta name="generator" content="Ghost 0.3" />\n' +
'<link rel="alternate" type="application/rss+xml" title="Ghost" href="/rss/">\n' +
'<link rel="canonical" href="http://testurl.com/page/2/" />\n' +
'<link rel="prev" href="http://testurl.com/" />\n' +
'<link rel="next" href="http://testurl.com/page/3/" />');
done();
}).catch(done);
});
});

describe('ghost_foot Helper', function () {
Expand Down

0 comments on commit 15e4dd1

Please sign in to comment.