Skip to content

A lightweight utility to convert strings into URL-friendly slugs, with support for removing Vietnamese diacritics and handling special characters.

License

Notifications You must be signed in to change notification settings

vnodesign/slugify

Repository files navigation

@vnodesign/slugify

npm license funding

Description

@vnodesign/slugify is a lightweight and efficient utility for converting strings into clean, URL-friendly slugs. It is specially designed to handle Vietnamese diacritics and special characters, making it ideal for projects that require localization or SEO optimization.

Features

  • Vietnamese Diacritics Removal: Converts characters like , ế, , and đ into their base form (e.g., a, e, o, d).
  • Special Character Stripping: Removes all non-alphanumeric characters, leaving only letters, numbers, spaces, and hyphens.
  • Space-to-Hyphen Conversion: Converts spaces into hyphens to create slugs suitable for URLs.
  • Redundant Hyphen Handling: Removes consecutive or trailing hyphens for a clean result.
  • Case Insensitive: Converts all input to lowercase for uniformity.

Installation

Install the package via npm or pnpm:

npm install @vnodesign/slugify
# or
pnpm add @vnodesign/slugify

Usage

Here’s how to use @vnodesign/slugify in your project:

Example

import { slugify } from '@vnodesign/slugify'

const title = 'Chào bạn! Đây là bài viết của tôi.'
const slug = slugify(title)

console.log(slug) // Output: "chao-ban-day-la-bai-viet-cua-toi"

Input and Output Examples

Input Output
Chào bạn! chao-ban
Ngày hôm nay trời rất đẹp. ngay-hom-nay-troi-rat-dep
Hello @World!!! hello-world
Test--Case test-case

API

slugify(str: string): string

Converts a given string into a URL-friendly slug.

Parameters

  • str (string): The input string to be converted.

Returns

  • (string): The cleaned and formatted slug.

Testing

This package includes unit tests powered by Vitest.

Run tests with:

pnpm test

Development

Build

To build the package, run:

pnpm build

Lint

Lint the code using:

pnpm lint

Type Checking

Ensure all types are valid:

pnpm typecheck

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue on GitHub.

License

This project is licensed under the MIT License.

Funding

If you find this package helpful, consider sponsoring me on GitHub ❤️. Your support helps maintain and improve this project!

About

A lightweight utility to convert strings into URL-friendly slugs, with support for removing Vietnamese diacritics and handling special characters.

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published