slugify

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2023 License: MIT Imports: 4 Imported by: 1

README

go-slugify

Go Report Card

Make Pretty Slugs.

Installation

go get -u github.com/kong/go-slugify

Install CLI tool:

go get -u github.com/kong/go-slugify/slugify
$ slugify "北京kožušček,abc"
bei-jing-kozuscek-abc

Documentation

API documentation can be found here: https://godoc.org/github.com/kong/go-slugify

Usage

package main

import (
	"fmt"
	"github.com/kong/go-slugify"
)

func main() {
	slugifier := (&slugify.Slugifier{}).ToLower(false).InvalidChar("-").WordSeparator("-")
	s := "北京kožušček,abc"
	fmt.Println(slugifier.Slugify(s))
	// Output: bei-jing-kozuscek-abc
}

Documentation

Overview

Package slugify implements Make Pretty Slug

slugifier := (&slugify.Slugifier{}).ToLower(false).InvalidChar("-").WordSeparator("-")
s := "北京kožušček,abc"
fmt.Println(slugifier.Slugify(s))
// Output: bei-jing-kozuscek-abc

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Version

func Version() string

Version return version

Types

type Slugifier

type Slugifier struct {
	// contains filtered or unexported fields
}

func NewSlugifier

func NewSlugifier() Slugifier

NewSlugifier creates a new slugifier. Defaults to lowercasing and using a dash ("-") as the word separator and as the invalid character replacement.

func (*Slugifier) AllowedSet

func (s *Slugifier) AllowedSet(allowedSet string) *Slugifier

AllowedSet sets the allowed set of characters. Defaults to "a-zA-Z0-9". The allowed set is used to replace characters that are not in the allowed set with the invalid character replacement. It must be a valid regex set (typically in [] brackets), any characters that need escaping must be properly escaped. The word separator is automatically added to the allowed set.

func (*Slugifier) InvalidChar

func (s *Slugifier) InvalidChar(invalidCharReplacement string) *Slugifier

InvalidChar sets the character to use to replace invalid characters (anything not a-z, A-Z, 0-9, the word separator, or the InvalidChar). Defaults to a dash ("-"). Leading and trailing InvalidCharReplacements are trimmed. Multiple successive InvalidCharReplacements are NOT replaced with a single InvalidChar. Returns the slugifier for easy chaining.

func (*Slugifier) Slugify

func (s *Slugifier) Slugify(txt string) string

Slugify implements making a pretty slug from the given text. e.g. Slugify("kožušček hello world") => "kozuscek-hello-world"

func (*Slugifier) ToLower

func (s *Slugifier) ToLower(toLower bool) *Slugifier

ToLower sets the flag indicating if the slugified result should be lowercased. Returns the slugifier for easy chaining.

func (*Slugifier) WordSeparator

func (s *Slugifier) WordSeparator(wordSeparator string) *Slugifier

WordSeparator sets the word separator character to use. Defaults to a dash ("-"). The word separator is used to replace whitespace. Leading and trailing word separators are trimmed. Multiple successive word separators are replaced with a single word separator. Returns the slugifier for easy chaining.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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