mirror of
https://github.com/rlespinasse/slugify-value.git
synced 2026-06-14 02:21:23 +00:00
fix(slug_url): remove ending hypen if any
This commit is contained in:
committed by
GitHub
parent
930d4521cc
commit
4e258f56ef
6
.github/workflows/slugify-value.yaml
vendored
6
.github/workflows/slugify-value.yaml
vendored
@@ -75,12 +75,12 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
key: ANOTHER_MAX_LENGTH
|
key: ANOTHER_MAX_LENGTH
|
||||||
value: "Never gonna give you up Never gonna let you down"
|
value: "Never gonna give you up Never gonna let you down"
|
||||||
slug-maxlength: 23
|
slug-maxlength: 24
|
||||||
- name: Validate // Slugify with another max length
|
- name: Validate // Slugify with another max length
|
||||||
run: |
|
run: |
|
||||||
[[ "${{ env.ANOTHER_MAX_LENGTH }}" == "Never gonna give you up Never gonna let you down" ]]
|
[[ "${{ 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 }}" == "never-gonna-give-you-up-" ]]
|
||||||
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG_CS }}" == "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 }}" == "never-gonna-give-you-up" ]]
|
||||||
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG_URL_CS }}" == "Never-gonna-give-you-up" ]]
|
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG_URL_CS }}" == "Never-gonna-give-you-up" ]]
|
||||||
shell: bash
|
shell: bash
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ Produce some `slug`-ed environment variables based on the input one.
|
|||||||
- `<env name>_SLUG_URL` (or `<env name>_SLUG_URL_CS`)
|
- `<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 `-`
|
- like `<env name>_SLUG` (or `<env name>_SLUG_CS`) with the `.`, and `_` characters also replaced by `-`
|
||||||
|
- will not end with `-`
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
|||||||
16
slugify.sh
16
slugify.sh
@@ -33,7 +33,7 @@ slug() {
|
|||||||
# 3d : Remove leading dashes
|
# 3d : Remove leading dashes
|
||||||
# 4d : Remove trailing dashes
|
# 4d : Remove trailing dashes
|
||||||
output=$(sed -E 's#refs/[^\/]*/##;s/[^a-zA-Z0-9._-]+/-/g;s/^-*//;s/-*$//' <<<"$1")
|
output=$(sed -E 's#refs/[^\/]*/##;s/[^a-zA-Z0-9._-]+/-/g;s/^-*//;s/-*$//' <<<"$1")
|
||||||
reduce "$output"
|
reduce "$output" false
|
||||||
}
|
}
|
||||||
|
|
||||||
slug_url() {
|
slug_url() {
|
||||||
@@ -42,15 +42,19 @@ slug_url() {
|
|||||||
# 3d : Remove leading dashes
|
# 3d : Remove leading dashes
|
||||||
# 4d : Remove trailing dashes
|
# 4d : Remove trailing dashes
|
||||||
output=$(sed -E 's#refs/[^\/]*/##;s/[^a-zA-Z0-9-]+/-/g;s/^-*//;s/-*$//' <<<"$1")
|
output=$(sed -E 's#refs/[^\/]*/##;s/[^a-zA-Z0-9-]+/-/g;s/^-*//;s/-*$//' <<<"$1")
|
||||||
reduce "$output"
|
reduce "$output" true
|
||||||
}
|
}
|
||||||
|
|
||||||
reduce() {
|
reduce() {
|
||||||
if [ "${MAX_LENGTH}" == "nolimit" ]; then
|
reduced_value="$1"
|
||||||
echo "$1"
|
remove_ending_hypen="$2"
|
||||||
else
|
if [ "${MAX_LENGTH}" != "nolimit" ]; then
|
||||||
cut -c1-"${MAX_LENGTH}" <<<"$1"
|
reduced_value=$(cut -c1-"${MAX_LENGTH}" <<<"$1")
|
||||||
fi
|
fi
|
||||||
|
if [ "$remove_ending_hypen" == "true" ]; then
|
||||||
|
reduced_value=$(sed -E 's/-*$//' <<<"$reduced_value")
|
||||||
|
fi
|
||||||
|
echo "$reduced_value"
|
||||||
}
|
}
|
||||||
|
|
||||||
SLUG_VALUE=$(slug "$VALUE")
|
SLUG_VALUE=$(slug "$VALUE")
|
||||||
|
|||||||
Reference in New Issue
Block a user