add theme
This commit is contained in:
67
themes/butterfly/layout/includes/third-party/newest-comments/artalk.pug
vendored
Normal file
67
themes/butterfly/layout/includes/third-party/newest-comments/artalk.pug
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
- const { server, site, option } = theme.artalk
|
||||
- const avatarCdn = (option !== null && option.gravatar && option.gravatar.mirror) || ''
|
||||
- const avatarDefault = (option !== null && option.gravatar && (option.gravatar.params || option.gravatar.default)) || ''
|
||||
|
||||
!= partial("includes/third-party/newest-comments/common.pug", {}, { cache: true })
|
||||
|
||||
script.
|
||||
window.addEventListener('load', () => {
|
||||
const keyName = 'artalk-newest-comments'
|
||||
const { changeContent, generateHtml, run } = window.newestComments
|
||||
|
||||
const getAvatarValue = async () => {
|
||||
const predefinedAvatarCdn = '!{avatarCdn}'
|
||||
const predefinedAvatarDefault = '!{avatarDefault}'
|
||||
|
||||
const avatarDefaultFormat = e => e.startsWith('d=') ? e : `d=${e}`
|
||||
|
||||
if (predefinedAvatarCdn && predefinedAvatarDefault) {
|
||||
return { avatarCdn: predefinedAvatarCdn, avatarDefault: avatarDefaultFormat(predefinedAvatarDefault) }
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch('!{server}/api/v2/conf')
|
||||
const result = await res.json()
|
||||
const { mirror, params, default: defaults } = result.frontend_conf.gravatar
|
||||
const avatarCdn = predefinedAvatarCdn || mirror
|
||||
let avatarDefault = avatarDefaultFormat(predefinedAvatarDefault || params || defaults)
|
||||
return { avatarCdn, avatarDefault}
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
return { avatarCdn: predefinedAvatarCdn, avatarDefault: avatarDefaultFormat(predefinedAvatarDefault) }
|
||||
}
|
||||
}
|
||||
|
||||
const searchParams = new URLSearchParams({
|
||||
'site_name': '!{site}',
|
||||
'limit': '!{newestCommentsLimit * 2}', // Fetch more comments to filter pending comments
|
||||
})
|
||||
|
||||
const getComment = async (ele) => {
|
||||
try {
|
||||
const res = await fetch(`!{server}/api/v2/stats/latest_comments?${searchParams}`)
|
||||
const result = await res.json()
|
||||
const { avatarCdn, avatarDefault } = await getAvatarValue()
|
||||
const artalk = result.data
|
||||
.filter(e => !e.is_pending) // Filter pending comments
|
||||
.slice(0, !{newestCommentsLimit}) // Limit the number of comments
|
||||
.map(e => {
|
||||
const avatar = avatarCdn && e.email_encrypted ? `${avatarCdn}${e.email_encrypted}?${avatarDefault}` : ''
|
||||
return {
|
||||
'avatar': avatar,
|
||||
'content': changeContent(e.content_marked),
|
||||
'nick': e.nick,
|
||||
'url': e.page_url,
|
||||
'date': e.date,
|
||||
}
|
||||
})
|
||||
btf.saveToLocal.set(keyName, JSON.stringify(artalk), !{theme.aside.card_newest_comments.storage}/(60*24))
|
||||
generateHtml(artalk, ele)
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
ele.textContent= "!{_p('aside.card_newest_comments.error')}"
|
||||
}
|
||||
}
|
||||
|
||||
run(keyName, getComment)
|
||||
})
|
||||
61
themes/butterfly/layout/includes/third-party/newest-comments/common.pug
vendored
Normal file
61
themes/butterfly/layout/includes/third-party/newest-comments/common.pug
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
script.
|
||||
window.newestComments = {
|
||||
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(/<code>.*?<\/code>/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
|
||||
},
|
||||
|
||||
generateHtml: (array, ele) => {
|
||||
let result = ''
|
||||
|
||||
if (array.length) {
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
result += '<div class="aside-list-item">'
|
||||
|
||||
if (!{theme.aside.card_newest_comments.avatar} && array[i].avatar) {
|
||||
const imgAttr = '!{theme.lazyload.enable && !theme.lazyload.native ? "data-lazy-src" : "src"}'
|
||||
const lazyloadNative = '!{theme.lazyload.enable && theme.lazyload.native ? "loading=\"lazy\"" : ""}'
|
||||
result += `<a href="${array[i].url}" class="thumbnail"><img ${imgAttr}="${array[i].avatar}" alt="${array[i].nick}" ${lazyloadNative}></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")}'
|
||||
}
|
||||
|
||||
ele.innerHTML = result
|
||||
window.lazyLoadInstance && window.lazyLoadInstance.update()
|
||||
window.pjax && window.pjax.refresh(ele)
|
||||
},
|
||||
|
||||
newestCommentInit: (name, getComment) => {
|
||||
const $dom = document.querySelector('#card-newest-comments .aside-list')
|
||||
if ($dom) {
|
||||
const data = btf.saveToLocal.get(name)
|
||||
if (data) {
|
||||
newestComments.generateHtml(JSON.parse(data), $dom)
|
||||
} else {
|
||||
getComment($dom)
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
run: (name, getComment) => {
|
||||
newestComments.newestCommentInit(name, getComment)
|
||||
btf.addGlobalFn('pjaxComplete', () => newestComments.newestCommentInit(name, getComment), name)
|
||||
}
|
||||
}
|
||||
34
themes/butterfly/layout/includes/third-party/newest-comments/disqus-comment.pug
vendored
Normal file
34
themes/butterfly/layout/includes/third-party/newest-comments/disqus-comment.pug
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
!= partial("includes/third-party/newest-comments/common.pug", {}, { cache: true })
|
||||
|
||||
script.
|
||||
window.addEventListener('load', () => {
|
||||
const keyName = 'disqus-newest-comments'
|
||||
const { changeContent, generateHtml, run } = window.newestComments
|
||||
|
||||
const getComment = ele => {
|
||||
fetch('https://disqus.com/api/3.0/forums/listPosts.json?forum=!{forum}&related=thread&limit=!{newestCommentsLimit}&api_key=!{apiKey}')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
const disqusArray = data.response.map(item => {
|
||||
return {
|
||||
'avatar': item.author.avatar.cache,
|
||||
'content': changeContent(item.message),
|
||||
'nick': item.author.name,
|
||||
'url': item.url,
|
||||
'date': item.createdAt
|
||||
}
|
||||
})
|
||||
|
||||
btf.saveToLocal.set(keyName, JSON.stringify(disqusArray), !{theme.aside.card_newest_comments.storage}/(60*24))
|
||||
generateHtml(disqusArray, ele)
|
||||
}).catch(e => {
|
||||
console.error(e)
|
||||
ele.textContent= "!{_p('aside.card_newest_comments.error')}"
|
||||
})
|
||||
}
|
||||
|
||||
run(keyName, getComment)
|
||||
})
|
||||
|
||||
|
||||
|
||||
62
themes/butterfly/layout/includes/third-party/newest-comments/github-issues.pug
vendored
Normal file
62
themes/butterfly/layout/includes/third-party/newest-comments/github-issues.pug
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
!= partial("includes/third-party/newest-comments/common.pug", {}, { cache: true })
|
||||
|
||||
script.
|
||||
window.addEventListener('load', () => {
|
||||
const keyName = 'github-newest-comments'
|
||||
const { changeContent, generateHtml, run } = window.newestComments
|
||||
|
||||
const findTrueUrl = (array, ele) => {
|
||||
Promise.all(array.map(item =>
|
||||
fetch(item.url).then(resp => resp.json()).then(data => {
|
||||
let urlArray = data.body ? data.body.match(/(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?/ig) : []
|
||||
if (!Array.isArray(urlArray) || urlArray.length === 0) {
|
||||
urlArray = [`${data.html_url}`]
|
||||
}
|
||||
if (data.user.login === 'utterances-bot') {
|
||||
return urlArray.pop()
|
||||
} else {
|
||||
return urlArray.shift()
|
||||
}
|
||||
})
|
||||
)).then(res => {
|
||||
array = array.map((i,index)=> {
|
||||
return {
|
||||
...i,
|
||||
url: res[index]
|
||||
}
|
||||
})
|
||||
|
||||
btf.saveToLocal.set(keyName, JSON.stringify(array), !{theme.aside.card_newest_comments.storage}/(60*24))
|
||||
generateHtml(array, ele)
|
||||
});
|
||||
}
|
||||
|
||||
const getComment = ele => {
|
||||
fetch('https://api.github.com/repos/!{userRepo}/issues/comments?sort=updated&direction=desc&per_page=!{newestCommentsLimit}&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_html || item.body),
|
||||
'nick': item.user.login,
|
||||
'url': item.issue_url,
|
||||
'date': item.updated_at
|
||||
}
|
||||
})
|
||||
findTrueUrl(githubArray, ele)
|
||||
}).catch(e => {
|
||||
console.error(e)
|
||||
ele.textContent= "!{_p('aside.card_newest_comments.error')}"
|
||||
})
|
||||
}
|
||||
run(keyName, getComment)
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
34
themes/butterfly/layout/includes/third-party/newest-comments/index.pug
vendored
Normal file
34
themes/butterfly/layout/includes/third-party/newest-comments/index.pug
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
- let { use } = theme.comments
|
||||
|
||||
if use
|
||||
-
|
||||
let forum,apiKey,userRepo
|
||||
let { limit:newestCommentsLimit } = theme.aside.card_newest_comments
|
||||
if (newestCommentsLimit > 10 || newestCommentsLimit < 1) newestCommentsLimit = 6
|
||||
|
||||
case use[0]
|
||||
when 'Valine'
|
||||
include ./valine.pug
|
||||
when 'Waline'
|
||||
include ./waline.pug
|
||||
when 'Twikoo'
|
||||
include ./twikoo-comment.pug
|
||||
when 'Disqus'
|
||||
- forum = theme.disqus.shortname
|
||||
- apiKey = theme.disqus.apikey
|
||||
include ./disqus-comment.pug
|
||||
when 'Disqusjs'
|
||||
- forum = theme.disqusjs.shortname
|
||||
- apiKey = theme.disqusjs.apikey
|
||||
include ./disqus-comment.pug
|
||||
when 'Gitalk'
|
||||
- let { repo,owner } = theme.gitalk
|
||||
- userRepo = owner + '/' + repo
|
||||
include ./github-issues.pug
|
||||
when 'Utterances'
|
||||
- userRepo = theme.utterances.repo
|
||||
include ./github-issues.pug
|
||||
when 'Remark42'
|
||||
include ./remark42.pug
|
||||
when 'Artalk'
|
||||
include ./artalk.pug
|
||||
31
themes/butterfly/layout/includes/third-party/newest-comments/remark42.pug
vendored
Normal file
31
themes/butterfly/layout/includes/third-party/newest-comments/remark42.pug
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
- const { host, siteId } = theme.remark42
|
||||
!= partial("includes/third-party/newest-comments/common.pug", {}, { cache: true })
|
||||
|
||||
script.
|
||||
window.addEventListener('load', () => {
|
||||
const keyName = 'remark42-newest-comments'
|
||||
const { changeContent, generateHtml, run } = window.newestComments
|
||||
|
||||
const getComment = ele => {
|
||||
fetch('!{host}/api/v1/last/!{newestCommentsLimit}?site=!{siteId}')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
const remark42 = data.map(e => {
|
||||
return {
|
||||
'avatar': e.user.picture,
|
||||
'content': changeContent(e.text),
|
||||
'nick': e.user.name,
|
||||
'url': e.locator.url,
|
||||
'date': e.time,
|
||||
}
|
||||
})
|
||||
btf.saveToLocal.set(keyName, JSON.stringify(remark42), !{theme.aside.card_newest_comments.storage}/(60*24))
|
||||
generateHtml(remark42, ele)
|
||||
}).catch(e => {
|
||||
console.error(e)
|
||||
ele.textContent= "!{_p('aside.card_newest_comments.error')}"
|
||||
})
|
||||
}
|
||||
|
||||
run(keyName, getComment)
|
||||
})
|
||||
45
themes/butterfly/layout/includes/third-party/newest-comments/twikoo-comment.pug
vendored
Normal file
45
themes/butterfly/layout/includes/third-party/newest-comments/twikoo-comment.pug
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
!= partial("includes/third-party/newest-comments/common.pug", {}, { cache: true })
|
||||
|
||||
script.
|
||||
window.addEventListener('load', () => {
|
||||
const keyName = 'twikoo-newest-comments'
|
||||
const { changeContent, generateHtml, run } = window.newestComments
|
||||
|
||||
const getComment = ele => {
|
||||
const runTwikoo = () => {
|
||||
twikoo.getRecentComments({
|
||||
envId: '!{theme.twikoo.envId}',
|
||||
region: '!{theme.twikoo.region}',
|
||||
pageSize: !{newestCommentsLimit},
|
||||
includeReply: true
|
||||
}).then(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()
|
||||
}
|
||||
})
|
||||
|
||||
btf.saveToLocal.set(keyName, JSON.stringify(twikooArray), !{theme.aside.card_newest_comments.storage}/(60*24))
|
||||
generateHtml(twikooArray, ele)
|
||||
}).catch(err => {
|
||||
console.error(err)
|
||||
ele.textContent= "!{_p('aside.card_newest_comments.error')}"
|
||||
})
|
||||
}
|
||||
|
||||
if (typeof twikoo === 'object') {
|
||||
runTwikoo()
|
||||
} else {
|
||||
btf.getScript('!{url_for(theme.asset.twikoo)}').then(runTwikoo)
|
||||
}
|
||||
}
|
||||
|
||||
run(keyName, getComment)
|
||||
})
|
||||
|
||||
|
||||
|
||||
51
themes/butterfly/layout/includes/third-party/newest-comments/valine.pug
vendored
Normal file
51
themes/butterfly/layout/includes/third-party/newest-comments/valine.pug
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
- let default_avatar = theme.valine.avatar
|
||||
|
||||
script(src=url_for(theme.asset.blueimp_md5))
|
||||
!= partial("includes/third-party/newest-comments/common.pug", {}, { cache: true })
|
||||
|
||||
script.
|
||||
window.addEventListener('load', () => {
|
||||
const keyName = 'valine-newest-comments'
|
||||
const { changeContent, generateHtml, run } = window.newestComments
|
||||
|
||||
const getIcon = (icon, mail) => {
|
||||
if (icon) return icon
|
||||
let defaultIcon = '!{ default_avatar ? `?d=${default_avatar}` : ''}'
|
||||
let iconUrl = `https://gravatar.loli.net/avatar/${md5(mail.toLowerCase()) + defaultIcon}`
|
||||
return iconUrl
|
||||
}
|
||||
|
||||
const getComment = ele => {
|
||||
const serverURL = '!{theme.valine.serverURLs || `https://${theme.valine.appId.substring(0,8)}.api.lncldglobal.com` }'
|
||||
|
||||
var settings = {
|
||||
"method": "GET",
|
||||
"headers": {
|
||||
"X-LC-Id": '!{theme.valine.appId}',
|
||||
"X-LC-Key": '!{theme.valine.appKey}',
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
}
|
||||
|
||||
fetch(`${serverURL}/1.1/classes/Comment?limit=!{newestCommentsLimit}&order=-createdAt`,settings)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
const valineArray = data.results.map(e => {
|
||||
return {
|
||||
'avatar': getIcon(e.QQAvatar, e.mail),
|
||||
'content': changeContent(e.comment),
|
||||
'nick': e.nick,
|
||||
'url': e.url + '#' + e.objectId,
|
||||
'date': e.updatedAt,
|
||||
}
|
||||
})
|
||||
btf.saveToLocal.set(keyName, JSON.stringify(valineArray), !{theme.aside.card_newest_comments.storage}/(60*24))
|
||||
generateHtml(valineArray, ele)
|
||||
}).catch(e => {
|
||||
console.error(e)
|
||||
ele.textContent= "!{_p('aside.card_newest_comments.error')}"
|
||||
})
|
||||
}
|
||||
|
||||
run(keyName, getComment)
|
||||
})
|
||||
32
themes/butterfly/layout/includes/third-party/newest-comments/waline.pug
vendored
Normal file
32
themes/butterfly/layout/includes/third-party/newest-comments/waline.pug
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
- const serverURL = theme.waline.serverURL.replace(/\/$/, '')
|
||||
|
||||
!= partial("includes/third-party/newest-comments/common.pug", {}, { cache: true })
|
||||
|
||||
script.
|
||||
window.addEventListener('load', () => {
|
||||
const keyName = 'waline-newest-comments'
|
||||
const { changeContent, generateHtml, run } = window.newestComments
|
||||
|
||||
const getComment = async (ele) => {
|
||||
try {
|
||||
const res = await fetch('!{serverURL}/api/comment?type=recent&count=!{newestCommentsLimit}')
|
||||
const result = await res.json()
|
||||
const walineArray = result.data.map(e => {
|
||||
return {
|
||||
'content': changeContent(e.comment),
|
||||
'avatar': e.avatar,
|
||||
'nick': e.nick,
|
||||
'url': e.url + '#' + e.objectId,
|
||||
'date': e.time || e.insertedAt
|
||||
}
|
||||
})
|
||||
btf.saveToLocal.set(keyName, JSON.stringify(walineArray), !{theme.aside.card_newest_comments.storage}/(60*24))
|
||||
generateHtml(walineArray, ele)
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
ele.textContent= "!{_p('aside.card_newest_comments.error')}"
|
||||
}
|
||||
}
|
||||
|
||||
run(keyName, getComment)
|
||||
})
|
||||
Reference in New Issue
Block a user