~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
34 lines · 2.0 KB Raw History
// What a browser's page translation (Chrome's, Safari's, Google
// Translate's) leaves alone: words that are UI or identifiers, not prose.
// A page of mixed languages is translated whole, so "ALT" became German's
// "alt", "OLD". An element always an identifier (ALT, a wordmark, a
// handle's span, "3h") says `translate="no"` itself; a text that may be one
// (a Text's runs, a title, a link's label) asks `translateOf`. People's
// words (captions, titles, comments) and the app's ("Show more") are
// translated as before.

/** A whole text that is an account's name for itself: an @handle (a
 * domain, its last label starting with a letter) or a DID. */
const IDENTIFIER = /^(?:@?(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z](?:[a-z0-9-]*[a-z0-9])?|did:[a-z]+:[a-z0-9._:%-]+)$/i;

/** A whole text that is a web address: with its scheme, or a domain and a path, as a link shows it (`grain.social/prints`, `example.com/a…`). */
const ADDRESS = /^(?:https?:\/\/(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)*[a-z0-9-]+(?::\d+)?(?:[/?#]\S*)?|(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z](?:[a-z0-9-]*[a-z0-9])?(?::\d+)?[/?#]\S*)$/i;

/** `translate` for an element holding `text`: "no" where the whole of it is
 * an @handle, a DID or a web address, else nothing, so it is translated with the page. */
export function translateOf(text: string | null | undefined): 'no' | undefined {
	if (!text) return undefined;
	const whole = text.trim();
	return IDENTIFIER.test(whole) || ADDRESS.test(whole) ? 'no' : undefined;
}

/** A text's words cut from the spaces a page's translation would lose: those
 * at either end (beside a link, mention or tag, kept from translation, it
 * drops them) and every run with a line break (it joins the lines), `kept`
 * to be drawn `translate="no"`; the words between are translated. */
export function spacing(text: string): { text: string; kept: boolean }[] {
	return text
		.split(/(^\s+|\s*\n\s*|\s+$)/)
		.filter((part) => part !== '')
		.map((part) => ({ text: part, kept: !/\S/.test(part) }));
}