2021-11-23 11:56:02 +05:30
|
|
|
/* eslint-disable import/prefer-default-export */
|
2022-07-08 20:29:07 +05:30
|
|
|
import linkifyHtml from 'linkify-html';
|
2021-11-23 11:56:02 +05:30
|
|
|
import parse from 'html-react-parser';
|
|
|
|
|
import { sanitizeText } from './sanitize';
|
|
|
|
|
|
2023-01-14 18:51:42 +05:30
|
|
|
export const TWEMOJI_BASE_URL = 'https://cdn.jsdelivr.net/gh/twitter/twemoji@14.0.2/assets/';
|
|
|
|
|
|
2021-11-23 11:56:02 +05:30
|
|
|
/**
|
|
|
|
|
* @param {string} text - text to twemojify
|
2023-10-19 17:44:18 +11:00
|
|
|
* @param {object|undefined} opts - DEPRECATED - options for tweomoji.parse
|
2021-11-23 11:56:02 +05:30
|
|
|
* @param {boolean} [linkify=false] - convert links to html tags (default: false)
|
|
|
|
|
* @param {boolean} [sanitize=true] - sanitize html text (default: true)
|
2023-10-19 17:44:18 +11:00
|
|
|
* @param {boolean} [maths=false] - DEPRECATED - render maths (default: false)
|
2021-11-23 11:56:02 +05:30
|
|
|
* @returns React component
|
|
|
|
|
*/
|
2023-10-19 17:44:18 +11:00
|
|
|
export function twemojify(text, opts, linkify = false, sanitize = true) {
|
2021-11-23 11:56:02 +05:30
|
|
|
if (typeof text !== 'string') return text;
|
2022-01-25 12:15:47 +05:30
|
|
|
let content = text;
|
|
|
|
|
|
|
|
|
|
if (sanitize) {
|
|
|
|
|
content = sanitizeText(content);
|
|
|
|
|
}
|
2023-01-14 18:51:42 +05:30
|
|
|
|
2021-11-23 11:56:02 +05:30
|
|
|
if (linkify) {
|
2022-01-25 12:15:47 +05:30
|
|
|
content = linkifyHtml(content, {
|
|
|
|
|
target: '_blank',
|
|
|
|
|
rel: 'noreferrer noopener',
|
|
|
|
|
});
|
2021-11-23 11:56:02 +05:30
|
|
|
}
|
2023-10-19 17:44:18 +11:00
|
|
|
return parse(content);
|
2021-11-23 11:56:02 +05:30
|
|
|
}
|