// SOCIAL PROMOTION TEXT (release 29 G2) — THE one copy of how a post for X or // Bluesky is counted, what its link is, and what `publish.x` / // `publish.bluesky` may hold. Text only: nothing here (or anywhere) posts. // // Plain JS (with ./social.d.mts) for the reason ./drafted.mjs is: the common // task (TypeScript), the manifest's validators (drafted.mjs `validatePublish`), // umtool's report-to-video composer and its page all count the same way. No // node imports, so the page may import it. // // THE FIELD. `{ post, thread? }`: `post` the first post, and `thread` up to // SOCIAL_LIMITS.thread more short posts under it. The link is in `post`: the // social task APPENDS it in code (`withLink`), the model never writes one. // // THE COUNTS. X: 280, weighed as X weighs a post (twitter-text's v3 rules): // every URL is 23 (t.co), an emoji 2, a character in the Latin and common // punctuation ranges 1, any other 2. Bluesky: 300 GRAPHEMES (what a reader // sees as one character: an emoji with its modifiers, a flag), the link // counted as written. /** The platforms a social post is drafted for: X and Bluesky. */ export const SOCIAL_PLATFORMS = Object.freeze(["x", "bluesky"]); /** Each platform's limit per post; `thread` the most posts under the first. */ export const SOCIAL_LIMITS = Object.freeze({ x: 280, bluesky: 300, thread: 4, urlWeight: 23 }); /** What the page calls each platform. */ export const SOCIAL_NAMES = Object.freeze({ x: "X", bluesky: "Bluesky" }); // The top-level domains a scheme-less `label.tld` is taken as a link for // (X auto-links `Kick.com`, `x.com`, `jeralyzer.pages.dev` and weighs each as a // URL). Conservative: the common generic TLDs and the country codes a link in // a post is likely to carry — counting a domain X would not link as 23 only // makes a post shorter than it could be, never too long. export const SOCIAL_TLDS = Object.freeze([ "com", "net", "org", "edu", "gov", "info", "biz", "io", "co", "dev", "app", "ai", "gg", "tv", "fm", "me", "ly", "so", "to", "xyz", "live", "news", "page", "site", "online", "blog", "link", "social", "video", "media", "art", "store", "shop", "us", "uk", "ca", "au", "de", "fr", "es", "it", "nl", "be", "ch", "at", "se", "no", "dk", "fi", "ie", "pl", "pt", "ru", "br", "mx", "ar", "in", "jp", "kr", "cn", "nz", "za", "eu", "is", "sh", "ws", "cc", "la", "im", "ms", "gl", "vc", "lol", ]); const TLD = SOCIAL_TLDS.join("|"); // A scheme-less domain: labels and a known TLD, not glued to a word, an @ (a // handle or an address) or a dot before it, a path after it if any. const BARE_DOMAIN = `(?"]*)?`; /** A URL as X sees one in a post: a scheme, a `www.` host, or a bare `label.tld` (SOCIAL_TLDS). */ export const SOCIAL_URL_RE = new RegExp(`\\bhttps?:\\/\\/[^\\s<>"]+|\\bwww\\.[^\\s<>"]+\\.[a-z]{2,}[^\\s<>"]*|${BARE_DOMAIN}`, "giu"); /** Whether a text holds anything X would turn into a link (a URL, a www. host, a bare domain). */ export function hasLink(text) { return new RegExp(SOCIAL_URL_RE.source, "iu").test(String(text ?? "")); } const X_LIGHT = [ [0, 4351], [8192, 8205], [8208, 8223], [8242, 8247], ]; const EMOJI = /\p{Extended_Pictographic}|\p{Regional_Indicator}|\u20e3/u; const segmenter = () => new Intl.Segmenter(undefined, { granularity: "grapheme" }); /** The graphemes of a text: what a reader counts as characters. */ export function graphemes(text) { return Array.from(segmenter().segment(String(text ?? "")), (s) => s.segment); } /** How long a post is on X: 280 is the limit; a URL is 23, an emoji 2. */ export function xLength(text) { const t = String(text ?? "").normalize("NFC"); let n = 0; let at = 0; const weigh = (s) => { for (const g of graphemes(s)) { if (EMOJI.test(g)) { n += 2; continue; } for (const ch of g) { const cp = ch.codePointAt(0) ?? 0; n += X_LIGHT.some(([lo, hi]) => cp >= lo && cp <= hi) ? 1 : 2; } } }; for (const m of t.matchAll(SOCIAL_URL_RE)) { weigh(t.slice(at, m.index)); n += SOCIAL_LIMITS.urlWeight; at = (m.index ?? 0) + m[0].length; } weigh(t.slice(at)); return n; } /** How long a post is on Bluesky: its graphemes (300 is the limit). */ export function blueskyLength(text) { return graphemes(String(text ?? "").normalize("NFC")).length; } /** A post's length as its platform counts it. */ export function socialLength(platform, text) { if (platform === "x") return xLength(text); if (platform === "bluesky") return blueskyLength(text); throw new Error(`no social platform "${platform}" (${SOCIAL_PLATFORMS.join(", ")})`); } /** The limit of one post on a platform. */ export const socialLimit = (platform) => SOCIAL_LIMITS[platform]; /** The post with its link under it (a blank line between); no link: the post. */ export function withLink(post, link) { const p = String(post ?? "").trim(); return link ? `${p}\n\n${link}` : p; } /** * Every reason a `publish.` value cannot be published as it is * (empty: it can). `where` names it in the sentences. * * @param {"x" | "bluesky"} platform * @param {unknown} v * @param {string} [where] * @returns {string[]} */ export function validateSocialPost(platform, v, where = `publish.${platform}`) { if (v === null || typeof v !== "object" || Array.isArray(v)) return [`${where} must be { post, thread? }`]; const errors = []; for (const key of Object.keys(v)) { if (key === "drafted") errors.push(`${where}.drafted: the mark goes in the top-level drafted map`); else if (key !== "post" && key !== "thread") errors.push(`${where}.${key} is not a social post field (post, thread)`); } const limit = socialLimit(platform); const one = (text, at) => { if (typeof text !== "string" || !text.trim()) { errors.push(`${at} must be a post (a string)`); return; } const n = socialLength(platform, text); if (n > limit) errors.push(`${at} is ${n} ${platform === "bluesky" ? "graphemes" : "characters as X counts them"} (at most ${limit})`); }; one(v.post, `${where}.post`); if (v.thread !== undefined) { if (!Array.isArray(v.thread)) errors.push(`${where}.thread must be a list of posts`); else { if (v.thread.length > SOCIAL_LIMITS.thread) errors.push(`${where}.thread has ${v.thread.length} posts (at most ${SOCIAL_LIMITS.thread})`); v.thread.forEach((t, i) => one(t, `${where}.thread[${i}]`)); } } return errors; } /** * The link a social post carries: the video's public URL when the manifest * has one (`publish.videoUrl`, the operator's), else the article's public * page (`articleUrl`, the caller's: a public article's page on the homepage), * else none. * * @param {any} manifest * @param {{ articleUrl?: string | null }} [o] * @returns {string | null} */ export function socialLinkOf(manifest, { articleUrl = null } = {}) { const v = manifest?.publish?.videoUrl; if (typeof v === "string" && /^https?:\/\/\S+$/i.test(v.trim())) return v.trim(); return articleUrl && /^https?:\/\/\S+$/i.test(articleUrl) ? articleUrl : null; }