@redocly/portal-legacy-ui
Version:
Library of legacy portal UI components
63 lines • 2.59 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HighlightContext = void 0;
exports.Highlight = Highlight;
const React = __importStar(require("react"));
const highlight_words_core_1 = require("highlight-words-core");
exports.HighlightContext = React.createContext([]);
function Highlight(props) {
const { children } = props;
const searchWords = React.useContext(exports.HighlightContext);
if (!searchWords.length) {
return children ? React.createElement(React.Fragment, null, children) : null;
}
function highlight(str, childIdx = 0) {
const chunks = (0, highlight_words_core_1.findAll)({
searchWords,
textToHighlight: str,
});
return (React.createElement(React.Fragment, { key: childIdx }, chunks.map((chunk, idx) => {
const { end, highlight, start } = chunk;
const text = str.substr(start, end - start);
if (highlight) {
return React.createElement("mark", { key: idx }, text);
}
else {
return text;
}
})));
}
if (typeof children === 'string') {
return highlight(children);
}
else if (Array.isArray(children)) {
return (React.createElement(React.Fragment, null, children.map((child, idx) => typeof children === 'string' ? highlight(child, idx) : child || null)));
}
else {
return React.createElement(React.Fragment, null, "children") || null;
}
}
//# sourceMappingURL=Highlight.js.map