♻️ 调整解析逻辑,修复部分数据解析异常

This commit is contained in:
BTMuli
2026-01-17 13:58:59 +08:00
parent 734b01706f
commit 75b6ba40e9
2 changed files with 15 additions and 12 deletions

View File

@@ -9,7 +9,7 @@
import showHyperLink from "@comp/func/hyperlink.js";
import showSnackbar from "@comp/func/snackbar.js";
import { parseHtmlText } from "@utils/toolFunc.js";
import { defineProps, onMounted, shallowRef } from "vue";
import { onMounted, shallowRef } from "vue";
import { WikiHyperLinkData } from "@/data/index.js";

View File

@@ -190,7 +190,21 @@ export function getRandomString(length: number, type: string = "all"): string {
* @returns 解析后的文本
*/
export function parseHtmlText(desc: string): string {
const colorReg = /<color=(.*?)>(.*?)<\/color>/g;
const linkReg = /\{LINK#(.*?)}(.*?)\{\/LINK}/g;
let colorMatch = colorReg.exec(desc);
while (colorMatch !== null) {
const color = colorMatch[1];
const text = new DOMParser().parseFromString(colorMatch[2], "text/html").body.textContent;
let title = text;
const colorLinkMatch = linkReg.exec(text);
if (colorLinkMatch !== null) title = colorLinkMatch[2];
desc = desc.replace(
colorMatch[0],
`<span title="${title}" style="color: ${color}">${text}</span>`,
);
colorMatch = colorReg.exec(desc);
}
let linkMatch = linkReg.exec(desc);
while (linkMatch !== null) {
const link = linkMatch[1];
@@ -201,17 +215,6 @@ export function parseHtmlText(desc: string): string {
);
linkMatch = linkReg.exec(desc);
}
const colorReg = /<color=(.*?)>(.*?)<\/color>/g;
let colorMatch = colorReg.exec(desc);
while (colorMatch !== null) {
const color = colorMatch[1];
const text = new DOMParser().parseFromString(colorMatch[2], "text/html").body.textContent;
desc = desc.replace(
colorMatch[0],
`<span title="${text}" style="color: ${color}">${text}</span>`,
);
colorMatch = colorReg.exec(desc);
}
desc = desc.replace(/\\n/g, "<br />");
return desc;
}