@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.
- 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.
Install the package via npm or pnpm:
npm install @vnodesign/slugify
# or
pnpm add @vnodesign/slugifyHere’s how to use @vnodesign/slugify in your project:
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 | 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 |
Converts a given string into a URL-friendly slug.
str(string): The input string to be converted.
- (string): The cleaned and formatted slug.
This package includes unit tests powered by Vitest.
Run tests with:
pnpm testTo build the package, run:
pnpm buildLint the code using:
pnpm lintEnsure all types are valid:
pnpm typecheckContributions are welcome! Please feel free to submit a pull request or open an issue on GitHub.
This project is licensed under the MIT License.
If you find this package helpful, consider sponsoring me on GitHub ❤️. Your support helps maintain and improve this project!