mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-28 14:03:48 +03:00
Customise slug base name for different models
fixes #2480 - when the santisation of a slug results in an empty string, we use a basename instead. - this was 'post' and is now the table name for the model, minus the trailing 's' - this isn't massively robust but works as a temporary fix
This commit is contained in:
parent
f981e71e3c
commit
a0dff19b7a
@ -199,36 +199,39 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
||||
generateSlug: function (Model, base, readOptions) {
|
||||
var slug,
|
||||
slugTryCount = 1,
|
||||
baseName = Model.prototype.tableName.replace(/s$/, ''),
|
||||
// Look for a post with a matching slug, append an incrementing number if so
|
||||
checkIfSlugExists = function (slugToFind) {
|
||||
var args = {slug: slugToFind};
|
||||
//status is needed for posts
|
||||
if (readOptions && readOptions.status) {
|
||||
args.status = readOptions.status;
|
||||
checkIfSlugExists;
|
||||
|
||||
checkIfSlugExists = function (slugToFind) {
|
||||
var args = {slug: slugToFind};
|
||||
//status is needed for posts
|
||||
if (readOptions && readOptions.status) {
|
||||
args.status = readOptions.status;
|
||||
}
|
||||
return Model.findOne(args, readOptions).then(function (found) {
|
||||
var trimSpace;
|
||||
|
||||
if (!found) {
|
||||
return when.resolve(slugToFind);
|
||||
}
|
||||
return Model.findOne(args, readOptions).then(function (found) {
|
||||
var trimSpace;
|
||||
|
||||
if (!found) {
|
||||
return when.resolve(slugToFind);
|
||||
}
|
||||
slugTryCount += 1;
|
||||
|
||||
slugTryCount += 1;
|
||||
// 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);
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
slugToFind += slugTryCount;
|
||||
|
||||
slugToFind += slugTryCount;
|
||||
|
||||
return checkIfSlugExists(slugToFind);
|
||||
});
|
||||
};
|
||||
return checkIfSlugExists(slugToFind);
|
||||
});
|
||||
};
|
||||
|
||||
slug = base.trim();
|
||||
|
||||
@ -249,11 +252,11 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
||||
|
||||
// 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|rss)$/g
|
||||
.test(slug) ? slug + '-post' : slug;
|
||||
.test(slug) ? slug + '-' + baseName : slug;
|
||||
|
||||
//if slug is empty after trimming use "post"
|
||||
if (!slug) {
|
||||
slug = 'post';
|
||||
slug = baseName;
|
||||
}
|
||||
// Test for duplicate slugs.
|
||||
return checkIfSlugExists(slug);
|
||||
|
Loading…
Reference in New Issue
Block a user