[feat] linkcard self-hosting
This commit is contained in:
parent
76a2f37979
commit
e1eb07a11e
|
@ -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
|
||||
|
|
|
@ -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 += '"'
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue