slug

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2021 License: MIT Imports: 5 Imported by: 6

README

slug -- Create slugs of text or URLs, for use in e.g. URL path segments

Use the Go import path

github.com/tv42/slug

Documentation at http://godoc.org/github.com/tv42/slug

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Slug

func Slug(s string) string

Slug returns a slugified string. The result will only contain lowercase letters, digits and dashes. It will not begin or end with a dash, and it will not contain runs of multiple dashes.

It is NOT forced into being ASCII, but may contain any Unicode characters, with the above restrictions.

Example
package main

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

func main() {
	fmt.Println(slug.Slug("Rødgrød med fløde"))
	fmt.Println(slug.Slug("BUY NOW!!!11eleven"))
	fmt.Println(slug.Slug("../../etc/passwd"))
}
Output:

rødgrød-med-fløde
buy-now-11eleven
etc-passwd

func URL

func URL(u *url.URL) string

URL returns a slugified string based on the URL. In addition to the usual slugification rules, the following simplifications are done:

  • schemes `http` and `https` are removed
  • a leading `www.` in hostname is removed
  • a trailing `/index.html` or `.html` is removed

func URLString

func URLString(s string) (string, error)

URLString returns a slugified string based on the URL passed as a string. This is a convenience wrapper over URL. It fails only if parsing the URL fails.

Example
package main

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

func main() {
	s, err := slug.URLString("https://www.example.com/foo/index.html")
	fmt.Println(s, err)
}
Output:

example-com-foo <nil>

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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