react-highlight-me 1.0.0 → 1.1.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.
Files changed (3) hide show
  1. package/README.md +9 -7
  2. package/highlight.tsx +30 -11
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -5,7 +5,7 @@ A flexible React component for highlighting specific words or phrases within tex
5
5
  ## Features
6
6
 
7
7
  - 🎯 **Flexible Input**: Works with both plain text and React components
8
- - 🔍 **Multiple Words**: Highlight single words or arrays of words
8
+ - 🔍 **Multiple Words**: Highlight single words, arrays of words, regex patterns, or arrays of regex patterns
9
9
  - 🎨 **Custom Styling**: Fully customizable highlight styles
10
10
  - 📝 **Case Sensitivity**: Optional case-sensitive or case-insensitive matching
11
11
  - 🔧 **TypeScript Support**: Written in TypeScript with proper type definitions
@@ -122,12 +122,14 @@ function App() {
122
122
 
123
123
  ## Props
124
124
 
125
- | Prop | Type | Default | Description |
126
- |------|------|---------|-------------|
127
- | `children` | `React.ReactElement \| null` | `''` | The content to process for highlighting |
128
- | `words` | `string[] \| string` | `[]` | Word or array of words to highlight |
129
- | `highlightStyle` | `React.CSSProperties` | `{ backgroundColor: 'yellow', fontWeight: 'bold' }` | CSS styles to apply to highlighted text |
130
- | `caseSensitive` | `boolean` | `false` | Whether to perform case-sensitive matching |
125
+ | Prop | Type | Default | Description |
126
+ |-------------------|--------------------------------------------|-----------------------------------------------------|--------------------------------------------|
127
+ | `children` | `React.ReactElement \| null` | `''` | The content to process for highlighting |
128
+ | `words` | `string[] \| string \| RegExp \| RegExp[]` | `[]` | Word or array of words to highlight |
129
+ | `highlightStyle` | `React.CSSProperties` | `{ backgroundColor: 'yellow', fontWeight: 'bold' }` | CSS styles to apply to highlighted text |
130
+ | `caseSensitive` | `boolean` | `false` | Whether to perform case-sensitive matching |
131
+ | `isEscapePattern` | `boolean` | `true` | Mask regexp chars in words |
132
+ | `isWordBoundary` | `boolean` | `true` | Match words only at word boundaries |
131
133
 
132
134
  ## Examples
133
135
 
package/highlight.tsx CHANGED
@@ -2,9 +2,11 @@ import React from 'react';
2
2
 
3
3
  type Props = {
4
4
  children?: React.ReactElement | null;
5
- words?: string[] | string;
5
+ words?: string[] | string | RegExp | RegExp[];
6
6
  highlightStyle?: React.CSSProperties;
7
7
  caseSensitive?: boolean;
8
+ isEscapePattern?: boolean;
9
+ isWordBoundary?: boolean;
8
10
  }
9
11
 
10
12
  const TextHighlighter = ({
@@ -12,7 +14,9 @@ const TextHighlighter = ({
12
14
  words = [],
13
15
  highlightStyle = { backgroundColor: 'yellow', fontWeight: 'bold' },
14
16
  caseSensitive = false,
15
- }) => {
17
+ isEscapePattern = true,
18
+ isWordBoundary = true,
19
+ }: Props) => {
16
20
  // Convert words to array if it's a string
17
21
  const wordsArray = Array.isArray(words) ? words : [words];
18
22
 
@@ -22,14 +26,26 @@ const TextHighlighter = ({
22
26
  }
23
27
 
24
28
  // Function to escape regex special characters
25
- const escapeRegex = (string) => {
26
- return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
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
27
43
  };
28
44
 
29
45
  // Create a regex pattern for all words
30
46
  const pattern = wordsArray
31
- .filter(word => word && word.trim())
32
- .map(word => escapeRegex(word.trim()))
47
+ .filter(word => word)
48
+ .map(word => escapeRegex(word))
33
49
  .join('|');
34
50
 
35
51
  if (!pattern) {
@@ -45,13 +61,16 @@ const TextHighlighter = ({
45
61
  }
46
62
 
47
63
  const parts = textContent.split(regex);
48
-
64
+
49
65
  return parts.map((part, index) => {
50
- const shouldHighlight = wordsArray.some(word =>
51
- caseSensitive
66
+ const shouldHighlight = wordsArray.some(word => {
67
+ if (word instanceof RegExp) {
68
+ return word.test(part);
69
+ }
70
+ return caseSensitive
52
71
  ? part === word.trim()
53
- : part.toLowerCase() === word.trim().toLowerCase()
54
- );
72
+ : part.toLowerCase() === word.trim().toLowerCase();
73
+ });
55
74
 
56
75
  return shouldHighlight ? (
57
76
  <mark key={index} style={highlightStyle}>
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "react-highlight-me",
3
- "version": "1.0.0",
4
- "description": "Highligh words in React components or text",
3
+ "version": "1.1.0",
4
+ "description": "Highlight words in React components or text",
5
5
  "main": "highlight.tsx",
6
6
  "scripts": {
7
7
  "test": "echo \"Error: no test specified\" && exit 1"