UNPKG

@automattic/social-previews

Version:

A suite of components to generate previews for a post for both social and search engines.

25 lines 1.07 kB
import { firstValid, hardTruncation, shortEnough, stripHtmlTags, preparePreviewText, } from '../helpers'; import { DEFAULT_MASTODON_INSTANCE } from './constants'; const TITLE_LENGTH = 200; const BODY_LENGTH = 500; const URL_LENGTH = 30; const ADDRESS_PATTERN = /^@([^@]*)@([^@]*)$/i; export const mastodonTitle = (text) => firstValid(shortEnough(TITLE_LENGTH), hardTruncation(TITLE_LENGTH))(stripHtmlTags(text)) || ''; export const mastodonBody = (text, options) => { const { instance, offset } = options; return preparePreviewText(text, { platform: 'mastodon', maxChars: BODY_LENGTH - URL_LENGTH - offset, hashtagDomain: instance, }); }; export const mastodonUrl = (text) => firstValid(shortEnough(URL_LENGTH), hardTruncation(URL_LENGTH))(stripHtmlTags(text)) || ''; export const getMastodonAddressDetails = (address) => { const matches = address.match(ADDRESS_PATTERN); return { username: matches?.[1] || '', instance: matches?.[2] || DEFAULT_MASTODON_INSTANCE, }; }; //# sourceMappingURL=helpers.js.map