https://github.com/jmrashed/go-slugger
go-slugger is a Go package for generating slugs from strings.
https://github.com/jmrashed/go-slugger
go package slugger
Last synced: 4 months ago
JSON representation
go-slugger is a Go package for generating slugs from strings.
- Host: GitHub
- URL: https://github.com/jmrashed/go-slugger
- Owner: jmrashed
- License: mit
- Created: 2024-11-22T05:50:25.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-11-22T08:09:28.000Z (8 months ago)
- Last Synced: 2025-01-28T05:43:06.959Z (5 months ago)
- Topics: go, package, slugger
- Language: Go
- Homepage:
- Size: 188 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: docs/CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
```bash
_____ _____ _
/ ____| / ____|| |
| | __ ___ | (___ | | _ _ __ _ __ _ ___ _ __
| | |_ | / _ \ \___ \ | || | | | / _` | / _` | / _ \| '__|
| |__| || (_) | ____) || || |_| || (_| || (_| || __/| |
\_____| \___/ |_____/ |_| \__,_| \__, | \__, | \___||_|
__/ | __/ |
|___/ |___/GoSlugger version v1.0.1 2024-11-22
```# go-slugger
`go-slugger` is a Go package for generating slugs from strings. It supports multiple languages, custom delimiters, diacritic removal, and more. This package is useful for generating clean, SEO-friendly slugs for URLs, making it easy to integrate with your web applications, CMS, or any system that needs slug generation.
[](https://github.com/jmrashed/go-slugger/forks)
[](https://github.com/jmrashed/go-slugger/issues)
[](https://github.com/jmrashed/go-slugger/releases)
## Features
- **Multi-language Support**: Generates slugs for strings in multiple languages, converting accented characters (e.g., `é`, `ö`) to their nearest ASCII equivalents.
- **Custom Delimiters**: Allows you to specify a custom delimiter (default is `-`) for separating words in the slug.
- **Diacritic Removal**: Automatically removes diacritical marks (accents) from characters.
- **Truncation**: Optionally truncate slugs to a specified length.
- **Case Sensitivity**: Control over whether the generated slug is lowercase.## Installation
To install `go-slugger`, use the Go Modules feature to fetch the package:
```bash
go get github.com/jmrashed/go-slugger
```## Usage
### Basic Usage
To start using `go-slugger`, import the package into your Go file and use the `GenerateSlug` function.
```go
package mainimport (
"fmt"
"github.com/jmrashed/go-slugger"
)func main() {
input := "Hello World! Welcome to Go."
slug := slugger.GenerateSlug(input)
fmt.Println(slug) // Output: hello-world-welcome-to-go
}
```### Custom Delimiters
You can specify a custom delimiter (default is `-`) to separate words in the slug.
```go
slug := slugger.GenerateSlug("Hello World!", "_")
fmt.Println(slug) // Output: hello_world
```### Diacritic Removal
The package automatically removes diacritical marks (accents) for characters like `é`, `ö`, etc.
```go
slug := slugger.GenerateSlug("Café de Paris")
fmt.Println(slug) // Output: cafe-de-paris
```### Case Sensitivity
The default behavior is to generate slugs in lowercase. If you want to preserve the original case, you can pass a `false` flag for the `lowercase` parameter.
```go
slug := slugger.GenerateSlug("Hello World!", "-", false)
fmt.Println(slug) // Output: Hello-World
```### Truncating Slugs
You can specify the maximum length of the generated slug. If the length exceeds this, it will be truncated.
```go
slug := slugger.GenerateSlug("This is a very long title", "-", true, 20)
fmt.Println(slug) // Output: this-is-a-very-lo
```## API Reference
### `GenerateSlug(input string, delimiter string, lowercase bool, maxLength int) string`
- `input` (string): The string to convert into a slug.
- `delimiter` (string, optional): The delimiter used in the slug. Default is `-`.
- `lowercase` (bool, optional): Whether to generate the slug in lowercase (default: `true`).
- `maxLength` (int, optional): Maximum length of the slug. The slug will be truncated if it exceeds this length.Returns the generated slug as a string.
## Example
### Full Example
```go
package mainimport (
"fmt"
"github.com/jmrashed/go-slugger"
)func main() {
input := "München ist schön!"
// Generate slug with default settings
slug1 := slugger.GenerateSlug(input)
fmt.Println(slug1) // Output: muenchen-ist-schoen
// Generate slug with custom delimiter and case preservation
slug2 := slugger.GenerateSlug(input, "_", false)
fmt.Println(slug2) // Output: Muenchen_ist_schoen
// Generate slug with truncation and custom length
slug3 := slugger.GenerateSlug(input, "-", true, 15)
fmt.Println(slug3) // Output: muenchen-ist-s
}
```## Contribution
We welcome contributions to `go-slugger`! If you’d like to contribute, please follow the steps below:
1. Fork the repository
2. Clone your fork locally
3. Create a new branch for your changes
4. Make your changes and add tests
5. Open a pull request to the `main` branchFor more details, refer to the [CONTRIBUTING.md](CONTRIBUTING.md) file.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## Changelog
For a detailed list of changes, refer to the [CHANGELOG.md](CHANGELOG.md).
## Support
If you have any questions or need support, feel free to open an issue in the [GitHub Issues](https://github.com/jmrashed/go-slugger/issues) section.
For more detailed documentation, visit the [documentation folder](docs/).