Files
xLog/src/lib/default-slug.ts
T

20 lines
454 B
TypeScript

import { pinyin } from "pinyin-pro"
export const getDefaultSlug = (title: string, id?: string) => {
let generated =
pinyin(title, {
toneType: "none",
type: "array",
nonZh: "consecutive",
})
?.map((word) => word.trim())
?.filter((word) => word)
?.join("-")
?.replace(/\s+/g, "-") ||
id?.replace(`local-`, "") ||
""
generated = generated.replace(/[^a-zA-Z0-9\s-_]/g, "")
return generated
}