Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146// The rest of a document record: when it was published, how long it takes to// read, what it is tagged with, and who else wrote it.//// Every field is optional and written by a third party, so each one is// validated here instead of in the popup. A field that fails validation costs// its own line, never the article.
import type { DocumentRecord } from './types'
export interface Contributor { did: string /** The display name when the record carries one, otherwise the DID. */ name: string role?: string}
export interface ArticleDetails { /** Normalized ISO timestamps; absent when the record's value was not a date. */ publishedAt?: string updatedAt?: string readingMinutes?: number tags: string[] contributors: Contributor[]}
/** A common words-per-minute figure, used only for a rough estimate. */const WORDS_PER_MINUTE = 200/** Below this, a word count says more about the record than the article. */const MIN_READING_WORDS = 50const MAX_TAGS = 8const MAX_TAG_CHARS = 64const MAX_CONTRIBUTORS = 8
function clean(value: unknown): string | undefined { if (typeof value !== 'string') return undefined const trimmed = value.trim() return trimmed || undefined}
/** A datetime as normalized ISO, or undefined when it is not one. */export function isoDate(value: unknown): string | undefined { if (typeof value !== 'string') return undefined const ms = Date.parse(value) return Number.isNaN(ms) ? undefined : new Date(ms).toISOString()}
/** * A rough reading time from the record's plaintext. Undefined for a record * with no plaintext, and for one too short for the estimate to mean anything. */export function readingMinutes(text: unknown): number | undefined { const plain = clean(text) if (!plain) return undefined const words = plain.split(/\s+/).filter(Boolean).length if (words < MIN_READING_WORDS) return undefined return Math.max(1, Math.ceil(words / WORDS_PER_MINUTE))}
/** * The record's tags, trimmed and deduplicated. A leading `#` is removed * because the lexicon asks publishers not to add one and the popup adds its * own; an entry that is not a short string is dropped. */export function articleTags(record: Pick<DocumentRecord, 'tags'> | undefined): string[] { if (!Array.isArray(record?.tags)) return [] const tags: string[] = [] for (const entry of record.tags) { const tag = clean(entry)?.replace(/^#+/, '').trim() if (!tag || tag.length > MAX_TAG_CHARS || tags.includes(tag)) continue tags.push(tag) if (tags.length >= MAX_TAGS) break } return tags}
/** Co-authors the record names, at most a handful for the popup's one line. */export function articleContributors( record: Pick<DocumentRecord, 'contributors'> | undefined,): Contributor[] { if (!Array.isArray(record?.contributors)) return [] const contributors: Contributor[] = [] for (const entry of record.contributors) { if (!entry || typeof entry !== 'object') continue const raw = entry as { did?: unknown; displayName?: unknown; role?: unknown } if (typeof raw.did !== 'string' || !raw.did.startsWith('did:')) continue const name = clean(raw.displayName) ?? raw.did const role = clean(raw.role) contributors.push({ did: raw.did, name, ...(role ? { role } : {}) }) if (contributors.length >= MAX_CONTRIBUTORS) break } return contributors}
export function articleDetails(record: DocumentRecord | undefined): ArticleDetails { const publishedAt = isoDate(record?.publishedAt) const updated = isoDate(record?.updatedAt) // Only an actual edit is worth a second date: a record that writes the same // timestamp twice should read as published, not updated. const updatedAt = updated && (!publishedAt || Date.parse(updated) > Date.parse(publishedAt)) ? updated : undefined const minutes = readingMinutes(record?.textContent) return { ...(publishedAt ? { publishedAt } : {}), ...(updatedAt ? { updatedAt } : {}), ...(minutes ? { readingMinutes: minutes } : {}), tags: articleTags(record), contributors: articleContributors(record), }}
/** * A publication date is a calendar date as much as an instant, and the record * writes it in UTC. Formatting it in UTC keeps the date the publisher chose * from moving for readers west of Greenwich. */export function formatDate(iso: string, locale?: string): string { return new Intl.DateTimeFormat(locale, { dateStyle: 'medium', timeZone: 'UTC' }).format( new Date(iso), )}
function nameList(contributors: Contributor[]): string { const names = contributors.slice(0, 3).map((c) => c.name) if (names.length === 0) return '' const extra = contributors.length - names.length if (extra > 0) return `${names.join(', ')} and ${extra} more` if (names.length === 1) return names[0]! return `${names.slice(0, -1).join(', ')} and ${names[names.length - 1]}`}
/** * The one line under the article: “Updated Aug 20, 2026 · 2 min read · with * Daniel”. Empty when the record says none of it. */export function articleMetaLine(details: ArticleDetails, locale?: string): string { const parts: string[] = [] if (details.updatedAt) parts.push(`Updated ${formatDate(details.updatedAt, locale)}`) else if (details.publishedAt) parts.push(`Published ${formatDate(details.publishedAt, locale)}`) if (details.readingMinutes) parts.push(`${details.readingMinutes} min read`) const names = nameList(details.contributors) if (names) parts.push(`with ${names}`) return parts.join(' · ')}