From 8e7c5be4c878ff4a6729b489f3773bde03943a4d Mon Sep 17 00:00:00 2001 From: larsi Date: Sat, 14 Nov 2026 01:33:48 +0100 Subject: [PATCH] feat: pretty post URLs /p// 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// links are rewritten in the address bar. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011c5BZVwWdVLrdQi9htmuth --- src/hooks.ts | 4 ++ src/lib/app/slug.ts | 71 +++++++++++++++++++ src/lib/feature/post/PostMeta.svelte | 5 +- .../feature/post/actions/PostActions.svelte | 5 +- src/lib/feature/post/helpers.ts | 2 + .../post/[instance]/[id=integer]/+page.svelte | 26 ++++++- 6 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 src/lib/app/slug.ts 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 @@