diff --git a/_config.yml b/_config.yml index 7126c5c..cfdb13a 100755 --- a/_config.yml +++ b/_config.yml @@ -280,6 +280,11 @@ footer: ######## Tag Plugins ######## tag_plugins: + # {% link %} + linkcard: + # 设置 api 可以自动提取网页标题、图标,服务部署方法:https://github.com/xaoxuu/site-info-api/ + # 接口测试通过后,把按钮的 href 部分替换成 ${href} 之后填写到下方,例如:https://api.vlts.cc/site_info/v1?url=${href} + api: # {% box %} / {% note %} note: default_color: '' # light, dark, red, orange, yellow, green, cyan, blue, purple, warning, error diff --git a/scripts/tags/lib/link.js b/scripts/tags/lib/link.js index de03382..7299b88 100644 --- a/scripts/tags/lib/link.js +++ b/scripts/tags/lib/link.js @@ -10,6 +10,13 @@ module.exports = ctx => function(args) { const full_url_for = require('hexo-util').full_url_for.bind(ctx) args = ctx.args.map(args, ['icon', 'desc'], ['url', 'title']) + if (args.url == null) { + return ''; + } + args.api = ctx.theme.config.tag_plugins.linkcard.api + if (args.api) { + args.api = args.api.replace('${href}', args.url) + } var autofill = [] if (!args.title) { autofill.push('title') @@ -27,6 +34,9 @@ module.exports = ctx => function(args) { el += ' target="_blank" rel="external nofollow noopener noreferrer"' } el += ' cardlink' + if (args.api) { + el += ` api="${args.api}"` + } el += ' autofill="' el += autofill.join(',') el += '"' diff --git a/source/js/plugins/linkcard.js b/source/js/plugins/linkcard.js index 6f8e99e..02712f2 100644 --- a/source/js/plugins/linkcard.js +++ b/source/js/plugins/linkcard.js @@ -30,11 +30,11 @@ function setCardLink(nodes) { nodes = 'forEach' in (nodes || {}) ? nodes : document.querySelectorAll('a[cardlink]') nodes.forEach((el) => { // If it is not a tag element then it is not processed - if (el.nodeType !== 1) return + if (el.nodeType !== 1) return; el.removeAttribute('cardlink'); - const link = el.href; - const api = 'https://api.vlts.cc/site_info/v1?url='; - fetch(api + link).then(function(response) { + const api = el.getAttribute('api'); + if (api == null) return; + fetch(api).then(function(response) { if (response.ok) { return response.json(); }