react-highlight-me 1.1.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ import React from 'react';
2
+ type Props = {
3
+ children?: React.ReactElement | string | null;
4
+ words?: string[] | string | RegExp | RegExp[];
5
+ highlightStyle?: React.CSSProperties;
6
+ caseSensitive?: boolean;
7
+ isEscapePattern?: boolean;
8
+ isWordBoundary?: boolean;
9
+ };
10
+ declare const TextHighlighter: ({ children, words, highlightStyle, caseSensitive, isEscapePattern, isWordBoundary, }: Props) => unknown;
11
+ export default TextHighlighter;
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var react_1 = __importDefault(require("react"));
7
+ var TextHighlighter = function (_a) {
8
+ var _b = _a.children, children = _b === void 0 ? '' : _b, _c = _a.words, words = _c === void 0 ? [] : _c, _d = _a.highlightStyle, highlightStyle = _d === void 0 ? { backgroundColor: 'yellow', fontWeight: 'bold' } : _d, _e = _a.caseSensitive, caseSensitive = _e === void 0 ? false : _e, _f = _a.isEscapePattern, isEscapePattern = _f === void 0 ? true : _f, _g = _a.isWordBoundary, isWordBoundary = _g === void 0 ? true : _g;
9
+ // Convert words to array if it's a string
10
+ var wordsArray = Array.isArray(words) ? words : [words];
11
+ // If no words to highlight, return original content
12
+ if (!wordsArray.length || wordsArray.every(function (word) { return !word; })) {
13
+ return children;
14
+ }
15
+ // Function to escape regex special characters
16
+ var escapeRegex = function (term) {
17
+ if (term instanceof RegExp) {
18
+ return term.source;
19
+ }
20
+ if (isEscapePattern) {
21
+ term = term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
22
+ }
23
+ if (isWordBoundary) {
24
+ term = "\\b".concat(term, "\\b");
25
+ }
26
+ return term;
27
+ };
28
+ // Create a regex pattern for all words
29
+ var pattern = wordsArray
30
+ .filter(function (word) { return word; })
31
+ .map(function (word) { return escapeRegex(word); })
32
+ .join('|');
33
+ if (!pattern) {
34
+ return children;
35
+ }
36
+ var regex = new RegExp("(".concat(pattern, ")"), caseSensitive ? 'g' : 'gi');
37
+ // Function to highlight text content
38
+ var highlightText = function (textContent) {
39
+ if (!textContent || typeof textContent !== 'string') {
40
+ return textContent;
41
+ }
42
+ var parts = textContent.split(regex);
43
+ return parts.map(function (part, index) {
44
+ var shouldHighlight = wordsArray.some(function (word) {
45
+ if (word instanceof RegExp) {
46
+ return word.test(part);
47
+ }
48
+ return caseSensitive
49
+ ? part === word.trim()
50
+ : part.toLowerCase() === word.trim().toLowerCase();
51
+ });
52
+ return shouldHighlight ? (react_1.default.createElement("mark", { key: index, style: highlightStyle }, part)) : (part);
53
+ });
54
+ };
55
+ // Function to recursively process React elements
56
+ var processElement = function (element) {
57
+ if (typeof element === 'string') {
58
+ return highlightText(element);
59
+ }
60
+ if (typeof element === 'number') {
61
+ return element;
62
+ }
63
+ if (!react_1.default.isValidElement(element)) {
64
+ return element;
65
+ }
66
+ // Clone element and process its children
67
+ var processedChildren = react_1.default.Children.map(element.props.children, function (child) {
68
+ if (typeof child === 'string') {
69
+ return highlightText(child);
70
+ }
71
+ return processElement(child);
72
+ });
73
+ return react_1.default.cloneElement(element, {}, processedChildren);
74
+ };
75
+ return processElement(children);
76
+ };
77
+ exports.default = TextHighlighter;
@@ -0,0 +1,11 @@
1
+ import React from 'react';
2
+ type Props = {
3
+ children?: React.ReactElement | string | null;
4
+ words?: string[] | string | RegExp | RegExp[];
5
+ highlightStyle?: React.CSSProperties;
6
+ caseSensitive?: boolean;
7
+ isEscapePattern?: boolean;
8
+ isWordBoundary?: boolean;
9
+ };
10
+ declare const TextHighlighter: ({ children, words, highlightStyle, caseSensitive, isEscapePattern, isWordBoundary, }: Props) => unknown;
11
+ export default TextHighlighter;
@@ -0,0 +1,72 @@
1
+ import React from 'react';
2
+ var TextHighlighter = function (_a) {
3
+ var _b = _a.children, children = _b === void 0 ? '' : _b, _c = _a.words, words = _c === void 0 ? [] : _c, _d = _a.highlightStyle, highlightStyle = _d === void 0 ? { backgroundColor: 'yellow', fontWeight: 'bold' } : _d, _e = _a.caseSensitive, caseSensitive = _e === void 0 ? false : _e, _f = _a.isEscapePattern, isEscapePattern = _f === void 0 ? true : _f, _g = _a.isWordBoundary, isWordBoundary = _g === void 0 ? true : _g;
4
+ // Convert words to array if it's a string
5
+ var wordsArray = Array.isArray(words) ? words : [words];
6
+ // If no words to highlight, return original content
7
+ if (!wordsArray.length || wordsArray.every(function (word) { return !word; })) {
8
+ return children;
9
+ }
10
+ // Function to escape regex special characters
11
+ var escapeRegex = function (term) {
12
+ if (term instanceof RegExp) {
13
+ return term.source;
14
+ }
15
+ if (isEscapePattern) {
16
+ term = term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
17
+ }
18
+ if (isWordBoundary) {
19
+ term = "\\b".concat(term, "\\b");
20
+ }
21
+ return term;
22
+ };
23
+ // Create a regex pattern for all words
24
+ var pattern = wordsArray
25
+ .filter(function (word) { return word; })
26
+ .map(function (word) { return escapeRegex(word); })
27
+ .join('|');
28
+ if (!pattern) {
29
+ return children;
30
+ }
31
+ var regex = new RegExp("(".concat(pattern, ")"), caseSensitive ? 'g' : 'gi');
32
+ // Function to highlight text content
33
+ var highlightText = function (textContent) {
34
+ if (!textContent || typeof textContent !== 'string') {
35
+ return textContent;
36
+ }
37
+ var parts = textContent.split(regex);
38
+ return parts.map(function (part, index) {
39
+ var shouldHighlight = wordsArray.some(function (word) {
40
+ if (word instanceof RegExp) {
41
+ return word.test(part);
42
+ }
43
+ return caseSensitive
44
+ ? part === word.trim()
45
+ : part.toLowerCase() === word.trim().toLowerCase();
46
+ });
47
+ return shouldHighlight ? (React.createElement("mark", { key: index, style: highlightStyle }, part)) : (part);
48
+ });
49
+ };
50
+ // Function to recursively process React elements
51
+ var processElement = function (element) {
52
+ if (typeof element === 'string') {
53
+ return highlightText(element);
54
+ }
55
+ if (typeof element === 'number') {
56
+ return element;
57
+ }
58
+ if (!React.isValidElement(element)) {
59
+ return element;
60
+ }
61
+ // Clone element and process its children
62
+ var processedChildren = React.Children.map(element.props.children, function (child) {
63
+ if (typeof child === 'string') {
64
+ return highlightText(child);
65
+ }
66
+ return processElement(child);
67
+ });
68
+ return React.cloneElement(element, {}, processedChildren);
69
+ };
70
+ return processElement(children);
71
+ };
72
+ export default TextHighlighter;
package/package.json CHANGED
@@ -1,13 +1,32 @@
1
1
  {
2
2
  "name": "react-highlight-me",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "Highlight words in React components or text",
5
- "main": "highlight.tsx",
5
+ "main": "dist/index.cjs.js",
6
+ "module": "dist/index.esm.js",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.esm.js",
10
+ "require": "./dist/index.cjs.js"
11
+ }
12
+ },
13
+ "files": [
14
+ "dist"
15
+ ],
6
16
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
17
+ "test": "echo \"Error: no test specified\" && exit 1",
18
+ "build": "npm run build:cjs && npm run build:esm",
19
+ "build:cjs": "tsc -p tsconfig.cjs.json",
20
+ "build:esm": "tsc -p tsconfig.esm.json",
21
+ "clean": "rm -rf dist",
22
+ "prepublishOnly": "npm run build"
8
23
  },
