Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
1.5 kB · 51 lines
TypeScript
at master
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152import type { NoteDocument } from "./types";
export const EMPTY_DOC: NoteDocument = { schema_version: 1, content: { type: "doc", content: [] },};
export function textFromDocument(document: NoteDocument) { const walk = (value: unknown): string => { if (!value || typeof value !== "object") return ""; const node = value as { text?: string; content?: unknown[] }; return [node.text ?? "", ...(node.content ?? []).map(walk)].join(" "); }; return walk(document.content).replace(/\s+/g, " ").trim();}
export function relativeDate(value: string) { const difference = Date.now() - new Date(value).getTime(); const minutes = Math.floor(difference / 60_000); if (minutes < 1) return "Just now"; if (minutes < 60) return `${minutes}m`; const hours = Math.floor(minutes / 60); if (hours < 24) return `${hours}h`; const days = Math.floor(hours / 24); if (days < 7) return `${days}d`; return new Date(value).toLocaleDateString("en-US", { day: "numeric", month: "short", timeZone: "UTC", });}
export function formatDateTime(value: string) { return new Date(value).toLocaleString("en-US", { dateStyle: "medium", timeStyle: "short", timeZone: "UTC", });}
export function youtubeEmbed(url: string) { try { const parsed = new URL(url); const id = parsed.hostname.includes("youtu.be") ? parsed.pathname.slice(1) : parsed.searchParams.get("v"); return id ? `https://www.youtube.com/embed/${id}` : url; } catch { return url; }}