This commit is contained in:
2025-01-23 13:33:19 +08:00
parent d652ab2354
commit 4d96d4386e

View File

@@ -1059,6 +1059,9 @@ export default {
return emptyContentIndexes; return emptyContentIndexes;
}, },
escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
},
highlightText(text, annotations) { highlightText(text, annotations) {
if (!this.isPreview) { if (!this.isPreview) {
let tempText = text; let tempText = text;
@@ -1069,8 +1072,9 @@ export default {
if (annotation.content == '') { if (annotation.content == '') {
return tempText; return tempText;
} }
const escapedContent = this.escapeRegExp(annotation.content); // 转义正则表达式中的特殊字符
const placeholder = `{{remark_placeholder_${index}}}`; // 占位符 const placeholder = `{{remark_placeholder_${index}}}`; // 占位符
const regExp = new RegExp(`(${annotation.content})`, 'gi'); // 全局匹配批注内容 const regExp = new RegExp(`(${escapedContent})`, 'gi'); // 全局匹配批注内容
// 如果批注内容存在,替换成占位符 // 如果批注内容存在,替换成占位符
if (tempText.includes(annotation.content)) { if (tempText.includes(annotation.content)) {
@@ -1104,8 +1108,7 @@ export default {
</span> </span>
</span> </span>
`; `;
}); });
}); });
// 3. 返回处理后的文本 // 3. 返回处理后的文本
@@ -1113,7 +1116,8 @@ export default {
} else { } else {
return text; return text;
} }
}, },
getCommentsData() { getCommentsData() {
if (!this.isPreview) { if (!this.isPreview) {