use issues api
This commit is contained in:
parent
d218fef18d
commit
f8747a726b
|
@ -3,64 +3,74 @@
|
|||
* 格式与官方标签插件一致使用空格分隔,中括号内的是可选参数(中括号不需要写出来)
|
||||
*
|
||||
* {% friends [style:rect] [group:name] %}
|
||||
* {% friends [api:https://issues-api.vercel.app] [repo:xaoxuu/friends] %}
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
hexo.extend.tag.register('friends', function(args) {
|
||||
args = hexo.args.map(args, ['style', 'group']);
|
||||
args = hexo.args.map(args, ['style', 'group', 'repo']);
|
||||
var friends = hexo.locals.get('data').friends;
|
||||
if (friends == undefined) {
|
||||
return;
|
||||
friends = {};
|
||||
}
|
||||
var el = '<div class="tag-plugin friends-wrap ';
|
||||
var el = '<div class="tag-plugin friends-wrap issues-wrap ';
|
||||
if (args.style && args.style.length > 0) {
|
||||
el += args.style;
|
||||
} else {
|
||||
el += ' round';
|
||||
el += 'round';
|
||||
}
|
||||
el += '">';
|
||||
function groupHeader(group) {
|
||||
var header = '<div class="group-header">';
|
||||
if (!!group.title) {
|
||||
header += '<p class="title">' + group.title + '</p>';
|
||||
}
|
||||
if (!!group.description) {
|
||||
header += '<p class="description">' + group.description + '</p>';
|
||||
}
|
||||
header += '</div>';
|
||||
return header;
|
||||
el += '"';
|
||||
if (args.repo) {
|
||||
el += ' id="issues-api"';
|
||||
el += ' api="' + (args.api || 'https://issues-api.vercel.app') + '/' + args.repo + '"';
|
||||
}
|
||||
function cell(friend) {
|
||||
if (friend.url && friend.title) {
|
||||
var cell = '<div class="user-simple">';
|
||||
cell += '<a href="' + friend.url + '">';
|
||||
cell += '<img src="' + friend.avatar + '"/>';
|
||||
cell += '<div class="name"><span>' + friend.title + '</span></div>';
|
||||
cell += '</a></div>'
|
||||
return cell;
|
||||
} else {
|
||||
return '';
|
||||
el += '>';
|
||||
if (args.repo) {
|
||||
el += '<div class="group-body"></div>';
|
||||
} else {
|
||||
function groupHeader(group) {
|
||||
var header = '<div class="group-header">';
|
||||
if (!!group.title) {
|
||||
header += '<p class="title">' + group.title + '</p>';
|
||||
}
|
||||
if (!!group.description) {
|
||||
header += '<p class="description">' + group.description + '</p>';
|
||||
}
|
||||
header += '</div>';
|
||||
return header;
|
||||
}
|
||||
}
|
||||
for (let groupId of Object.keys(friends)) {
|
||||
function f() {
|
||||
if (groupId in friends) {
|
||||
let group = friends[groupId];
|
||||
el += groupHeader(group);
|
||||
el += '<div class="group-body">';
|
||||
group.items.forEach((friend, i) => {
|
||||
el += cell(friend);
|
||||
});
|
||||
el += '</div>';
|
||||
function cell(friend) {
|
||||
if (friend.url && friend.title) {
|
||||
var cell = '<div class="user-simple">';
|
||||
cell += '<a href="' + friend.url + '">';
|
||||
cell += '<img src="' + friend.avatar + '"/>';
|
||||
cell += '<div class="name"><span>' + friend.title + '</span></div>';
|
||||
cell += '</a></div>'
|
||||
return cell;
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
if (args.group && args.group.length > 0) {
|
||||
if (args.group == groupId) {
|
||||
for (let groupId of Object.keys(friends)) {
|
||||
function f() {
|
||||
if (groupId in friends) {
|
||||
let group = friends[groupId];
|
||||
el += groupHeader(group);
|
||||
el += '<div class="group-body">';
|
||||
group.items.forEach((friend, i) => {
|
||||
el += cell(friend);
|
||||
});
|
||||
el += '</div>';
|
||||
}
|
||||
}
|
||||
if (args.group && args.group.length > 0) {
|
||||
if (args.group == groupId) {
|
||||
f();
|
||||
}
|
||||
} else {
|
||||
f();
|
||||
}
|
||||
} else {
|
||||
f();
|
||||
}
|
||||
}
|
||||
el += '</div>';
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
const IssuesAPI = {
|
||||
requestIssuesAPI(url, callback, timeout) {
|
||||
let retryTimes = 10;
|
||||
let retryTimes = 5;
|
||||
function request() {
|
||||
return new Promise((resolve, reject) => {
|
||||
let status = 0; // 0 等待 1 完成 2 超时
|
||||
|
@ -42,144 +42,45 @@ const IssuesAPI = {
|
|||
}
|
||||
request();
|
||||
},
|
||||
parseIssueStrToJson(str) {
|
||||
let jsonStr = str.match(/```json[\s|\S]*```/);
|
||||
if (jsonStr && jsonStr.length > 0) {
|
||||
jsonStr = jsonStr[0];
|
||||
}
|
||||
if (jsonStr) {
|
||||
jsonStr = jsonStr.split('```json')[1].split('```')[0];
|
||||
if (jsonStr) {
|
||||
return JSON.parse(jsonStr);
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
},
|
||||
groupIssuesData(cfg, data) {
|
||||
var groups = new Object();
|
||||
if (data.length > 0) {
|
||||
if (cfg.group != undefined) {
|
||||
const arr = cfg.group.split('=');
|
||||
if (arr.length > 1) {
|
||||
const groupKey = arr[0];
|
||||
let groupList = arr[1];
|
||||
if (groupKey && groupList) {
|
||||
groupList = groupList.split(',');
|
||||
}
|
||||
cfg.group = groupList;
|
||||
for (i = 0; i < data.length; i++) {
|
||||
const obj = this.parseIssueStrToJson(data[i].body);
|
||||
if (obj && (groupKey in obj)) {
|
||||
let tmp = obj[groupKey];
|
||||
tmp = tmp.replace(', ', ',').split(',');
|
||||
for (var j = 0; j < tmp.length; j++) {
|
||||
if (groupList.includes(tmp[j])) {
|
||||
let arr = groups[tmp[j]];
|
||||
if (arr == undefined) {
|
||||
arr = new Array();
|
||||
}
|
||||
arr.push(obj);
|
||||
groups[tmp[j]] = arr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
cfg.group = [''];
|
||||
for (i = 0; i < data.length; i++) {
|
||||
const obj = this.parseIssueStrToJson(data[i].body);
|
||||
if (obj) {
|
||||
let arr = groups[''];
|
||||
if (arr == undefined) {
|
||||
arr = new Array();
|
||||
}
|
||||
arr.push(obj);
|
||||
groups[''] = arr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return groups;
|
||||
},
|
||||
getIssuesAPIForSites(cfg) {
|
||||
getIssuesAPIForFriends(cfg) {
|
||||
const el = $(cfg.el)[0];
|
||||
$(el).append('<div class="loading"><i class="fa fa-cog fa-2x fa-spin"></i><p>正在加载</p></div>');
|
||||
$(el).append('<div class="loading"><p>正在加载</p></div>');
|
||||
this.requestIssuesAPI(cfg.api, function(data) {
|
||||
$(el).find('.loading').remove();
|
||||
const dt = IssuesAPI.groupIssuesData(cfg, data);
|
||||
const groupTitles = Object.keys(dt);
|
||||
cfg.group.forEach((groupTitle, i) => {
|
||||
const issues = dt[groupTitle];
|
||||
if (issues && issues.length > 0) {
|
||||
if (groupTitle.length > 0) {
|
||||
$(el).append('<h2>' + groupTitle + '</h2>');
|
||||
} else if (name == '' && groupTitles.length > 1) {
|
||||
$(el).append('<h2>' + '未分组' + '</h2>');
|
||||
}
|
||||
$(el).append('<div class="site-card-group ' + i + '"></div>');
|
||||
// layout items
|
||||
for (j = 0; j < issues.length; j++) {
|
||||
const issue = issues[j];
|
||||
let imgTag = '';
|
||||
if (issue.screenshot && issue.screenshot.length > 0) {
|
||||
imgTag = '<div class="img"><img src="' + issue.screenshot + '" onerror="javascript:this.src=\'https://image.thum.io/get/width/1024/crop/768/' + issue.url + '\';"/></div>';
|
||||
} else {
|
||||
imgTag = '<div class="img"></div>';
|
||||
}
|
||||
let infoTag = '<div class="info">';
|
||||
if (issue.avatar && issue.avatar.length > 0) {
|
||||
infoTag += '<img src="' + issue.avatar + '" onerror="javascript:this.src=\'https://image.thum.io/get/width/1024/crop/768/' + issue.url + '\';"/>';
|
||||
}
|
||||
infoTag += '<span class="title">' + issue.title + '</span><span class="desc">' + issue.description + '</span></div>';
|
||||
const cardTag = '<div><a class=\'site-card\' target=\'_blank\' href=\'' + issue.url + '\'>' + imgTag + infoTag + '</a></div>';
|
||||
$(el).find('.site-card-group.' + i).append(cardTag);
|
||||
}
|
||||
}
|
||||
const arr = data.content;
|
||||
arr.forEach((item, i) => {
|
||||
var user = '<div class="user-simple">';
|
||||
user += '<a class="user-link" target="_blank" rel="external nofollow noopener noreferrer"';
|
||||
user += ' href="' + item.url + '">';
|
||||
user += '<img src="' + item.avatar + '" onerror="javascript:this.src=\'https://image.thum.io/get/width/1024/crop/768/' + item.url + '\';"/>';
|
||||
user += '<div class="name"><span>' + item.title + '</span></div>';
|
||||
user += '</a>';
|
||||
user += '</div>';
|
||||
$(el).find('.group-body').append(user);
|
||||
});
|
||||
}, function() {
|
||||
$(el).find('.loading i').remove();
|
||||
$(el).find('.loading p').text('加载失败,请稍后重试。');
|
||||
});
|
||||
},
|
||||
getIssuesAPIForTimeline(cfg) {
|
||||
const el = $(cfg.el)[0];
|
||||
$(el).append('<div class="loading"><i class="fa fa-cog fa-2x fa-spin"></i><p>正在加载</p></div>');
|
||||
this.requestIssuesAPI(cfg.api, function(data) {
|
||||
$(el).find('.loading').remove();
|
||||
if (data.length > 0) {
|
||||
for (i = 0; i < data.length; i++) {
|
||||
const a = ' <a class="comments" target="_blank" href="' + data[i].html_url + '"><i class="fa fa-comment-dots fa-fw"></i>' + data[i].comments + '</a>';
|
||||
const header = '<div class="header"><p></p><p>' + data[i].title + a + '</p><p></p></div>';
|
||||
const body = '<div class="body"><p>' + data[i].body + '</p></div>';
|
||||
const tag = '<div class="timenode">' + header + body + '</div>';
|
||||
$(el).append(tag);
|
||||
}
|
||||
}
|
||||
}, function() {
|
||||
$(el).find('.loading i').remove();
|
||||
$(el).find('.loading p').text('加载失败,请稍后重试。');
|
||||
});
|
||||
},
|
||||
request() {
|
||||
const els = document.getElementsByClassName('issues-wrap');
|
||||
for (var i = 0; i < els.length; i++) {
|
||||
const el = els[i];
|
||||
const api = el.getAttribute('api');
|
||||
const group = el.getAttribute('group');
|
||||
if (api == null) {
|
||||
continue;
|
||||
}
|
||||
var cfg = new Object();
|
||||
cfg.class = el.getAttribute('class');
|
||||
cfg.el = el;
|
||||
cfg.api = api;
|
||||
cfg.group = group;
|
||||
if (cfg.class.split(' ').includes('sites')) {
|
||||
this.getIssuesAPIForSites(cfg);
|
||||
} else if (cfg.class.split(' ').includes('timeline')) {
|
||||
this.getIssuesAPIForTimeline(cfg);
|
||||
}
|
||||
this.getIssuesAPIForFriends(cfg);
|
||||
}
|
||||
}
|
||||
};
|
||||
$(function () {
|
||||
IssuesAPI.request();
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue