url-slug-generator
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

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:

npm install url-slug-generator

Or using yarn:

yarn add url-slug-generator

Usage

First, import the generateSlug function from the package:

import { generateSlug } from 'url-slug-generator';

Then, use the function to convert any string into a slug:

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.

Readme

Keywords

Package Sidebar

Install

npm i url-slug-generator

Weekly Downloads

0

Version

2.0.1

License

MIT

Unpacked Size

24 kB

Total Files

11

Last publish

Collaborators

  • qianhechen