Skip to content

Commit c9d7d12

Browse files
Merge pull request #138 from istex/preserve-term-in-attribute
pass unitext term to attributes
2 parents 22ec5b6 + 0796e6f commit c9d7d12

3 files changed

Lines changed: 99 additions & 50 deletions

File tree

packages/react-tei/src/unitex/enrichDocumentWithUnitex.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export const enrichDocumentWithUnitex = (
6969
const sortedTerms = [...terms].sort((a, b) => b.term.length - a.term.length);
7070
const termRegexes = sortedTerms.map(({ term, groups, subTerms }) => ({
7171
termRegex: termToRegex(term),
72+
term,
7273
groups,
7374
value: subTerms?.length ? subTerms.map(termToTag) : term,
7475
}));

packages/react-tei/src/unitex/highlightTermsInTextTag.spec.tsx

Lines changed: 82 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@ import {
55
highlightTermInString,
66
highlightTermInTextTag,
77
highlightTermsInTextTag,
8+
type TermData,
89
type TextTag,
910
} from "./highlightTermsInTextTag";
1011

1112
describe("highlightTermInTextTag", () => {
1213
describe("highlightTermInString", () => {
1314
it("should return original fragment in at #text tag if termsRegexp has no match", () => {
1415
const text = "This is a sample text.";
15-
const term: RegExp = /^search text$/gi;
16-
const result = highlightTermInString(text, term, ["word"], undefined);
16+
const termData: TermData = {
17+
termRegex: /^search text$/gi,
18+
term: "search text",
19+
groups: ["word"],
20+
};
21+
const result = highlightTermInString(text, termData);
1722
expect(result).toStrictEqual([
1823
{
1924
tag: "#text",
@@ -24,8 +29,13 @@ describe("highlightTermInTextTag", () => {
2429

2530
it("should highlight single term in text", () => {
2631
const text = "This is a sample text.";
27-
const term: RegExp = /sample/gi;
28-
const result = highlightTermInString(text, term, ["word"], undefined);
32+
const termData: TermData = {
33+
termRegex: /sample/gi,
34+
term: "sample",
35+
groups: ["word"],
36+
};
37+
38+
const result = highlightTermInString(text, termData);
2939

3040
expect(result).toStrictEqual([
3141
{
@@ -51,8 +61,13 @@ describe("highlightTermInTextTag", () => {
5161

5262
it("should highlight all occurrences of a single term in text", () => {
5363
const text = "This is a sample text for testing called sample.";
54-
const term: RegExp = /sample/gi;
55-
const result = highlightTermInString(text, term, ["group"], undefined);
64+
65+
const termData: TermData = {
66+
termRegex: /sample/gi,
67+
term: "sample",
68+
groups: ["group"],
69+
};
70+
const result = highlightTermInString(text, termData);
5671
expect(result).toStrictEqual([
5772
{
5873
tag: "#text",
@@ -75,8 +90,12 @@ describe("highlightTermInTextTag", () => {
7590

7691
it("should preserve space between words when highlighting", () => {
7792
const text = "Highlight this term.";
78-
const term: RegExp = /this/gi;
79-
const result = highlightTermInString(text, term, ["group"], undefined);
93+
const termData: TermData = {
94+
termRegex: /this/gi,
95+
term: "this",
96+
groups: ["group"],
97+
};
98+
const result = highlightTermInString(text, termData);
8099
expect(result).toStrictEqual([
81100
{ tag: "#text", value: "Highlight " },
82101
{
@@ -92,6 +111,30 @@ describe("highlightTermInTextTag", () => {
92111
{ tag: "#text", value: " term." },
93112
]);
94113
});
114+
115+
it("should set term in attributes term and keep matched value in value", () => {
116+
const text = "Testing matching\nTERM with complex regex.";
117+
const termData: TermData = {
118+
termRegex: /matching\sterm/gi,
119+
term: "term",
120+
groups: ["group"],
121+
};
122+
const result = highlightTermInString(text, termData);
123+
expect(result).toStrictEqual([
124+
{ tag: "#text", value: "Testing " },
125+
{
126+
tag: "highlight",
127+
value: [
128+
{
129+
tag: "#text",
130+
value: "matching\nTERM",
131+
},
132+
],
133+
attributes: { groups: ["group"], term: "term" },
134+
},
135+
{ tag: "#text", value: " with complex regex." },
136+
]);
137+
});
95138
});
96139

97140
describe("highlightTermInTextTag", () => {
@@ -116,8 +159,12 @@ describe("highlightTermInTextTag", () => {
116159
} as HighlightTag,
117160
{ tag: "#text", value: "Another sample here." },
118161
];
119-
const term: RegExp = /sample/gi;
120-
const result = highlightTermInTextTag(fragments, term, ["group2"]);
162+
const termData: TermData = {
163+
termRegex: /sample/gi,
164+
term: "sample",
165+
groups: ["group2"],
166+
};
167+
const result = highlightTermInTextTag(fragments, termData);
121168
expect(result).toStrictEqual([
122169
{
123170
tag: "#text",
@@ -190,8 +237,12 @@ describe("highlightTermInTextTag", () => {
190237
value: "Another sample here.",
191238
},
192239
];
193-
const term: RegExp = /sample/gi;
194-
const result = highlightTermInTextTag(fragments, term, ["group2"]);
240+
const termData: TermData = {
241+
termRegex: /sample/gi,
242+
term: "sample",
243+
groups: ["group2"],
244+
};
245+
const result = highlightTermInTextTag(fragments, termData);
195246
expect(result).toStrictEqual([
196247
{
197248
tag: "#text",
@@ -234,8 +285,12 @@ describe("highlightTermInTextTag", () => {
234285

235286
it("should return empty array when given empty fragments", () => {
236287
const fragments: (HighlightTag | TextTag)[] = [];
237-
const term: RegExp = /sample/gi;
238-
const result = highlightTermInTextTag(fragments, term, ["group"]);
288+
const termData: TermData = {
289+
termRegex: /sample/gi,
290+
term: "sample",
291+
groups: ["group"],
292+
};
293+
const result = highlightTermInTextTag(fragments, termData);
239294
expect(result).toStrictEqual([]);
240295
});
241296
});
@@ -248,12 +303,12 @@ describe("highlightTermInTextTag", () => {
248303
},
249304
value: "This is a sample text for testing.",
250305
};
251-
const terms = [
252-
{ termRegex: /sample/gi, groups: ["group1"] },
253-
{ termRegex: /testing/gi, groups: ["group2"] },
254-
{ termRegex: /example/gi, groups: ["group3"] },
306+
const termDataList = [
307+
{ termRegex: /sample/gi, term: "sample", groups: ["group1"] },
308+
{ termRegex: /testing/gi, term: "testing", groups: ["group2"] },
309+
{ termRegex: /example/gi, term: "example", groups: ["group3"] },
255310
];
256-
const result = highlightTermsInTextTag(textTag, terms);
311+
const result = highlightTermsInTextTag(textTag, termDataList);
257312
expect(result).toStrictEqual({
258313
tag: "highlightedText",
259314
attributes: { lang: "en" },
@@ -302,11 +357,11 @@ describe("highlightTermInTextTag", () => {
302357
},
303358
value: "This is a sample text for testing.",
304359
};
305-
const terms = [
306-
{ termRegex: /example/gi, groups: ["group1"] },
307-
{ termRegex: /demo/gi, groups: ["group2"] },
360+
const termDataList: TermData[] = [
361+
{ termRegex: /example/gi, term: "example", groups: ["group1"] },
362+
{ termRegex: /demo/gi, term: "demo", groups: ["group2"] },
308363
];
309-
const result = highlightTermsInTextTag(fragments as any, terms);
364+
const result = highlightTermsInTextTag(fragments as any, termDataList);
310365
expect(result).toStrictEqual({
311366
tag: "highlightedText",
312367
attributes: { lang: "en" },
@@ -346,11 +401,11 @@ describe("highlightTermInTextTag", () => {
346401
tag: "#text",
347402
value: "Term1 Term2",
348403
};
349-
const terms = [
350-
{ termRegex: /Term1/gi, groups: ["group1"] },
351-
{ termRegex: /Term2/gi, groups: ["group2"] },
404+
const termDataList: TermData[] = [
405+
{ termRegex: /Term1/gi, term: "term1", groups: ["group1"] },
406+
{ termRegex: /Term2/gi, term: "term2", groups: ["group2"] },
352407
];
353-
const result = highlightTermsInTextTag(fragments, terms);
408+
const result = highlightTermsInTextTag(fragments, termDataList);
354409
expect(result).toStrictEqual({
355410
tag: "highlightedText",
356411
attributes: undefined,

packages/react-tei/src/unitex/highlightTermsInTextTag.tsx

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,22 @@ export type HighlightedTextTag = {
2323
value: (HighlightTag | TextTag)[];
2424
} & DocumentJson;
2525

26+
export type TermData = {
27+
termRegex: RegExp;
28+
term: string;
29+
groups: string[];
30+
value?: (TextTag | HighlightTag)[] | string;
31+
};
32+
2633
export const isTextTag = (node: DocumentJson): node is TextTag => {
2734
return node.tag === "#text";
2835
};
2936

3037
export const highlightTermInString = (
3138
text: string,
32-
termRegex: RegExp,
33-
groups: string[],
34-
value: string | (TextTag | HighlightTag)[] | undefined,
39+
termData: TermData,
3540
): (TextTag | HighlightTag)[] => {
41+
const { termRegex, term, groups, value } = termData;
3642
const matches = Array.from(text.matchAll(termRegex));
3743

3844
if (!matches.length) {
@@ -70,7 +76,7 @@ export const highlightTermInString = (
7076
],
7177
attributes: {
7278
groups,
73-
term: kebabCasify(matchText),
79+
term: kebabCasify(term),
7480
},
7581
} as HighlightTag);
7682

@@ -92,9 +98,7 @@ export const highlightTermInString = (
9298

9399
export const highlightTermInTextTag = (
94100
textFragments: (HighlightTag | TextTag)[],
95-
termRegex: RegExp,
96-
groups: string[],
97-
value?: string | (TextTag | HighlightTag)[],
101+
termData: TermData,
98102
): (HighlightTag | TextTag)[] => {
99103
const stack = [...textFragments];
100104
const result: (HighlightTag | TextTag)[] = [];
@@ -108,32 +112,21 @@ export const highlightTermInTextTag = (
108112
value: textFragment.value,
109113
});
110114
} else {
111-
const highlighted = highlightTermInString(
112-
textFragment.value,
113-
termRegex,
114-
groups,
115-
value,
116-
);
115+
const highlighted = highlightTermInString(textFragment.value, termData);
117116
result.push(...highlighted);
118117
}
119118
}
120119

121120
return result;
122121
};
123122

124-
type TermRegex = {
125-
termRegex: RegExp;
126-
groups: string[];
127-
value?: (TextTag | HighlightTag)[] | string;
128-
};
129-
130123
export const highlightTermsInTextTag = (
131124
textTag: TextTag,
132-
termRegexes: TermRegex[],
125+
termDataList: TermData[],
133126
): HighlightedTextTag => {
134-
const value = termRegexes.reduce(
135-
(textTag: (HighlightTag | TextTag)[], { termRegex, groups, value }) =>
136-
highlightTermInTextTag(textTag, termRegex, groups, value),
127+
const value = termDataList.reduce(
128+
(textTag: (HighlightTag | TextTag)[], termData) =>
129+
highlightTermInTextTag(textTag, termData),
137130
[textTag] as (HighlightTag | TextTag)[],
138131
);
139132

0 commit comments

Comments
 (0)