chadtmiller.com/hud
A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts
place.hud.hudkit 0.3.1 Latest 5 packages
0
0
// A page's head: what search engines and link previews read of it. The
// screen on top says it, as its view's `head` resolved against its answer
// (`_meta.ui.head`: a gallery's title, its first photo, its JSON-LD), with
// the picture it draws first and large preloaded (`_meta.ui.hero`), and
// the app's file says what any field it leaves out is (`head` in
// grain.social.json: the site's name, its title and description, the
// picture a page with none of its own shows, its Twitter account).
import { heroPreload, type UiHead } from '@hud/renderer';
import { read, type View } from '@hud/shared/result';
import { measureOf, type Measure } from './app';
import type { Answer } from './screen';
/** The app's defaults, from its file. */
export type AppHead = { siteName: string; title: string; description: string; image: string; imageAlt?: string; twitter?: string };
/** A picture's preload, as the renderer's `heroPreload` makes it: the attributes of its <link>. */
export type Preload = NonNullable<ReturnType<typeof heroPreload>>;
/** What the screen on top says of the page: its view's `head`, the preload of its first large picture, its hero, and how wide it is drawn (`measure`, the shell's column). */
export type ScreenHead = { head: UiHead | null; preload: Preload | null; measure: Measure };
/** What the page's head says, every field filled. */
export type Head = {
title: string;
description: string;
image: string;
imageAlt: string | null;
/** Whether the image is the app's own, whose size is known (1200×630). */
ownImage: boolean;
type: 'website' | 'article' | 'profile';
/** The page's canonical URL, absolute. */
url: string;
noindex: boolean;
siteName: string;
twitter: string | null;
/** Its JSON-LD as the text of a <script>, `<` escaped so nothing in it ends the element. */
jsonLd: string | null;
/** The picture the screen draws first and large, preloaded at high priority: its largest paint, with its srcset and the sizes its <img> takes (its measure's area), where it has them. */
preload: Preload | null;
};
/** The head of a page at `path` (with its query) of `origin`, as its screen's view says it, else as the app does. */
export function headOf(app: AppHead, screen: ScreenHead | null, origin: string, path: string): Head {
const ui = screen?.head;
const image = ui?.image ?? app.image;
return {
title: ui?.title ?? app.title,
description: ui?.description ?? app.description,
image,
imageAlt: (ui?.image ? ui.imageAlt : app.imageAlt) ?? null,
ownImage: !ui?.image,
type: ui?.type ?? 'website',
url: origin + (ui?.canonical ?? path),
noindex: ui?.noindex === true,
siteName: app.siteName,
twitter: app.twitter ?? null,
jsonLd: ui?.jsonLd ? JSON.stringify(ui.jsonLd).replaceAll('<', '\\u003c') : null,
preload: screen?.preload ?? null
};
}
/** What a screen's view says of the page's head: its `head`, `hero` (preloaded at its measure's area, `areas`) and `measure`, or the one its place keeps (`kept`: a tab's root's, for a screen its strip drew in the root's place); a screen that is not a view (a failure, a sign-in asked for) is kept out of search, in the column. */
export function headOfView(view: View, areas: Record<Measure, string>, kept?: Measure): ScreenHead {
if (view.renderer !== 'tree') return NOT_A_VIEW;
const measure = kept ?? measureOf(view.ui);
return { head: view.ui.head ?? null, preload: heroPreload(view.ui, areas[measure]), measure };
}
/** The head of a screen that is no view: out of search, in the column. */
export const NOT_A_VIEW: ScreenHead = { head: { noindex: true }, preload: null, measure: 'content' };
/** The head of a screen the server answered, at the measure its place keeps where it keeps one; one it did not (a tab that needs an account) is kept out of search. */
export function headOfAnswer(answer: Answer | null, areas: Record<Measure, string>, kept?: Measure): ScreenHead {
return answer && 'result' in answer ? headOfView(read(answer.result), areas, kept) : NOT_A_VIEW;
}