2021-11-23 06:26:02 +00:00
|
|
|
/* eslint-disable import/prefer-default-export */
|
|
|
|
import linkifyHtml from 'linkifyjs/html';
|
|
|
|
import parse from 'html-react-parser';
|
|
|
|
import twemoji from 'twemoji';
|
|
|
|
import { sanitizeText } from './sanitize';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} text - text to twemojify
|
|
|
|
* @param {object|undefined} opts - options for tweomoji.parse
|
|
|
|
* @param {boolean} [linkify=false] - convert links to html tags (default: false)
|
|
|
|
* @param {boolean} [sanitize=true] - sanitize html text (default: true)
|
|
|
|
* @returns React component
|
|
|
|
*/
|
|
|
|
export function twemojify(text, opts, linkify = false, sanitize = true) {
|
|
|
|
if (typeof text !== 'string') return text;
|
2022-01-25 06:45:47 +00:00
|
|
|
let content = text;
|
|
|
|
|
|
|
|
if (sanitize) {
|
|
|
|
content = sanitizeText(content);
|
|
|
|
}
|
|
|
|
content = twemoji.parse(content, opts);
|
2021-11-23 06:26:02 +00:00
|
|
|
if (linkify) {
|
2022-01-25 06:45:47 +00:00
|
|
|
content = linkifyHtml(content, {
|
|
|
|
target: '_blank',
|
|
|
|
rel: 'noreferrer noopener',
|
|
|
|
});
|
2021-11-23 06:26:02 +00:00
|
|
|
}
|
|
|
|
return parse(content);
|
|
|
|
}
|