mirror of
https://github.com/jerryc127/hexo-theme-butterfly.git
synced 2026-04-08 12:07:06 +08:00
feat: 最新評論更新 [
1. <a> 標簽將會顯示 `[鏈接]` 2. <img>標簽將會顯示 `[圖片]` 3. <pre><code> 標簽將顯示 `[代碼]` 4. github-issues 獲取到的 url 改爲對應文章地址,而不是issues地址 5. 時間 time 增加 datetime屬性 6. waline 獲取到頭像會先匹配 waline數據庫是否有qq頭像屬性 7. 部分評論顯示的時間改爲更新時間 ]
This commit is contained in:
@@ -74,6 +74,9 @@ aside:
|
||||
loading_text: loading...
|
||||
error: Unable to get the data, please make sure the settings are correct.
|
||||
zero: No Comment
|
||||
image: image
|
||||
link: link
|
||||
code: code
|
||||
card_toc: Catalog
|
||||
|
||||
date_suffix:
|
||||
|
||||
@@ -74,6 +74,9 @@ aside:
|
||||
loading_text: loading...
|
||||
error: Unable to get the data, please make sure the settings are correct.
|
||||
zero: No Comment
|
||||
image: image
|
||||
link: link
|
||||
code: code
|
||||
card_toc: Catalog
|
||||
|
||||
date_suffix:
|
||||
|
||||
@@ -75,6 +75,9 @@ aside:
|
||||
loading_text: 正在加载中...
|
||||
error: 无法获取评论,请确认相关配置是否正确
|
||||
zero: 没有评论
|
||||
image: 图片
|
||||
link: 链接
|
||||
code: 代码
|
||||
card_toc: 目录
|
||||
|
||||
date_suffix:
|
||||
|
||||
@@ -20,7 +20,7 @@ post:
|
||||
created: 發表於
|
||||
updated: 更新於
|
||||
wordcount: 字數總計
|
||||
min2read: 閲讀時長
|
||||
min2read: 閱讀時長
|
||||
min2read_unit: 分鐘
|
||||
page_pv: 閱讀量
|
||||
comments: 評論數
|
||||
@@ -72,9 +72,12 @@ aside:
|
||||
more_button: 檢視更多
|
||||
card_newest_comments:
|
||||
headline: 最新評論
|
||||
loading_text: 正在加載中...
|
||||
loading_text: 正在載入中...
|
||||
error: 無法獲取評論,請確認相關配置是否正確
|
||||
zero: 沒有評論
|
||||
image: 圖片
|
||||
link: 連結
|
||||
code: 代碼
|
||||
card_toc: 目錄
|
||||
|
||||
date_suffix:
|
||||
|
||||
@@ -3,9 +3,10 @@ script.
|
||||
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(/<code>.*?<\/code>/gi, '[!{_p("aside.card_newest_comments.code")}]') // replace code
|
||||
content = content.replace(/<[^>]+>/g,"") // remove html tag
|
||||
content = content.replace(/(http(s?):)([/|.|\w|\s|-])*\.(?:jpg|jpeg|gif|png|webp)/g, '') // remove image link
|
||||
content = content.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi, '') // remove url
|
||||
|
||||
if (content.length > 150) {
|
||||
content = content.substring(0,150) + '...'
|
||||
@@ -26,7 +27,7 @@ script.
|
||||
'date': item.createdAt
|
||||
}
|
||||
})
|
||||
// set expiry to 10 min
|
||||
|
||||
saveToLocal.set('disqus-newest-comments', JSON.stringify(disqusArray), !{theme.newest_comments.storage}/(60*24))
|
||||
generateHtml(disqusArray)
|
||||
}).catch(e => {
|
||||
|
||||
@@ -3,9 +3,10 @@ script.
|
||||
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
|
||||
content = content.replace(/(http(s?):)([/|.|\w|\s|-])*\.(?:jpg|jpeg|gif|png|webp)/g, '') // remove image link
|
||||
content = content.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi, '') // remove url
|
||||
|
||||
if (content.length > 150) {
|
||||
content = content.substring(0,150) + '...'
|
||||
@@ -13,21 +14,50 @@ script.
|
||||
return content
|
||||
}
|
||||
|
||||
const findTrueUrl = (array) => {
|
||||
let url = ''
|
||||
Promise.all(array.map(item =>
|
||||
fetch(item.url).then(resp => resp.json()).then(data => {
|
||||
if (data.user.login === 'utterances-bot') {
|
||||
url = data.body.match(/https?\:\/\/[^\" ]+/ig).slice(-1)
|
||||
return url[0]
|
||||
} else {
|
||||
url = data.body.match(/https?\:\/\/[^\" ]+/i)
|
||||
return url[0]
|
||||
}
|
||||
})
|
||||
)).then(res => {
|
||||
array = array.map((i,index)=> {
|
||||
return {
|
||||
...i,
|
||||
url: res[index]
|
||||
}
|
||||
})
|
||||
|
||||
saveToLocal.set('github-newest-comments', JSON.stringify(array), !{theme.newest_comments.storage}/(60*24))
|
||||
generateHtml(array)
|
||||
});
|
||||
}
|
||||
|
||||
const getComment = () => {
|
||||
fetch('https://api.github.com/repos/!{theme.newest_comments.github_issues.repo}/issues/comments?sort=updated&direction=desc&per_page=!{theme.newest_comments.limit}&page=1')
|
||||
fetch('https://api.github.com/repos/!{theme.newest_comments.github_issues.repo}/issues/comments?sort=updated&direction=desc&per_page=!{theme.newest_comments.limit}&page=1',{
|
||||
"headers": {
|
||||
Accept: 'application/vnd.github.v3.html+json'
|
||||
}
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
const githubArray = data.map(item => {
|
||||
return {
|
||||
'avatar': item.user.avatar_url,
|
||||
'content': changeContent(item.body),
|
||||
'content': changeContent(item.body_html),
|
||||
'nick': item.user.login,
|
||||
'url': item.html_url,
|
||||
'date': item.updated_at
|
||||
'url': item.issue_url,
|
||||
'date': item.updated_at,
|
||||
'githubUrl': item.html_url
|
||||
}
|
||||
})
|
||||
saveToLocal.set('github-newest-comments', JSON.stringify(githubArray), !{theme.newest_comments.storage}/(60*24))
|
||||
generateHtml(githubArray)
|
||||
findTrueUrl(githubArray)
|
||||
}).catch(e => {
|
||||
const $dom = document.querySelector('#card-newest-comments .aside-list')
|
||||
$dom.innerHTML= "!{_p('aside.card_newest_comments.error')}"
|
||||
@@ -43,12 +73,12 @@ script.
|
||||
|
||||
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 += `<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}'>${array[i].content}</a>
|
||||
<div class='name'><span>${array[i].nick}</span><time> / ${btf.diffDate(array[i].date, true)}</time></div>
|
||||
<div class='name'><span>${array[i].nick} / </span><time datetime="${array[i].date}">${btf.diffDate(array[i].date, true)}</time></div>
|
||||
</div></div>`
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -3,9 +3,10 @@ script.
|
||||
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
|
||||
content = content.replace(/(http(s?):)([/|.|\w|\s|-])*\.(?:jpg|jpeg|gif|png|webp)/g, '') // remove image link
|
||||
content = content.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi, '') // remove url
|
||||
|
||||
if (content.length > 150) {
|
||||
content = content.substring(0,150) + '...'
|
||||
@@ -21,8 +22,18 @@ script.
|
||||
pageSize: !{theme.newest_comments.limit},
|
||||
includeReply: true
|
||||
}).then(function (res) {
|
||||
saveToLocal.set('twikoo-newest-comments', JSON.stringify(res), !{theme.newest_comments.storage}/(60*24))
|
||||
generateHtml(res)
|
||||
const twikooArray = res.map(e => {
|
||||
return {
|
||||
'content': changeContent(e.comment),
|
||||
'avatar': e.avatar,
|
||||
'nick': e.nick,
|
||||
'url': e.url + '#' + e.id,
|
||||
'date': new Date(e.created).toISOString()
|
||||
}
|
||||
})
|
||||
|
||||
saveToLocal.set('twikoo-newest-comments', JSON.stringify(twikooArray), !{theme.newest_comments.storage}/(60*24))
|
||||
generateHtml(twikooArray)
|
||||
}).catch(function (err) {
|
||||
const $dom = document.querySelector('#card-newest-comments .aside-list')
|
||||
$dom.innerHTML= "!{_p('aside.card_newest_comments.error')}"
|
||||
@@ -45,12 +56,12 @@ script.
|
||||
|
||||
if (!{theme.newest_comments.avatar}) {
|
||||
const name = '!{theme.lazyload.enable ? "data-lazy-src" : "src"}'
|
||||
result += `<a href='${array[i].url + '#' + array[i].id}' class='thumbnail'><img ${name}='${array[i].avatar}' alt='${array[i].nick}'></a>`
|
||||
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 + '#' + array[i].id}'>${changeContent(array[i].commentText)}</a>
|
||||
<div class='name'><span>${array[i].nick}</span><time> / ${btf.diffDate(array[i].created, true)}</time></div>
|
||||
<a class='comment' href='${array[i].url}'>${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 {
|
||||
|
||||
@@ -6,9 +6,10 @@ script.
|
||||
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
|
||||
content = content.replace(/(http(s?):)([/|.|\w|\s|-])*\.(?:jpg|jpeg|gif|png|webp)/g, '') // remove image link
|
||||
content = content.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi, '') // remove url
|
||||
|
||||
if (content.length > 150) {
|
||||
content = content.substring(0,150) + '...'
|
||||
@@ -32,12 +33,12 @@ script.
|
||||
|
||||
if (!{theme.newest_comments.avatar}) {
|
||||
const name = '!{theme.lazyload.enable ? "data-lazy-src" : "src"}'
|
||||
result += `<a href='${array[i].url}' class="thumbnail"><img ${name}='${getIcon(array[i].avatar, array[i].mail)}' alt='${array[i].nick}'></a>`
|
||||
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}'>${array[i].content}</a>
|
||||
<div class='name'><span>${array[i].nick}</span><time> / ${btf.diffDate(array[i].date, true)}</time></div>
|
||||
<div class='name'><span>${array[i].nick} / </span><time datetime="${array[i].date}">${btf.diffDate(array[i].date, true)}</time></div>
|
||||
</div></div>`
|
||||
}
|
||||
} else {
|
||||
@@ -67,12 +68,11 @@ script.
|
||||
.then(data => {
|
||||
const valineArray = data.results.map(function (e) {
|
||||
return {
|
||||
'avatar': e.QQAvatar,
|
||||
'avatar': getIcon(e.QQAvatar, e.mail),
|
||||
'content': changeContent(e.comment),
|
||||
'mail': e.mail,
|
||||
'nick': e.nick,
|
||||
'url': e.url + '#' + e.objectId,
|
||||
'date': e.createdAt,
|
||||
'date': e.updatedAt,
|
||||
}
|
||||
})
|
||||
saveToLocal.set('valine-newest-comments', JSON.stringify(valineArray), !{theme.newest_comments.storage}/(60*24))
|
||||
|
||||
@@ -5,9 +5,10 @@ script.
|
||||
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
|
||||
content = content.replace(/(http(s?):)([/|.|\w|\s|-])*\.(?:jpg|jpeg|gif|png|webp)/g, '') // remove image link
|
||||
content = content.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi, '') // remove url
|
||||
|
||||
if (content.length > 150) {
|
||||
content = content.substring(0,150) + '...'
|
||||
@@ -15,7 +16,8 @@ script.
|
||||
return content
|
||||
}
|
||||
|
||||
const getIcon = (mail) => {
|
||||
const getIcon = (avatar,mail) => {
|
||||
if (avatar) return avatar
|
||||
let defaultIcon = '!{ default_avatar ? `?d=${default_avatar}` : ''}'
|
||||
let iconUrl = `https://gravatar.loli.net/avatar/${mail + defaultIcon}`
|
||||
return iconUrl
|
||||
@@ -30,12 +32,12 @@ script.
|
||||
|
||||
if (!{theme.newest_comments.avatar}) {
|
||||
const name = '!{theme.lazyload.enable ? "data-lazy-src" : "src"}'
|
||||
result += `<a href='${array[i].url}' class="thumbnail"><img ${name}='${getIcon(array[i].mail)}' alt='${array[i].nick}'></a>`
|
||||
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}'>${array[i].content}</a>
|
||||
<div class='name'><span>${array[i].nick}</span><time> / ${btf.diffDate(array[i].date, true)}</time></div>
|
||||
<div class='name'><span>${array[i].nick} / </span><time datetime="${array[i].date}">${btf.diffDate(array[i].date, true)}</time></div>
|
||||
</div></div>`
|
||||
}
|
||||
} else {
|
||||
@@ -58,10 +60,10 @@ script.
|
||||
const walineArray = comments.map(function (e) {
|
||||
return {
|
||||
'content': changeContent(e.comment),
|
||||
'mail': e.mail,
|
||||
'avatar': getIcon(e.QQAvatar,e.mail),
|
||||
'nick': e.nick,
|
||||
'url': e.url + '#' + e.objectId,
|
||||
'date': e.createdAt,
|
||||
'date': e.updatedAt,
|
||||
}
|
||||
})
|
||||
saveToLocal.set('waline-newest-comments', JSON.stringify(walineArray), !{theme.newest_comments.storage}/(60*24))
|
||||
|
||||
Reference in New Issue
Block a user