diff --git a/_config.yml b/_config.yml index 01eab67..a850039 100755 --- a/_config.yml +++ b/_config.yml @@ -272,6 +272,7 @@ plugins: # issues api sitesjs: /js/plugins/sites.js friendsjs: /js/plugins/friends.js + ghinfo: /js/plugins/ghinfo.js ## optional plugins ## # preload diff --git a/layout/_partial/scripts/index.ejs b/layout/_partial/scripts/index.ejs index 7f26f83..0586568 100644 --- a/layout/_partial/scripts/index.ejs +++ b/layout/_partial/scripts/index.ejs @@ -106,6 +106,7 @@ jQuery: '<%- url_for(theme.plugins.jquery || "https://fastly.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js") %>', sitesjs: '<%- url_for(theme.plugins.sitesjs) %>', friendsjs: '<%- url_for(theme.plugins.friendsjs) %>', + ghinfo: '<%- url_for(theme.plugins.ghinfo) %>', }; // optional plugins diff --git a/layout/_partial/sidebar/widgets/repo_info.ejs b/layout/_partial/sidebar/widgets/repo_info.ejs index fe08826..c6e509c 100644 --- a/layout/_partial/sidebar/widgets/repo_info.ejs +++ b/layout/_partial/sidebar/widgets/repo_info.ejs @@ -26,7 +26,7 @@ function layoutDiv() { el += ''; // body el += '
'; - el += '
'; + el += '
'; var items = []; // GitHub items.push({ @@ -34,6 +34,20 @@ function layoutDiv() { text: 'GitHub', href: 'https://github.com/' + repo }); + // Stars + items.push({ + icon: '', + text: 'Stars', + apiKey: 'stargazers_count', + href: 'https://github.com/' + repo + '/stargazers' + }); + // Forks + items.push({ + icon: '', + text: 'Forks', + apiKey: 'forks_count', + href: 'https://github.com/' + repo + '/network/members' + }); // Releases items.push({ icon: '', @@ -43,15 +57,9 @@ function layoutDiv() { // Download items.push({ icon: '', - text: 'Download', + text: 'Download Zip', href: 'https://github.com/' + repo + '/archive/refs/heads/' + branch + '.zip' }); - // Issues - items.push({ - icon: '', - text: 'Issues', - href: 'https://github.com/' + repo + '/issues' - }); items.forEach((item, i) => { el += '
'; el += ''; + el += '
'; + // right + el += '
'; + if (item.apiKey) { + el += ''; + } + if (item.icon) { + el += item.icon; + } + el += '
'; el += '
'; }); el += '
'; diff --git a/source/css/_common/base.styl b/source/css/_common/base.styl index 750dad1..0789201 100644 --- a/source/css/_common/base.styl +++ b/source/css/_common/base.styl @@ -14,7 +14,7 @@ a // md > a p:not([class]), li:not([class]) - >a:not([class]) + a:not([class]) position: relative margin: 0 1px padding: 2px diff --git a/source/css/_layout/sidebar/repo_info.styl b/source/css/_layout/sidebar/repo_info.styl index 42dede2..c48b268 100644 --- a/source/css/_layout/sidebar/repo_info.styl +++ b/source/css/_layout/sidebar/repo_info.styl @@ -20,9 +20,9 @@ padding: 0.5rem .5rem font-size: $fs-13 justify-content: space-between - flex-direction: row-reverse svg width: 1em height: 1em + margin-left: 4px a:hover background: var(--block-hover) diff --git a/source/js/main.js b/source/js/main.js index 3158927..222564e 100644 --- a/source/js/main.js +++ b/source/js/main.js @@ -230,6 +230,14 @@ if (stellar.plugins.friendsjs) { }) } } +if (stellar.plugins.ghinfo) { + const issues_api = document.getElementById('ghinfo-api'); + if (issues_api != undefined) { + stellar.jQuery(() => { + stellar.loadScript(stellar.plugins.ghinfo, { defer: true }) + }) + } +} // swiper if (stellar.plugins.swiper) { diff --git a/source/js/plugins/friends.js b/source/js/plugins/friends.js index 0187d3b..2649b07 100644 --- a/source/js/plugins/friends.js +++ b/source/js/plugins/friends.js @@ -48,7 +48,6 @@ const friendsjs = { friendsjs.requestAPI(cfg.api, function(data) { $(el).find('.loading-wrap').remove(); const arr = data.content || data; - console.log(data); arr.forEach((item, i) => { var user = '
'; user += ' { + let retryTimes = 5; + function request() { + return new Promise((resolve, reject) => { + let status = 0; // 0 等待 1 完成 2 超时 + let timer = setTimeout(() => { + if (status === 0) { + status = 2; + timer = null; + reject('请求超时'); + if (retryTimes == 0) { + timeout(); + } + } + }, 5000); + fetch(url).then(function(response) { + if (status !== 2) { + clearTimeout(timer); + resolve(response); + timer = null; + status = 1; + } + if (response.ok) { + return response.json(); + } + throw new Error('Network response was not ok.'); + }).then(function(data) { + retryTimes = 0; + callback(data); + }).catch(function(error) { + if (retryTimes > 0) { + retryTimes -= 1; + setTimeout(() => { + request(); + }, 5000); + } else { + timeout(); + } + }); + }); + } + request(); + }, + layout: (cfg) => { + const el = $(cfg.el)[0]; + GitHubInfo.requestAPI(cfg.api, function(data) { + const arr = data.content || data; + for (let key of Object.keys(data)) { + $(el).find("[type=text]#" + key).text(data[key]); + $(el).find("[type=link]#" + key).attr("href", data[key]); + } + }, function() { + }); + }, +} + +$(function () { + const els = document.getElementsByClassName('github-info-wrap'); + for (var i = 0; i < els.length; i++) { + const el = els[i]; + const api = el.getAttribute('api'); + if (api == null) { + continue; + } + var cfg = new Object(); + cfg.el = el; + cfg.api = api; + cfg.class = el.getAttribute('class'); + GitHubInfo.layout(cfg); + } +});