@eccenca/gui-elements
Version:
GUI elements based on other libraries, usable in React application, written in Typescript.
39 lines • 1.52 kB
JavaScript
import React from "react";
import { renderToString } from "react-dom/server.js";
import * as ReactIs from "react-is";
export var reduceToText = function (input, options) {
var _a = options || {}, maxNodes = _a.maxNodes, maxLength = _a.maxLength;
var content = input;
var nodeCount = 0;
var onlyText = function (nodes) {
if (typeof maxNodes !== "undefined" && nodeCount >= maxNodes)
return "";
return React.Children.toArray(nodes)
.slice(0, maxNodes)
.map(function (child) {
var _a;
if (typeof maxNodes !== "undefined" && nodeCount >= maxNodes)
return "";
if (ReactIs.isFragment(child))
return onlyText((_a = child.props) === null || _a === void 0 ? void 0 : _a.children);
if (typeof child === "string" || typeof child === "number") {
nodeCount++;
return child.toString();
}
if (ReactIs.isElement(child)) {
nodeCount++;
return renderToString(React.createElement("span", null, child));
}
return "";
})
.join(" ");
};
var text = typeof content === "string" ? content : onlyText(content);
// Basic HTML cleanup
text = text.replace(/<[^\s][^>]*>/g, "").replace(/\n/g, " ");
if (typeof maxLength === "number") {
text = text.slice(0, maxLength);
}
return text.trim();
};
//# sourceMappingURL=reduceToText.js.map