friends v2 & sites v2

This commit is contained in:
xaoxuu 2022-10-29 16:25:40 +08:00
parent 517327ca77
commit 33243bb13e
4 changed files with 86 additions and 184 deletions

7
_data/links.yml Normal file
View File

@ -0,0 +1,7 @@
'示例':
- title: XAOXUU
avatar: https://bu.dusays.com/2021/09/24/2f74810ceb3d3.png
url: https://xaoxuu.com
screenshot: https://bu.dusays.com/2022/10/23/63542895cfd29.png
description:

View File

@ -1,92 +1,56 @@
/**
* friends.js v1 | https://github.com/xaoxuu/hexo-theme-stellar/
* friends.js v2 | https://github.com/xaoxuu/hexo-theme-stellar/
* 格式与官方标签插件一致使用空格分隔中括号内的是可选参数中括号不需要写出来
*
* {% friends [only:group1] [not:group2] %}
* {% friends [group] [repo:owner/repo] [data:http] %}
*/
'use strict';
hexo.extend.tag.register('friends', function(args) {
args = hexo.args.map(args, ['only', 'not', 'repo', 'api']);
if (args.only) {
args.only = args.only.split(',');
args = hexo.args.map(args, ['repo', 'data'], ['group']);
var links = hexo.locals.get('data').links;
if (links == undefined) {
links = {};
}
if (args.not) {
args.not = args.not.split(',');
}
var friends = hexo.locals.get('data').friends;
if (friends == undefined) {
friends = {};
}
if (args.repo) {
friends = {
group: {
api: args.api,
repo: args.repo
}
}
var data;
if (args.data) {
data = args.data;
} else if (args.repo) {
data = 'https://raw.github.xaoxuu.com/' + args.repo + '/output/v2/data.json';
}
var el = '<div class="tag-plugin users-wrap">';
function groupHeader(group) {
var header = '<div class="group-header">';
if (group.title) {
header += hexo.render.renderSync({text: group.title, engine: 'markdown'}).split('\n').join('');
}
if (group.description) {
header += hexo.render.renderSync({text: group.description, engine: 'markdown'}).split('\n').join('');
}
header += '</div>';
return header;
}
function cell(friend) {
if (friend.url && friend.title) {
var cell = '<div class="user-card">';
cell += '<a class="card-link" target="_blank" rel="external nofollow noopener noreferrer" href="' + friend.url + '">';
cell += '<img src="' + (friend.avatar || hexo.theme.config.default.avatar) + '" onerror="javascript:this.removeAttribute(&quot;data-src&quot;);this.src=&quot;' + hexo.theme.config.default.avatar + '&quot;;"/>';
cell += '<div class="name"><span>' + friend.title + '</span></div>';
cell += '</a></div>'
return cell;
} else {
return '';
}
}
for (let groupId of Object.keys(friends)) {
function f() {
if (args.not && args.not.includes(groupId)) {
return;
}
if (groupId in friends) {
let group = friends[groupId];
if ((typeof group == 'object') && group.constructor == Array) {
group = {items: group};
}
if (group.title || group.description) {
el += groupHeader(group);
}
if (group.repo) {
el += '<div class="stellar-friends-api"';
el += ' api="' + (group.api || 'https://issues-api.xaoxuu.com') + '/v1/' + group.repo + '"';
el += '>';
el += '<div class="group-body"></div>';
el += '</div>';
} else if (group.items) {
el += '<div class="group-body">';
group.items.forEach((friend, i) => {
el += cell(friend);
});
el += '</div>';
}
if (data) {
el += '<div class="stellar-friends-api"';
el += ' api="' + data + '"';
el += '>';
el += '<div class="group-body"></div>';
el += '</div>';
} else if (args.group) {
function cell(item) {
if (item.url && item.title) {
var cell = '<div class="user-card">';
cell += '<a class="card-link" target="_blank" rel="external nofollow noopener noreferrer" href="' + item.url + '">';
cell += '<img src="' + (item.avatar || hexo.theme.config.default.avatar) + '" onerror="javascript:this.removeAttribute(&quot;data-src&quot;);this.src=&quot;' + hexo.theme.config.default.avatar + '&quot;;"/>';
cell += '<div class="name"><span>' + item.title + '</span></div>';
cell += '</a></div>'
return cell;
} else {
return '';
}
}
if (args.only) {
if (args.only.includes(groupId)) {
f();
}
} else {
f();
}
el += '<div class="group-body">';
const items = links[args.group] || [];
console.log('links', links);
console.log('group', args.group);
console.log('items', items);
items.forEach((item, i) => {
el += cell(item);
});
el += '</div>';
}
el += '</div>';
return el;
});

View File

@ -1,30 +0,0 @@
/**
* issues.js v1 | https://github.com/xaoxuu/hexo-theme-stellar/
* 格式与官方标签插件一致使用空格分隔中括号内的是可选参数中括号不需要写出来
*
* {% issues [sites/timeline/friends] api:xxx [group:key=value1,value2,value3] %}
*
* example:
* {% issues sites api:https://api.github.com/repos/volantis-x/examples/issues?sort=updated&state=open&page=1&per_page=100 group:version=版本:^4.0,版本:^3.0,版本:^2.0 %}
*/
'use strict';
hexo.extend.tag.register('issues', function(args) {
args = hexo.args.map(args, ['api', 'group'], ['type']);
// 所有支持的参数
let type = args.type || '';
let api = args.api || '';
let group = args.group || '';
if (type.length == 0 || api.length == 0) {
return;
}
// 布局
var el = '<div class="tag-plugin issues-wrap ' + type + '" id="issues-api"';
el += 'api="' + api + '"';
if (group.length > 0) {
el += 'group="' + group + '"';
}
el += '></div>';
return el;
});

View File

@ -1,96 +1,57 @@
/**
* sites.js v1 | https://github.com/xaoxuu/hexo-theme-stellar/
* sites.js v2 | https://github.com/xaoxuu/hexo-theme-stellar/
* 格式与官方标签插件一致使用空格分隔中括号内的是可选参数中括号不需要写出来
*
* {% sites [only:group1] [not:group2] %}
* {% sites [group] [repo:owner/repo] [data:http] %}
*/
'use strict';
hexo.extend.tag.register('sites', function(args) {
args = hexo.args.map(args, ['only', 'not', 'repo', 'api']);
if (args.only) {
args.only = args.only.split(',');
args = hexo.args.map(args, ['repo', 'data'], ['group']);
var links = hexo.locals.get('data').links;
if (links == undefined) {
links = {};
}
if (args.not) {
args.not = args.not.split(',');
}
var sites = hexo.locals.get('data').sites;
if (sites == undefined) {
sites = {};
}
if (args.repo) {
sites = {
group: {
api: args.api,
repo: args.repo
}
}
var data;
if (args.data) {
data = args.data;
} else if (args.repo) {
data = 'https://raw.github.xaoxuu.com/' + args.repo + '/output/v2/data.json';
}
var el = '<div class="tag-plugin sites-wrap">';
function groupHeader(group) {
var header = '<div class="group-header">';
if (group.title) {
header += hexo.render.renderSync({text: group.title, engine: 'markdown'}).split('\n').join('');
}
if (group.description) {
header += hexo.render.renderSync({text: group.description, engine: 'markdown'}).split('\n').join('');
}
header += '</div>';
return header;
}
function cell(site) {
if (site.url && site.title) {
var cell = '<div class="site-card">';
cell += '<a class="card-link" target="_blank" rel="external nofollow noopener noreferrer" href="' + site.url + '">';
cell += '<img src="' + (site.screenshot || ('https://screenshot-api.xaoxuu.com/api?url=' + site.url + '&width=1280&height=720')) + '" onerror="javascript:this.removeAttribute(&quot;data-src&quot;);this.src=&quot;' + hexo.theme.config.default.cover + '&quot;;"/>';
cell += '<div class="info">';
cell += '<img src="' + (site.avatar || hexo.theme.config.default.link) + '" onerror="javascript:this.removeAttribute(&quot;data-src&quot;);this.src=&quot;' + hexo.theme.config.default.link + '&quot;;"/>';
cell += '<span class="title">' + site.title + '</span>';
cell += '<span class="desc">' + (site.description || site.url) + '</span>';
cell += '</div>';
cell += '</a></div>'
return cell;
} else {
return '';
}
}
for (let groupId of Object.keys(sites)) {
function f() {
if (args.not && args.not.includes(groupId)) {
return;
}
if (groupId in sites) {
let group = sites[groupId];
if ((typeof group == 'object') && group.constructor == Array) {
group = {items: group};
}
if (group.title || group.description) {
el += groupHeader(group);
}
if (group.repo) {
el += '<div class="stellar-sites-api"';
el += ' api="' + (group.api || 'https://issues-api.xaoxuu.com') + '/v1/' + group.repo + '"';
el += '>';
el += '<div class="group-body"></div>';
el += '</div>';
} else if (group.items) {
el += '<div class="group-body">';
group.items.forEach((site, i) => {
el += cell(site);
});
el += '</div>';
}
if (data) {
el += '<div class="stellar-sites-api"';
el += ' api="' + data + '"';
el += '>';
el += '<div class="group-body"></div>';
el += '</div>';
} else if (args.group) {
function cell(item) {
if (item.url && item.title) {
var cell = '<div class="site-card">';
cell += '<a class="card-link" target="_blank" rel="external nofollow noopener noreferrer" href="' + item.url + '">';
cell += '<img src="' + (item.screenshot || ('https://screenshot-api.xaoxuu.com/api?url=' + item.url + '&width=1280&height=720')) + '" onerror="javascript:this.removeAttribute(&quot;data-src&quot;);this.src=&quot;' + hexo.theme.config.default.cover + '&quot;;"/>';
cell += '<div class="info">';
cell += '<img src="' + (item.avatar || hexo.theme.config.default.link) + '" onerror="javascript:this.removeAttribute(&quot;data-src&quot;);this.src=&quot;' + hexo.theme.config.default.link + '&quot;;"/>';
cell += '<span class="title">' + item.title + '</span>';
cell += '<span class="desc">' + (item.description || item.url) + '</span>';
cell += '</div>';
cell += '</a></div>'
return cell;
} else {
return '';
}
}
if (args.only) {
if (args.only.includes(groupId)) {
f();
}
} else {
f();
}
el += '<div class="group-body">';
const items = links[args.group] || [];
items.forEach((item, i) => {
el += cell(item);
});
el += '</div>';
}
el += '</div>';
return el;
});