mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
Fix multiple title slug generation
Change slug generation to not append multiple hyphens
This commit is contained in:
parent
e765af4633
commit
53061a4c04
2 changed files with 39 additions and 11 deletions
|
@ -71,15 +71,26 @@ Post = GhostBookshelf.Model.extend({
|
|||
// 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) {
|
||||
var trimSpace;
|
||||
|
||||
if (!found) {
|
||||
return when.resolve(slugToFind);
|
||||
}
|
||||
|
||||
slugTryCount += 1;
|
||||
|
||||
// TODO: Bug out (when.reject) if over 10 tries or something?
|
||||
// If this is the first time through, add the hyphen
|
||||
if (slugTryCount === 2) {
|
||||
slugToFind += '-';
|
||||
} else {
|
||||
// Otherwise, trim the number off the end
|
||||
trimSpace = -(String(slugTryCount - 1).length);
|
||||
slugToFind = slugToFind.slice(0, trimSpace);
|
||||
}
|
||||
|
||||
return checkIfSlugExists(slugToFind + '-' + slugTryCount);
|
||||
slugToFind += slugTryCount;
|
||||
|
||||
return checkIfSlugExists(slugToFind);
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
/*globals describe, beforeEach, it */
|
||||
var _ = require("underscore"),
|
||||
when = require('when'),
|
||||
sequence = require('when/sequence'),
|
||||
should = require('should'),
|
||||
helpers = require('./helpers'),
|
||||
Models = require('../../server/models');
|
||||
|
@ -110,19 +112,34 @@ describe('Post Model', function () {
|
|||
content_raw: 'Test Content 1'
|
||||
};
|
||||
|
||||
PostModel.add(newPost).then(function (createdPost) {
|
||||
// Create 12 posts with the sametitle
|
||||
sequence(_.times(12, function (i) {
|
||||
return function () {
|
||||
return PostModel.add({
|
||||
title: "Test Title",
|
||||
content_raw: "Test Content " + (i+1)
|
||||
});
|
||||
};
|
||||
})).then(function (createdPosts) {
|
||||
// Should have created 12 posts
|
||||
createdPosts.length.should.equal(12);
|
||||
|
||||
createdPost.get('slug').should.equal('test-title');
|
||||
// Should have unique slugs and contents
|
||||
_(createdPosts).each(function (post, i) {
|
||||
var num = i + 1;
|
||||
|
||||
newPost.content_raw = 'Test Content 2';
|
||||
return PostModel.add(newPost);
|
||||
}).then(function (secondPost) {
|
||||
|
||||
secondPost.get('slug').should.equal('test-title-2');
|
||||
secondPost.get('content_raw').should.equal("Test Content 2");
|
||||
// First one has normal title
|
||||
if (num === 1) {
|
||||
post.get('slug').should.equal('test-title');
|
||||
return;
|
||||
}
|
||||
|
||||
post.get('slug').should.equal('test-title-' + num);
|
||||
post.get('content_raw').should.equal('Test Content ' + num);
|
||||
});
|
||||
|
||||
done();
|
||||
}).then(null, done);
|
||||
}).otherwise(done);
|
||||
});
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue