@automattic/social-previews
Version:
A suite of components to generate previews for a post for both social and search engines.
32 lines • 1.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMastodonAddressDetails = exports.mastodonUrl = exports.mastodonBody = exports.mastodonTitle = void 0;
const helpers_1 = require("../helpers");
const constants_1 = require("./constants");
const TITLE_LENGTH = 200;
const BODY_LENGTH = 500;
const URL_LENGTH = 30;
const ADDRESS_PATTERN = /^@([^@]*)@([^@]*)$/i;
const mastodonTitle = (text) => (0, helpers_1.firstValid)((0, helpers_1.shortEnough)(TITLE_LENGTH), (0, helpers_1.hardTruncation)(TITLE_LENGTH))((0, helpers_1.stripHtmlTags)(text)) || '';
exports.mastodonTitle = mastodonTitle;
const mastodonBody = (text, options) => {
const { instance, offset } = options;
return (0, helpers_1.preparePreviewText)(text, {
platform: 'mastodon',
maxChars: BODY_LENGTH - URL_LENGTH - offset,
hashtagDomain: instance,
});
};
exports.mastodonBody = mastodonBody;
const mastodonUrl = (text) => (0, helpers_1.firstValid)((0, helpers_1.shortEnough)(URL_LENGTH), (0, helpers_1.hardTruncation)(URL_LENGTH))((0, helpers_1.stripHtmlTags)(text)) ||
'';
exports.mastodonUrl = mastodonUrl;
const getMastodonAddressDetails = (address) => {
const matches = address.match(ADDRESS_PATTERN);
return {
username: matches?.[1] || '',
instance: matches?.[2] || constants_1.DEFAULT_MASTODON_INSTANCE,
};
};
exports.getMastodonAddressDetails = getMastodonAddressDetails;
//# sourceMappingURL=helpers.js.map