searchShowRed.ets
2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { Logger } from "wdKit";
export interface textItem {
content: string,
isRed: boolean
}
export interface titleInitRes {
titleMarked: boolean,
textArr: textItem[]
}
const TAG = "SearchShowRed"
export class SearchShowRed {
// title: this.contentDTO.title
static titleInit(title: string) {
let titleMarked: boolean = false;
let textArr: textItem[] = [];
if (title?.includes('<em>') && title?.includes('</em>')) {
titleMarked = true;
const html: string = title;
const pattern = /<[a-z]+[1-6]?\b[^>]*>(.*?)<\/[a-z]+[1-6]?>/g;
const res: string[] = [];
let match: RegExpExecArray | null;
while ((match = pattern.exec(html)) !== null) {
const content: string = match[1].trim();
res.push(content);
}
Logger.debugOptimize(TAG, () => {
return 'SearchShowRed-res' + JSON.stringify(res)
})
SearchShowRed.formatTitle(
html.replaceAll('<em>', '').replaceAll('</em>', ''),
res,
0,
textArr
);
}
const titleInitRes: titleInitRes = {
titleMarked,
textArr
}
Logger.debugOptimize(TAG, () => {
return 'SearchShowRed-titleInitRes ' + JSON.stringify(titleInitRes)
})
return titleInitRes
}
private static formatTitle(textStr: string, matchArr: string[], index: number, textArr: textItem[]) {
const item: string = matchArr[index];
if (!item) {
textArr.push({
content: textStr,
isRed: false
})
return;
}
const ind = textStr.indexOf(item);
const len = item.length;
if (ind === 0) {
textArr.push({
content: item,
isRed: true
})
SearchShowRed.formatTitle(textStr.slice(len), matchArr, index + 1, textArr);
} else if (ind + len === textStr.length) {
textArr.push({
content: textStr.slice(0, ind),
isRed: false
})
textArr.push({
content: item,
isRed: true
})
} else {
textArr.push({
content: textStr.slice(0, ind),
isRed: false
})
textArr.push({
content: item,
isRed: true
})
SearchShowRed.formatTitle(textStr.slice(ind + len), matchArr, index + 1, textArr);
}
}
}