import sanitizeHtml from 'sanitize-html';

const basicTags = [
	'h2',
	'h3',
	'h4',
	'h5',
	'h6',
	'p',
	'a',
	'ul',
	'ol',
	'nl',
	'li',
	'blockquote',
	'code',
	'table',
	'thead',
	'caption',
	'tbody',
	'tr',
	'th',
	'td',
	'pre',
	'figcaption',
];
const extraTags = ['b', 'br', 'i', 'strong', 'em', 'strike', 'img'];

const options = {
	allowedIframeHostnames: false,
	allowedTags: [...basicTags, ...extraTags],
	allowedAttributes: {
		a: ['href', 'target', 'id'],
		img: ['src', 'title', 'alt', 'data-*'],
		'*': ['id'],
	},
	transformTags: basicTags.reduce((acc, tag) => {
		acc[tag] = (tagName, attribs) => ({
			tagName,
			attribs: {
				...attribs,
				id: Math.random()
					.toString()
					.substring(2, 12),
			},
		});
		return acc;
	}, {}),
};

const sanitize = (dirty) => {
	const html = sanitizeHtml(dirty, options).replace(/\n|\r/g, '');
	if (html === '') return '<p></p>';
	return html;
};

export default sanitize;

@@ -42,21 +42,23 @@
 %0A%0Aconst 
-basic
+allowed
 Tags = %5B
@@ -247,46 +247,28 @@
 n',%0A
-%5D;%0Aconst extraTags = %5B
+%09
 'b',
- 
+%0A%09
 'br',
- 
+%0A%09
 'i',
- 
+%0A%09
 'str
@@ -276,15 +276,17 @@
 ng',
- 
+%0A%09
 'em',
- 
+%0A%09
 'str
@@ -294,14 +294,17 @@
 ke',
- 
+%0A%09
 'img'
+,%0A
 %5D;%0A%0A
@@ -369,38 +369,8 @@
 Tags
-: %5B...basicTags, ...extraTags%5D
 ,%0A%09a
@@ -467,251 +467,9 @@
 %5D,%0A%09
-%09'*': %5B'id'%5D,%0A%09%7D,%0A%09transformTags: basicTags.reduce((acc, tag) =%3E %7B%0A%09%09acc%5Btag%5D = (tagName, attribs) =%3E (%7B%0A%09%09%09tagName,%0A%09%09%09attribs: %7B%0A%09%09%09%09...attribs,%0A%09%09%09%09id: Math.random()%0A%09%09%09%09%09.toString()%0A%09%09%09%09%09.substring(2, 12),%0A%09%09%09%7D,%0A%09%09%7D);%0A%09%09return acc;%0A%09%7D, %7B%7D)
+%7D
 ,%0A%7D;

remove sanitize ids