Files
photon-tbw/src/lib/app/slug.ts
T
larsiandClaude Opus 5.5 8e7c5be4c8 feat: pretty post URLs /p/<code>/<title-slug> in single-community mode
The code is the post id through a reversible 32-bit mix in base36: unique, short, and not the
raw sequential id. Only the code resolves the post (via the reroute hook), so the slug can
change with the title. Feed links, the share button, canonical and og:url use the pretty
form, and old /post/<instance>/<id> links are rewritten in the address bar.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011c5BZVwWdVLrdQi9htmuth
2026-11-14 01:33:48 +01:00

72 lines
2.4 KiB
TypeScript

import { env } from '$env/dynamic/public'
import { SINGLE_COMMUNITY } from '$lib/app/single'
/**
* Pretty post URLs for single-community mode: /p/<code>/<title-slug>.
*
* <code> is the post id run through a reversible 32-bit mix and written in base36, so it is
* unique per post, short, and does not expose the raw sequential id. Only the code is used to
* find the post; the slug is decoration, so a renamed post keeps working links.
*/
const MASK = 0xffffffff
const MUL = 0x2c1b3c6d // odd, so it has an inverse mod 2^32
const MUL_INV = inverse(MUL)
const XOR = 0x5bd1e995
const MIN = 36 ** 5 // pad codes to at least 6 characters
function inverse(a: number): number {
// Newton's iteration for the multiplicative inverse mod 2^32.
let x = a
for (let i = 0; i < 5; i++) x = Math.imul(x, 2 - Math.imul(a, x)) >>> 0
return x >>> 0
}
export function encodeId(id: number): string {
const mixed = Math.imul((id ^ XOR) >>> 0, MUL) >>> 0
return (mixed + MIN).toString(36)
}
export function decodeId(code: string): number | undefined {
if (!/^[0-9a-z]{6,8}$/.test(code)) return undefined
const n = parseInt(code, 36) - MIN
if (n < 0 || n > MASK) return undefined
const id = ((Math.imul(n >>> 0, MUL_INV) >>> 0) ^ XOR) >>> 0
return id > 0 && id < 2 ** 31 ? id : undefined
}
export function slugify(title: string, max = 60): string {
const slug = title
.toLowerCase()
.replace(/[æ]/g, 'ae')
.replace(/[øœ]/g, 'oe')
.replace(/ß/g, 'ss')
.replace(/[đð]/g, 'd')
.replace(/þ/g, 'th')
.replace(/ł/g, 'l')
.normalize('NFKD')
.replace(/[\u0300-\u036f]/g, '')
.replace(/&/g, ' and ')
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '')
if (slug.length <= max) return slug || 'post'
const cut = slug.slice(0, max)
return cut.slice(0, cut.lastIndexOf('-') > 20 ? cut.lastIndexOf('-') : max)
}
export const prettyPostUrls = Boolean(SINGLE_COMMUNITY)
export function prettyPostPath(id: number, title?: string): string {
return `/p/${encodeId(id)}/${slugify(title ?? '')}`
}
/** Internal route for a pretty path, or undefined when the path is not one. */
export function resolvePrettyPath(path: string): string | undefined {
const m = path.match(/^\/p\/([0-9a-z]+)(?:\/[^/]*)?\/?$/)
if (!m) return undefined
const id = decodeId(m[1])
if (!id) return undefined
const instance = (env.PUBLIC_INSTANCE_URL || '').toLowerCase()
return `/post/${encodeURIComponent(instance)}/${id}`
}