Merge pull request #1408 from halfdan/1285-unidecode

Automatically replace unicode characters with ascii characters for slugs...
This commit is contained in:
Hannah Wolfe 2013-11-06 05:44:10 -08:00
commit ab878decef
3 changed files with 16 additions and 0 deletions

View File

@ -6,6 +6,7 @@ var ghostBookshelf,
uuid = require('node-uuid'),
config = require('../../../config'),
Validator = require('validator').Validator,
unidecode = require('unidecode'),
sanitize = require('validator').sanitize;
// Initializes a new Bookshelf instance, for reference elsewhere in Ghost.
@ -125,6 +126,8 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
// Remove trailing hyphen
slug = slug.charAt(slug.length - 1) === '-' ? slug.substr(0, slug.length - 1) : slug;
// Remove non ascii characters
slug = unidecode(slug);
// Check the filtered slug doesn't match any of the reserved keywords
slug = /^(ghost|ghost\-admin|admin|wp\-admin|wp\-login|dashboard|logout|login|signin|signup|signout|register|archive|archives|category|categories|tag|tags|page|pages|post|posts|user|users)$/g
.test(slug) ? slug + '-post' : slug;

View File

@ -228,6 +228,18 @@ describe('Post Model', function () {
}).then(null, done);
});
it('can generate slugs without non-ascii characters', function (done) {
var newPost = {
title: 'भुते धडकी भरवणारा आहेत',
markdown: 'Test Content 1'
};
PostModel.add(newPost).then(function (createdPost) {
createdPost.get('slug').should.equal('bhute-dhddkii-bhrvnnaaraa-aahet');
done();
})
});
it('detects duplicate slugs before saving', function (done) {
var firstPost = {
title: 'First post',

View File

@ -47,6 +47,7 @@
"showdown": "0.3.1",
"sqlite3": "2.1.19",
"underscore": "1.5.2",
"unidecode": "0.1.3",
"validator": "1.4.0",
"when": "2.5.1"
},