Skip to content

Commit

Permalink
Merge pull request #393 from LLoyderino/fix/slug-autogeneration
Browse files Browse the repository at this point in the history
Monkey-patch cleanForSlug
  • Loading branch information
DiogoMarques29 authored Aug 25, 2023
2 parents c805fc5 + 1adb015 commit 5ddde55
Showing 1 changed file with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$(document).ready(function () {
isLive = $('body').hasClass('page-is-live');
if(!wagtailModelTranslations.translate_slugs) {
if (!wagtailModelTranslations.translate_slugs) {
lang_code = wagtailModelTranslations.defaultLanguage.replace("-", "_");
title_selector = '#id_title_' + lang_code;
slug_selector = '#id_slug';
Expand All @@ -19,6 +19,49 @@ $(document).ready(function () {
}
});

/**
* Returns the supplied string as a slug optionally using the vendor URLify util.
* If not using URLify it will read the global unicodeSlugsEnabled and return a slugified string.
*
* HACK this function has been removed from Wagtail 5+, we restore it temporarily
* to allow for slug generation to work as before, but it should be replaced
* with the current slug generation system in use by Wagtail
*
* @param {string} val - value to be parsed into a slug
* @param {boolean} useURLify - if true, the vendor URLify will be used
* @returns {string}
*/
function cleanForSlug(
val,
useURLify,
{ unicodeSlugsEnabled = window.unicodeSlugsEnabled } = {}
) {
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
const cleaned = window.URLify(val, 255);

// 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();
}

return val
.replace(/\s+/g, '-')
.replace(/[^A-Za-z0-9\-_]/g, '')
.toLowerCase();
}

function slugAutoPopulateTranslation(title_selector, slug_selector) {
var slugFollowsTitle = false;

Expand Down

0 comments on commit 5ddde55

Please sign in to comment.