commit
6d387eadcc
@ -0,0 +1,18 @@
|
||||
- const { host, siteId, option } = theme.remark42
|
||||
|
||||
script.
|
||||
(()=>{
|
||||
window.remark_config = Object.assign({
|
||||
host: '!{host}',
|
||||
site_id: '!{siteId}',
|
||||
},!{JSON.stringify(option)})
|
||||
|
||||
function getCount () {
|
||||
const s = document.createElement('script')
|
||||
s.src = remark_config.host + '/web/counter.js'
|
||||
s.defer = true
|
||||
document.head.appendChild(s)
|
||||
}
|
||||
|
||||
window.pjax ? getCount() : window.addEventListener('load', getCount)
|
||||
})()
|
@ -0,0 +1,67 @@
|
||||
- const { host, siteId, option } = theme.remark42
|
||||
script.
|
||||
var remark_config = Object.assign({
|
||||
host: '!{host}',
|
||||
site_id: '!{siteId}',
|
||||
components: ['embed'],
|
||||
theme: document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'light'
|
||||
},!{JSON.stringify(option)})
|
||||
|
||||
function addRemark42(){
|
||||
for (let i = 0; i < remark_config.components.length; i++) {
|
||||
const s = document.createElement('script')
|
||||
s.src = remark_config.host + '/web/' + remark_config.components[i] + '.js'
|
||||
s.defer = true
|
||||
document.head.appendChild(s)
|
||||
}
|
||||
}
|
||||
|
||||
function initRemark42() {
|
||||
if (window.REMARK42) {
|
||||
if (this.remark42Instance) {
|
||||
this.remark42Instance.destroy()
|
||||
}
|
||||
|
||||
this.remark42Instance = window.REMARK42.createInstance({
|
||||
...remark_config
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function getCount () {
|
||||
const ele = document.querySelector('.remark42__counter')
|
||||
if (ele) {
|
||||
const s = document.createElement('script')
|
||||
s.src = remark_config.host + '/web/counter.js'
|
||||
s.defer = true
|
||||
document.head.appendChild(s)
|
||||
}
|
||||
}
|
||||
|
||||
function loadRemark42 () {
|
||||
if (window.REMARK42) {
|
||||
this.initRemark42()
|
||||
getCount()
|
||||
} else {
|
||||
addRemark42()
|
||||
window.addEventListener('REMARK42::ready', () => {
|
||||
this.initRemark42()
|
||||
getCount()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
document.getElementById('darkmode').addEventListener('click',()=>{
|
||||
if (!window.REMARK42) return
|
||||
let theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'light' : 'dark'
|
||||
window.REMARK42.changeTheme(theme)
|
||||
})
|
||||
|
||||
if ('!{theme.comments.use[0]}' === 'Remark42' || !!{theme.comments.lazyload}) {
|
||||
if (!{theme.comments.lazyload}) btf.loadComment(document.getElementById('remark42'), loadRemark42)
|
||||
else loadRemark42()
|
||||
} else {
|
||||
function loadOtherComment () {
|
||||
loadRemark42()
|
||||
}
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
- const { host, siteId } = theme.remark42
|
||||
|
||||
script.
|
||||
window.addEventListener('load', () => {
|
||||
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 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 getComment = () => {
|
||||
fetch('!{host}/api/v1/last/!{theme.newest_comments.limit}?site=!{siteId}')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
const remark42 = data.map(function (e) {
|
||||
return {
|
||||
'avatar': e.user.picture,
|
||||
'content': changeContent(e.text),
|
||||
'nick': e.user.name,
|
||||
'url': e.locator.url,
|
||||
'date': e.time,
|
||||
}
|
||||
})
|
||||
saveToLocal.set('remark42-newest-comments', JSON.stringify(remark42), !{theme.newest_comments.storage}/(60*24))
|
||||
generateHtml(remark42)
|
||||
}).catch(e => {
|
||||
const $dom = document.querySelector('#card-newest-comments .aside-list')
|
||||
$dom.innerHTML= "!{_p('aside.card_newest_comments.error')}"
|
||||
})
|
||||
}
|
||||
|
||||
const newestCommentInit = () => {
|
||||
if (document.querySelector('#card-newest-comments .aside-list')) {
|
||||
const data = saveToLocal.get('valine-newest-comments')
|
||||
if (data) {
|
||||
generateHtml(JSON.parse(data))
|
||||
} else {
|
||||
getComment()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
newestCommentInit()
|
||||
document.addEventListener('pjax:complete', newestCommentInit)
|
||||
})
|
Loading…
Reference in New Issue