Skip to content

Commit 83d047c

Browse files
committed
Add Customisable Permalinks
1 parent 548079c commit 83d047c

File tree

9 files changed

+51
-14
lines changed

9 files changed

+51
-14
lines changed

core/client/models/settings.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
//id:0 is used to issue PUT requests
55
Ghost.Models.Settings = Ghost.ProgressModel.extend({
66
url: Ghost.settings.apiRoot + '/settings/?type=blog,theme',
7-
id: '0'
7+
id: '0',
8+
parse: function (resp) {
9+
resp.permalinks = resp.permalinks === "/:slug/" ? "" : "1";
10+
return resp;
11+
}
812
});
913

10-
}());
14+
}());

core/client/tpl/settings/general.hbs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@
5454
<p>How many posts should be displayed on each page</p>
5555
</div>
5656

57+
<div class="form-group">
58+
<label for="permalinks">Dated Permalinks</label>
59+
<input id="permalinks" name="general[permalinks]" type="checkbox" value="1" {{#if permalinks}}checked{{/if}}>
60+
<label class="checkbox" for="permalinks"></label>
61+
<p>Include date in your post's links</p>
62+
</div>
63+
5764
<div class="form-group">
5865
<label for="activeTheme">Theme</label>
5966
<select id="activeTheme" name="general[activeTheme]">
@@ -66,4 +73,4 @@
6673

6774
</fieldset>
6875
</form>
69-
</section>
76+
</section>

core/client/views/settings.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@
157157
title = this.$('#blog-title').val(),
158158
description = this.$('#blog-description').val(),
159159
email = this.$('#email-address').val(),
160-
postsPerPage = this.$('#postsPerPage').val();
160+
postsPerPage = this.$('#postsPerPage').val(),
161+
permalinks = this.$('#permalinks').is(':checked') ? "/:year/:month/:day/:slug/" : "/:slug/";
161162

162163
Ghost.Validate._errors = [];
163164
Ghost.Validate
@@ -185,7 +186,8 @@
185186
description: description,
186187
email: email,
187188
postsPerPage: postsPerPage,
188-
activeTheme: this.$('#activeTheme').val()
189+
activeTheme: this.$('#activeTheme').val(),
190+
permalinks: permalinks
189191
}, {
190192
success: this.saveSuccess,
191193
error: this.saveError

core/server/controllers/frontend.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ frontendControllers = {
6565
});
6666
},
6767
'single': function (req, res, next) {
68-
api.posts.read({'slug': req.params.slug}).then(function (post) {
68+
api.posts.read(_.pick(req.params, ['id', 'slug'])).then(function (post) {
6969
if (post) {
7070
ghost.doFilter('prePostsRender', post).then(function (post) {
7171
var paths = ghost.paths().availableThemes[ghost.settings('activeTheme')];

core/server/data/default-settings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@
4444
"notNull": true,
4545
"isIn": ["true", "false"]
4646
}
47+
},
48+
"permalinks": {
49+
"defaultValue": "/:slug/",
50+
"validations": {
51+
"is": "^(/:?[a-z]+){1,}/$",
52+
"regex": "(:id|:slug)",
53+
"notContains": "/ghost/"
54+
}
4755
}
4856
},
4957
"theme": {

core/server/helpers/index.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,27 @@ coreHelpers.pageUrl = function (context, block) {
8080
// i.e. If inside a post context will return post permalink
8181
// absolute flag outputs absolute URL, else URL is relative
8282
coreHelpers.url = function (options) {
83-
var output = '';
83+
var output = '',
84+
self = this,
85+
tags = {
86+
year: function () { return self.created_at.getFullYear(); },
87+
month: function () { return self.created_at.getMonth() + 1; },
88+
day: function () { return self.created_at.getDate(); },
89+
slug: function () { return self.slug; },
90+
id: function () { return self.id; }
91+
};
8492

8593
if (options && options.hash.absolute) {
8694
output += coreHelpers.ghost.config().url;
8795
}
8896

8997
if (models.isPost(this)) {
90-
output += '/' + this.slug + '/';
98+
output += coreHelpers.ghost.settings('permalinks');
99+
output = output.replace(/(:[a-z]+)/g, function (match) {
100+
if (_.has(tags, match.substr(1))) {
101+
return tags[match.substr(1)]();
102+
}
103+
});
91104
}
92105

93106
return output;

core/server/routes/frontend.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
var frontend = require('../controllers/frontend');
1+
var frontend = require('../controllers/frontend'),
2+
Ghost = require('../../ghost'),
3+
4+
ghost = new Ghost();
25

36
module.exports = function (server) {
47
// ### Frontend routes
58
/* TODO: dynamic routing, homepage generator, filters ETC ETC */
69
server.get('/rss/', frontend.rss);
710
server.get('/rss/:page/', frontend.rss);
811
server.get('/page/:page/', frontend.homepage);
9-
server.get('/:slug/', frontend.single);
12+
server.get(ghost.settings('permalinks'), frontend.single);
1013
server.get('/', frontend.homepage);
11-
};
14+
};

core/test/unit/server_helpers_index_spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ describe('Core Helpers', function () {
277277
});
278278

279279
it('should return a the slug with a prefix slash if the context is a post', function () {
280-
var rendered = helpers.url.call({html: 'content', markdown: "ff", title: "title", slug: "slug"});
280+
var rendered = helpers.url.call({html: 'content', markdown: "ff", title: "title", slug: "slug", created_at: new Date(0)});
281281
should.exist(rendered);
282282
rendered.should.equal('/slug/');
283283
});
@@ -288,7 +288,7 @@ describe('Core Helpers', function () {
288288
}),
289289

290290
rendered = helpers.url.call(
291-
{html: 'content', markdown: "ff", title: "title", slug: "slug"},
291+
{html: 'content', markdown: "ff", title: "title", slug: "slug", created_at: new Date(0)},
292292
{hash: { absolute: 'true'}}
293293
);
294294

core/test/utils/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var _ = require('underscore'),
1010
'featured', 'image', 'status', 'language', 'author_id', 'created_at', 'created_by', 'updated_at', 'updated_by',
1111
'published_at', 'published_by', 'page', 'author', 'user', 'tags'],
1212
// TODO: remove databaseVersion
13-
settings: ['databaseVersion', 'title', 'description', 'email', 'logo', 'cover', 'defaultLang',
13+
settings: ['databaseVersion', 'title', 'description', 'email', 'logo', 'cover', 'defaultLang', "permalinks",
1414
'postsPerPage', 'forceI18n', 'activeTheme', 'activePlugins', 'installedPlugins', 'availableThemes'],
1515
tag: ['id', 'uuid', 'name', 'slug', 'description', 'parent_id',
1616
'meta_title', 'meta_description', 'created_at', 'created_by', 'updated_at', 'updated_by'],

0 commit comments

Comments
 (0)