highlight-plus
Version:
A React component that extends react-highlight to highlight search terms within syntax-highlighted code.
20 lines • 1.03 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importDefault(require("react"));
const getHighlightedCode = (content = '', word_to_highlight = '', highlight_color = "yellow") => {
const spotlight_regex = new RegExp("<rhp-mark spotlight>(.*?)</rhp-mark>", 'g');
console.log({ content });
const rhp_content = content.replaceAll(word_to_highlight, `<rhp-mark spotlight>${word_to_highlight}</rhp-mark>`);
const highlightedCode = rhp_content.split(spotlight_regex).map((part, index) => {
if (index % 2 === 0) {
return part;
}
return (react_1.default.createElement("span", { className: "rhp-mark", style: { backgroundColor: highlight_color }, key: `rhp-${index}` }, part));
});
return highlightedCode;
};
exports.default = getHighlightedCode;
//# sourceMappingURL=getHighlightedCode.js.map