feat(parser): 写了个解析

This commit is contained in:
BTMuli
2023-04-02 16:34:53 +08:00
parent d3f7b4be13
commit 68c077326e
10 changed files with 214 additions and 31 deletions

View File

@@ -8,14 +8,14 @@
// Announcement
import { getAnnouncementList, getAnnouncementContent } from "./request/announcements";
import { getAnnoCards } from "./utils/announcements";
import { parseAnnoContent } from "./utils/annoParser";
const GenshinOper = {
Announcement: {
get: {
list: getAnnouncementList,
content: getAnnouncementContent,
},
getList: getAnnouncementList,
getContent: getAnnouncementContent,
card: getAnnoCards,
parser: parseAnnoContent,
},
};

View File

@@ -194,6 +194,7 @@ export interface AnnoContentItem {
* @property {string} subtitle 公告副标题
* @property {string} banner 公告图片
* @property {string} type_label 公告类型标签
* @property {string} tag_icon 公告标签图标
* @property {string} start_time 公告开始时间
* @property {string} end_time 公告结束时间
* @return {AnnoListCard}
@@ -204,6 +205,7 @@ export interface AnnoListCard {
subtitle: string;
banner: string;
type_label: string;
tag_icon: string;
start_time: string;
end_time: string;
}

View File

@@ -0,0 +1,58 @@
/**
* @file plugins Genshin utils annoParser.ts
* @description 原神游戏内公告解析工具
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.1.1
*/
/**
* @description 解析游戏内公告数据
* @since Alpha v0.1.1
* @param {string} data 游戏内公告数据
* @return {string} 解析后的数据
*/
export function parseAnnoContent(data: string): string {
const htmlBase = new DOMParser().parseFromString(data, "text/html");
// 遍历所有 span 标签
htmlBase.querySelectorAll("span").forEach(span => {
return (span.innerHTML = deleteRedundantTag(span.innerHTML));
});
// 遍历所有 p 标签
htmlBase.querySelectorAll("p").forEach(p => {
// 如果没有子元素
if (p.children.length === 0) {
return (p.innerHTML = deleteRedundantTag(p.innerHTML));
}
});
return htmlBase.body.innerHTML;
}
/**
* @description 删除冗余的标签
* @since Alpha v0.1.1
* @param {string} data 内容
* @return {string} 删除后的内容
*/
export function deleteRedundantTag(data: string): string {
// 先转义一下
return decodeRegExp(data);
}
/**
* @description 转义正则表达式
* @since Alpha v0.1.1
* @param {string} data 内容
* @return {string} 转义后的内容
*/
export function decodeRegExp(data: string): string {
let res = data;
if (res.length === 0) return res;
res = res.replace(/&amp;/g, "&");
res = res.replace(/&lt;/g, "<");
res = res.replace(/&gt;/g, ">");
res = res.replace(/&nbsp;/g, " ");
res = res.replace(/&#39;/g, "'");
res = res.replace(/&quot;/g, '"');
res = res.replace(/&apos;/g, "'");
return res;
}

View File

@@ -23,6 +23,7 @@ export function getAnnoCards(data: AnnoListData): AnnoListCard[] {
subtitle: anno.subtitle,
banner: anno.banner,
type_label: anno.type_label,
tag_icon: anno.tag_icon,
start_time: anno.start_time,
end_time: anno.end_time,
});