feat: QQ 互联 SDK (#8421)

* feat: QQ SDK changelog

* refactor: functional style

* refactor: migrate to v2

Co-authored-by: TonyRL <TonyRL@users.noreply.github.com>
This commit is contained in:
nuomi1
2022-02-25 11:08:32 +08:00
committed by GitHub
parent c9f1ed41f7
commit 76b3bba258
7 changed files with 124 additions and 87 deletions

View File

@@ -0,0 +1,56 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const platform = ctx.params.platform;
let title = '';
let link = '';
if (platform === 'iOS') {
title = 'iOS SDK 历史变更';
link = 'https://wiki.connect.qq.com/ios_sdk历史变更';
} else if (platform === 'Android') {
title = 'Android SDK 历史变更';
link = 'https://wiki.connect.qq.com/android_sdk历史变更';
} else {
throw Error('not support platform');
}
const response = await got.get(link);
const $ = cheerio.load(response.data);
// 获取主要文本,并且过滤空行
const contents = $('.wp-editor')
.children('p')
.filter((_, element) => $(element).text() !== '');
const pList = [];
const titleIndex = [];
// 遍历文本 p 标签,并且获取标题索引
contents.each((index, element) => {
if ($(element).find('strong').length) {
titleIndex.push(index);
}
pList.push($(element).text().replace('\n', ''));
});
// 用标题索引切割数组
const changelogs = titleIndex.map((_, index) => {
const section = pList.slice(titleIndex[index], titleIndex[index + 1]);
const changelog = {
title: section[0],
description: section.slice(1).join('\n'),
};
return changelog;
});
ctx.state.data = {
title,
link,
item: changelogs,
};
};