link
This commit is contained in:
parent
26a212cf10
commit
bab88f4965
|
@ -11,7 +11,16 @@ const full_url_for = require('hexo-util').full_url_for.bind(hexo);
|
|||
|
||||
hexo.extend.tag.register('link', function(args) {
|
||||
args = hexo.args.map(args, ['icon', 'desc'], ['url', 'title']);
|
||||
|
||||
var autofill = [];
|
||||
if (!args.title) {
|
||||
autofill.push('title');
|
||||
}
|
||||
if (!args.icon) {
|
||||
autofill.push('icon');
|
||||
}
|
||||
if (args.desc) {
|
||||
autofill.push('desc');
|
||||
}
|
||||
var el = '';
|
||||
el += '<div class="tag-plugin link dis-select">';
|
||||
el += '<a class="link-card' + (args.desc ? ' rich' : ' plain') + '" title="' + (args.title || '') + '" href="' + args.url + '"';
|
||||
|
@ -19,6 +28,9 @@ hexo.extend.tag.register('link', function(args) {
|
|||
el += ' target="_blank" rel="external nofollow noopener noreferrer"';
|
||||
}
|
||||
el += ' cardlink';
|
||||
el += ' autofill="';
|
||||
el += autofill.join(',');
|
||||
el += '"';
|
||||
el += '>';
|
||||
|
||||
function loadIcon() {
|
||||
|
|
|
@ -1,104 +1,25 @@
|
|||
// 本插件由CardLink定制而成,原项目源码: https://github.com/Lete114/CardLink
|
||||
|
||||
var cardLink = {};
|
||||
cardLink.caches = {};
|
||||
cardLink.server = 'https://api.allorigins.win/raw?url=';
|
||||
|
||||
/**
|
||||
* Remove '/' and '/index.html'
|
||||
* @param {String} params
|
||||
* @returns { String }
|
||||
*/
|
||||
function indexHandler(params) {
|
||||
let path = params.replace(/(\/index\.html|\/)*$/gi, '')
|
||||
if (path.length === 0) path += '/'
|
||||
return path
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if it is a ['https://', 'http://', '//'] protocol
|
||||
* @param {String} url Website url
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
function isHttp(url) {
|
||||
return /^(https?:)?\/\//g.test(url)
|
||||
}
|
||||
|
||||
function renderer(el, obj) {
|
||||
if (obj.title && obj.title.length > 0) {
|
||||
var autofill = [];
|
||||
const autofillStr = el.getAttribute('autofill');
|
||||
if (autofillStr) {
|
||||
autofill = autofillStr.split(',');
|
||||
}
|
||||
if (obj.title && obj.title.length > 0 && autofill.includes('title')) {
|
||||
el.querySelector('.title').innerHTML = obj.title;
|
||||
el.title = obj.title;
|
||||
}
|
||||
if (obj.icon && obj.icon.length > 0) {
|
||||
if (obj.icon && obj.icon.length > 0 && autofill.includes('icon')) {
|
||||
el.querySelector('.img').style = 'background-image: url("' + obj.icon + '");';
|
||||
el.querySelector('.img').setAttribute('data-bg', obj.icon);
|
||||
}
|
||||
let desc = el.querySelector('.desc');
|
||||
if (desc && obj.desc && obj.desc.length > 0) {
|
||||
if (desc && obj.desc && obj.desc.length > 0 && autofill.includes('desc')) {
|
||||
desc.innerHTML = obj.desc;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get info
|
||||
* @param {Element} el Element
|
||||
* @param {String} html String type html
|
||||
* @param {String} link Website address
|
||||
*/
|
||||
// eslint-disable-next-line max-statements
|
||||
function getInfo(el, html, link) {
|
||||
try {
|
||||
let title, icon, desc
|
||||
const doc = new DOMParser().parseFromString(html, 'text/html')
|
||||
// If there is no title, no card link is generated
|
||||
title = doc.querySelector('title')
|
||||
if (title) {
|
||||
title = title.textContent
|
||||
|
||||
// Get the src of the first img tag in the body tag
|
||||
let tmp = doc.querySelector('head link[rel="apple-touch-icon"]')
|
||||
if (!tmp) {
|
||||
tmp = doc.querySelector('head link[rel="icon"]')
|
||||
}
|
||||
icon = tmp && tmp.getAttribute('href')
|
||||
|
||||
if (/^data:image/.test(icon)) icon = ''
|
||||
|
||||
// If there is no src then get the site icon
|
||||
if (!icon) {
|
||||
const links = [].slice.call(doc.querySelectorAll('link[rel][href]'))
|
||||
icon = links.find((_el) => _el.rel.includes('icon'))
|
||||
icon = icon && icon.getAttribute('href')
|
||||
}
|
||||
|
||||
desc = doc.querySelector('head meta[name="description"]')
|
||||
|
||||
if (desc) {
|
||||
desc = desc.content;
|
||||
}
|
||||
// If `icon` is not the ['https://', 'http://', '//'] protocol, splice on the `origin` of the a tag
|
||||
if (icon && !isHttp(icon)) icon = new URL(link).origin + icon
|
||||
cardLink.caches[link] = { title, link, icon, desc }
|
||||
|
||||
renderer(el, cardLink.caches[link])
|
||||
}
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn('CardLink Error: Failed to parse', error)
|
||||
}
|
||||
}
|
||||
|
||||
function fetchPage(link, callback) {
|
||||
fetch(link)
|
||||
.then((result) => result.text())
|
||||
.then(callback)
|
||||
.catch((error) => {
|
||||
const server = cardLink.server
|
||||
// eslint-disable-next-line no-console
|
||||
if (link.includes(server) || !server) return console.error('CardLink Error:', error)
|
||||
fetchPage(server + link, callback)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Create card links
|
||||
* @param {NodeList} nodes A collection of nodes or a collection of arrays,
|
||||
|
@ -110,16 +31,18 @@ function setCardLink(nodes) {
|
|||
nodes.forEach((el) => {
|
||||
// If it is not a tag element then it is not processed
|
||||
if (el.nodeType !== 1) return
|
||||
el.removeAttribute('cardlink')
|
||||
const link = el.href
|
||||
|
||||
const cache = cardLink.caches[link]
|
||||
if (cache) return renderer(el, cache)
|
||||
|
||||
if (isHttp(link)) {
|
||||
fetchPage(link, (html) => {
|
||||
getInfo(el, html, link)
|
||||
})
|
||||
}
|
||||
el.removeAttribute('cardlink');
|
||||
const link = el.href;
|
||||
const api = 'https://site-info.vlts.cc/api?url=';
|
||||
fetch(api + link).then(function(response) {
|
||||
if (response.ok) {
|
||||
return response.json();
|
||||
}
|
||||
throw new Error('Network response was not ok.');
|
||||
}).then(function(data) {
|
||||
renderer(el, data);
|
||||
}).catch(function(error) {
|
||||
console.log(error);
|
||||
});
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue