allow pages to have stopword-only slugs

This change modifies the cleanForSlug function used when creating a
page's slug from its title. The current behavior uses the Django URLify
function, which removes stopwords like "before". If a page title
consists only of such stopwords, the generated slug will be blank, thus
confusingly preventing page save.

This change handles this case by falling back to an alternate slug
generation approach that allows the stopwords to be used.

This does unfortunately introduce some potentially inconsistent
behavior; for example, a page titled "Before me" will be given a slug of
"me" and a page titled "Before" will be given a slug of "before".
(Honestly, the inclusion of "before" as a stopword is somewhat
unexpected.)

Fixes #4881.
This commit is contained in:
Andy Chosak 2018-11-02 13:54:20 -04:00 committed by Matt Westcott
parent b81e5c6b85
commit b4bb97336f

View file

@ -241,15 +241,21 @@ function cleanForSlug(val, useURLify) {
if (useURLify) {
// URLify performs extra processing on the string (e.g. removing stopwords) and is more suitable
// for creating a slug from the title, rather than sanitising a slug entered manually
return URLify(val, 255, unicodeSlugsEnabled);
} else {
// just do the "replace"
if (unicodeSlugsEnabled) {
return val.replace(/\s/g, '-').replace(/[&\/\\#,+()$~%.'":`@\^!*?<>{}]/g, '').toLowerCase();
} else {
return val.replace(/\s/g, '-').replace(/[^A-Za-z0-9\-\_]/g, '').toLowerCase();
let cleaned = URLify(val, 255, unicodeSlugsEnabled);
// if the result is blank (e.g. because the title consisted entirely of stopwords),
// fall through to the non-URLify method
if (cleaned) {
return cleaned;
}
}
// just do the "replace"
if (unicodeSlugsEnabled) {
return val.replace(/\s/g, '-').replace(/[&\/\\#,+()$~%.'":`@\^!*?<>{}]/g, '').toLowerCase();
} else {
return val.replace(/\s/g, '-').replace(/[^A-Za-z0-9\-\_]/g, '').toLowerCase();
}
}
function initSlugAutoPopulate() {