Files
community-rule/lib/sharePaths.ts
T
adilallo d9c475a967 Give finished Rules a print path, folded launch counts, and a two-day follow-up.
Export is PDF, RULE.md, and Print on US Letter, with every shared term, no member names, and a takeaway that points at Ask an organizer. Finished Rules keep type and place so the team can read folded counts without a database, Umami can sit beside the app with no cookies, and the person who holds the pen gets working PDF and RULE.md links 48 hours later.
2026-11-10 18:32:33 -07:00

89 lines
2.7 KiB
TypeScript

import { RULE_VISIBILITY, type RuleVisibility } from "./ruleVisibility";
const SHARE_SLUG_RE = /^[a-z0-9]{4}-[a-z0-9]{4}$/;
export function isShareSlug(value: string): boolean {
return SHARE_SLUG_RE.test(value);
}
export function publishedRulePath(ruleId: string): string {
return `/rules/${encodeURIComponent(ruleId)}`;
}
export function shareRulePath(shareSlug: string): string {
return `/r/${encodeURIComponent(shareSlug)}`;
}
export function buildPublicRuleUrl(origin: string, ruleId: string): string {
const base = origin.replace(/\/$/, "");
return `${base}${publishedRulePath(ruleId)}`;
}
export const ASK_ORGANIZER_HASH = "ask-organizer";
/** Home Ask an organizer section. Exports link here; they do not embed the form. */
export function askOrganizerUrl(origin: string): string {
const base = origin.replace(/\/$/, "");
return `${base}/#${ASK_ORGANIZER_HASH}`;
}
export function ruleExportPdfPath(ruleId: string, token: string): string {
return `/api/rules/${encodeURIComponent(ruleId)}/export/pdf?t=${encodeURIComponent(token)}`;
}
export function ruleExportMarkdownPath(ruleId: string, token: string): string {
return `/api/rules/${encodeURIComponent(ruleId)}/export/md?t=${encodeURIComponent(token)}`;
}
export function ruleExportPdfUrl(
origin: string,
ruleId: string,
token: string,
): string {
return `${origin.replace(/\/$/, "")}${ruleExportPdfPath(ruleId, token)}`;
}
export function ruleExportMarkdownUrl(
origin: string,
ruleId: string,
token: string,
): string {
return `${origin.replace(/\/$/, "")}${ruleExportMarkdownPath(ruleId, token)}`;
}
export function buildShareRuleUrl(origin: string, shareSlug: string): string {
const base = origin.replace(/\/$/, "");
return `${base}${shareRulePath(shareSlug)}`;
}
/** Host + path as drawn in the Share control, e.g. `communityrule.info/r/k7f2-9qxm`. */
export function formatShareUrlDisplay(origin: string, shareSlug: string): string {
try {
return `${new URL(origin).host}/r/${shareSlug}`;
} catch {
const host = origin.replace(/^https?:\/\//, "").replace(/\/$/, "");
return `${host}/r/${shareSlug}`;
}
}
/**
* URL the Share control copies and shows.
* Shared-by-link (and featured rows that still have a slug) use `/r/{slug}`.
* Featured without a slug uses `/rules/{id}`. Private uses `/rules/{id}`.
*/
export function shareCopyUrl(opts: {
origin: string;
ruleId: string;
shareSlug: string | null;
visibility: RuleVisibility;
}): string {
if (
opts.shareSlug &&
(opts.visibility === RULE_VISIBILITY.shared_by_link ||
opts.visibility === RULE_VISIBILITY.public_and_featured)
) {
return buildShareRuleUrl(opts.origin, opts.shareSlug);
}
return buildPublicRuleUrl(opts.origin, opts.ruleId);
}