mirror of
https://github.com/rlespinasse/slugify-value.git
synced 2026-05-18 10:31:31 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f1c7463d0d |
4
.github/workflows/slugify-value.yaml
vendored
4
.github/workflows/slugify-value.yaml
vendored
@@ -79,8 +79,8 @@ jobs:
|
||||
- name: Validate // Slugify with another max length
|
||||
run: |
|
||||
[[ "${{ env.ANOTHER_MAX_LENGTH }}" == "Never gonna give you up Never gonna let you down" ]]
|
||||
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG }}" == "never-gonna-give-you-up-" ]]
|
||||
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG_CS }}" == "Never-gonna-give-you-up-" ]]
|
||||
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG }}" == "never-gonna-give-you-up" ]]
|
||||
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG_CS }}" == "Never-gonna-give-you-up" ]]
|
||||
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG_URL }}" == "never-gonna-give-you-up" ]]
|
||||
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG_URL_CS }}" == "Never-gonna-give-you-up" ]]
|
||||
shell: bash
|
||||
|
||||
@@ -8,8 +8,9 @@ Produce some `slug`-ed environment variables based on the input one.
|
||||
|
||||
- put the variable content in lower case
|
||||
- replace any character by `-` except `0-9`, `a-z`, `.`, and `_`
|
||||
- remove leading and trailing `-` character
|
||||
- remove leading `-` character
|
||||
- limit the string size to 63 characters
|
||||
- remove trailing `-` character
|
||||
|
||||
- `<env name>_SLUG_CS`
|
||||
|
||||
@@ -18,7 +19,6 @@ Produce some `slug`-ed environment variables based on the input one.
|
||||
- `<env name>_SLUG_URL` (or `<env name>_SLUG_URL_CS`)
|
||||
|
||||
- like `<env name>_SLUG` (or `<env name>_SLUG_CS`) with the `.`, and `_` characters also replaced by `-`
|
||||
- will not end with `-`
|
||||
|
||||
## Usage
|
||||
|
||||
|
||||
23
slugify.sh
23
slugify.sh
@@ -30,31 +30,26 @@ fi
|
||||
slug() {
|
||||
# 1st : Remove refs prefix
|
||||
# 2d : Replace unwanted characters
|
||||
# 3d : Remove leading dashes
|
||||
# 4d : Remove trailing dashes
|
||||
output=$(sed -E 's#refs/[^\/]*/##;s/[^a-zA-Z0-9._-]+/-/g;s/^-*//;s/-*$//' <<<"$1")
|
||||
reduce "$output" false
|
||||
# 3d : Remove leading hypens
|
||||
output=$(sed -E 's#refs/[^\/]*/##;s/[^a-zA-Z0-9._-]+/-/g;s/^-*//' <<<"$1")
|
||||
reduce "$output"
|
||||
}
|
||||
|
||||
slug_url() {
|
||||
# 1st : Remove refs prefix
|
||||
# 2d : Replace unwanted characters
|
||||
# 3d : Remove leading dashes
|
||||
# 4d : Remove trailing dashes
|
||||
output=$(sed -E 's#refs/[^\/]*/##;s/[^a-zA-Z0-9-]+/-/g;s/^-*//;s/-*$//' <<<"$1")
|
||||
reduce "$output" true
|
||||
# 3d : Remove leading hypens
|
||||
output=$(sed -E 's#refs/[^\/]*/##;s/[^a-zA-Z0-9-]+/-/g;s/^-*//' <<<"$1")
|
||||
reduce "$output"
|
||||
}
|
||||
|
||||
reduce() {
|
||||
reduced_value="$1"
|
||||
remove_ending_hypen="$2"
|
||||
if [ "${MAX_LENGTH}" != "nolimit" ]; then
|
||||
reduced_value=$(cut -c1-"${MAX_LENGTH}" <<<"$1")
|
||||
reduced_value=$(cut -c1-"${MAX_LENGTH}" <<<"$reduced_value")
|
||||
fi
|
||||
if [ "$remove_ending_hypen" == "true" ]; then
|
||||
reduced_value=$(sed -E 's/-*$//' <<<"$reduced_value")
|
||||
fi
|
||||
echo "$reduced_value"
|
||||
# 1st : Remove trailing hypens
|
||||
sed -E 's/-*$//' <<<"$reduced_value"
|
||||
}
|
||||
|
||||
SLUG_VALUE=$(slug "$VALUE")
|
||||
|
||||
Reference in New Issue
Block a user