felix-berlin / astro-breadcrumbs

Well configurable breadcrumb component for Astro.js. Create breadcrumbs completely dynamically or specify exactly how they should look.
https://docs.astro-breadcrumbs.kasimir.dev
GNU General Public License v3.0
65 stars 7 forks source link

Format the text links #188

Closed mpepito13 closed 6 months ago

mpepito13 commented 7 months ago

Is your feature request related to a problem? Please describe. I'd like the text of the links to be formatted without the "-" hyphen.

Describe the solution you'd like You should be able to replace the hyphen with a space and decide which kind of formatted text you'd like.

Describe alternatives you've considered

export function unslugify(slug: string, {
    separator = "-",
    replaceWith = " ",
    format = "none",
} = {}): string | undefined {
    // Throw an error if the slug parameter is not a valid non-null string
    if (!slug || typeof slug !== "string") {
        throw new Error("Slug must be a non-null string");
    }

    // First remove extra separator
    slug = slug.replace(new RegExp(`[${separator}]+`, "g"), separator);

    // Split the slug on the separator,
    // replace the separator with the desired string then trim unecessary whitespace
    const unslugified = slug.split(separator).join(replaceWith).trim();

    switch (format) {
        case "none":
            return unslugified;

        case "lowercase":
            return unslugified.toLowerCase();

        case "uppercase":
            return unslugified.toUpperCase();

        case "capitalize":
            return unslugified.replace(/\w\S*/g, (txt) => {
                return txt.charAt(0).toUpperCase() + txt.substring(1).toLowerCase();
            });

        case "capitalize-first":
            return unslugified.charAt(0).toUpperCase() + unslugified.substring(1).toLowerCase();

        default:
            return unslugified
    }
}

Additional context I'm no professional developer so you should check the code but it seems pretty straightforward.