Something went wrong. Try again.
Gets Twitch, BTTV, FFZ and 7TV emotes as well as parsing text to emotes! kody.tngl.io/twitch-emoticons
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118import Constants from '../util/Constants.js'
class EmoteParser { /** * A parser to replace text with emotes. * @param {EmoteFetcher} fetcher - The {@linkcode EmoteFetcher} to use the cache of. * @param {object} [options={}] - Options for the parser. * @param {boolean} [options.allowNSFW=false] - Only for 7TV: allow usage of NSFW/unlisted emotes. * @param {string} [options.template=''] - The template to be used. * The strings that can be interpolated are: * - `{link}` The link of the emote. * - `{name}` The name of the emote. * - `{size}` The size index of the image. * - `{creator}` The channel/owner name of the emote. * @param {'html' | 'markdown' | 'bbcode' | 'plain'} [options.type='html'] - The type of the parser. * Can be one of `html`, `markdown`, `bbcode`, or `plain`. * If the `template` option is provided, this is ignored. * @param {RegExp} [options.match=/([^\s]+)/g] - The regular expression that matches an emote. * Must be a global regex, with one capture group for the emote code. */ constructor (fetcher, options = {}) { /** * The {@linkcode EmoteFetcher} being used. * @type {EmoteFetcher} */ this.fetcher = fetcher
/** * The parser's options. * @type {object} */ this.options = { allowNSFW: false, template: '', type: 'html', match: /([^\s]+)/g, ...options, }
this._validateOptions(this.options) }
/** * Validates the parser options. * @private * @param {object} [options] - Options for the parser. * @param {string} [options.template] - The template to be used. * The strings that can be interpolated are: * - `{link}` The link of the emote. * - `{name}` The name of the emote. * - `{size}` The size of the image. * - `{creator}` The channel/owner name of the emote. * @param {'html' | 'markdown' | 'bbcode' | 'plain'} [options.type] - The type of the parser. * Can be one of `html`, `markdown`, `bbcode`, or `plain`. * If the `template` option is provided, this is ignored. * @param {RegExp} [options.match] - The regular expression that matches an emote. * Must be a global regex, with one capture group for the emote code. * @throws {TypeError} When template is not a string. * @throws {TypeError} When type is not one of the supported types. * @throws {TypeError} When match is not a global RegExp. */ _validateOptions (options) { if (options.template && typeof options.template !== 'string') { throw new TypeError('Template must be a string') }
if (!['html', 'markdown', 'bbcode', 'plain'].includes(options.type)) { throw new TypeError('Parse type must be one of `html`, `markdown`, `bbcode`, or `plain`') }
if (!(options.match instanceof RegExp) || !options.match.global) { throw new TypeError('Match must be a global RegExp.') } }
/** * Parses text. * @param {string} text - Text to parse. * @param {object} [options] - Parameters for parsing. * @param {number} [options.size] - Size (scale) for emotes. * @param {boolean} [options.forceStatic] - Whether to force the emote to be static (non-animated). Defaults to the fetcher's forceStatic or `false`. * @param {'dark' | 'light'} [options.themeMode] - Only for Twitch: the preferred theme mode. Defaults to the fetcher's twitchThemeMode or `dark`. * @returns {string} - The parsed text. */ parse (text, options) { // @NOTE(kody): Not setting defaults here, they will be handled by each emote's toLink method. const { size, forceStatic, themeMode, } = options || {}
const parsed = text.replace(this.options.match, (matched, id) => { const emote = this.fetcher.emotes.get(id) if (!emote) return matched if (emote.modifier) return '' if (emote.nsfw && !this.options.allowNSFW) return matched
const template = this.options.template || Constants.Templates[this.options.type] const link = emote.toLink({ size, forceStatic, themeMode }) const res = template .replaceAll('{link}', link) .replaceAll('{name}', emote.code) .replaceAll('{size}', size) .replaceAll('{creator}', emote.ownerName || 'global') .replaceAll('{is-animated}', emote.animated ? 'true' : 'false') .replaceAll('{is-zero-width}', emote.zeroWidth ? 'true' : 'false') .replaceAll('{is-nsfw}', emote.nsfw ? 'true' : 'false')
return res })
return parsed }}
export default EmoteParser