You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
791 B
27 lines
791 B
export const POST_SLUG_RE = /^[a-z0-9][a-z0-9-]{0,98}[a-z0-9]$|^[a-z0-9]$/
|
|
|
|
export function normalizePostSlugCandidate(input: string): string {
|
|
const ascii = input
|
|
.toLowerCase()
|
|
.normalize('NFKD')
|
|
.replace(/[\u0300-\u036f]/g, '')
|
|
.replace(/['"]/g, '')
|
|
.replace(/[^a-z0-9]+/g, '-')
|
|
.replace(/^-+|-+$/g, '')
|
|
.replace(/-{2,}/g, '-')
|
|
|
|
const limited = ascii.slice(0, 100).replace(/-+$/g, '')
|
|
return limited
|
|
}
|
|
|
|
export function generateRandomPostSlug(): string {
|
|
// 输出如 post-a1b2c3d4e5,满足现有 slug 校验规则。
|
|
const alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789'
|
|
let suffix = ''
|
|
for (let i = 0; i < 10; i += 1) {
|
|
const idx = Math.floor(Math.random() * alphabet.length)
|
|
suffix += alphabet[idx]
|
|
}
|
|
return `post-${suffix}`
|
|
}
|
|
|
|
|