[feat] linkcard self-hosting

This commit is contained in:
xaoxuu 2024-02-04 15:22:45 +08:00
parent 76a2f37979
commit e1eb07a11e
3 changed files with 19 additions and 4 deletions

View File

@ -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

View File

@ -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 += '"'

View File

@ -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();
}