9
24
  "keywords": [],
10
25
  "author": "Andrey Hohutkin <Andrey.Hohutkin@gmail.com",
11
26
  "license": "ISC",
12
- "packageManager": "pnpm@10.9.0"
27
+ "packageManager": "pnpm@10.9.0",
28
+ "devDependencies": {
29
+ "@types/react": "^19.1.8",
30
+ "typescript": "^5.3.3"
31
+ }
13
32
  }
package/highlight.tsx DELETED
@@ -1,113 +0,0 @@
1
- import React from 'react';
2
-
3
- type Props = {
4
- children?: React.ReactElement | null | string;
5
- words?: string[] | string | RegExp | RegExp[];
6
- highlightStyle?: React.CSSProperties;
7
- caseSensitive?: boolean;
8
- isEscapePattern?: boolean;
9
- isWordBoundary?: boolean;
10
- }
11
-
12
- const TextHighlighter = ({
13
- children = '',
14
- words = [],
15
- highlightStyle = { backgroundColor: 'yellow', fontWeight: 'bold' },
16
- caseSensitive = false,
17
- isEscapePattern = true,
18
- isWordBoundary = true,
19
- }: Props) => {
20
- // Convert words to array if it's a string
21
- const wordsArray = Array.isArray(words) ? words : [words];
22
-
23
- // If no words to highlight, return original content
24
- if (!wordsArray.length || wordsArray.every(word => !word)) {
25
- return children;
26
- }
27
-
28
- // Function to escape regex special characters
29
- const escapeRegex = (term: string | RegExp) => {
30
- if (term instanceof RegExp) {
31
- return term.source
32
- }
33
-
34
- if (isEscapePattern) {
35
- term = term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
36
- }
37
-
38
- if (isWordBoundary) {
39
- term = `\\b${term}\\b`;
40
- }
41
-
42
- return term
43
- };
44
-
45
- // Create a regex pattern for all words
46
- const pattern = wordsArray
47
- .filter(word => word)
48
- .map(word => escapeRegex(word))
49
- .join('|');
50
-
51
- if (!pattern) {
52
- return children;
53
- }
54
-
55
- const regex = new RegExp(`(${pattern})`, caseSensitive ? 'g' : 'gi');
56
-
57
- // Function to highlight text content
58
- const highlightText = (textContent) => {
59
- if (!textContent || typeof textContent !== 'string') {
60
- return textContent;
61
- }
62
-
63
- const parts = textContent.split(regex);
64
-
65
- return parts.map((part, index) => {
66
- const shouldHighlight = wordsArray.some(word => {
67
- if (word instanceof RegExp) {
68
- return word.test(part);
69
- }
70
- return caseSensitive
71
- ? part === word.trim()
72
- : part.toLowerCase() === word.trim().toLowerCase();
73
- });
74
-
75
- return shouldHighlight ? (
76
- <mark key={index} style={highlightStyle}>
77
- {part}
78
- </mark>
79
- ) : (
80
- part
81
- );
82
- });
83
- };
84
-
85
- // Function to recursively process React elements
86
- const processElement = (element) => {
87
- if (typeof element === 'string') {
88
- return highlightText(element);
89
- }
90
-
91
- if (typeof element === 'number') {
92
- return element;
93
- }
94
-
95
- if (!React.isValidElement(element)) {
96
- return element;
97
- }
98
-
99
- // Clone element and process its children
100
- const processedChildren = React.Children.map(element.props.children, (child) => {
101
- if (typeof child === 'string') {
102
- return highlightText(child);
103
- }
104
- return processElement(child);
105
- });
106
-
107
- return React.cloneElement(element, {}, processedChildren);
108
- };
109
-
110
- return processElement(children);
111
- };
112
-
113
- export default TextHighlighter;