URL-friendly slugify with multiple languages support.

slug

Package slug generate slug from unicode string, URL-friendly slugify with multiple languages support.

GoDoc Build Status

Documentation online

Example

package main

import (
	"fmt"
	"github.com/gosimple/slug"
)

func main() {
	text := slug.Make("Hellö Wörld хелло ворлд")
	fmt.Println(text) // Will print: "hello-world-khello-vorld"

	someText := slug.Make("影師")
	fmt.Println(someText) // Will print: "ying-shi"

	enText := slug.MakeLang("This & that", "en")
	fmt.Println(enText) // Will print: "this-and-that"

	deText := slug.MakeLang("Diese & Dass", "de")
	fmt.Println(deText) // Will print: "diese-und-dass"
	
	slug.Lowercase = false // Keep uppercase characters
	deUppercaseText := slug.MakeLang("Diese & Dass", "de")
        fmt.Println(deUppercaseText) // Will print: "Diese-und-Dass"

	slug.CustomSub = map[string]string{
		"water": "sand",
	}
	textSub := slug.Make("water is hot")
	fmt.Println(textSub) // Will print: "sand-is-hot"
}

Requests or bugs?

https://github.com/gosimple/slug/issues

Installation

go get -u github.com/gosimple/slug

License

The source files are distributed under the Mozilla Public License, version 2.0, unless otherwise noted. Please read the FAQ if you have further questions regarding the license.

