@eccenca/gui-elements
Version:
GUI elements based on other libraries, usable in React application, written in Typescript.
107 lines • 4.15 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
import { highlighterUtils } from "../../components/Typography/Highlighter.js";
/**
* Creates a react-markdown reHype plugin that marks text based on a multi-word search query.
*/
var highlightSearchWordsPluginFactory = function (searchQuery) {
var searchStringParts = searchQuery ? highlighterUtils.extractSearchWords(searchQuery) : [];
var multiWordRegex = highlighterUtils.createMultiWordRegex(searchStringParts);
var createTextNode = function (text) { return ({ type: "text", value: text }); };
// Highlight a text node by returning an array of text and mark elements
var highlightTextNode = function (textNode) {
if (searchStringParts.length === 0) {
return [textNode];
}
var result = [];
var text = textNode.value;
var offset = 0;
// loop through matches and add unmatched and matched parts to result array
var matchArray = multiWordRegex.exec(text);
while (matchArray !== null) {
result.push(createTextNode(text.slice(offset, matchArray.index)));
result.push({
type: "element",
tagName: "mark",
properties: {},
children: [createTextNode(matchArray[0])],
});
offset = multiWordRegex.lastIndex;
matchArray = multiWordRegex.exec(text);
}
// Add remaining unmatched string
result.push(createTextNode(text.slice(offset)));
return result;
};
// Upper level highlight function
var highlightRootNode = function (node) { return highlightParentNode(node); };
// Highlight function to be called on Parent nodes
var highlightParentNode = function (parentNode) {
var newChildren = [];
parentNode.children.forEach(function (child) {
var highlightedChild = highlightTextNodes(child);
if (Array.isArray(highlightedChild)) {
newChildren.push.apply(newChildren, __spreadArray([], __read(highlightedChild), false));
}
else {
newChildren.push(highlightedChild);
}
});
return __assign(__assign({}, parentNode), { children: newChildren });
};
// Highlight function to be called on generic inner nodes
var highlightTextNodes = function (node) {
if (node.type === "text") {
return highlightTextNode(node);
}
else {
if (node.children) {
return highlightParentNode(node);
}
else {
return node;
}
}
};
return function highlightSearchWords() {
return function (input) { return highlightRootNode(input); };
};
};
export var markdownUtils = {
highlightSearchWordsPluginFactory: highlightSearchWordsPluginFactory,
};
//# sourceMappingURL=highlightSearchWords.js.map