hugo-theme-minima/assets/js/friends.js

57 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-02-17 11:18:35 +01:00
const dom = document.getElementById('friends')
const topk = '{{.Site.Params.friends.topk}}'
const raw = '{{.Site.Params.friends.feeds}}'
const feeds = raw.replace(/^\[|\]$/g, '').split(' ')
feeds.forEach(v => fetch(v).then(r => r.text()).then(r => rss(r)))
/**
* @param {string} xml
*/
function rss(xml) {
xml = xml.trim().replace(/\n/g, '')
const g = xml.matchAll(/<(item|entry)>.*?<\/(item|entry)>/g)
let n = +topk || 2;
while (n) {
const next = g.next()
if (next.done) {
break
}
// title
const title = next.value[0].match(/(?<=<title>).*(?=<\/title>)/)[0]
// link
const link = next.value[0].match(/(?<=<(link|id)>).*(?=<\/(link|id)>)/)[0]
// date
const date = next.value[0].match(/(?<=<(pubDate|updated)>).*(?=<\/(pubDate|updated)>)/)[0]
// innsert dom
const div = document.createElement('div')
2022-11-08 11:07:48 +01:00
div.className = 'flex justify-between'
2022-02-17 11:18:35 +01:00
div.innerHTML = template(link, title, date)
dom.appendChild(div)
n--;
}
}
/**
* @param {string} date
*/
function format(date) {
const d = new Date(date)
return d.toDateString()
}
function template() {
const a = arguments
return `
<a href=${a[0]}>${a[1]}</a>
2022-11-08 11:07:48 +01:00
<div>${format(a[2])}</div>
2022-02-17 11:18:35 +01:00
`.trim()
}