import type { CountedLink, PageMetadata } from './layouts'; const dateFormat = new Intl.DateTimeFormat('en-GB', { day: '2-digit', month: 'short', year: 'numeric', timeZone: 'UTC', }); export type MetaValue = { text: string; tag: 'a' | 'time' | 'span'; attributes: Record; }; export type MetaItem = { values: MetaValue[]; category?: boolean }; const item = ( text: string, tag: MetaValue['tag'] = 'span', attributes: Record = {}, ): MetaValue => ({ text, tag, attributes }); const countedLinks = (links: CountedLink[], attributes: Record = {}) => links.map(({ name, href, count }) => item(`${name} (${count})`, 'a', { href, ...attributes })); export const metadataItems = ( { authors = [], date, categories = [] }: PageMetadata, minutes?: number, ): MetaItem[] => [ { values: countedLinks(authors, { rel: 'author' }) }, { values: date ? [item(dateFormat.format(new Date(date)), 'time', { datetime: date.slice(0, 10) })] : [], }, { values: minutes ? [item(`${minutes} min read`)] : [] }, { values: countedLinks(categories), category: true }, ].filter(({ values }) => values.length);