export type BlockData = Record; export type Item = Record; export type Block = { id: string; type: string; data: BlockData; style: Record; items?: Item[] }; /** An address that is safe in an `href` or `src`: http(s), a path, `mailto:`, `tel:` or an anchor; anything else becomes `#`. */ export function safeUrl(url: unknown): string { const value = typeof url === 'string' ? url.trim() : ''; return /^(https?:\/\/|\/(?!\/)|#|mailto:|tel:)/i.test(value) && !/[\s"'()<>\\]/.test(value) ? value : '#'; } /** CSS classes for the style options of a block: `mb--background-dark mb--width-wide`. */ export function classes(style: Record | undefined): string { return Object.entries(style ?? {}) .map(([name, value]) => `mb--${name.replace(/[^a-z0-9_-]/gi, '')}-${String(value).replace(/[^a-z0-9_-]/gi, '')}`) .join(' '); } /** The first non-empty text of a record under one of the usual names (a card shows a title or name, a summary or quote, a photo, a url). */ export function pick(item: Item, keys: string[]): string | undefined { for (const key of keys) { if (typeof item[key] === 'string' && item[key] !== '') { return item[key]; } } return undefined; }