2021-06-04 09:09:23 +08:00
|
|
|
Vue.component("keyword", {
|
|
|
|
|
props: ["v-word"],
|
|
|
|
|
data: function () {
|
|
|
|
|
let word = this.vWord
|
|
|
|
|
if (word == null) {
|
|
|
|
|
word = ""
|
2021-07-08 16:23:51 +08:00
|
|
|
} else {
|
|
|
|
|
word = word.replace(/\)/, "\\)")
|
|
|
|
|
word = word.replace(/\(/, "\\(")
|
|
|
|
|
word = word.replace(/\+/, "\\+")
|
|
|
|
|
word = word.replace(/\^/, "\\^")
|
|
|
|
|
word = word.replace(/\$/, "\\$")
|
2021-06-04 09:09:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let slot = this.$slots["default"][0]
|
2021-07-12 17:35:33 +08:00
|
|
|
let text = this.encodeHTML(slot.text)
|
2021-06-04 09:09:23 +08:00
|
|
|
if (word.length > 0) {
|
2021-07-08 16:23:51 +08:00
|
|
|
text = text.replace(new RegExp("(" + word + ")", "ig"), "<span style=\"border: 1px #ccc dashed; color: #ef4d58\">$1</span>")
|
2021-06-04 09:09:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
word: word,
|
|
|
|
|
text: text
|
|
|
|
|
}
|
|
|
|
|
},
|
2021-07-12 17:35:33 +08:00
|
|
|
methods: {
|
|
|
|
|
encodeHTML: function (s) {
|
2022-02-23 17:34:54 +08:00
|
|
|
s = s.replace(/&/g, "&")
|
|
|
|
|
s = s.replace(/</g, "<")
|
|
|
|
|
s = s.replace(/>/g, ">")
|
|
|
|
|
s = s.replace(/"/g, """)
|
2021-07-12 17:35:33 +08:00
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
},
|
2021-06-04 09:09:23 +08:00
|
|
|
template: `<span><span style="display: none"><slot></slot></span><span v-html="text"></span></span>`
|
|
|
|
|
})
|