From b4bb97336f6cf140c928092816ab3880be5e5862 Mon Sep 17 00:00:00 2001 From: Andy Chosak Date: Fri, 2 Nov 2018 13:54:20 -0400 Subject: [PATCH] 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. --- .../static_src/wagtailadmin/js/page-editor.js | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/wagtail/admin/static_src/wagtailadmin/js/page-editor.js b/wagtail/admin/static_src/wagtailadmin/js/page-editor.js index dc42dc54b..d2913933f 100644 --- a/wagtail/admin/static_src/wagtailadmin/js/page-editor.js +++ b/wagtail/admin/static_src/wagtailadmin/js/page-editor.js @@ -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() {