Skip to content

Commit

Permalink
Fix automatic slug update on sbs form
Browse files Browse the repository at this point in the history
  • Loading branch information
lunars97 committed Jun 11, 2024
1 parent 234e040 commit cd23bd9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion integreat_cms/cms/templates/pages/page_sbs.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ <h1 class="heading">
{{ page_translation_form.instance.base_link }}
</label>
{% translate " Leave blank to generate unique permalink from title" as slug_placeholder %}
{% render_field page_translation_form.slug placeholder=slug_placeholder %}
{% render_field page_translation_form.slug placeholder=slug_placeholder id="target_slug-link" %}
</div>
<label for="{{ page_translation_form.title.id_for_label }}">
{{ page_translation_form.title.label }}
Expand Down
18 changes: 18 additions & 0 deletions integreat_cms/static/src/js/forms/update-permalink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,22 @@ window.addEventListener("load", () => {
slugField.value = currentSlug;
toggleSlugMode();
});

// This is used to slugify the title of the slug in target language
const slugifyStr = (str: string) => {
let trimmedStr = str.replace(/^\s+|\s+$/g, ""); // trim leading/trailing white space
let loweredCaseStr = trimmedStr.toLowerCase(); // convert string to lowercase
let slugStr = loweredCaseStr
.replace(/[^a-z0-9 -]/g, "") // remove any non-alphanumeric characters
.replace(/\s+/g, "-") // replace spaces with hyphens
.replace(/-+/g, "-"); // remove consecutive hyphens
return slugStr;
};
document.getElementById("target_translation_title")?.addEventListener("focusout", () => {
const targetLinkElement = document.getElementById("target_slug-link") as HTMLInputElement;
const targetTitleElement = (document.getElementById("target_translation_title") as HTMLInputElement).value
.trim()
.replace(/\s+/g, " ");
targetLinkElement.value = slugifyStr(targetTitleElement);
});
});

0 comments on commit cd23bd9

Please sign in to comment.