Truncate slug values to 63 characters.

This commit is contained in:
Franz Diebold 2022-09-11 14:19:45 +00:00
parent 960f2f11d8
commit 9d7426b893
3 changed files with 25 additions and 12 deletions

View file

@ -4,17 +4,20 @@ const core = require('@actions/core');
const github = require('@actions/github');
/**
* Slugify a given string.
* Slugify a given string to a maximum length.
* @param {string} inputString
* @param {int} maxLength
* @return {string} The slugified string.
*/
function slugify(inputString) {
function slugify(inputString, maxLength = 63) {
return inputString
.toLowerCase()
.replace(/[^a-z0-9 -]/g, ' ') // remove invalid chars
.replace(/^\s+|\s+$/g, '') // trim
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes
.replace(/-+/g, '-') // collapse dashes
.slice(0, maxLength) // truncate to maximum length
.replace(/[-]+$/g, ''); // trim trailing -
}
/**