mirror of
https://github.com/jerryc127/hexo-theme-butterfly.git
synced 2025-09-15 12:58:48 +08:00
feat: Remark42支持显示最新评论
This commit is contained in:
parent
1f3f2f471b
commit
cbf52c3053
@ -23,4 +23,6 @@ if use
|
||||
include ./github-issues.pug
|
||||
when 'Utterances'
|
||||
- userRepo = theme.utterances.repo
|
||||
include ./github-issues.pug
|
||||
include ./github-issues.pug
|
||||
when 'Remark42'
|
||||
include ./remark42.pug
|
||||
92
layout/includes/third-party/newest-comments/remark42.pug
vendored
Normal file
92
layout/includes/third-party/newest-comments/remark42.pug
vendored
Normal file
@ -0,0 +1,92 @@
|
||||
- const { host, siteId } = theme.remark42
|
||||
- const { limit } = theme.newest_comments
|
||||
|
||||
script.
|
||||
window.addEventListener('load', () => {
|
||||
const api = ['!{host}','/api/v1/last/','!{limit}','?site=','!{siteId}'].join('')
|
||||
|
||||
const changeContent = (content) => {
|
||||
if (content === '') return content
|
||||
|
||||
content = content.replace(/<img.*?src="(.*?)"?[^\>]+>/ig, '[!{_p("aside.card_newest_comments.image")}]') // replace image link
|
||||
content = content.replace(/<a[^>]+?href=["']?([^"']+)["']?[^>]*>([^<]+)<\/a>/gi, '[!{_p("aside.card_newest_comments.link")}]') // replace url
|
||||
content = content.replace(/<pre><code>.*?<\/pre>/gi, '[!{_p("aside.card_newest_comments.code")}]') // replace code
|
||||
content = content.replace(/<[^>]+>/g,"") // remove html tag
|
||||
|
||||
if (content.length > 150) {
|
||||
content = content.substring(0,150) + '...'
|
||||
}
|
||||
return content
|
||||
}
|
||||
|
||||
const getComment = () => {
|
||||
try {
|
||||
let request = new XMLHttpRequest()
|
||||
request.open("GET",api)
|
||||
request.send(null)
|
||||
request.onload = () => {
|
||||
if (request.status == 200) {
|
||||
const commentsData = JSON.parse(request.responseText)
|
||||
const commentsArray = commentsData.map(k => {
|
||||
return {
|
||||
'content': changeContent(k.text), //Maybe we can use e.orig
|
||||
'avatar': k.user.picture,
|
||||
'nick': k.user.name,
|
||||
'url': k.locator.url,
|
||||
'date': new Date(k.time).toISOString()
|
||||
}
|
||||
})
|
||||
|
||||
saveToLocal.set('remark42-newest-comments', JSON.stringify(commentsArray), !{theme.newest_comments.storage}/(60*24))
|
||||
generateHtml(commentsArray)
|
||||
} else {
|
||||
throw Error("Request to Remark42 Failed")
|
||||
}
|
||||
}
|
||||
} catch(e) {
|
||||
const $dom = document.querySelector('#card-newest-comments .aside-list')
|
||||
$dom.innerHTML= "!{_p('aside.card_newest_comments.error')}"
|
||||
}
|
||||
}
|
||||
|
||||
const generateHtml = array => {
|
||||
let result = ''
|
||||
|
||||
if (array.length) {
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
result += '<div class=\'aside-list-item\'>'
|
||||
|
||||
if (!{theme.newest_comments.avatar}) {
|
||||
const name = '!{theme.lazyload.enable ? "data-lazy-src" : "src"}'
|
||||
result += `<a href='${array[i].url}' class='thumbnail'><img ${name}='${array[i].avatar}' alt='${array[i].nick}'></a>`
|
||||
}
|
||||
|
||||
result += `<div class='content'>
|
||||
<a class='comment' href='${array[i].url}' title='${array[i].content}'>${array[i].content}</a>
|
||||
<div class='name'><span>${array[i].nick} / </span><time datetime="${array[i].date}">${btf.diffDate(array[i].date, true)}</time></div>
|
||||
</div></div>`
|
||||
}
|
||||
} else {
|
||||
result += '!{_p("aside.card_newest_comments.zero")}'
|
||||
}
|
||||
|
||||
let $dom = document.querySelector('#card-newest-comments .aside-list')
|
||||
$dom.innerHTML= result
|
||||
window.lazyLoadInstance && window.lazyLoadInstance.update()
|
||||
window.pjax && window.pjax.refresh($dom)
|
||||
}
|
||||
|
||||
const newestCommentInit = () => {
|
||||
if (document.querySelector('#card-newest-comments .aside-list')) {
|
||||
const data = saveToLocal.get('remark42-newest-comments')
|
||||
if (data) {
|
||||
generateHtml(JSON.parse(data))
|
||||
} else {
|
||||
getComment()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
newestCommentInit()
|
||||
document.addEventListener('pjax:complete', newestCommentInit)
|
||||
})
|
||||
Loading…
Reference in New Issue
Block a user