slug

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 16, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

README

Slug

Go Reference Go Report Card Apache License

Slug is a simple, lightweight, and fast slug generator for Go.

Installation

go get github.com/9ssi7/slug

Usslug

packslug main

import (
    "fmt"

    "github.com/9ssi7/slug"
)

func main() {
    fmt.Println(slug.New("Hello World!"))
    // Output: hello-world

    // check if a string is a valid slug
    res := slug.Is("hello-world") // true
}

Documentation

Documentation is available at pkg.go.dev.

Contributing

Contributions are always welcome!

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// MaxLength is the maximum length of a slug.
	MaxLength int = 50

	// NonAuthorizedChars is a regular expression that matches all non-authorized characters.
	// It is used to replace non-authorized characters with dashes.
	// For example, if you want to replace all non-authorized characters with dashes, you can use:
	// slug.NonAuthorizedChars = regexp.MustCompile("[^a-zA-Z0-9-_]")
	// You can use any regular expression you want.
	NonAuthorizedChars *regexp.Regexp = regexp.MustCompile("[^a-zA-Z0-9-_]")

	// MultipleDashes is a regular expression that matches multiple dashes.
	// It is used to replace multiple dashes with a single dash.
	// For example, if you want to replace multiple dashes with a single dash, you can use:
	// slug.MultipleDashes = regexp.MustCompile("-+")
	// You can use any regular expression you want.
	MultipleDashes *regexp.Regexp = regexp.MustCompile("-+")
)
View Source
var ENSub = Sub{
	'&': "and",
	'@': "at",
}
View Source
var TRSub = Sub{
	'&': "ve",
	'@': "et",
	'ş': "s",
	'Ş': "S",
	'ü': "u",
	'Ü': "U",
	'ö': "o",
	'Ö': "O",
	'İ': "I",
	'ı': "i",
	'ğ': "g",
	'Ğ': "G",
	'ç': "c",
	'Ç': "C",
}

Functions

func AddSub

func AddSub(lang Lang, sub Sub)

Add a new sub. lang is the language of the sub. sub is the sub to be added. If you want to add a new sub, you can use: slug.AddSub(slug.TR, slug.Sub{'ş': "s"}) You can use any language and sub you want.

func Is

func Is(slug string) bool

Check if a string is a slug. slug is the string to be checked.

func New

func New(str string, l ...Lang) string

Generate a new slug. str is the string to be slugified. l is the language of the string. Default is English. If you want to use a different language, you can use: slug.New("Merhaba Dünya", slug.TR) You can use any language you want.

Types

type Lang

type Lang string
const (
	EN Lang = "en"
	TR Lang = "tr"
)

type Sub

type Sub map[rune]string

Sub is a map of runes to strings. It is used to replace runes with strings. For example, if you want to replace "ş" with "s", you can use: slug.AddSub(slug.TR, slug.Sub{'ş': "s"}) You can add multiple runes to string mappings.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL