diff --git a/src/hooks.ts b/src/hooks.ts index 4c28673d..5a7a8bf9 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -1,5 +1,6 @@ import type { Reroute } from '@sveltejs/kit' import { SINGLE_COMMUNITY } from '$lib/app/single' +import { resolvePrettyPath } from '$lib/app/slug' /** * Single-community mode: serve the community page for "/" and for every route that would @@ -9,6 +10,9 @@ import { SINGLE_COMMUNITY } from '$lib/app/single' export const reroute: Reroute = ({ url }) => { if (!SINGLE_COMMUNITY) return const path = url.pathname + // Pretty post URLs: /p// -> /post//. + const post = resolvePrettyPath(path) + if (post) return post const home = `/c/${SINGLE_COMMUNITY}` if (path === '/' || path === '/communities' || path.startsWith('/explore')) return home const other = path.match(/^\/c\/([^/]+)\/?$/) diff --git a/src/lib/app/slug.ts b/src/lib/app/slug.ts new file mode 100644 index 00000000..d1fa5614 --- /dev/null +++ b/src/lib/app/slug.ts @@ -0,0 +1,71 @@ +import { env } from '$env/dynamic/public' +import { SINGLE_COMMUNITY } from '$lib/app/single' + +/** + * Pretty post URLs for single-community mode: /p//. + * + * 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}` +} diff --git a/src/lib/feature/post/PostMeta.svelte b/src/lib/feature/post/PostMeta.svelte index 9efd68c4..cc9edffd 100644 --- a/src/lib/feature/post/PostMeta.svelte +++ b/src/lib/feature/post/PostMeta.svelte @@ -1,5 +1,6 @@