搜索结果关键词高亮

1
2
3
4
5
6
7
formatContent (content = '') {
let temp = content.replace(/</g, '&lt').replace(/>/g, '&gt')
this.analyzeKeywordList.forEach(item => {
temp = temp.replace(new RegExp(item, 'g'), `<span style="color: #F56C6C;font-weight: bold;">${item}</span>`)
})
return temp.replace(/\n/g, '<br>')
}
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
formatContent (content = '', analyzeKeywordList = []) {
if (!content) return ''
let temp = content.replace(/</g, '&lt').replace(/>/g, '&gt')
let signList = []
let generateSignList = (list = []) => {
let temp = []
let tran = item => {
const [start, end] = item.split('-').map(i => +i)
const plain = [start]
let len = end - start - 1
while (len--) {
plain.push(start + len)
}
return plain
}
list.forEach(item => {
temp = [...temp, ...tran(item)]
})
return [...new Set(temp)]
}
let generateTagList = (list = [], content = '') => {
let tempContent = content.split('')
list.forEach(i => {
tempContent[i] = `<span style="color: #F56C6C;font-weight: bold;">${tempContent[i]}</span>`
})
return tempContent.join('')
}
analyzeKeywordList.forEach(item => {
temp.replace(new RegExp(item, 'g'), (match, index) => {
const start = index
const end = start + match.length
signList.push(`${start}-${end}`)
return match
}
)
}
)
temp = generateTagList(generateSignList(signList), temp)
return temp.replace(/\n/g, '<br>')
},
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
formatContent (content = '', analyzeKeywordList) {
if (!content) return ''
let tempContent = content.replace(/</g, '&lt').replace(/>/g, '&gt')
let signList = []
const merge = (inputList = []) => {
let compareList = []
inputList.forEach(sign => {
const [start, end] = sign.split('-')
if (compareList.length) {
let add = ''
compareList.forEach((compare, index) => {
const [ compareStart, compareEnd ] = compare.split('-')
if (start > compareEnd || end < compareStart) {
// 不相交
add = sign
} else if (start >= compareStart && end <= compareEnd) {
// 包含
compareList[index] = compare
} else if (compareStart >= start && compareEnd <= end) {
// 包含
compareList[index] = sign
} else {
// 相交
compareList[index] = `${Math.min(start, compareStart)}-${Math.max(end, compareEnd)}`
}
})
add && compareList.push(add)
} else {
compareList.push(`${start}-${end}`)
}
})
if (compareList.length === inputList.length) {
return compareList
} else {
return merge(compareList)
}
}
analyzeKeywordList.forEach((item, idx) => {
// 生成标记列表
tempContent = tempContent.replace(new RegExp(item, 'g'), (match, index) => {
const start = index
const end = start + match.length
Array.isArray(signList[idx]) || (signList[idx] = [])
signList[idx].push(`${start}-${end}`)
return match
})
// 合并列表项
signList[idx] = merge(signList[idx])
})
signList = signList.flat()
// 生成最终字符串
let finalStr = ''
let s = 0
signList.forEach(sign => {
const [start, end] = sign.split('-')
if (start > s) {
finalStr += tempContent.substring(s, start)
}
finalStr += `<span style="color: #F56C6C;font-weight: bold;">${tempContent.substring(start, end)}</span>`
s = end
})
return finalStr.replace(/\n/g, '<br>')
},