mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2026-03-15 03:53:16 +08:00
♻️ 调整解析逻辑,修复部分数据解析异常
This commit is contained in:
@@ -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";
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user