Add Customisable Permalinks

This commit is contained in:
Jakob Gillich 2013-11-21 19:44:18 +01:00
parent 548079ca94
commit 83d047c0ba
9 changed files with 51 additions and 14 deletions

View File

@ -4,7 +4,11 @@
//id:0 is used to issue PUT requests //id:0 is used to issue PUT requests
Ghost.Models.Settings = Ghost.ProgressModel.extend({ Ghost.Models.Settings = Ghost.ProgressModel.extend({
url: Ghost.settings.apiRoot + '/settings/?type=blog,theme', url: Ghost.settings.apiRoot + '/settings/?type=blog,theme',
id: '0' id: '0',
parse: function (resp) {
resp.permalinks = resp.permalinks === "/:slug/" ? "" : "1";
return resp;
}
}); });
}()); }());

View File

@ -54,6 +54,13 @@
<p>How many posts should be displayed on each page</p> <p>How many posts should be displayed on each page</p>
</div> </div>
<div class="form-group">
<label for="permalinks">Dated Permalinks</label>
<input id="permalinks" name="general[permalinks]" type="checkbox" value="1" {{#if permalinks}}checked{{/if}}>
<label class="checkbox" for="permalinks"></label>
<p>Include date in your post's links</p>
</div>
<div class="form-group"> <div class="form-group">
<label for="activeTheme">Theme</label> <label for="activeTheme">Theme</label>
<select id="activeTheme" name="general[activeTheme]"> <select id="activeTheme" name="general[activeTheme]">
@ -66,4 +73,4 @@
</fieldset> </fieldset>
</form> </form>
</section> </section>

View File

@ -157,7 +157,8 @@
title = this.$('#blog-title').val(), title = this.$('#blog-title').val(),
description = this.$('#blog-description').val(), description = this.$('#blog-description').val(),
email = this.$('#email-address').val(), email = this.$('#email-address').val(),
postsPerPage = this.$('#postsPerPage').val(); postsPerPage = this.$('#postsPerPage').val(),
permalinks = this.$('#permalinks').is(':checked') ? "/:year/:month/:day/:slug/" : "/:slug/";
Ghost.Validate._errors = []; Ghost.Validate._errors = [];
Ghost.Validate Ghost.Validate
@ -185,7 +186,8 @@
description: description, description: description,
email: email, email: email,
postsPerPage: postsPerPage, postsPerPage: postsPerPage,
activeTheme: this.$('#activeTheme').val() activeTheme: this.$('#activeTheme').val(),
permalinks: permalinks
}, { }, {
success: this.saveSuccess, success: this.saveSuccess,
error: this.saveError error: this.saveError

View File

@ -65,7 +65,7 @@ frontendControllers = {
}); });
}, },
'single': function (req, res, next) { 'single': function (req, res, next) {
api.posts.read({'slug': req.params.slug}).then(function (post) { api.posts.read(_.pick(req.params, ['id', 'slug'])).then(function (post) {
if (post) { if (post) {
ghost.doFilter('prePostsRender', post).then(function (post) { ghost.doFilter('prePostsRender', post).then(function (post) {
var paths = ghost.paths().availableThemes[ghost.settings('activeTheme')]; var paths = ghost.paths().availableThemes[ghost.settings('activeTheme')];

View File

@ -44,6 +44,14 @@
"notNull": true, "notNull": true,
"isIn": ["true", "false"] "isIn": ["true", "false"]
} }
},
"permalinks": {
"defaultValue": "/:slug/",
"validations": {
"is": "^(/:?[a-z]+){1,}/$",
"regex": "(:id|:slug)",
"notContains": "/ghost/"
}
} }
}, },
"theme": { "theme": {

View File

@ -80,14 +80,27 @@ coreHelpers.pageUrl = function (context, block) {
// i.e. If inside a post context will return post permalink // i.e. If inside a post context will return post permalink
// absolute flag outputs absolute URL, else URL is relative // absolute flag outputs absolute URL, else URL is relative
coreHelpers.url = function (options) { coreHelpers.url = function (options) {
var output = ''; var output = '',
self = this,
tags = {
year: function () { return self.created_at.getFullYear(); },
month: function () { return self.created_at.getMonth() + 1; },
day: function () { return self.created_at.getDate(); },
slug: function () { return self.slug; },
id: function () { return self.id; }
};
if (options && options.hash.absolute) { if (options && options.hash.absolute) {
output += coreHelpers.ghost.config().url; output += coreHelpers.ghost.config().url;
} }
if (models.isPost(this)) { if (models.isPost(this)) {
output += '/' + this.slug + '/'; output += coreHelpers.ghost.settings('permalinks');
output = output.replace(/(:[a-z]+)/g, function (match) {
if (_.has(tags, match.substr(1))) {
return tags[match.substr(1)]();
}
});
} }
return output; return output;

View File

@ -1,4 +1,7 @@
var frontend = require('../controllers/frontend'); var frontend = require('../controllers/frontend'),
Ghost = require('../../ghost'),
ghost = new Ghost();
module.exports = function (server) { module.exports = function (server) {
// ### Frontend routes // ### Frontend routes
@ -6,6 +9,6 @@ module.exports = function (server) {
server.get('/rss/', frontend.rss); server.get('/rss/', frontend.rss);
server.get('/rss/:page/', frontend.rss); server.get('/rss/:page/', frontend.rss);
server.get('/page/:page/', frontend.homepage); server.get('/page/:page/', frontend.homepage);
server.get('/:slug/', frontend.single); server.get(ghost.settings('permalinks'), frontend.single);
server.get('/', frontend.homepage); server.get('/', frontend.homepage);
}; };

View File

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

View File

@ -10,7 +10,7 @@ var _ = require('underscore'),
'featured', 'image', 'status', 'language', 'author_id', 'created_at', 'created_by', 'updated_at', 'updated_by', 'featured', 'image', 'status', 'language', 'author_id', 'created_at', 'created_by', 'updated_at', 'updated_by',
'published_at', 'published_by', 'page', 'author', 'user', 'tags'], 'published_at', 'published_by', 'page', 'author', 'user', 'tags'],
// TODO: remove databaseVersion // TODO: remove databaseVersion
settings: ['databaseVersion', 'title', 'description', 'email', 'logo', 'cover', 'defaultLang', settings: ['databaseVersion', 'title', 'description', 'email', 'logo', 'cover', 'defaultLang', "permalinks",
'postsPerPage', 'forceI18n', 'activeTheme', 'activePlugins', 'installedPlugins', 'availableThemes'], 'postsPerPage', 'forceI18n', 'activeTheme', 'activePlugins', 'installedPlugins', 'availableThemes'],
tag: ['id', 'uuid', 'name', 'slug', 'description', 'parent_id', tag: ['id', 'uuid', 'name', 'slug', 'description', 'parent_id',
'meta_title', 'meta_description', 'created_at', 'created_by', 'updated_at', 'updated_by'], 'meta_title', 'meta_description', 'created_at', 'created_by', 'updated_at', 'updated_by'],