Documentation
¶
Overview ¶
Package slug generates unique URL-safe slugs backed by a Bun database.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Generate ¶
Generate creates a unique slug for the given table using a Bun database.
Example ¶
Generate slugifies the title and appends a random suffix, retrying until the `slug` column of the given table has no collision.
package main
import (
"context"
"database/sql"
"fmt"
"strings"
"github.com/rahmadafandi/fibr/slug"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/sqlitedialect"
"github.com/uptrace/bun/driver/sqliteshim"
)
func main() {
sqldb, err := sql.Open(sqliteshim.ShimName, "file::memory:?cache=shared")
if err != nil {
panic(err)
}
db := bun.NewDB(sqldb, sqlitedialect.New())
defer db.Close()
if _, err := db.Exec("CREATE TABLE articles (slug TEXT)"); err != nil {
panic(err)
}
s, err := slug.Generate(context.Background(), db, "articles", "Hello, World!")
if err != nil {
panic(err)
}
fmt.Println(strings.HasPrefix(s, "hello-world-"))
}
Output: true
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.