Something went wrong. Try again.
🌿 Collaborative wiki on ATProto forked from juprodh.me/lichen.wiki
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162import slugifyAscii from "@sindresorhus/slugify";import { LIMITS } from "./limits.ts";import { truncateToLimit, withinLimit } from "./text-length.ts";
export { default as slugify } from "@sindresorhus/slugify";
const SLUG_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
// Any script's letters, numbers and combining marks, plus "-" and "_". Excludes// anything with meaning in a URL or an HTML attribute.const UNSAFE_CHARS = /[^\p{L}\p{N}\p{M}_-]/u;
/** The canonical form we mint: lowercase ASCII words joined by hyphens. */export function isValidSlug(slug: string): boolean { return SLUG_RE.test(slug) && withinLimit(slug, LIMITS.note.slug);}
// What we serve, as opposed to what we mint: another client may legitimately// write a Japanese slug. Strictly wider than isValidSlug, so it cannot// invalidate an existing slug.export function isSafeSlug(slug: string): boolean { return ( slug.length >= 1 && withinLimit(slug, LIMITS.note.slug) && !UNSAFE_CHARS.test(slug) );}
// NFC only. "が" as one code point and as か + dakuten are different SQLite keys,// so stored and looked-up slugs must agree on a form. No case folding: the PDS// record is canonical, so we must not serve a slug its author didn't write.export function normalizeSlug(slug: string): string { return slug.normalize("NFC");}
// slugify drops what it cannot transliterate, so "ノート#1" comes back as "1".// Per character tells us whether that would lose script or just punctuation.function transliteratesCleanly(text: string): boolean { for (const ch of text) { if (!/[\p{L}\p{N}]/u.test(ch)) continue; if (!slugifyAscii(ch)) return false; } return true;}
// Prefers ASCII transliteration where it preserves the text, so Latin, Cyrillic,// Greek and Arabic are unchanged; falls back to Unicode for scripts slugify would// drop. Only ASCII is lowercased — Unicode lowercasing is locale-dependent.export function toSlug(text: string): string { const normalized = normalizeSlug(text); if (transliteratesCleanly(normalized)) return slugifyAscii(normalized);
const unicode = normalized .replace(/[A-Z]/g, (c) => c.toLowerCase()) .replace(/\s+/gu, "-") .replace(new RegExp(UNSAFE_CHARS.source, "gu"), "") .replace(/-{2,}/g, "-") .replace(/^-+|-+$/g, "");
return truncateToLimit(unicode, LIMITS.note.slug);}