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'; 'use strict';
hexo.extend.tag.register('friends', function(args) { hexo.extend.tag.register('friends', function(args) {
args = hexo.args.map(args, ['only', 'not', 'repo', 'api']); args = hexo.args.map(args, ['repo', 'data'], ['group']);
if (args.only) { var links = hexo.locals.get('data').links;
args.only = args.only.split(','); 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">'; var el = '<div class="tag-plugin users-wrap">';
function groupHeader(group) { if (data) {
var header = '<div class="group-header">'; el += '<div class="stellar-friends-api"';
if (group.title) { el += ' api="' + data + '"';
header += hexo.render.renderSync({text: group.title, engine: 'markdown'}).split('\n').join(''); el += '>';
} el += '<div class="group-body"></div>';
if (group.description) { el += '</div>';
header += hexo.render.renderSync({text: group.description, engine: 'markdown'}).split('\n').join(''); } else if (args.group) {
} function cell(item) {
header += '</div>'; if (item.url && item.title) {
return header;
}
function cell(friend) {
if (friend.url && friend.title) {
var cell = '<div class="user-card">'; var cell = '<div class="user-card">';
cell += '<a class="card-link" target="_blank" rel="external nofollow noopener noreferrer" href="' + friend.url + '">'; cell += '<a class="card-link" target="_blank" rel="external nofollow noopener noreferrer" href="' + item.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 += '<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>' + friend.title + '</span></div>'; cell += '<div class="name"><span>' + item.title + '</span></div>';
cell += '</a></div>' cell += '</a></div>'
return cell; return cell;
} else { } else {
return ''; 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">'; el += '<div class="group-body">';
group.items.forEach((friend, i) => { const items = links[args.group] || [];
el += cell(friend); console.log('links', links);
console.log('group', args.group);
console.log('items', items);
items.forEach((item, i) => {
el += cell(item);
}); });
el += '</div>'; el += '</div>';
} }
}
}
if (args.only) {
if (args.only.includes(groupId)) {
f();
}
} else {
f();
}
}
el += '</div>'; el += '</div>';
return el; 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,53 +1,42 @@
/** /**
* 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'; 'use strict';
hexo.extend.tag.register('sites', function(args) { hexo.extend.tag.register('sites', function(args) {
args = hexo.args.map(args, ['only', 'not', 'repo', 'api']); args = hexo.args.map(args, ['repo', 'data'], ['group']);
if (args.only) { var links = hexo.locals.get('data').links;
args.only = args.only.split(','); 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">'; var el = '<div class="tag-plugin sites-wrap">';
function groupHeader(group) { if (data) {
var header = '<div class="group-header">'; el += '<div class="stellar-sites-api"';
if (group.title) { el += ' api="' + data + '"';
header += hexo.render.renderSync({text: group.title, engine: 'markdown'}).split('\n').join(''); el += '>';
} el += '<div class="group-body"></div>';
if (group.description) { el += '</div>';
header += hexo.render.renderSync({text: group.description, engine: 'markdown'}).split('\n').join(''); } else if (args.group) {
} function cell(item) {
header += '</div>'; if (item.url && item.title) {
return header;
}
function cell(site) {
if (site.url && site.title) {
var cell = '<div class="site-card">'; var cell = '<div class="site-card">';
cell += '<a class="card-link" target="_blank" rel="external nofollow noopener noreferrer" href="' + site.url + '">'; cell += '<a class="card-link" target="_blank" rel="external nofollow noopener noreferrer" href="' + item.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 += '<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 += '<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 += '<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">' + site.title + '</span>'; cell += '<span class="title">' + item.title + '</span>';
cell += '<span class="desc">' + (site.description || site.url) + '</span>'; cell += '<span class="desc">' + (item.description || item.url) + '</span>';
cell += '</div>'; cell += '</div>';
cell += '</a></div>' cell += '</a></div>'
return cell; return cell;
@ -55,42 +44,14 @@ hexo.extend.tag.register('sites', function(args) {
return ''; 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">'; el += '<div class="group-body">';
group.items.forEach((site, i) => { const items = links[args.group] || [];
el += cell(site); items.forEach((item, i) => {
el += cell(item);
}); });
el += '</div>'; el += '</div>';
} }
}
}
if (args.only) {
if (args.only.includes(groupId)) {
f();
}
} else {
f();
}
}
el += '</div>'; el += '</div>';
return el; return el;
}); });