https://github.com/chenqianhe/slugify
A simple TypeScript library for converting strings into URL-friendly slugs.
https://github.com/chenqianhe/slugify
Last synced: 3 months ago
JSON representation
A simple TypeScript library for converting strings into URL-friendly slugs.
- Host: GitHub
- URL: https://github.com/chenqianhe/slugify
- Owner: chenqianhe
- License: mit
- Created: 2024-03-03T16:46:18.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-03-25T08:02:45.000Z (about 1 year ago)
- Last Synced: 2024-04-26T07:45:22.951Z (about 1 year ago)
- Language: TypeScript
- Size: 3.79 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# URL Slug Generator
A simple TypeScript library for converting strings into URL-friendly slugs, with support for ASCII conversion, custom replacements, word removal, and length restrictions.
## Features
- Converts strings to URL-friendly slugs.
- Transforms non-ASCII characters to their ASCII equivalents using `unidecode`.
- Allows for custom replacement characters instead of the default hyphen.
- Supports removal of specified words from the input string.
- Enforces maximum slug length constraints.## Installation
Install the package using npm:
```bash
npm install url-slug-generator
```Or using yarn:
```bash
yarn add url-slug-generator
```## Usage
First, import the `generateSlug` function from the package:
```typescript
import { generateSlug } from 'url-slug-generator';
```Then, use the function to convert any string into a slug:
```typescript
const options = {
replacement: '-', // Default replacement is '-'
removeWords: ['a', 'the'], // Words to be removed from the input string
lower: true, // Convert to lowercase
maxLength: 50, // Maximum length of the slug
};const slug = generateSlug("Hello, world! This is a test.", options);
console.log(slug); // Outputs: "hello-world-this-is-test"
```## API Reference
### `generateSlug(text: string, options?: SlugifyOptions): string`
Generates a URL-friendly slug from the provided text.
#### Parameters
- `text` (string): The text to slugify.
- `options` (SlugifyOptions, optional): Configuration options for slug generation.##### `SlugifyOptions`
- `replacement` (string, optional): The character to replace spaces with, defaults to '-'.
- `removeWords` (string[], optional): An array of words to remove from the input text.
- `lower` (boolean, optional): Whether to convert the text to lowercase.
- `maxLength` (number, optional): The maximum length of the generated slug.## Contributing
Contributions are welcome! Please fork the repository and submit a pull request with your proposed changes or enhancements.
## License
This project is licensed under the MIT License - see the LICENSE file for details.