0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Removing multiple hyphens

Closes #228. Also deals with the fact that if there's a trailing hyphen, it no longer removes one extra character.
This commit is contained in:
Gabor Javorszky 2013-07-04 20:53:45 +01:00 committed by Hannah Wolfe
parent d8d88f40cd
commit a6b3851491
2 changed files with 5 additions and 35 deletions

View file

@ -85,30 +85,13 @@ Post = GhostBookshelf.Model.extend({
};
// Remove reserved chars: `:/?#[]@!$&'()*+,;=` as well as `\` and convert spaces to hyphens
slug = title.replace(/[:\/\?#\[\]@!$&'()*+,;=\\]/g, '').replace(/\s/g, '-').toLowerCase();
// Remove trailing hyphen
slug = slug.charAt(slug.length - 1) === '-' ? slug.substr(0, slug.length - 2) : slug;
slug = title.replace(/[:\/\?#\[\]@!$&'()*+,;=\\]/g, '').replace(/(\s|\.)/g, '-').replace(/-+/g, '-').toLowerCase();
// Remove trailing hypen
slug = slug.charAt(slug.length - 1) === '-' ? slug.substr(0, slug.length - 1) : slug;
// Check the filtered slug doesn't match any of the reserved keywords
slug = /^(ghost|ghost\-admin|admin|wp\-admin|dashboard|login|archive|archives|category|categories|tag|tags|page|pages|post|posts)$/g
.test(slug) ? slug + '-post' : slug;
// Replace multiple hyphens and anything with less than two letters with one hyphen
shortSlug = _.filter(slug.split('-'), function (part) {
return part.length > 2;
}).join('-');
// If we've gotten rid of everything, come back through with lower threshold
if (shortSlug.length < 1) {
shortSlug = _.filter(slug.split('-'), function (part) {
return part.length > 1;
}).join('-');
// If we've still got no slug, default to just 'post'
if (shortSlug.length < 1) {
shortSlug = "post";
}
}
// Test for duplicate slugs.
return checkIfSlugExists(shortSlug);
},
@ -264,4 +247,4 @@ Posts = GhostBookshelf.Collection.extend({
module.exports = {
Post: Post,
Posts: Posts
};
};

View file

@ -121,19 +121,6 @@ describe('Post Model', function () {
}).otherwise(done);
});
it('can generate slugs without 2 letter words', function (done) {
var newPost = {
title: 'I am an idiot',
content: 'Test Content 1'
};
PostModel.add(newPost).then(function (createdPost) {
createdPost.get('slug').should.equal('idiot');
done();
});
});
it('can generate slugs without duplicate hyphens', function (done) {
var newPost = {
@ -212,4 +199,4 @@ describe('Post Model', function () {
done();
}).then(null, done);
});
});
});