react-highlight-me 2.2.3 → 2.3.1
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.
- package/README.md +3 -0
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +77 -40
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +78 -41
- package/dist/esm/index.js.map +1 -1
- package/package.json +15 -13
- package/src/index.tsx +87 -47
package/README.md
CHANGED
|
@@ -20,6 +20,9 @@ A flexible React component for highlighting specific words or phrases within tex
|
|
|
20
20
|
|
|
21
21
|
*) pnpm is a preferable package manager for me at the moment.
|
|
22
22
|
|
|
23
|
+
## [Demo](https://ahgpuk.github.io/react-highlight-me/)
|
|
24
|
+
|
|
25
|
+
|
|
23
26
|
## Usage
|
|
24
27
|
|
|
25
28
|
### Basic Text Highlighting
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ type Props = {
|
|
|
3
3
|
children?: React.ReactNode;
|
|
4
4
|
words?: string[] | string | RegExp | RegExp[];
|
|
5
5
|
highlightStyle?: React.CSSProperties;
|
|
6
|
-
caseSensitive?: boolean;
|
|
6
|
+
caseSensitive?: boolean | undefined;
|
|
7
7
|
isWordBoundary?: boolean;
|
|
8
8
|
isDebug?: boolean;
|
|
9
9
|
escapeRegex?: RegExp;
|
package/dist/cjs/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA8E,MAAM,OAAO,CAAC;AAEnG,KAAK,KAAK,GAAG;IACX,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAC3B,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC;IAC9C,cAAc,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC;IACrC,aAAa,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACpC,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAA;AAMD,eAAO,MAAM,aAAa,oCAAmC,CAAC;AAC9D,eAAO,MAAM,uBAAuB,wHAAkF,CAAC;AAEvH,UAAU,wBAAyB,SAAQ,KAAK,CAAC,yBAAyB,CAAC,KAAK,GAAG,KAAK,CAAC,aAAa,CAAC,cAAc,CAAC,CAAC;IACrH,uBAAuB,EAAE,MAAM,CAAC;IAChC,aAAa,EAAE,MAAM,CAAC;IACtB,qBAAqB,EAAE,MAAM,CAAC;CAC/B;AAED,QAAA,MAAM,eAAe,EAgQf,wBAAwB,CAAC;AAM/B,eAAe,eAAe,CAAC"}
|
package/dist/cjs/index.js
CHANGED
|
@@ -41,7 +41,7 @@ const ROOT_ELEMENT_SELECTOR = `[${ROOT_ELEMENT_ATTR}="${ROOT_ELEMENT_ID}"]`;
|
|
|
41
41
|
const MARK_ATTRIBUTE = 'data-highlighter';
|
|
42
42
|
exports.MARK_SELECTOR = `mark[${MARK_ATTRIBUTE}="true"]`;
|
|
43
43
|
exports.MARKS_IN_SCOPE_SELECTOR = `:scope ${exports.MARK_SELECTOR}:not(:scope ${ROOT_ELEMENT_SELECTOR} ${exports.MARK_SELECTOR})`;
|
|
44
|
-
const TextHighlighter = (0, react_1.forwardRef)(({ children, words = [], highlightStyle = { backgroundColor: 'yellow', fontWeight: 'bold' }, caseSensitive =
|
|
44
|
+
const TextHighlighter = (0, react_1.forwardRef)(({ children, words = [], highlightStyle = { backgroundColor: 'yellow', fontWeight: 'bold' }, caseSensitive = undefined, isWordBoundary = false, isDebug = false, escapeRegex = /[.*+?^${}()|[\]\\]/g, }, ref) => {
|
|
45
45
|
const containerRef = (0, react_1.useRef)(null);
|
|
46
46
|
const observerRef = (0, react_1.useRef)(null);
|
|
47
47
|
const lastHighlightSignature = (0, react_1.useRef)('');
|
|
@@ -49,15 +49,17 @@ const TextHighlighter = (0, react_1.forwardRef)(({ children, words = [], highlig
|
|
|
49
49
|
const currentProps = { words, highlightStyle, caseSensitive, isWordBoundary, escapeRegex };
|
|
50
50
|
const propsRef = (0, react_1.useRef)(currentProps);
|
|
51
51
|
propsRef.current = currentProps;
|
|
52
|
-
const nodeFilter = {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
52
|
+
const nodeFilter = (0, react_1.useMemo)(() => {
|
|
53
|
+
return {
|
|
54
|
+
acceptNode: (node) => {
|
|
55
|
+
if (!containerRef.current)
|
|
56
|
+
return NodeFilter.FILTER_SKIP;
|
|
57
|
+
return isInMyScope(containerRef.current, node, ROOT_ELEMENT_SELECTOR)
|
|
58
|
+
? NodeFilter.FILTER_ACCEPT
|
|
59
|
+
: NodeFilter.FILTER_SKIP;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}, [containerRef === null || containerRef === void 0 ? void 0 : containerRef.current]);
|
|
61
63
|
const getTextSignature = (0, react_1.useCallback)((element) => {
|
|
62
64
|
var _a;
|
|
63
65
|
// Create a signature of all text content to detect real changes
|
|
@@ -70,20 +72,24 @@ const TextHighlighter = (0, react_1.forwardRef)(({ children, words = [], highlig
|
|
|
70
72
|
}
|
|
71
73
|
}
|
|
72
74
|
return textParts.join('|');
|
|
73
|
-
}, []);
|
|
75
|
+
}, [nodeFilter]);
|
|
74
76
|
const highlightTextInElement = (0, react_1.useCallback)((element) => {
|
|
75
77
|
const { words, highlightStyle, caseSensitive, isWordBoundary, escapeRegex } = propsRef.current;
|
|
76
78
|
const wordsArray = Array.isArray(words) ? words : [words];
|
|
77
79
|
isDebug && console.log('Highlighting with words:', wordsArray);
|
|
78
80
|
// Remove existing highlights
|
|
79
81
|
const existingMarks = element.querySelectorAll(TextHighlighter.MARKS_IN_SCOPE_SELECTOR);
|
|
82
|
+
const elementsToNormalize = new Set();
|
|
80
83
|
existingMarks.forEach(mark => {
|
|
81
84
|
var _a;
|
|
82
85
|
const textContent = mark.textContent || '';
|
|
83
86
|
const textNode = document.createTextNode(textContent);
|
|
84
|
-
(
|
|
87
|
+
if (mark.parentNode) {
|
|
88
|
+
elementsToNormalize.add(mark.parentNode);
|
|
89
|
+
(_a = mark.parentNode) === null || _a === void 0 ? void 0 : _a.replaceChild(textNode, mark);
|
|
90
|
+
}
|
|
85
91
|
});
|
|
86
|
-
|
|
92
|
+
elementsToNormalize.forEach(node => node.normalize());
|
|
87
93
|
if (!wordsArray.length || wordsArray.every(word => !word)) {
|
|
88
94
|
lastHighlightSignature.current = getTextSignature(element);
|
|
89
95
|
return;
|
|
@@ -101,47 +107,77 @@ const TextHighlighter = (0, react_1.forwardRef)(({ children, words = [], highlig
|
|
|
101
107
|
const text = textNode.textContent || '';
|
|
102
108
|
if (!text)
|
|
103
109
|
return;
|
|
104
|
-
const
|
|
105
|
-
|
|
110
|
+
const regexes = [];
|
|
111
|
+
const stringTerms = wordsArray
|
|
112
|
+
.filter((word) => typeof word === 'string' && word !== '')
|
|
106
113
|
.map(word => {
|
|
107
|
-
if (word instanceof RegExp) {
|
|
108
|
-
return word.source;
|
|
109
|
-
}
|
|
110
114
|
let term = escapeRegex ? word.replace(escapeRegex, '\\$&') : word;
|
|
111
115
|
if (isWordBoundary) {
|
|
112
116
|
term = `\\b${term}\\b`;
|
|
113
117
|
}
|
|
114
118
|
return term;
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
|
|
119
|
+
});
|
|
120
|
+
if (stringTerms.length > 0) {
|
|
121
|
+
regexes.push(new RegExp(stringTerms.join('|'), !!caseSensitive ? 'g' : 'gi'));
|
|
122
|
+
}
|
|
123
|
+
wordsArray
|
|
124
|
+
.filter((word) => word instanceof RegExp)
|
|
125
|
+
.forEach(word => {
|
|
126
|
+
const flags = word.flags.includes('g') ? word.flags : word.flags + 'g';
|
|
127
|
+
regexes.push(new RegExp(word.source, flags));
|
|
128
|
+
});
|
|
129
|
+
if (regexes.length === 0)
|
|
118
130
|
return;
|
|
119
|
-
const
|
|
120
|
-
const
|
|
121
|
-
|
|
131
|
+
const ranges = [];
|
|
132
|
+
for (const regex of regexes) {
|
|
133
|
+
let match;
|
|
134
|
+
while ((match = regex.exec(text)) !== null) {
|
|
135
|
+
if (match[0].length === 0) {
|
|
136
|
+
regex.lastIndex++;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
ranges.push({ start: match.index, end: match.index + match[0].length });
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (ranges.length === 0)
|
|
143
|
+
return;
|
|
144
|
+
ranges.sort((a, b) => a.start - b.start || b.end - a.end);
|
|
145
|
+
const merged = [ranges[0]];
|
|
146
|
+
for (let i = 1; i < ranges.length; i++) {
|
|
147
|
+
const prev = merged[merged.length - 1];
|
|
148
|
+
if (ranges[i].start <= prev.end) {
|
|
149
|
+
prev.end = Math.max(prev.end, ranges[i].end);
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
merged.push(ranges[i]);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const parts = [];
|
|
156
|
+
let lastIndex = 0;
|
|
157
|
+
for (const { start, end } of merged) {
|
|
158
|
+
if (start > lastIndex) {
|
|
159
|
+
parts.push({ text: text.slice(lastIndex, start), isMatch: false });
|
|
160
|
+
}
|
|
161
|
+
parts.push({ text: text.slice(start, end), isMatch: true });
|
|
162
|
+
lastIndex = end;
|
|
163
|
+
}
|
|
164
|
+
if (lastIndex < text.length) {
|
|
165
|
+
parts.push({ text: text.slice(lastIndex), isMatch: false });
|
|
166
|
+
}
|
|
167
|
+
if (parts.some(p => p.isMatch)) {
|
|
122
168
|
const fragment = document.createDocumentFragment();
|
|
123
|
-
parts.forEach(
|
|
124
|
-
if (!
|
|
169
|
+
parts.forEach(({ text: partText, isMatch }) => {
|
|
170
|
+
if (!partText)
|
|
125
171
|
return;
|
|
126
|
-
|
|
127
|
-
if (word instanceof RegExp) {
|
|
128
|
-
const flags = caseSensitive ? word.flags : [...new Set([...word.flags.split(''), 'i'])].join('');
|
|
129
|
-
const testRegex = new RegExp(word.source, flags);
|
|
130
|
-
return testRegex.test(part);
|
|
131
|
-
}
|
|
132
|
-
return caseSensitive
|
|
133
|
-
? part === word
|
|
134
|
-
: part.toLowerCase() === word.toLowerCase();
|
|
135
|
-
});
|
|
136
|
-
if (shouldHighlight) {
|
|
172
|
+
if (isMatch) {
|
|
137
173
|
const mark = document.createElement('mark');
|
|
138
174
|
mark.setAttribute(MARK_ATTRIBUTE, 'true');
|
|
139
175
|
Object.assign(mark.style, highlightStyle);
|
|
140
|
-
mark.textContent =
|
|
176
|
+
mark.textContent = partText;
|
|
141
177
|
fragment.appendChild(mark);
|
|
142
178
|
}
|
|
143
179
|
else {
|
|
144
|
-
fragment.appendChild(document.createTextNode(
|
|
180
|
+
fragment.appendChild(document.createTextNode(partText));
|
|
145
181
|
}
|
|
146
182
|
});
|
|
147
183
|
(_a = textNode.parentNode) === null || _a === void 0 ? void 0 : _a.replaceChild(fragment, textNode);
|
|
@@ -188,7 +224,8 @@ const TextHighlighter = (0, react_1.forwardRef)(({ children, words = [], highlig
|
|
|
188
224
|
observerRef.current.observe(currentRef, {
|
|
189
225
|
childList: true,
|
|
190
226
|
subtree: true,
|
|
191
|
-
characterData: true
|
|
227
|
+
characterData: true,
|
|
228
|
+
attributes: true,
|
|
192
229
|
});
|
|
193
230
|
return () => {
|
|
194
231
|
if (observerRef.current) {
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.tsx"],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AuB,GAAG,+BAAuB,CAAC;AAElE,kBAAe,eAAe,CAAC;AAE/B,SAAS,WAAW,CAAC,QAAiB,EAAE,MAAY,EAAE,YAAoB;IACxE,uCAAuC;IACvC,MAAM,aAAa,GAAG,MAAM,YAAY,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC;IAChF,IAAI,CAAC,aAAa,EAAE,CAAC;QACnB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,0CAA0C;IAC1C,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,aAAa,CAAC,EAAE,CAAC;QACtC,OAAO,KAAK,CAAC;IACf,CAAC;IAED,+CAA+C;IAC/C,MAAM,yBAAyB,GAAG,UAAU,YAAY,eAAe,YAAY,IAAI,YAAY,GAAG,CAAC;IACvG,MAAM,iBAAiB,GAAG,QAAQ,CAAC,gBAAgB,CAAC,yBAAyB,CAAC,CAAC;IAE/E,uDAAuD;IACvD,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CACtD,UAAU,CAAC,QAAQ,CAAC,aAAa,CAAC,CACnC,CAAC;AACJ,CAAC"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ type Props = {
|
|
|
3
3
|
children?: React.ReactNode;
|
|
4
4
|
words?: string[] | string | RegExp | RegExp[];
|
|
5
5
|
highlightStyle?: React.CSSProperties;
|
|
6
|
-
caseSensitive?: boolean;
|
|
6
|
+
caseSensitive?: boolean | undefined;
|
|
7
7
|
isWordBoundary?: boolean;
|
|
8
8
|
isDebug?: boolean;
|
|
9
9
|
escapeRegex?: RegExp;
|
package/dist/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA8E,MAAM,OAAO,CAAC;AAEnG,KAAK,KAAK,GAAG;IACX,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAC3B,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC;IAC9C,cAAc,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC;IACrC,aAAa,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACpC,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAA;AAMD,eAAO,MAAM,aAAa,oCAAmC,CAAC;AAC9D,eAAO,MAAM,uBAAuB,wHAAkF,CAAC;AAEvH,UAAU,wBAAyB,SAAQ,KAAK,CAAC,yBAAyB,CAAC,KAAK,GAAG,KAAK,CAAC,aAAa,CAAC,cAAc,CAAC,CAAC;IACrH,uBAAuB,EAAE,MAAM,CAAC;IAChC,aAAa,EAAE,MAAM,CAAC;IACtB,qBAAqB,EAAE,MAAM,CAAC;CAC/B;AAED,QAAA,MAAM,eAAe,EAgQf,wBAAwB,CAAC;AAM/B,eAAe,eAAe,CAAC"}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import React, { useLayoutEffect, useRef, useCallback, useState, forwardRef } from 'react';
|
|
1
|
+
import React, { useLayoutEffect, useRef, useCallback, useState, forwardRef, useMemo } from 'react';
|
|
2
2
|
const ROOT_ELEMENT_ID = 'react-highlight-me';
|
|
3
3
|
const ROOT_ELEMENT_ATTR = 'data-id';
|
|
4
4
|
const ROOT_ELEMENT_SELECTOR = `[${ROOT_ELEMENT_ATTR}="${ROOT_ELEMENT_ID}"]`;
|
|
5
5
|
const MARK_ATTRIBUTE = 'data-highlighter';
|
|
6
6
|
export const MARK_SELECTOR = `mark[${MARK_ATTRIBUTE}="true"]`;
|
|
7
7
|
export const MARKS_IN_SCOPE_SELECTOR = `:scope ${MARK_SELECTOR}:not(:scope ${ROOT_ELEMENT_SELECTOR} ${MARK_SELECTOR})`;
|
|
8
|
-
const TextHighlighter = forwardRef(({ children, words = [], highlightStyle = { backgroundColor: 'yellow', fontWeight: 'bold' }, caseSensitive =
|
|
8
|
+
const TextHighlighter = forwardRef(({ children, words = [], highlightStyle = { backgroundColor: 'yellow', fontWeight: 'bold' }, caseSensitive = undefined, isWordBoundary = false, isDebug = false, escapeRegex = /[.*+?^${}()|[\]\\]/g, }, ref) => {
|
|
9
9
|
const containerRef = useRef(null);
|
|
10
10
|
const observerRef = useRef(null);
|
|
11
11
|
const lastHighlightSignature = useRef('');
|
|
@@ -13,15 +13,17 @@ const TextHighlighter = forwardRef(({ children, words = [], highlightStyle = { b
|
|
|
13
13
|
const currentProps = { words, highlightStyle, caseSensitive, isWordBoundary, escapeRegex };
|
|
14
14
|
const propsRef = useRef(currentProps);
|
|
15
15
|
propsRef.current = currentProps;
|
|
16
|
-
const nodeFilter = {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
16
|
+
const nodeFilter = useMemo(() => {
|
|
17
|
+
return {
|
|
18
|
+
acceptNode: (node) => {
|
|
19
|
+
if (!containerRef.current)
|
|
20
|
+
return NodeFilter.FILTER_SKIP;
|
|
21
|
+
return isInMyScope(containerRef.current, node, ROOT_ELEMENT_SELECTOR)
|
|
22
|
+
? NodeFilter.FILTER_ACCEPT
|
|
23
|
+
: NodeFilter.FILTER_SKIP;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}, [containerRef === null || containerRef === void 0 ? void 0 : containerRef.current]);
|
|
25
27
|
const getTextSignature = useCallback((element) => {
|
|
26
28
|
var _a;
|
|
27
29
|
// Create a signature of all text content to detect real changes
|
|
@@ -34,20 +36,24 @@ const TextHighlighter = forwardRef(({ children, words = [], highlightStyle = { b
|
|
|
34
36
|
}
|
|
35
37
|
}
|
|
36
38
|
return textParts.join('|');
|
|
37
|
-
}, []);
|
|
39
|
+
}, [nodeFilter]);
|
|
38
40
|
const highlightTextInElement = useCallback((element) => {
|
|
39
41
|
const { words, highlightStyle, caseSensitive, isWordBoundary, escapeRegex } = propsRef.current;
|
|
40
42
|
const wordsArray = Array.isArray(words) ? words : [words];
|
|
41
43
|
isDebug && console.log('Highlighting with words:', wordsArray);
|
|
42
44
|
// Remove existing highlights
|
|
43
45
|
const existingMarks = element.querySelectorAll(TextHighlighter.MARKS_IN_SCOPE_SELECTOR);
|
|
46
|
+
const elementsToNormalize = new Set();
|
|
44
47
|
existingMarks.forEach(mark => {
|
|
45
48
|
var _a;
|
|
46
49
|
const textContent = mark.textContent || '';
|
|
47
50
|
const textNode = document.createTextNode(textContent);
|
|
48
|
-
(
|
|
51
|
+
if (mark.parentNode) {
|
|
52
|
+
elementsToNormalize.add(mark.parentNode);
|
|
53
|
+
(_a = mark.parentNode) === null || _a === void 0 ? void 0 : _a.replaceChild(textNode, mark);
|
|
54
|
+
}
|
|
49
55
|
});
|
|
50
|
-
|
|
56
|
+
elementsToNormalize.forEach(node => node.normalize());
|
|
51
57
|
if (!wordsArray.length || wordsArray.every(word => !word)) {
|
|
52
58
|
lastHighlightSignature.current = getTextSignature(element);
|
|
53
59
|
return;
|
|
@@ -65,47 +71,77 @@ const TextHighlighter = forwardRef(({ children, words = [], highlightStyle = { b
|
|
|
65
71
|
const text = textNode.textContent || '';
|
|
66
72
|
if (!text)
|
|
67
73
|
return;
|
|
68
|
-
const
|
|
69
|
-
|
|
74
|
+
const regexes = [];
|
|
75
|
+
const stringTerms = wordsArray
|
|
76
|
+
.filter((word) => typeof word === 'string' && word !== '')
|
|
70
77
|
.map(word => {
|
|
71
|
-
if (word instanceof RegExp) {
|
|
72
|
-
return word.source;
|
|
73
|
-
}
|
|
74
78
|
let term = escapeRegex ? word.replace(escapeRegex, '\\$&') : word;
|
|
75
79
|
if (isWordBoundary) {
|
|
76
80
|
term = `\\b${term}\\b`;
|
|
77
81
|
}
|
|
78
82
|
return term;
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
|
|
83
|
+
});
|
|
84
|
+
if (stringTerms.length > 0) {
|
|
85
|
+
regexes.push(new RegExp(stringTerms.join('|'), !!caseSensitive ? 'g' : 'gi'));
|
|
86
|
+
}
|
|
87
|
+
wordsArray
|
|
88
|
+
.filter((word) => word instanceof RegExp)
|
|
89
|
+
.forEach(word => {
|
|
90
|
+
const flags = word.flags.includes('g') ? word.flags : word.flags + 'g';
|
|
91
|
+
regexes.push(new RegExp(word.source, flags));
|
|
92
|
+
});
|
|
93
|
+
if (regexes.length === 0)
|
|
94
|
+
return;
|
|
95
|
+
const ranges = [];
|
|
96
|
+
for (const regex of regexes) {
|
|
97
|
+
let match;
|
|
98
|
+
while ((match = regex.exec(text)) !== null) {
|
|
99
|
+
if (match[0].length === 0) {
|
|
100
|
+
regex.lastIndex++;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
ranges.push({ start: match.index, end: match.index + match[0].length });
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (ranges.length === 0)
|
|
82
107
|
return;
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
108
|
+
ranges.sort((a, b) => a.start - b.start || b.end - a.end);
|
|
109
|
+
const merged = [ranges[0]];
|
|
110
|
+
for (let i = 1; i < ranges.length; i++) {
|
|
111
|
+
const prev = merged[merged.length - 1];
|
|
112
|
+
if (ranges[i].start <= prev.end) {
|
|
113
|
+
prev.end = Math.max(prev.end, ranges[i].end);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
merged.push(ranges[i]);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const parts = [];
|
|
120
|
+
let lastIndex = 0;
|
|
121
|
+
for (const { start, end } of merged) {
|
|
122
|
+
if (start > lastIndex) {
|
|
123
|
+
parts.push({ text: text.slice(lastIndex, start), isMatch: false });
|
|
124
|
+
}
|
|
125
|
+
parts.push({ text: text.slice(start, end), isMatch: true });
|
|
126
|
+
lastIndex = end;
|
|
127
|
+
}
|
|
128
|
+
if (lastIndex < text.length) {
|
|
129
|
+
parts.push({ text: text.slice(lastIndex), isMatch: false });
|
|
130
|
+
}
|
|
131
|
+
if (parts.some(p => p.isMatch)) {
|
|
86
132
|
const fragment = document.createDocumentFragment();
|
|
87
|
-
parts.forEach(
|
|
88
|
-
if (!
|
|
133
|
+
parts.forEach(({ text: partText, isMatch }) => {
|
|
134
|
+
if (!partText)
|
|
89
135
|
return;
|
|
90
|
-
|
|
91
|
-
if (word instanceof RegExp) {
|
|
92
|
-
const flags = caseSensitive ? word.flags : [...new Set([...word.flags.split(''), 'i'])].join('');
|
|
93
|
-
const testRegex = new RegExp(word.source, flags);
|
|
94
|
-
return testRegex.test(part);
|
|
95
|
-
}
|
|
96
|
-
return caseSensitive
|
|
97
|
-
? part === word
|
|
98
|
-
: part.toLowerCase() === word.toLowerCase();
|
|
99
|
-
});
|
|
100
|
-
if (shouldHighlight) {
|
|
136
|
+
if (isMatch) {
|
|
101
137
|
const mark = document.createElement('mark');
|
|
102
138
|
mark.setAttribute(MARK_ATTRIBUTE, 'true');
|
|
103
139
|
Object.assign(mark.style, highlightStyle);
|
|
104
|
-
mark.textContent =
|
|
140
|
+
mark.textContent = partText;
|
|
105
141
|
fragment.appendChild(mark);
|
|
106
142
|
}
|
|
107
143
|
else {
|
|
108
|
-
fragment.appendChild(document.createTextNode(
|
|
144
|
+
fragment.appendChild(document.createTextNode(partText));
|
|
109
145
|
}
|
|
110
146
|
});
|
|
111
147
|
(_a = textNode.parentNode) === null || _a === void 0 ? void 0 : _a.replaceChild(fragment, textNode);
|
|
@@ -152,7 +188,8 @@ const TextHighlighter = forwardRef(({ children, words = [], highlightStyle = { b
|
|
|
152
188
|
observerRef.current.observe(currentRef, {
|
|
153
189
|
childList: true,
|
|
154
190
|
subtree: true,
|
|
155
|
-
characterData: true
|
|
191
|
+
characterData: true,
|
|
192
|
+
attributes: true,
|
|
156
193
|
});
|
|
157
194
|
return () => {
|
|
158
195
|
if (observerRef.current) {
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,eAAe,EAAE,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,eAAe,EAAE,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAYnG,MAAM,eAAe,GAAG,oBAAoB,CAAC;AAC7C,MAAM,iBAAiB,GAAG,SAAS,CAAC;AACpC,MAAM,qBAAqB,GAAG,IAAI,iBAAiB,KAAK,eAAe,IAAI,CAAC;AAC5E,MAAM,cAAc,GAAG,kBAAkB,CAAC;AAC1C,MAAM,CAAC,MAAM,aAAa,GAAG,QAAQ,cAAc,UAAU,CAAC;AAC9D,MAAM,CAAC,MAAM,uBAAuB,GAAG,UAAU,aAAa,eAAe,qBAAqB,IAAI,aAAa,GAAG,CAAC;AAQvH,MAAM,eAAe,GAAG,UAAU,CAAwB,CAAC,EACzD,QAAQ,EACR,KAAK,GAAG,EAAE,EACV,cAAc,GAAG,EAAE,eAAe,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,EAAE,EAClE,aAAa,GAAG,SAAS,EACzB,cAAc,GAAG,KAAK,EACtB,OAAO,GAAG,KAAK,EACf,WAAW,GAAG,qBAAqB,GACpC,EAAE,GAAG,EAAE,EAAE;IACR,MAAM,YAAY,GAAG,MAAM,CAAqB,IAAI,CAAC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
"description": "Highlight words in React components or text",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -22,26 +22,28 @@
|
|
|
22
22
|
"src",
|
|
23
23
|
"README.md"
|
|
24
24
|
],
|
|
25
|
-
"keywords": [],
|
|
26
|
-
"author": "Andrey Hohutkin <Andrey.Hohutkin@gmail.com",
|
|
27
|
-
"repository": {
|
|
28
|
-
"type": "git",
|
|
29
|
-
"url": "git+https://github.com/AHgPuK/react-highlight-me.git"
|
|
30
|
-
},
|
|
31
|
-
"license": "ISC",
|
|
32
|
-
"devDependencies": {
|
|
33
|
-
"@types/react": "^19.1.8",
|
|
34
|
-
"typescript": "^5.3.3"
|
|
35
|
-
},
|
|
36
25
|
"scripts": {
|
|
37
26
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
38
27
|
"build": "npm run build:cjs && npm run build:esm",
|
|
39
28
|
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
40
29
|
"build:esm": "tsc -p tsconfig.esm.json",
|
|
41
30
|
"clean": "rm -rf dist",
|
|
31
|
+
"prepublishOnly": "npm run build",
|
|
42
32
|
"pack:check": "npm pack && tar -tzf *.tgz && rm *.tgz",
|
|
43
33
|
"demo:dev": "cd demo && pnpm run dev",
|
|
44
34
|
"demo:build": "pnpm run build && cd demo && pnpm run build",
|
|
45
35
|
"demo:preview": "cd demo && pnpm run preview"
|
|
36
|
+
},
|
|
37
|
+
"keywords": [],
|
|
38
|
+
"author": "Andrey Hohutkin <Andrey.Hohutkin@gmail.com>",
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/AHgPuK/react-highlight-me.git"
|
|
42
|
+
},
|
|
43
|
+
"license": "ISC",
|
|
44
|
+
"packageManager": "pnpm@10.9.0",
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/react": "^19.1.8",
|
|
47
|
+
"typescript": "^5.3.3"
|
|
46
48
|
}
|
|
47
|
-
}
|
|
49
|
+
}
|
package/src/index.tsx
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import React, { useLayoutEffect, useRef, useCallback, useState, forwardRef } from 'react';
|
|
1
|
+
import React, { useLayoutEffect, useRef, useCallback, useState, forwardRef, useMemo } from 'react';
|
|
2
2
|
|
|
3
3
|
type Props = {
|
|
4
4
|
children?: React.ReactNode;
|
|
5
5
|
words?: string[] | string | RegExp | RegExp[];
|
|
6
6
|
highlightStyle?: React.CSSProperties;
|
|
7
|
-
caseSensitive?: boolean;
|
|
7
|
+
caseSensitive?: boolean | undefined;
|
|
8
8
|
isWordBoundary?: boolean;
|
|
9
9
|
isDebug?: boolean;
|
|
10
10
|
escapeRegex?: RegExp;
|
|
@@ -27,7 +27,7 @@ const TextHighlighter = forwardRef<HTMLDivElement, Props>(({
|
|
|
27
27
|
children,
|
|
28
28
|
words = [],
|
|
29
29
|
highlightStyle = { backgroundColor: 'yellow', fontWeight: 'bold' },
|
|
30
|
-
caseSensitive =
|
|
30
|
+
caseSensitive = undefined,
|
|
31
31
|
isWordBoundary = false,
|
|
32
32
|
isDebug = false,
|
|
33
33
|
escapeRegex = /[.*+?^${}()|[\]\\]/g,
|
|
@@ -41,15 +41,17 @@ const TextHighlighter = forwardRef<HTMLDivElement, Props>(({
|
|
|
41
41
|
const propsRef = useRef(currentProps);
|
|
42
42
|
propsRef.current = currentProps;
|
|
43
43
|
|
|
44
|
-
const nodeFilter = {
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
const nodeFilter = useMemo(() => {
|
|
45
|
+
return {
|
|
46
|
+
acceptNode: (node: Node) => {
|
|
47
|
+
if (!containerRef.current) return NodeFilter.FILTER_SKIP;
|
|
47
48
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
return isInMyScope(containerRef.current, node, ROOT_ELEMENT_SELECTOR)
|
|
50
|
+
? NodeFilter.FILTER_ACCEPT
|
|
51
|
+
: NodeFilter.FILTER_SKIP;
|
|
52
|
+
}
|
|
51
53
|
}
|
|
52
|
-
}
|
|
54
|
+
}, [containerRef?.current]);
|
|
53
55
|
|
|
54
56
|
const getTextSignature = useCallback((element: HTMLElement): string => {
|
|
55
57
|
// Create a signature of all text content to detect real changes
|
|
@@ -68,7 +70,7 @@ const TextHighlighter = forwardRef<HTMLDivElement, Props>(({
|
|
|
68
70
|
}
|
|
69
71
|
|
|
70
72
|
return textParts.join('|');
|
|
71
|
-
}, []);
|
|
73
|
+
}, [nodeFilter]);
|
|
72
74
|
|
|
73
75
|
const highlightTextInElement = useCallback((element: HTMLElement) => {
|
|
74
76
|
const { words, highlightStyle, caseSensitive, isWordBoundary, escapeRegex } = propsRef.current;
|
|
@@ -78,13 +80,17 @@ const TextHighlighter = forwardRef<HTMLDivElement, Props>(({
|
|
|
78
80
|
|
|
79
81
|
// Remove existing highlights
|
|
80
82
|
const existingMarks = element.querySelectorAll(TextHighlighter.MARKS_IN_SCOPE_SELECTOR);
|
|
83
|
+
const elementsToNormalize = new Set<Node>();
|
|
81
84
|
existingMarks.forEach(mark => {
|
|
82
85
|
const textContent = mark.textContent || '';
|
|
83
86
|
const textNode = document.createTextNode(textContent);
|
|
84
|
-
mark.parentNode
|
|
87
|
+
if (mark.parentNode) {
|
|
88
|
+
elementsToNormalize.add(mark.parentNode);
|
|
89
|
+
mark.parentNode?.replaceChild(textNode, mark);
|
|
90
|
+
}
|
|
85
91
|
});
|
|
86
92
|
|
|
87
|
-
|
|
93
|
+
elementsToNormalize.forEach(node => node.normalize());
|
|
88
94
|
|
|
89
95
|
if (!wordsArray.length || wordsArray.every(word => !word)) {
|
|
90
96
|
lastHighlightSignature.current = getTextSignature(element);
|
|
@@ -110,50 +116,83 @@ const TextHighlighter = forwardRef<HTMLDivElement, Props>(({
|
|
|
110
116
|
const text = textNode.textContent || '';
|
|
111
117
|
if (!text) return;
|
|
112
118
|
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
119
|
+
const regexes: RegExp[] = [];
|
|
120
|
+
|
|
121
|
+
const stringTerms = wordsArray
|
|
122
|
+
.filter((word): word is string => typeof word === 'string' && word !== '')
|
|
123
|
+
.map(word => {
|
|
124
|
+
let term = escapeRegex ? word.replace(escapeRegex, '\\$&') : word;
|
|
125
|
+
if (isWordBoundary) {
|
|
126
|
+
term = `\\b${term}\\b`;
|
|
127
|
+
}
|
|
128
|
+
return term;
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
if (stringTerms.length > 0) {
|
|
132
|
+
regexes.push(new RegExp(stringTerms.join('|'), !!caseSensitive ? 'g' : 'gi'));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
wordsArray
|
|
136
|
+
.filter((word): word is RegExp => word instanceof RegExp)
|
|
137
|
+
.forEach(word => {
|
|
138
|
+
const flags = word.flags.includes('g') ? word.flags : word.flags + 'g';
|
|
139
|
+
regexes.push(new RegExp(word.source, flags));
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
if (regexes.length === 0) return;
|
|
143
|
+
|
|
144
|
+
const ranges: { start: number; end: number }[] = [];
|
|
145
|
+
for (const regex of regexes) {
|
|
146
|
+
let match;
|
|
147
|
+
while ((match = regex.exec(text)) !== null) {
|
|
148
|
+
if (match[0].length === 0) {
|
|
149
|
+
regex.lastIndex++;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
ranges.push({ start: match.index, end: match.index + match[0].length });
|
|
122
153
|
}
|
|
123
|
-
|
|
124
|
-
})
|
|
125
|
-
.join('|');
|
|
154
|
+
}
|
|
126
155
|
|
|
127
|
-
if (
|
|
156
|
+
if (ranges.length === 0) return;
|
|
128
157
|
|
|
129
|
-
|
|
130
|
-
const
|
|
158
|
+
ranges.sort((a, b) => a.start - b.start || b.end - a.end);
|
|
159
|
+
const merged: { start: number; end: number }[] = [ranges[0]];
|
|
160
|
+
for (let i = 1; i < ranges.length; i++) {
|
|
161
|
+
const prev = merged[merged.length - 1];
|
|
162
|
+
if (ranges[i].start <= prev.end) {
|
|
163
|
+
prev.end = Math.max(prev.end, ranges[i].end);
|
|
164
|
+
} else {
|
|
165
|
+
merged.push(ranges[i]);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
131
168
|
|
|
132
|
-
|
|
169
|
+
const parts: { text: string; isMatch: boolean }[] = [];
|
|
170
|
+
let lastIndex = 0;
|
|
171
|
+
for (const { start, end } of merged) {
|
|
172
|
+
if (start > lastIndex) {
|
|
173
|
+
parts.push({ text: text.slice(lastIndex, start), isMatch: false });
|
|
174
|
+
}
|
|
175
|
+
parts.push({ text: text.slice(start, end), isMatch: true });
|
|
176
|
+
lastIndex = end;
|
|
177
|
+
}
|
|
178
|
+
if (lastIndex < text.length) {
|
|
179
|
+
parts.push({ text: text.slice(lastIndex), isMatch: false });
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (parts.some(p => p.isMatch)) {
|
|
133
183
|
const fragment = document.createDocumentFragment();
|
|
134
184
|
|
|
135
|
-
parts.forEach(
|
|
136
|
-
if (!
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (word instanceof RegExp) {
|
|
140
|
-
const flags = caseSensitive ? word.flags : [...new Set([...word.flags.split(''), 'i'])].join('');
|
|
141
|
-
const testRegex = new RegExp(word.source, flags);
|
|
142
|
-
return testRegex.test(part);
|
|
143
|
-
}
|
|
144
|
-
return caseSensitive
|
|
145
|
-
? part === word
|
|
146
|
-
: part.toLowerCase() === word.toLowerCase();
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
if (shouldHighlight) {
|
|
185
|
+
parts.forEach(({ text: partText, isMatch }) => {
|
|
186
|
+
if (!partText) return;
|
|
187
|
+
|
|
188
|
+
if (isMatch) {
|
|
150
189
|
const mark = document.createElement('mark');
|
|
151
190
|
mark.setAttribute(MARK_ATTRIBUTE, 'true');
|
|
152
191
|
Object.assign(mark.style, highlightStyle);
|
|
153
|
-
mark.textContent =
|
|
192
|
+
mark.textContent = partText;
|
|
154
193
|
fragment.appendChild(mark);
|
|
155
194
|
} else {
|
|
156
|
-
fragment.appendChild(document.createTextNode(
|
|
195
|
+
fragment.appendChild(document.createTextNode(partText));
|
|
157
196
|
}
|
|
158
197
|
});
|
|
159
198
|
|
|
@@ -209,7 +248,8 @@ const TextHighlighter = forwardRef<HTMLDivElement, Props>(({
|
|
|
209
248
|
observerRef.current.observe(currentRef, {
|
|
210
249
|
childList: true,
|
|
211
250
|
subtree: true,
|
|
212
|
-
characterData: true
|
|
251
|
+
characterData: true,
|
|
252
|
+
attributes: true,
|
|
213
253
|
});
|
|
214
254
|
|
|
215
255
|
return () => {
|