init...
This commit is contained in:
59
themes/butterfly/layout/includes/third-party/comments/disqus.pug
vendored
Normal file
59
themes/butterfly/layout/includes/third-party/comments/disqus.pug
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
- const disqusPageTitle = page.title.replace(/'/ig,"\\'")
|
||||
- const { shortname, apikey } = theme.disqus
|
||||
- const { use, lazyload, count } = theme.comments
|
||||
|
||||
script.
|
||||
(() => {
|
||||
const disqus_config = function () {
|
||||
this.page.url = '!{ page.permalink }'
|
||||
this.page.identifier = '!{ url_for(page.path) }'
|
||||
this.page.title = '!{ disqusPageTitle }'
|
||||
}
|
||||
|
||||
const disqusReset = () => {
|
||||
window.DISQUS && window.DISQUS.reset({
|
||||
reload: true,
|
||||
config: disqus_config
|
||||
})
|
||||
}
|
||||
|
||||
btf.addGlobalFn('themeChange', disqusReset, 'disqus')
|
||||
|
||||
const loadDisqus = () =>{
|
||||
if (window.DISQUS) disqusReset()
|
||||
else {
|
||||
const script = document.createElement('script')
|
||||
script.src = 'https://!{shortname}.disqus.com/embed.js'
|
||||
script.setAttribute('data-timestamp', +new Date())
|
||||
document.head.appendChild(script)
|
||||
}
|
||||
}
|
||||
|
||||
const getCount = async() => {
|
||||
try {
|
||||
const eleGroup = document.querySelector('#post-meta .disqus-comment-count')
|
||||
if (!eleGroup) return
|
||||
const cleanedLinks = eleGroup.href.replace(/#post-comment$/, '')
|
||||
|
||||
const res = await fetch(`https://disqus.com/api/3.0/threads/set.json?forum=!{shortname}&api_key=!{apikey}&thread:link=${cleanedLinks}`,{
|
||||
method: 'GET'
|
||||
})
|
||||
const result = await res.json()
|
||||
|
||||
const count = result.response.length ? result.response[0].posts : 0
|
||||
eleGroup.textContent = count
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
if ('!{use[0]}' === 'Disqus' || !!{lazyload}) {
|
||||
if (!{lazyload}) btf.loadComment(document.getElementById('disqus_thread'), loadDisqus)
|
||||
else {
|
||||
loadDisqus()
|
||||
!{ count ? 'GLOBAL_CONFIG_SITE.isPost && getCount()' : '' }
|
||||
}
|
||||
} else {
|
||||
window.loadOtherComment = loadDisqus
|
||||
}
|
||||
})()
|
||||
Reference in New Issue
Block a user