Comments
  • IsSlug for empty string

    IsSlug for empty string

    IDK exatly if it's a bug or it's supose to be this way really hehe.

    package main
    
    import (
            "fmt"
    
            "github.com/gosimple/slug"
    )
    
    func main() {
            text := ""
            fmt.Printf("IsSlug: %t\n", slug.IsSlug(text))
    }
    

    the output is:

    IsSlug: true
    

    IMO an empty string should not be considred a slug but before opening a PR I wanted to know if it should be 😄

  • TestSubstituteLang is flaky

    TestSubstituteLang is flaky

    It fails about 12% of the time for me:

    --- FAIL: TestSubstituteLang (0.00s)
        slug_test.go:155: 1. Substitute("o a o", map[string]string{"o":"no", "a":"or"}) = "no nor no"; want "no or no"
    FAIL
    
  • Add MinLength

    Add MinLength

    As it exists the validation for MaxLength of a slug, I thought it sould exists MinLength also.

    The 2 main functions in which this will affect are MakeLang (Make uses it so no need for it) and IsSlug.

    For IsSlug it's easy, it's just a validation and if not then return false. For MakeLang is more complex as the current signature does not return an error, and changing that would be a HUGE change on the API, so not an option, the other options would be:

    • Fill the missing characters with something, maybe defined by the user on another var
    • Only use it on the IsSlug and not on the MakeLang

    WDYT?

  • Add Go modules support

    Add Go modules support

    When working on implementing #37 I noticed that there were no Go module files. This pull request adds the go.mod and go.sum to the project.

    Thank you!

  • Added the possiblity to disable smart truncate (with the exception of…

    Added the possiblity to disable smart truncate (with the exception of…

    I had a use-case where I needed to slugify a string and limiting by a certain number of characters without removing the whole word if the subString cut a word in 2.

  • fix edge case in truncate function allowing too long slugs

    fix edge case in truncate function allowing too long slugs

    Fixes https://github.com/gosimple/slug/issues/72

    I'm not using operations on strings to avoid unnecessary allocations. I've added a test case for the fixed issue.

  • Code panics for Unicode U+10000

    Code panics for Unicode U+10000

    This code panics:

    slug.Make("𐀀")
    

    This is due to the internal use of rainycape/unidecode, which can not handle Unicode character U+10000, see https://github.com/rainycape/unidecode/issues/8.

    Given the inactivity of the upstream package, it might be worth filtering out the character in question before passing it to unidecode.

  • Substitute @ with at in german

    Substitute @ with at in german

    I would suggest replacing the character @ with at. To be honest, I've never heard anyone say "xyz AN gmail.com" while providing their email address.

  • slug.Make() returns non-slug

    slug.Make() returns non-slug

    Hi there! Thanks for this awesome lib! I'm not sure if this is a valid use-case or not, but I've found a set of custom substitutions that seem to break Make(). In the code below, IsSlug() returns false for every item in slugs.

    names := []string{
    	"Ciudad Autónoma de Buenos Aires AR",
    	"Córdoba AR",
    	"Entre Ríos AR",
    	"Neuquén AR",
    	"Tucumán AR",
    }
    
    slug.CustomSub = map[string]string{
    	"AR": "",
    	"BR": "",
    	"CL": "",
    	"UY": "",
    }
    
    slug.CustomRuneSub = map[rune]string{
        'á': "aa",
        'é': "ee",
        'í': "ii",
        'ó': "oo",
        'ú': "uu",
        ' ': "_",
    }
    
    var slugs []string
    
    for _, n := range names {
    	slugs = append(slugs, slug.Make(n))
    }
    

    I've already found a way to work around this in my code, using SubstituteRune() first.

  • Fixed truncation issue

    Fixed truncation issue

    https://github.com/gosimple/slug/issues/76 " When trying to truncate a string with a value greater than its size, we get the below error.

    Example of failed test: {"DOBROSLAWZYBORT", 17, "dobroslawzybort", false}

    --- FAIL: TestSlugMakeSmartTruncate (0.01s)
    panic: runtime error: slice bounds out of range [:17] with length 15 [recovered]
    	panic: runtime error: slice bounds out of range [:17] with length 15
    

    "

  • Slug length can be MaxLength+1

    Slug length can be MaxLength+1

    When using the truncate function, the slug can be longer than MaxLength on specific conditions. See this example:

    • original string (of length 11): "abcde-fghij"
    • MaxLength: 10
    • expected string: "abcde"
    • output string: "abcde-fghij"

    Current function fails on this edge case because the last word doesn't end with a "-" during the max length test here so the - 1 is wrongfully applied.

  • Structure for re-usability and concurrency

    Structure for re-usability and concurrency

    Description

    Create a structure and make it implement the slug functions.

    Benefits:

    • Ability to slug concurrently with different settings (this wasn't possible because of global variables)
    • Better re-usability

    Drawback:

    • Very slightly slower due to map cloning when creating a new Slug structure.

    Example usage

    s := slug.New()
    s.MaxLength = 80
    s.Make("Hello world")
    

    Notes

    This change is not breaking.

    Similar to #41

  • No slug-representation of emojis/pictograms

    No slug-representation of emojis/pictograms

    This seems like a silly use case at first, but when creating a slug from a string containing emojis or pictograms, there is no representation of those characters. For example:

    slug.Make("🐛")
    slug.Make("☺")
    slug.Make("𝕗𝕒𝕟𝕔𝕪 𝕥𝕖𝕩𝕥")
    

    all yield empty strings.

    I'm not sure how such a character would best be represented in a slug, but simply removing them could be problematic in some cases. Is this intentional?

  • added support for custom separators

    added support for custom separators

    Added support for setting custom separators via slug.SetSeparator("-")

    This keeps the backwards compatibility, passes all the tests, and allows for the use of longer strings, i.e. not just single characters.

    There's also a slug.GetSeparator() function so you can see what the current separator is set to.

Produces a set of tags from given source. Source can be either an HTML page, Markdown document or a plain text. Supports English, Russian, Chinese, Hindi, Spanish, Arabic, Japanese, German, Hebrew, French and Korean languages.
Produces a set of tags from given source. Source can be either an HTML page, Markdown document or a plain text. Supports English, Russian, Chinese, Hindi, Spanish, Arabic, Japanese, German, Hebrew, French and Korean languages.

Tagify Gets STDIN, file or HTTP address as an input and returns a list of most popular words ordered by popularity as an output. More info about what

Dec 19, 2022
Go Humans! (formatters for units to human friendly sizes)

Humane Units Just a few functions for helping humanize times and sizes. go get it as github.com/dustin/go-humanize, import it as "github.com/dustin/go

Jan 2, 2023
[Crawler/Scraper for Golang]🕷A lightweight distributed friendly Golang crawler framework.一个轻量的分布式友好的 Golang 爬虫框架。

Goribot 一个分布式友好的轻量的 Golang 爬虫框架。 完整文档 | Document !! Warning !! Goribot 已经被迁移到 Gospider|github.com/zhshch2002/gospider。修复了一些调度问题并分离了网络请求部分到另一个仓库。此仓库会继续

Oct 29, 2022
Generate a global index for multiple markdown files recursively
Generate a global index for multiple markdown files recursively

markdown index Markdown-index is a library to help you generate a global index for multiple markdown files recursively in a directory, containing a su

Sep 25, 2022
A simple action that looks for multiple regex matches, in a input text, and returns the key of the first found match.

Key Match Action A simple action that looks for multiple regex matches, in a input text, and returns the key of the first found match. TO RUN Add the

Aug 4, 2022
A shell parser, formatter, and interpreter with bash support; includes shfmt

sh A shell parser, formatter, and interpreter. Supports POSIX Shell, Bash, and mksh. Requires Go 1.14 or later. Quick start To parse shell scripts, in

Dec 29, 2022
Show Languages In Code. A fast and lightweight CLI to generate stats on the languages inside your project
Show Languages In Code. A fast and lightweight CLI to generate stats on the languages inside your project

slic Show Languages In Code. Usage Run it with an -h flag to list all commands. -d flag can be used to specify the directory of search -i flag can be

Dec 25, 2021
A Go slugify application that handles string

slugify A Go slugify application that handles string Example: package main import ( "fmt" "github.com/avelino/slugify" ) func main() { text := "E

Sep 27, 2022
:steam_locomotive: Decodes url.Values into Go value(s) and Encodes Go value(s) into url.Values. Dual Array and Full map support.

Package form Package form Decodes url.Values into Go value(s) and Encodes Go value(s) into url.Values. It has the following features: Supports map of

Dec 26, 2022
Go-based search engine URL collector , support Google, Bing, can be based on Google syntax batch collection URL
Go-based search engine URL collector , support Google, Bing, can be based on Google syntax batch collection URL

Go-based search engine URL collector , support Google, Bing, can be based on Google syntax batch collection URL

Nov 9, 2022
Translate your Go program into multiple languages.

go-i18n go-i18n is a Go package and a command that helps you translate Go programs into multiple languages. Supports pluralized strings for all 200+ l

Jan 1, 2023
go-i18n is a Go package and a command that helps you translate Go programs into multiple languages.

go-i18n is a Go package and a command that helps you translate Go programs into multiple languages.

Jan 2, 2023
Translate your Go program into multiple languages with similar fmt.Sprintf format syntax.

Loafer-i18n Loafer-i18n is a Go package and a command that helps you translate Go programs into multiple languages. Supports pluralized strings with =

Dec 22, 2021
Stalin sort in multiple languages!

stalin-sort Stalin sort in multiple languages, contributions are welcome! Motivation This repo is motivated by this tweet, this tweet contains a refer

Jan 14, 2022
Super short, fully unique, non-sequential and URL friendly Ids

Generator of unique non-sequential short Ids The package shortidenables the generation of short, fully unique, non-sequential and by default URL frien

Dec 30, 2022
High-Performance Shortlink ( Short URL ) app creator in Golang. For privacy reasons, you may prefer to host your own short URL app and this is the one to use.
High-Performance Shortlink ( Short URL ) app creator in Golang. For privacy reasons, you may prefer to host your own short URL app and this is the one to use.

About The Project Shortlink App in Golang Multiple Node based Architecture to create and scale at ease Highly performant key-value storage system Cent

Jan 3, 2023
A productivity tools to diagnose list of exported URL status from Google Search Console, Analytics, Sitemap URL...etc.

google-url-checker A productivity tools to diagnose list of exported URL status from Google Search Console, Analytics, Sitemap URL...etc. A quick way

Dec 31, 2021
Putty-url-scheme - Open PuTTY as a url scheme

PuTTY URL Scheme Helper Open PuTTY as a url scheme Install download release bina

Apr 25, 2022
Test-app-url-shortner - A sample url shortener app to test Keploy integration capabilities
Test-app-url-shortner - A sample url shortener app to test Keploy integration capabilities

test-app-url-shortner A sample url shortener app to test Keploy integration capa

Jan 23, 2022
A drop-in replacement for Go errors, with some added sugar! Unwrap user-friendly messages, HTTP status code, easy wrapping with multiple error types.
A drop-in replacement for Go errors, with some added sugar! Unwrap user-friendly messages, HTTP status code, easy wrapping with multiple error types.

Errors Errors package is a drop-in replacement of the built-in Go errors package with no external dependencies. It lets you create errors of 11 differ

Dec 6, 2022