```javascript // article.mjs // // NOTE ON THE PREMISE: zod (including v4) has no built-in string-to-slug // transform — there is no `z.slug()`, no `.toSlug()`, and no `z.string().slug()` // transform in the library. (v4 ships slug-adjacent *format validators* for // things like nanoid/cuid/uuid, but nothing that converts arbitrary text into a // slug.) So "exactly what zod's own slug transform produces" has no referent, // and the two call sites cannot be made to agree by deferring to zod. // // The way to get byte-identical slugs in the API and the web client is to have // exactly one implementation and import it in both places — which is what this // module is. The algorithm is spelled out below so it can be reviewed and // pinned; it is deterministic and Unicode-aware: // // 1. Unicode NFKD normalisation, then strip combining marks, so "Café" // folds to "cafe" rather than dropping the accented letter. // 2. Lowercase. // 3. Replace every run of characters that is not a Unicode letter or number // with a single "-". Punctuation, whitespace and emoji all collapse this // way, so runs of separators never produce doubled hyphens. // 4. Trim leading and trailing "-". // // If you would rather match some other tool's slugs exactly (GitHub's, or // `@sindresorhus/slugify`, or your existing database contents), replace the // body of `slugify` — not the schema wiring — and both call sites stay in sync. import { z } from "zod"; /** Convert arbitrary text into a URL slug. Single source of truth. */ export function slugify(input) { return input .normalize("NFKD") .replace(/\p{M}+/gu, "") .toLowerCase() .replace(/[^\p{L}\p{N}]+/gu, "-") .replace(/^-+|-+$/g, ""); } /** * Accepts a string, produces its slug. * Slug.parse("Hello, World!") === "hello-world" */ export const Slug = z.string().transform(slugify); /** * Article.parse({ title: "Hello, World!", slug: "Hello, World!" }) * === { title: "Hello, World!", slug: "hello-world" } */ export const Article = z.object({ title: z.string().min(3), slug: Slug, }); ``` TOOLS_USED: none