@t7/utils
Version:
Utility methods for T7 components.
55 lines (38 loc) • 1.67 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
/*
You would call this after getting an element's
`.innerHTML` value, while the user is typing.
*/
var contentToText = function contentToText() {
var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
// Convert `&` to `&`.
value = value.replace(/&/gi, '&'); // Replace spaces.
value = value.replace(/ /gi, ' ');
value = value.replace(/\s+/g, ' '); // Remove `<b>`.
value = value.replace(/<b>/gi, '');
value = value.replace(/<\/b>/gi, ''); // Remove `<strong>`.
value = value.replace(/<strong>/gi, '');
value = value.replace(/<\/strong>/gi, ''); // Remove `<i>`.
value = value.replace(/<i>/gi, '');
value = value.replace(/<\/i>/gi, ''); // Remove `<em>`.
value = value.replace(/<em>/gi, '');
value = value.replace(/<\/em>/gi, ''); // Remove `<u>`.
value = value.replace(/<u>/gi, '');
value = value.replace(/<\/u>/gi, ''); // Tighten up `<` and `>`.
value = value.replace(/>\s+/g, '>');
value = value.replace(/\s+</g, '<'); // Replace `<br>`.
value = value.replace(/<br>/gi, '\n'); // Replace `<div>` (from Chrome).
value = value.replace(/<div>/gi, '\n');
value = value.replace(/<\/div>/gi, ''); // Replace `<p>` (from IE).
value = value.replace(/<p>/gi, '\n');
value = value.replace(/<\/p>/gi, ''); // No more than 2x newline, per "paragraph".
value = value.replace(/\n\n+/g, '\n\n'); // Whitespace before/after.
value = value.trim(); // Expose string.
return value;
}; // Expose function.
var _default = contentToText;
exports.default = _default;
;