Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fixed the slug generator to not allow single character slugs #21261

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion ghost/core/core/server/models/base/plugins/generate-slug.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ module.exports = function (Bookshelf) {
// If it's a user, let's try to cut it down (unless this is a human request)
if (baseName === 'user' && options && options.shortSlug && slugTryCount === 1 && slug !== 'ghost-owner') {
longSlug = slug;
slug = (slug.indexOf('-') > -1) ? slug.slice(0, slug.indexOf('-')) : slug;
// find the index of the first hyphen after the second character.
const hyphenIndex = slug.indexOf('-', 3);

slug = hyphenIndex > -1 ? slug.slice(0, hyphenIndex) : slug;
}

if (!_.has(options, 'importing') || !options.importing) {
Expand All @@ -90,6 +93,11 @@ module.exports = function (Bookshelf) {
}
}

// single character slugs break things. Don't let that happen.
if (slug.length === 1) {
slug = baseName + '-' + slug;
}

// Some keywords cannot be changed
slug = _.includes(urlUtils.getProtectedSlugs(), slug) ? slug + '-' + baseName : slug;

Expand Down
6 changes: 3 additions & 3 deletions ghost/core/test/regression/models/model_posts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1897,9 +1897,9 @@ describe('Post Model', function () {

updatedPost.tags.should.have.lengthOf(3);

updatedPost.tags[0].should.have.properties({name: 'C', slug: 'c'});
updatedPost.tags[1].should.have.properties({name: 'C++', slug: 'c-2'});
updatedPost.tags[2].should.have.properties({name: 'C#', slug: 'c-3'});
updatedPost.tags[0].should.have.properties({name: 'C', slug: 'tag-c'});
updatedPost.tags[1].should.have.properties({name: 'C++', slug: 'tag-c-2'});
updatedPost.tags[2].should.have.properties({name: 'C#', slug: 'tag-c-3'});
});
});

Expand Down
Loading