mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-17 23:44:39 -05:00
Merge pull request #222 from jgable/slugIncrement
Increment slug if duplicate
This commit is contained in:
commit
9bb4ae62a6
4 changed files with 54 additions and 10 deletions
|
@ -20,7 +20,7 @@ errors = {
|
||||||
logError: function (err) {
|
logError: function (err) {
|
||||||
err = err || "Unknown";
|
err = err || "Unknown";
|
||||||
// TODO: Logging framework hookup
|
// TODO: Logging framework hookup
|
||||||
console.log("Error occurred: ", err.message || err);
|
console.log("Error occurred: ", err.message || err, err.stack || "");
|
||||||
},
|
},
|
||||||
|
|
||||||
logAndThrowError: function (err) {
|
logAndThrowError: function (err) {
|
||||||
|
|
|
@ -45,10 +45,7 @@ Post = GhostBookshelf.Model.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
creating: function () {
|
creating: function () {
|
||||||
if (!this.get('slug')) {
|
var self = this;
|
||||||
this.generateSlug();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!this.get('created_by')) {
|
if (!this.get('created_by')) {
|
||||||
this.set('created_by', 1);
|
this.set('created_by', 1);
|
||||||
}
|
}
|
||||||
|
@ -56,20 +53,46 @@ Post = GhostBookshelf.Model.extend({
|
||||||
if (!this.get('author_id')) {
|
if (!this.get('author_id')) {
|
||||||
this.set('author_id', 1);
|
this.set('author_id', 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!this.get('slug')) {
|
||||||
|
// Generating a slug requires a db call to look for conflicting slugs
|
||||||
|
return this.generateSlug(this.get('title'))
|
||||||
|
.then(function (slug) {
|
||||||
|
self.set({slug: slug});
|
||||||
|
});
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// #### generateSlug
|
// #### generateSlug
|
||||||
// Create a string act as the permalink for a post.
|
// Create a string act as the permalink for a post.
|
||||||
generateSlug: function () {
|
generateSlug: function (title) {
|
||||||
|
var slug,
|
||||||
|
slugTryCount = 1,
|
||||||
|
// Look for a post with a matching slug, append an incrementing number if so
|
||||||
|
checkIfSlugExists = function (slugToFind) {
|
||||||
|
return Post.read({slug: slugToFind}).then(function (found) {
|
||||||
|
if (!found) {
|
||||||
|
return when.resolve(slugToFind);
|
||||||
|
}
|
||||||
|
|
||||||
|
slugTryCount += 1;
|
||||||
|
|
||||||
|
// TODO: Bug out (when.reject) if over 10 tries or something?
|
||||||
|
|
||||||
|
return checkIfSlugExists(slugToFind + slugTryCount);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// Remove reserved chars: `:/?#[]@!$&'()*+,;=` as well as `\` and convert spaces to hyphens
|
// Remove reserved chars: `:/?#[]@!$&'()*+,;=` as well as `\` and convert spaces to hyphens
|
||||||
var slug = this.get('title').replace(/[:\/\?#\[\]@!$&'()*+,;=\\]/g, '').replace(/\s/g, '-').toLowerCase();
|
slug = title.replace(/[:\/\?#\[\]@!$&'()*+,;=\\]/g, '').replace(/\s/g, '-').toLowerCase();
|
||||||
// Remove trailing hypen
|
// Remove trailing hypen
|
||||||
slug = slug.charAt(slug.length - 1) === '-' ? slug.substr(0, slug.length - 2) : slug;
|
slug = slug.charAt(slug.length - 1) === '-' ? slug.substr(0, slug.length - 2) : slug;
|
||||||
// Check the filtered slug doesn't match any of the reserved keywords
|
// 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
|
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;
|
.test(slug) ? slug + '-post' : slug;
|
||||||
// TODO: test for duplicate slugs and increment
|
|
||||||
return this.set('slug', slug);
|
// Test for duplicate slugs.
|
||||||
|
return checkIfSlugExists(slug);
|
||||||
},
|
},
|
||||||
|
|
||||||
user: function () {
|
user: function () {
|
||||||
|
|
|
@ -100,6 +100,27 @@ describe('Post Model', function () {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can generate a non conflicting slug', function (done) {
|
||||||
|
var newPost = {
|
||||||
|
title: 'Test Title',
|
||||||
|
content: 'Test Content 1'
|
||||||
|
};
|
||||||
|
|
||||||
|
PostModel.add(newPost).then(function (createdPost) {
|
||||||
|
|
||||||
|
createdPost.get('slug').should.equal('test-title');
|
||||||
|
|
||||||
|
newPost.content = 'Test Content 2';
|
||||||
|
return PostModel.add(newPost);
|
||||||
|
}).then(function (secondPost) {
|
||||||
|
|
||||||
|
secondPost.get('slug').should.equal('test-title2');
|
||||||
|
secondPost.get('content').should.equal("Test Content 2");
|
||||||
|
|
||||||
|
done();
|
||||||
|
}).otherwise(done);
|
||||||
|
});
|
||||||
|
|
||||||
it('can delete', function (done) {
|
it('can delete', function (done) {
|
||||||
var firstPostId;
|
var firstPostId;
|
||||||
PostModel.browse().then(function (results) {
|
PostModel.browse().then(function (results) {
|
||||||
|
|
|
@ -20,7 +20,7 @@ describe('permissions', function () {
|
||||||
return when(helpers.insertDefaultUser());
|
return when(helpers.insertDefaultUser());
|
||||||
}).then(function (results) {
|
}).then(function (results) {
|
||||||
done();
|
done();
|
||||||
});
|
}).otherwise(errors.logAndThrowError);
|
||||||
});
|
});
|
||||||
|
|
||||||
// beforeEach(function (done) {
|
// beforeEach(function (done) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue